画布动画问题
我想围绕画布中间旋转正方形。但广场看起来是静止的。
// 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;
I would like to rotate the square around the middle of the canvas. But the square looks static.
// 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您可以将您的
draw_square()
替换为canvas 2d api 中的基本中风矩形()
第二次尝试使用变换而不是角度,您将获得更好的结果。
这是我的意思的 jsfiddle http://jsfiddle.net/austinbv/EP794/
这里是我的 JS,所以
我知道这并不是真正调试你的代码,但这是获得相同结果的更简洁的方法。
编辑:
我只是进一步查看了您的代码,您需要设置一个时间间隔尝试运行最后一行
window.onload.setInterval(init, 100)
First you can replace your
draw_square()
with a basicstrokeRect()
from the canvas 2d apiSecond try using transformations instead of the angles you will get better results.
Here is a jsfiddle of what I mean http://jsfiddle.net/austinbv/EP794/
And here is my JS for SO
I know this isn't really debugging your code but it is a much cleaner way to get the same result.
Edit:
I just looked over your code a little more and you need to set an interval try running the last line
window.onload.setInterval(init, 100)