我只是试图在HTML5的最受欢迎的功能,即画布。有2个矩形,我只想旋转1st并保持第二个原样。问题是当下面的代码运行时,我的整个画布被旋转并且两个矩形都被旋转。我什至不能找到任何API可以引用我绘制的对象,并只旋转该特定对象而不是整个上下文。整个画布正在旋转?
代码:
<script type="text/javascript">
var context;
var radian = 0.01;
var w, h;
$(document).ready(function() {
w = document.width;
h = document.height;
var canvas = $('#canvas');
context = canvas[0].getContext('2d');
canvas[0].width = w;
canvas[0].height = h;
setInterval(startAnim, 200);
});
function startAnim() {
context.clearRect(0, 0, w, h);
context.strokeStyle = 'rgb(0,0,0)';
context.fillStyle = 'rgb(0,0,0)';
context.fillRect(0, 0, w, h);
context.strokeStyle = 'rgb(0,0,0)';
context.fillStyle = 'rgb(255,255,0)';
context.rotate(radian);
context.strokeRect(400, 300, 200, 200);
context.fillRect(400, 300, 200, 200);
context.fillStyle = 'rgb(0,255,255)';
context.fillRect(500, 400, 200, 200);
radian += 0.01;
}
</script>
我怎样才能防止这种情况发生?
你碰巧有一个工作的JSFiddle吗?我很想看到它的行动。 –
http://jsfiddle.net/zEJuh/2/ – TCM
感谢您的工作示例! –