var canvas = document.createElement('canvas'); canvas.width = 150; canvas.height = 150; //var canvas = document.getElementById('myCanvas'); // create a new canvas or use the existing one in document var context = canvas.getContext('2d'); var sourceImg = new Image(); sourceImg.onload = function () { // make sure the image is loaded first before cropping it context.drawImage(sourceImg, source_starting_x_coord, source_starting_y_coord, width_to_be_cropped_from_source_coord, height_to_be_cropped_from_source_coord, starting_x_coord_on_destination_canvas, starting_y_coord_on_destination_canvas, width_of_cropped_image_to_put_on_canvas, heigth_of_cropped_image_to_put_on_canvas); //context.drawImage(sourceImg, 0, 50, 150, 150, 0, 0, 150, 150); // display or do anything we like with the result //imageDisplay = canvas.toDataURL(); } sourceImg.src = toBeCroppedImageSource; // can be an image url or base-64 encoded data
Thursday, 5 March 2015
Cropping Image with JavaScript and Canvas
Below is an example of how to crop image using JavaScript and HTML Canvas element.
Labels:
JavaScript
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment