作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让 Logo 在其圆圈内旋转(如果可能,以随机速度旋转),但我无法实现。
我试过这个:
ctx.save();
ctx.rotate(0.10);
ctx.drawImage(object.image, object.x-object.size/2, object.y-object.size/2, object.size, object.size);
ctx.restore();
但如您所见, Logo 不是在其自身的中心旋转。
最佳答案
关键是先translate
,然后rotate
在 spawnRandomObject
中
// add the new object to the objects[] array
object.rot = 0;
objects.push(object);
在 animate
中
for (var i = 0; i < objects.length; i++) {
var object = objects[i];
object.y += object.speed*0.1;
object.rot += 0.05; // rotation speed
ctx.globalAlpha = object.opacite;
ctx.beginPath();
ctx.arc(object.x, object.y, object.size/1.25, 0,2*Math.PI);
ctx.fillStyle = object.couleur;
ctx.fill();
ctx.restore();
ctx.save();
ctx.translate(object.x, object.y);
ctx.rotate(object.rot);
ctx.drawImage(object.image, -object.size/2, -object.size/2, object.size, object.size);
ctx.restore();
}
关于javascript - 如何在 html 5 Canvas 上的圆圈内旋转图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51240336/
我是一名优秀的程序员,十分优秀!