0
我想旋轉圍繞畫布中間的方塊。但廣場看起來是靜態的。畫布動畫問題
// JScript source code
var s_canvas;
var s_context;
var R = 70;
var beta = 0;
var Xs = R * Math.cos(beta);
var Ys = R*Math.sin(beta);
var WIDTH = 400;
var HEIGHT = 300;
var angle = 0;
var tetha = 1.8 * Math.PI/180;
var counterclockwise=true;
var n = 4;
function draw_square(c,n, x, y, r, angle, counterclockwise) {
c.fillStyle = "00ff00";
c.beginPath();
c.moveTo(x + r * Math.cos(angle), y - r * Math.sin(angle));
var alpha=2*Math.PI/n;
for(var i=1;i<n;i++)
{
angle += alpha;
c.lineTo(x + r * Math.cos(angle), y - r * Math.sin(angle));
}
c.closePath();
c.fill();
}
function clear_square() {
s_context.clearRect(0, 0, WIDTH, HEIGHT);
}
function init() {
s_canvas = document.getElementById("canvas_square");
// Check if the canvas is supported and if the getContext is available
if (s_canvas && s_canvas.getContext) {
s_context = s_canvas.getContext("2d");
return setInterval(draw, 10);
}
else {
alert("Canvas is not supported!");
}
}
function draw() {
clear_square();
s_context.save();
s_context.translate(200, 150);
s_context.rotate(1.8 * Math.PI/180);
draw_square(s_context, n, Xs, Ys, 30, 0, true);
beta += tetha;
s_context.restore();
// Xs=R*Math.cos()
}
window.onload = init;
感謝。我找到了另一個解代碼需要在context.save()之前設置旋轉角度。 – miaf 2011-04-19 15:01:22
你能分享你的解決方案Le_Bedel嗎? (將其作爲回答並驗證它) – halflings 2013-04-23 22:37:56