Examples: Move improved and simplified version of Bouncing Balls to Paperjs.org folder.

This commit is contained in:
Jonathan Puckey 2013-03-02 20:58:56 +01:00
parent a3880bab5e
commit 3db61a3509

View file

@ -6,11 +6,7 @@
<link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper.js"></script> <script type="text/javascript" src="../../dist/paper.js"></script>
<script type="text/paperscript" canvas="canvas"> <script type="text/paperscript" canvas="canvas">
var balls = []; var Ball = function(point, vector) {
var group = new Group();
var Ball = Base.extend({
initialize: function(point, vector) {
if (!vector || vector.isZero()) { if (!vector || vector.isZero()) {
this.vector = Point.random() * 5; this.vector = Point.random() * 5;
} else { } else {
@ -20,31 +16,31 @@
this.dampen = 0.4; this.dampen = 0.4;
this.gravity = 3; this.gravity = 3;
this.bounce = -0.6; this.bounce = -0.6;
this.radius = 50 * Math.random() + 30;
this.createPaths();
group.addChild(this.item);
},
createPaths: function() { var color = new HsbColor({
var overlayPos = this.point + this.radius / 8; hue: Math.random() * 360,
var compound = new CompoundPath([ saturation: 1,
new Path.Circle(this.point, this.radius), brightness: 1
new Path.Circle(overlayPos, this.radius / 2) });
]);
var color = new HsbColor(Math.random() * 360, 1, 1);
var gradient = new RadialGradient(color, 'black'); var gradient = new RadialGradient(color, 'black');
compound.fillColor = new GradientColor(gradient, this.point,
this.point + this.radius, overlayPos);
var overlay = new Path.Circle(overlayPos, this.radius / 2);
var overlayColor = color.clone();
var fullOverlay = color.clone();
overlayColor.alpha = 0.5;
var overlayGradient = new LinearGradient(new RgbColor(1, 1, 1, 0.5), new RgbColor(1, 1, 1, 1));
overlay.fillColor = new GradientColor(overlayGradient, overlayPos, overlayPos + this.radius / 2);
this.item = new Group(compound, overlay);
},
iterate: function() { var radius = this.radius = 50 * Math.random() + 30;
this.item = new CompoundPath({
children: [
new Path.Circle({
radius: radius
}),
new Path.Circle({
center: radius / 8,
radius: radius / 3
})
],
fillColor: new GradientColor(gradient, 0, radius, radius / 8),
position: this.point
});
}
Ball.prototype.iterate = function() {
var size = view.size; var size = view.size;
this.vector.y += this.gravity; this.vector.y += this.gravity;
this.vector.x *= 0.99; this.vector.x *= 0.99;
@ -59,10 +55,11 @@
var max = Point.max(this.radius, this.point + this.vector); var max = Point.max(this.radius, this.point + this.vector);
this.item.position = this.point = Point.min(max, size - this.radius); this.item.position = this.point = Point.min(max, size - this.radius);
this.item.rotate(this.vector.x / 2); this.item.rotate(this.vector.x);
} };
});
var balls = [];
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
var position = Point.random() * view.size, var position = Point.random() * view.size,
vector = (Point.random() - [0.5, 0]) * [50, 100], vector = (Point.random() - [0.5, 0]) * [50, 100],
@ -70,9 +67,11 @@
balls.push(ball); balls.push(ball);
} }
var textItem = new PointText(20, 30); var textItem = new PointText({
textItem.fillColor = 'black'; point: [20, 30],
textItem.content = 'Click, drag and release to add balls.' fillColor: 'black',
content: 'Click, drag and release to add balls.'
});
var lastDelta; var lastDelta;
function onMouseDrag(event) { function onMouseDrag(event) {