我在画布上有一个五边形图形,我想将其移动到鼠标所在的位置。
代码
<canvas id="fld" width="1000px" height="800"></canvas>
<script type="text/javascript"><!--
onload = function() { draw(); };
function draw() {
var canvas = document.getElementById('fld');
if ( ! canvas || ! canvas.getContext ) {return false;}
var ctx = canvas.getContext('2d');
//Pentagon
ctx.beginPath();
ctx.strokeStyle = 'rgb(255, 0, 0)';
ctx.moveTo(100,10);
for (var i=1;i<=5; ++i) {
th=i * 2 * Math.PI/5;
x=100+90*Math.sin(th);
y=100-90*Math.cos(th);
ctx.lineTo(x,y);
}
ctx.stroke();
ctx.beginPath();
ctx.arc(900, 60, 40, 0, 2 * Math.PI, false);
ctx.fillStyle = "rgb(255, 0, 0)";
ctx.fill();
ctx.strokeStyle = "black";
ctx.stroke();
}
</script>