我找到了一个绘制螺旋的代码,我需要让它旋转 360 度(类似于催眠效果)。我试图为它制作动画,但我不知道如何正确实现它。我没时间了。如果该代码尽可能简单,那就太好了。
这是螺旋代码:
<canvas id="myCanvas" width="300" height="300"></canvas>
<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
var centerX = 150;
var centerY = 150;
cxt.moveTo(centerX, centerY);
var STEPS_PER_ROTATION = 60;
var increment = 2*Math.PI/STEPS_PER_ROTATION;
var theta = increment;
while( theta < 40*Math.PI) {
var newX = centerX + theta * Math.cos(theta);
var newY = centerY + theta * Math.sin(theta);
cxt.lineTo(newX, newY);
theta = theta + increment;
}
cxt.stroke();
</script>