canvas drawimage draw zoomed image

妖精的绣舞 提交于 2019-12-11 05:24:21

问题


I need to set canvas size using code and than drawImage on this canvas. The problem is drawn image is zoomed.

code follows -

currentImageDIV = $("#cnv").css({ "position" : "relative" ,"height": 400, "width": 800,"top" :"100px" , "border": "1px solid red" });

var c = document.getElementById("cnv");
var ctx = c.getContext("2d");
ctx.imageSmoothingEnabled = false;

var img = new Image();
img.src = "/images/sample2.jpg";
img.onload = function () {
     ctx.drawImage(img, 0, 0); 
}

If I do not set the canvas size than image drawn as expected, not zoomed.

How can I control the canvas size without drawImage zoomed image ?


回答1:


I found out that if I hard code the width and height of the canvas tag than no zooming is taking place.

Another solution for this is to set the width and height using prop rather than css (e.g. $("#cnv").prop({"width": 800, "height": 600});)



来源:https://stackoverflow.com/questions/42944192/canvas-drawimage-draw-zoomed-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!