1 回答
TA贡献1873条经验 获得超9个赞
如果我很好地理解你的情况,这里有一个演示可能实现的草图。
这个想法是保留一个参考路径,我们根据该参考路径计算每个帧的临时路径,以生成动画。
该解决方案的优点是您可以将其应用于任何类型的路径。
// Create a path to animate.
const path = new Path.Circle({
center: view.center,
radius: 50,
selected: true,
closed: false
});
// Initialize the time variable that will control the animation.
let time = 0;
// On each frame...
function onFrame() {
// ...if the animation is not finished yet...
if (time <= 1) {
// ...animate.
time += 0.01;
drawTmpPath(time);
}
}
// Initialize the temporary path that will display our animation.
let tmpPath;
function drawTmpPath(t) {
// Make sure that t is never over 1.
t = Math.min(t, 1);
// Remove the previously drawn temporary path.
if (tmpPath) {
tmpPath.remove();
}
// Draw the new temporary path from the reference one.
tmpPath = path.clone().set({
selected: false,
strokeColor: 'orange',
strokeWidth: 5
});
// Split it at the appropriate location.
const remainingPath = tmpPath.splitAt(tmpPath.length * t);
// Remove the eventual remaining part.
if (remainingPath) {
remainingPath.remove();
}
}
// Scale things up.
project.activeLayer.fitBounds(view.bounds.scale(0.8));
编辑
为了回答您的评论,为了控制动画时间,您可以存储动画开始时间,并在每一帧上计算更新函数从当前时间开始所需的相对时间。
请注意,您还可以依赖GreenSock等外部动画库来更轻松地处理计时。
// Total animation time in milliseconds.
const totalTime = 10000;
// Create a path to animate.
const path = new Path.Circle({
center: view.center,
radius: 50,
selected: true,
closed: false
});
// Initialize the time variable that will control the animation.
const startTime = Date.now();
let animationDone = false;
// On each frame...
function onFrame() {
// ...if the animation is not finished yet...
if (!animationDone) {
// ...calculate the relative time needed to draw the tmp path.
const relativeTime = (Date.now() - startTime) / totalTime;
// ...animate.
if (relativeTime < 1) {
drawTmpPath(relativeTime);
} else {
drawTmpPath(1);
animationDone = true;
}
}
}
// Initialize the temporary path that will display our animation.
let tmpPath;
function drawTmpPath(t) {
// Make sure that t is never over 1.
t = Math.min(t, 1);
// Remove the previously drawn temporary path.
if (tmpPath) {
tmpPath.remove();
}
// Draw the new temporary path from the reference one.
tmpPath = path.clone().set({
selected: false,
strokeColor: 'orange',
strokeWidth: 5
});
// Split it at the appropriate location.
const remainingPath = tmpPath.splitAt(tmpPath.length * t);
// Remove the eventual remaining part.
if (remainingPath) {
remainingPath.remove();
}
}
// Scale things up.
project.activeLayer.fitBounds(view.bounds.scale(0.8));
添加回答
举报