How to scale an imageData in HTML canvas?

前端 未结 5 1641
礼貌的吻别
礼貌的吻别 2020-12-01 05:37

I have a canvas in my webpage; I create a new Image data in this canvas then I modify some pixel through myImgData.data[] array. Now I would like to scale this image and mak

5条回答
  •  悲&欢浪女
    2020-12-01 05:47

    You can scale the canvas using the drawImage method.

    context = canvas.getContext('2d');
    context.drawImage( canvas, 0, 0, 2*canvas.width, 2*canvas.height );
    

    This would scale the image to double the size and render the north-west part of it to the canvas. Scaling is achieved with the third and fourth parameters to the drawImage method, which specify the resulting width and height of the image.

    See docs at MDN https://developer.mozilla.org/en-US/docs/DOM/CanvasRenderingContext2D#drawImage%28%29

提交回复
热议问题