How to scale ImageData using HTML5?

岁酱吖の 提交于 2019-12-12 16:52:48

问题


Particularly i have this example but it does not seem to work :

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
your browser does not support the canvas tag </canvas>

<canvas id="myCanvas2" width="300" height="150" style="border:1px solid #d3d3d3;">
your browser does not support the canvas tag </canvas>

<script type="text/javascript">

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

var c2=document.getElementById("myCanvas2");
var ctx2=c2.getContext("2d");

ctx.strokeRect(5,5,25,15);
var imageData = ctx.getImageData(0,0,c.width, c.height);


ctx2.putImageData(imageData, 100, 100);
ctx2.scale(2,2);
</script> 

</body>
</html>

Shouldn't the rect get scaled in the second canvas ?


回答1:


Try the following code:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

var c2=document.getElementById("myCanvas2");
var ctx2=c2.getContext("2d");

ctx.strokeRect(5,5,25,15);

ctx2.scale(2, 2);
ctx2.drawImage(c, 10, 10);



回答2:


You need to do the transformation before you "write" the image into canvas.

ctx2.scale(2,2);
ctx2.putImageData(imageData, 100, 100);

Then everything should work fine.



来源:https://stackoverflow.com/questions/11391149/how-to-scale-imagedata-using-html5

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