Crop Canvas / Export HTML5 Canvas with certain width and height

JavascriptWeb DevelopmentFront End Scripts

For this, create a temporary canvas for drawing on the current canvas. After that use toDataUrl() method on the temporary canvas −

var c = document.getElementsByTagName("canvas");
var ctx = c[0].getContext("2d");
var data = ctx.getImageData(0, 0, c[0].width, c[0].height);
var compositeOperation = ctx.globalCompositeOperation;

ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = "#800000";
ctx.fillRect(0,0,c[0].width,c[0].height);

var tempCanvas = document.createElement("canvas"),
tCtx = tempCanvas.getContext("2d");
tempCanvas.width = 550;
tempCanvas.height = 280;
tCtx.drawImage(canvas[0],0,0);
raja
Published on 06-Apr-2018 13:38:23
Advertisements