[英]Enlarging a ball with paper.js
im using paper.js to create a ball and im trying to enlarge it per click. 即时通讯使用paper.js创建一个球,并尝试每次点击将其放大。
So i can easily enlarge the area by changing the radius which is what i do but it also uses segments for collision and physics etc. 因此,我可以通过更改半径轻松地扩大面积,这是我的工作,但是它也使用段进行碰撞和物理处理等。
Anyway when the ball is first created it applys the correct segments but when im enlarging it, i want more segments to compensate for its size. 无论如何,当第一次创建球时,它会应用正确的线段,但是当放大时,我希望有更多的线段来补偿其尺寸。
Im copying the code which works at the start but here it works for all but 1 segment that spawns at 0,0 and messes up the area of the circle. 我会复制开始时可用的代码,但在这里它适用于除1以外的所有片段,该片段在0,0生成并弄乱了圆的区域。 Here is that code :
这是代码:
balls[0].radius = Math.sqrt((balls[0].path.area + balls[0].score)/ Math.PI);
balls[0].maxVec = 15;
balls[0].numSegment = Math.floor(balls[0].radius / 3 + 2);
balls[0].boundOffset = [];
balls[0].boundOffsetBuff = [];
balls[0].sidePoints = [];
for (var i = 0; i < balls[0].numSegment; i ++) {
balls[0].boundOffset.push(balls[0].radius);
balls[0].boundOffsetBuff.push(balls[0].radius);
balls[0].path.add(new Point());
balls[0].sidePoints.push(new Point({
angle: 360 / balls[0].numSegment * i,
length: 1
}));
}
Here is the js fiddle - http://jsfiddle.net/wMQth/147/ 这是js小提琴-http: //jsfiddle.net/wMQth/147/
You need to remove existing points in your path before adding new ones. 在添加新点之前,您需要删除路径中的现有点。
Here is the modified fiddle . 这是经过修饰的小提琴 。
balls[0].radius = Math.sqrt((balls[0].path.area + balls[0].score)/ Math.PI);
balls[0].maxVec = 15;
balls[0].numSegment = Math.floor(balls[0].radius / 3 + 2);
balls[0].boundOffset = [];
balls[0].boundOffsetBuff = [];
balls[0].sidePoints = [];
// reset path before adding new points
balls[0].path.removeSegments();
for (var i = 0; i < balls[0].numSegment; i ++) {
balls[0].boundOffset.push(balls[0].radius);
balls[0].boundOffsetBuff.push(balls[0].radius);
balls[0].path.add(new Point());
balls[0].sidePoints.push(new Point({
angle: 360 / balls[0].numSegment * i,
length: 1
}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.