这应该很简单..但无法弄清楚:我在第一个矩形上使用旋转功能,但我正在旋转第二个矩形??:( 在 Remmy Sharp 的回答之后更新了代码。但是现在盒子已经消失了。我只是想让两个盒子之一在他们的注册点上旋转。
哪里有问题 ?
<!DOCTYPE HTML>
<html>
<head>
<style>
#myCanvas {
border: 1px solid #9C9898;
}
#myCanvas2 {
border: 1px solid #9C9898;
}
body {
margin: 0px;
padding: 0px;
}
</style>
<script>
window.onload = function() {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.rect(100, 100, 200, 100);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = 5;
context.strokeStyle = 'black';
context.stroke();
context.rotate(20*Math.PI/180); //<<<1st rectange should rotate
var canvas2 = document.getElementById('myCanvas2');
var context2 = canvas2.getContext('2d') //<< updated as per Remmy Sharp's answer.
context2.beginPath();
context2.rect(200, 200, 200, 100);
context2.fillStyle = '#8ED6FF';
context2.fill();
context2.lineWidth = 5;
context2.strokeStyle = 'black';
context2.stroke();
//canvas2.rotate = 45
//context2.rotate(20*Math.PI/180)
};
</script>
</head>
<body>
<canvas id="myCanvas" width="578" height="500">
<canvas id="myCanvas2" width="400" height="200"></canvas>
</canvas>
</body>
</html>