gpt4 book ai didi

javascript - paper.js 沿路径的动画

转载 作者:行者123 更新时间:2023-12-03 16:29:34 27 4
gpt4 key购买 nike

我正在使用 paper.js,我正在尝试沿我创建的路径为项目设置动画...

//Path : 
path = new Path();
path.add(new Point(0,100), new Point(120,100), new Point(120,150));
//Item
circle = new Path.Circle(0,100,4);
circle.strokeColor = "#EEE";

在制作动画时(使用 onFrame()),我希望圆圈跟随路径...有谁知道这是怎么做到的吗?我没有在文档或谷歌上找到它......我希望它足够清楚..

感谢您的回答!

最佳答案

还没测试,不过应该是这样的。

// vars
var point1 = [0, 100];
var point2 = [120, 100];
var point3 = [120, 150];

// draw the line
var path = new Path();
path.add(new Point(point1), new Point(point2), new Point(point3));
path.closed = true;

// draw the circle
var circle = new Path.Circle(0,100,4);
circle.strokeColor = "#EEE";

// target to move to
var target = point2;

// how many frame does it take to reach a target
var steps = 200;

// defined vars for onFrame
var dX = 0;
var dY = 0;

// position circle on path
circle.position.x = target[0];
circle.position.y = target[1];

function onFrame(event) {

//check if cricle reached its target
if (Math.round(circle.position.x) == target[0] && Math.round(circle.position.y) == target[1]) {
switch(target) {
case point1:
target = point2;
break;
case point2:
target = point3;
break;
case point3:
target = point1;
break;
}

// calculate the dX and dY
dX = (target[0] - circle.position.x)/steps;
dY = (target[1] - circle.position.y)/steps;

}

// do the movement
circle.position.x += dX;
circle.position.y += dY;
}

Working DEMO

关于javascript - paper.js 沿路径的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12295072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com