javascript - canvas html + javascript中的轨道位置
问题描述
我想做一些像这个网站https://theatreshowcase.boston这样的轨道/圆形或曲线路径动画
首先尝试我使用来自https://www.the-art-of-web.com/javascript/animate-curved-path的 javascript像这样
var duration = 20; // seconds
var gridSize = 150; // pixels
var start = null;
var stretchFactor;
function step(timestamp)
{
for (let i = 0; i < images.length; i++) {
const image = images[i];
var maxX = field.clientWidth - image.offsetWidth;
var maxY = field.clientHeight - image.offsetHeight;
var progress, x, y;
if(start === null) {
start = timestamp;
stretchFactor = 1 + (Math.random() * 3);
}
progress = (timestamp - start) / duration / 1000; // percent
x = Math.sin(progress * 2 * Math.PI); // x = ƒ(t)
y = Math.cos(progress * 2 * Math.PI); // y = ƒ(t)
var tx = maxX/2 + ((gridSize+i) * x);
var ty = maxY/2 + ((gridSize+i) * y);
image.style.transform = `translate(${tx}px,${ty}px)`;
}
if(progress >= 1) start = null; // reset to start position
requestAnimationFrame(step);
}
requestAnimationFrame(step);
不幸的是,性能很差,所以我决定使用画布
但我对算法感到困惑,我发现一个简单的方法是改变旋转位置,但我希望图像保持它的方向并且只改变它的位置
ctx.rotate(((2 * Math.PI) / 60) * time.getSeconds() + ((2 * Math.PI) / 60000) * time.getMilliseconds());
也许有人可以帮我找到一个简单的公式和算法
解决方案
推荐阅读
- sql - 当列上存在日期时间时获取唯一行
- jenkins - Jenkins 多分支管道等待其他执行者而不是完成发布操作
- html - 我可以将表格标题链接到 html 中的表格数据吗?
- node.js - nodemon 在 Windows 上通过 npm 脚本启动时不观看
- javascript - 到达画廊结束/开始时关闭 JavaScript 画廊(而不是循环)
- sql - 多对多左连接查询生成器
- sql-server - 从帐号计算用户公式
- angular - *ngIf 不存在时显示离子按钮
- android - Xamarin.Essentials 从不提示权限
- reactjs - Material-UI 主题停止工作升级@material-ui/core & @material-ui/styles (REACT & Next.js)