javascript - 如何在 html 5 画布上旋转圆圈内的图片?
问题描述
我试图让徽标在他们的圈子内旋转(如果可能的话以随机速度),但我无法做到这一点。
我试过这个:
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();
但正如你所看到的,这些标志并不是在它们自己的中心旋转。
解决方案
关键是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();
}
推荐阅读
- flutter - Flutter/Dart - 在 Future 中使用 ChangeNotifier 或 setState 之类的东西?
- c# - 签署 PDF 时如何以编程方式输入 X509Certificate2 证书的 PIN(在 C# 中)
- delphi - Delphi - 如何在运行时创建堆叠条形系列?
- c - 在 GNU GCC 和 clang 中使用外部头文件创建共享库
- c# - 如何提高以下代码的性能?
- javascript - How to close dropdown on outside click NEXT.JS
- python - 将颜色条刻度标签从标准格式重新格式化为十进制
- python - 使用 pip 安装 tensorflow 的问题
- android - kotlin.jvm.functions.Function0 到包含的布局
- javascript - 在sails中看不到上传的文件,以及如何设置上传的图像位置