gpt4 book ai didi

javascript - PaperJS 增长矩形(比例)

转载 作者:行者123 更新时间:2023-11-28 01:20:04 29 4
gpt4 key购买 nike

我在 paperjs 中有一个圆形和一个矩形对象。现在我想制作动画。在该动画中,圆圈上升并且矩形必须在圆圈之后(在圆圈底部)增长。我在这里有一个例子(不是我想要的那样工作)

example

代码:

var circlePath = new Path.Circle(new Point(50, 50), 25);
circlePath.strokeColor = 'black';

var rectanglePath = new Path.Rectangle({
point: [20, 20],
size: [60, 60],
strokeColor: 'black'
});

var backgroundLayer = new Layer({
children: [circlePath,rectanglePath],
position: view.center
});

var newPosition = 200;

circlePath.onFrame = function(event){
var vector = newPosition-circlePath.position.y;
circlePath.position.y += vector / 50;
growRect(rectanglePath.bounds.width, rectanglePath.position.y-circlePath.position.y, rectanglePath);
};

function growRect(width, height, elem){
var scaleX = width/elem.bounds.width;
var scaleY = height/elem.bounds.height;
var prevPos = new paper.Point(elem.bounds.x,elem.bounds.y);

elem.scale(scaleX,scaleY);

prevPos.x += elem.bounds.width/2;
prevPos.y += elem.bounds.height/2;

elem.position = prevPos;
};

也许有人可以帮助我!

最佳答案

scale 函数可以将中心点作为第三个参数。如果您使用矩形的底部作为刻度中心,它可能会如您所愿。此外,代码会稍微短一些。你可以试试here

var circlePath = new Path.Circle(new Point(50, 50), 25);
circlePath.strokeColor = 'black';

var rectanglePath = new Path.Rectangle({
point: [20, 20],
size: [60, 60],
strokeColor: 'black'
});

var backgroundLayer = new Layer({
children: [circlePath,rectanglePath],
position: view.center
});

var newPosition = 200;

circlePath.onFrame = function(event){
var vector = newPosition-circlePath.position.y;
circlePath.position.y += vector / 50;
growRect(rectanglePath.bounds.width, rectanglePath.position.y-circlePath.position.y, rectanglePath);
};

function growRect(width, height, elem){
var scaleX = width/elem.bounds.width;
var scaleY = height/elem.bounds.height;
elem.scale(scaleX,scaleY, new paper.Point(elem.bounds.x,elem.bounds.bottom));

关于javascript - PaperJS 增长矩形(比例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266747/

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