如何使用HTML5缩放ImageData?

时间:2012-07-09 08:04:04

标签: html5 canvas scale

特别是我有这个例子,但它似乎不起作用:

<!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>

不应该在第二个画布中缩放rect吗?

2 个答案:

答案 0 :(得分:3)

请尝试以下代码:

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);

答案 1 :(得分:-1)

在将图像“写入”画布之前,需要进行转换。

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

然后一切都应该正常。