在圆形方向上移动图像的
moving of an image in circular direction
我想在圆形方向移动图像。我用了setTimeout
函数…但是没有成功。
x = image_size/2+radius*Math.cos(Math.PI*angle)-ball_image_size/2;
y = image_size/2-radius*Math.sin(Math.PI*angle)-ball_image_size/2;
//-----image main circle--------
base_image = new Image();
base_image.src = 'img/test.jpg';
base_image.onload = function()
{
ctx.drawImage(base_image,0,0,image_size,image_size);
};
//------------image ball---------
ball_image = new Image();
ball_image.src = 'img/ball.jpg';
ball_image.onload = function()
{
ctx.drawImage(ball_image, x, y, ball_image_size, ball_image_size);
}
clr = setTimeout('ball()',20);
}
//--------function of animation------------
function ball () {
ball_image.style.left = Math.cos(Math.PI*angle)*radius;
ball_image.style.top = Math.sin(Math.PI*angle)*radius;
angle = angle + .1;
//setTimeout(ball,20);
}
查看这个版本的旋转星星(根据要求现在更改为图像,看看我强大的绘画技巧!)在画布上作为参考。再次注意,你的更新应该在画布ctx.drawImage(ball_image, x, y, ball_image_size, ball_image_size);
上使用draw调用,而不是在画布上设置图像元素的样式,画布不关心。理想情况下,您应该在开始任何操作之前加载图像。
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
let radius = canvas.width / 3;
let centerX = canvas.width / 2;
let centerY = canvas.height;
let x, y;
let angle = 0;
let star = new Image();
star.onload = draw;
//Imgur doesn't produce CORS issues luckily
star.src = "http://i.imgur.com/VIofbab.png";
function draw() {
//minus because it should start on the left
x = centerX - Math.cos(angle) * radius;
//minus because canvas y-coord system is from
//top to bottom
y = centerY - Math.sin(angle) * radius;
//Angle is in rad
angle += Math.PI / 180;
angle %= Math.PI;
//Draw a star (was circle before)
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(star, x - star.width / 2, y - star.height / 2);
setTimeout(draw, 50);
};
<html>
<body>
<canvas id="canvas" width="320" height="180" style="border:1px solid #000000;">not supported</canvas>
</body>
</html>
相关文章:
- JS-在一段时间后水平移动图像
- JS-移动图像,暂停设置的时间,然后将图像向后移动
- 如何在kineticjs中移动图像
- 锤击JS平移不移动图像
- 如何使用javascript中的箭头键在画布上移动图像
- 在iPad上使用touchmove移动图像(css3变换)
- 当我在另一个画布上同时拖动另一个图像时,如何移动图像
- 在web应用程序ASP.net中拖放和移动图像
- 如何使用javascript移动图像
- 如何在Javascript中在桌子周围随机移动图像
- 如何使用变换来更改或移动图像
- 如何使用reveal.js、javascript和fragment类移动图像
- 如何在 HTML5 中使用键盘箭头键在画布上移动图像
- 在 Javascript 中以 360° 角度移动图像
- 移动图像,显示值并保存当前值
- 移动图像 ..JavaScript 将变量声明为字符串
- 通过指向 X、Y 坐标来移动图像
- 尝试在单击时移动图像,但是 .click 甚至仅在第一次单击时触发
- 通过单击方向按钮在容器内移动图像
- 使用 JavaScript 移动图像