Answers for "change image size in canvas"

0

canvas set image height

var imageHeight=100;
var imageWidth=100;
context.drawImage(image, 0, 0,imageHeight,imageWidth);
Posted by: Guest on June-23-2021
0

change image size in canvas

/*Generally you can't resize drawn element in canvas, but you can redraw
it with changed parameters. Something like this.*/

function clearDrawAndGrow ( multiplier ){
      context.clear(0,0,canvas.width,canvas.height);
      /*Whats important that you should not change size of the origin
      image, but sizes on "drawImage()" method as below*/
    context.drawImage(image, 0, 0, image.width * multiplier, image.height * multiplier);
}

//Example code that change image size every 2 sec:
var image = document.getElementById("image");
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var size = 1;

function draw(){
      clearDrawAndGrow(size);
      size *= 1.1;
}

setInterval(draw, 2000);
Posted by: Guest on September-27-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language