我想我快到了,但我可能是错的。我试图让分组的弧形扇区在一个方向上作为一个完整的圆圈进行动画处理。如何在 animateWheel 函数中引用 createWheel 函数?
这是代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Circle - Arc 3</title>
<style>
h1 {
font-family:Arial, Helvetica, sans-serif;
font-size: 1.5em;
color:#333;
}
#canvas1{ background-color:#699;}
</style>
</head>
<body>
<h1>Circle - Arc</h1>
<canvas id="canvas1" width="600" height="600"> Your Browser does not support HTML 5
</canvas>
<script>
// arc sectors vars
var centerX = 200;
var centerY = 200;
var radius = 100;
var fullCircleDegree = 360;
// Closure Function to ceate dynamic arc sectors
var arcSectors = function(num) { // The outer function defines a variable called "num"
var getNum = function() {
return 360 / num; // The inner function has access to the "num" variable of the outer function
}
return getNum; // Return the inner function, thereby exposing it to outer scopes
},
createArcSectors = arcSectors(7);
var rotateAngle = createArcSectors() * Math.PI/180;
var startAngle = 0 * Math.PI/180;
var endAngle = createArcSectors() * Math.PI/180;
var animateRot = 0;
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
window.onload = function () {
createWheel();
}
function createWheel() {
var theCanvas = document.getElementById('canvas1');
var context = theCanvas.getContext('2d');
context.clearRect(0, 0, canvas1.width, canvas1.height);
context.arc(centerX, centerY, radius, startAngle, endAngle, false);
// create arc sectors
for (i = 0; i < 7; i++) {
context.translate(centerX, centerY);
context.rotate(rotateAngle);
context.translate(-centerX, -centerY);
context.beginPath();
context.moveTo(centerX, centerY);
context.lineTo(centerX + radius, centerY);
context.arc(centerX, centerY, radius, startAngle, endAngle, false);
context.closePath();
context.stroke();
}
animateWheel();
}
function animateWheel() {
var theCanvas = document.getElementById('canvas1');
var context = theCanvas.getContext('2d');
//rotateAngle = animateRot * Math.PI / 180;
rotateAngle = .002;
console.log(rotateAngle);
animateRot += .002;
if (rotateAngle > 360) {
animateRot -= 1;
}
requestAnimFrame(function() {
animateWheel();
});
}
</script>
</body>
</html>