我尝试绘制一条旋转的线,并在旋转后在该线的起点和终点绘制一个点。
这是我的代码(不能正常工作)
<!-- language: lang-html -->
<html><head><title>test</title></head><body>
<canvas id="canvas" width="100" height="100"></canvas>
<script type="text/javascript">
var _context = document.getElementById("canvas").getContext('2d');
var x = 40;
var y = 40;
var angle = 0.2;
_context.beginPath();
_context.translate( x, y);
//rotate and draw line
_context.rotate( angle );
_context.moveTo( 0, 0);
_context.lineTo( 0, 40);
_context.stroke();
//calculate the new x,y position from line
x = x * Math.cos( angle ) - y * Math.sin( angle );
y = x * Math.sin( angle ) + y * Math.cos( angle );
//draw a point to this coords
_context.save();
_context.fillStyle = "orange";
_context.beginPath();
_context.arc( x, y, 4, 0, 2*Math.PI, true);
_context.fill();
</script>
</body></html>
jsFiddle 演示:http: //jsfiddle.net/UT29j/
有人有想法吗?