mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-07-15 16:35:53 -04:00
Optimise WineGums example.
This commit is contained in:
parent
9d5e5632df
commit
3b3f86ae2d
1 changed files with 31 additions and 30 deletions
|
@ -16,21 +16,18 @@
|
||||||
this.numSegment = Math.floor(r / 3 + 2);
|
this.numSegment = Math.floor(r / 3 + 2);
|
||||||
this.boundOffset = [];
|
this.boundOffset = [];
|
||||||
this.boundOffsetBuff = [];
|
this.boundOffsetBuff = [];
|
||||||
this.cos = [];
|
this.path = new Path({
|
||||||
this.sin = [];
|
fillColor: {
|
||||||
this.path = new Path();
|
hue: Math.random() * 360,
|
||||||
this.path.fillColor = new Color({
|
saturation: 1,
|
||||||
hue:Math.random() * 360,
|
brightness: 1
|
||||||
saturation:1,
|
},
|
||||||
brightness:1
|
blendMode: 'screen'
|
||||||
});
|
});
|
||||||
|
|
||||||
this.path.blendMode = "screen";
|
|
||||||
for (var i = 0; i < this.numSegment; i ++) {
|
for (var i = 0; i < this.numSegment; i ++) {
|
||||||
this.boundOffset.push(this.radius);
|
this.boundOffset.push(this.radius);
|
||||||
this.boundOffsetBuff.push(this.radius);
|
this.boundOffsetBuff.push(this.radius);
|
||||||
this.cos.push(Math.cos(Math.PI * 2 / this.numSegment * i));
|
|
||||||
this.sin.push(Math.sin(Math.PI * 2 / this.numSegment * i));
|
|
||||||
this.path.add(new Point());
|
this.path.add(new Point());
|
||||||
}
|
}
|
||||||
this.path.add(new Point());
|
this.path.add(new Point());
|
||||||
|
@ -40,7 +37,7 @@
|
||||||
Ball.prototype.iterate = function() {
|
Ball.prototype.iterate = function() {
|
||||||
this.checkWallCollision();
|
this.checkWallCollision();
|
||||||
if (this.vector.length > this.maxVec) {
|
if (this.vector.length > this.maxVec) {
|
||||||
this.vector = this.vector.normalize(this.maxVec);
|
this.vector.length = this.maxVec;
|
||||||
}
|
}
|
||||||
this.point += this.vector;
|
this.point += this.vector;
|
||||||
this.updateShape();
|
this.updateShape();
|
||||||
|
@ -56,7 +53,10 @@
|
||||||
Ball.prototype.updateShape = function() {
|
Ball.prototype.updateShape = function() {
|
||||||
var points = [];
|
var points = [];
|
||||||
for (var i = 0; i < this.numSegment; i ++) {
|
for (var i = 0; i < this.numSegment; i ++) {
|
||||||
points.push(new Point(this.point.x + this.cos[i] * this.boundOffset[i], this.point.y + this.sin[i] * this.boundOffset[i]));
|
points.push(this.point + {
|
||||||
|
angle: 360 / this.numSegment * i,
|
||||||
|
length: this.boundOffset[i]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < this.path.segments.length; i ++) {
|
for (var i = 0; i < this.path.segments.length; i ++) {
|
||||||
|
@ -64,12 +64,12 @@
|
||||||
var point = (points[i % this.numSegment] + next) / 2;
|
var point = (points[i % this.numSegment] + next) / 2;
|
||||||
var vector = (next - point) / 4;
|
var vector = (next - point) / 4;
|
||||||
this.path.segments[i].point = point + vector;
|
this.path.segments[i].point = point + vector;
|
||||||
//this.path.segments[i].handleIn = -vector;
|
|
||||||
//this.path.segments[i].handleOut = vector;
|
|
||||||
}
|
}
|
||||||
this.path.smooth();
|
this.path.smooth();
|
||||||
for (var i = 0; i < this.numSegment; i ++) {
|
for (var i = 0; i < this.numSegment; i ++) {
|
||||||
if (this.boundOffset[i] < this.radius / 4) { this.boundOffset[i] = this.radius / 4};
|
if (this.boundOffset[i] < this.radius / 4) {
|
||||||
|
this.boundOffset[i] = this.radius / 4;
|
||||||
|
};
|
||||||
var next = (i + 1) % this.numSegment;
|
var next = (i + 1) % this.numSegment;
|
||||||
var prev = (i > 0) ? i - 1 : this.numSegment - 1;
|
var prev = (i > 0) ? i - 1 : this.numSegment - 1;
|
||||||
this.boundOffsetBuff[i] = this.boundOffset[i] += (this.radius - this.boundOffset[i]) / 15;
|
this.boundOffsetBuff[i] = this.boundOffset[i] += (this.radius - this.boundOffset[i]) / 15;
|
||||||
|
@ -80,10 +80,10 @@
|
||||||
Ball.prototype.react = function(b) {
|
Ball.prototype.react = function(b) {
|
||||||
var dist = this.point.getDistance(b.point);
|
var dist = this.point.getDistance(b.point);
|
||||||
if (dist < this.radius + b.radius && dist != 0) {
|
if (dist < this.radius + b.radius && dist != 0) {
|
||||||
var direc = (this.point - b.point).normalize();
|
|
||||||
var overlap = this.radius + b.radius - dist;
|
var overlap = this.radius + b.radius - dist;
|
||||||
this.vector += direc * overlap * 0.015;
|
var direc = (this.point - b.point).normalize(overlap * 0.015);
|
||||||
b.vector -= direc * overlap * 0.015;
|
this.vector += direc;
|
||||||
|
b.vector -= direc;
|
||||||
|
|
||||||
this.calcBounds(b);
|
this.calcBounds(b);
|
||||||
b.calcBounds(this);
|
b.calcBounds(this);
|
||||||
|
@ -100,7 +100,7 @@
|
||||||
|
|
||||||
Ball.prototype.calcBounds = function(b) {
|
Ball.prototype.calcBounds = function(b) {
|
||||||
for (var i = 0; i < this.numSegment; i ++) {
|
for (var i = 0; i < this.numSegment; i ++) {
|
||||||
var tp = this.point + new Point(this.cos[i] * this.boundOffset[i], this.sin[i] * this.boundOffset[i]);
|
var tp = this.getSidePoint(i);
|
||||||
var bLen = b.getboundOffset(tp);
|
var bLen = b.getboundOffset(tp);
|
||||||
var td = tp.getDistance(b.point);
|
var td = tp.getDistance(b.point);
|
||||||
if (td < bLen ) {
|
if (td < bLen ) {
|
||||||
|
@ -109,6 +109,13 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ball.prototype.getSidePoint = function(index) {
|
||||||
|
return this.point + {
|
||||||
|
angle: 360 / this.numSegment * index,
|
||||||
|
length: this.boundOffset[index]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Ball.prototype.updateBounds = function() {
|
Ball.prototype.updateBounds = function() {
|
||||||
for (var i = 0; i < this.numSegment; i ++) {
|
for (var i = 0; i < this.numSegment; i ++) {
|
||||||
this.boundOffset[i] = this.boundOffsetBuff[i];
|
this.boundOffset[i] = this.boundOffsetBuff[i];
|
||||||
|
@ -118,11 +125,7 @@
|
||||||
|
|
||||||
//--------------------- main ---------------------
|
//--------------------- main ---------------------
|
||||||
|
|
||||||
var unitAngle = 15 / 180 * Math.PI;
|
|
||||||
var bg = new Group();
|
|
||||||
var balls = [];
|
var balls = [];
|
||||||
var bgRadius;
|
|
||||||
|
|
||||||
var numBalls = 18;
|
var numBalls = 18;
|
||||||
for (var i = 0; i < numBalls; i++) {
|
for (var i = 0; i < numBalls; i++) {
|
||||||
var position = Point.random() * view.size ,
|
var position = Point.random() * view.size ,
|
||||||
|
@ -131,7 +134,6 @@
|
||||||
balls.push(ball);
|
balls.push(ball);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function onFrame() {
|
function onFrame() {
|
||||||
for (var i = 0; i < balls.length - 1; i++) {
|
for (var i = 0; i < balls.length - 1; i++) {
|
||||||
for (var j = i + 1; j < balls.length; j++) {
|
for (var j = i + 1; j < balls.length; j++) {
|
||||||
|
@ -141,7 +143,6 @@
|
||||||
for (var i = 0, l = balls.length; i < l; i++) {
|
for (var i = 0, l = balls.length; i < l; i++) {
|
||||||
balls[i].iterate();
|
balls[i].iterate();
|
||||||
}
|
}
|
||||||
bg.rotate(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue