2011-02-21 14:31:26 +01:00
|
|
|
<!DOCTYPE html>
|
2011-02-17 00:35:10 +01:00
|
|
|
<html>
|
|
|
|
<head>
|
2013-06-02 13:41:10 -07:00
|
|
|
<meta charset="UTF-8">
|
2011-06-30 09:57:17 -04:00
|
|
|
<title>Bouncing Balls</title>
|
2011-05-05 16:25:17 +01:00
|
|
|
<link rel="stylesheet" href="../css/style.css">
|
2014-04-06 13:44:19 +02:00
|
|
|
<script type="text/javascript" src="../../dist/paper-full.js"></script>
|
2011-03-04 12:38:38 +01:00
|
|
|
<script type="text/paperscript" canvas="canvas">
|
2013-03-02 20:58:56 +01:00
|
|
|
var Ball = function(point, vector) {
|
|
|
|
if (!vector || vector.isZero()) {
|
|
|
|
this.vector = Point.random() * 5;
|
|
|
|
} else {
|
|
|
|
this.vector = vector * 2;
|
|
|
|
}
|
|
|
|
this.point = point;
|
|
|
|
this.dampen = 0.4;
|
|
|
|
this.gravity = 3;
|
|
|
|
this.bounce = -0.6;
|
2011-06-19 17:34:32 +02:00
|
|
|
|
2013-04-07 10:03:51 -07:00
|
|
|
var color = {
|
2013-03-02 20:58:56 +01:00
|
|
|
hue: Math.random() * 360,
|
|
|
|
saturation: 1,
|
|
|
|
brightness: 1
|
2013-04-07 10:03:51 -07:00
|
|
|
};
|
2013-04-08 20:52:21 -07:00
|
|
|
var gradient = new Gradient([color, 'black'], true);
|
2011-02-21 03:37:35 +01:00
|
|
|
|
2013-03-02 20:58:56 +01:00
|
|
|
var radius = this.radius = 50 * Math.random() + 30;
|
2014-04-06 13:44:19 +02:00
|
|
|
// Wrap CompoundPath in a Group, since CompoundPaths directly
|
2013-04-19 13:52:17 -07:00
|
|
|
// applies the transformations to the content, just like Path.
|
2013-05-13 18:57:17 -07:00
|
|
|
var ball = new CompoundPath({
|
2013-03-02 20:58:56 +01:00
|
|
|
children: [
|
2013-05-13 18:57:17 -07:00
|
|
|
new Path.Circle({
|
|
|
|
radius: radius
|
|
|
|
}),
|
|
|
|
new Path.Circle({
|
|
|
|
center: radius / 8,
|
|
|
|
radius: radius / 3
|
2013-03-02 20:58:56 +01:00
|
|
|
})
|
|
|
|
],
|
2013-05-13 18:57:17 -07:00
|
|
|
fillColor: new Color(gradient, 0, radius, radius / 8),
|
|
|
|
});
|
|
|
|
|
2013-06-24 09:32:36 -07:00
|
|
|
this.item = new Group({
|
2013-05-13 18:57:17 -07:00
|
|
|
children: [ball],
|
2014-03-02 16:04:17 +01:00
|
|
|
applyMatrix: false,
|
2013-03-02 20:58:56 +01:00
|
|
|
position: this.point
|
|
|
|
});
|
|
|
|
}
|
2011-06-19 17:34:32 +02:00
|
|
|
|
2013-03-02 20:58:56 +01:00
|
|
|
Ball.prototype.iterate = function() {
|
|
|
|
var size = view.size;
|
|
|
|
this.vector.y += this.gravity;
|
|
|
|
this.vector.x *= 0.99;
|
|
|
|
var pre = this.point + this.vector;
|
|
|
|
if (pre.x < this.radius || pre.x > size.width - this.radius)
|
|
|
|
this.vector.x *= -this.dampen;
|
|
|
|
if (pre.y < this.radius || pre.y > size.height - this.radius) {
|
|
|
|
if (Math.abs(this.vector.x) < 3)
|
|
|
|
this.vector = Point.random() * [150, 100] + [-75, 20];
|
|
|
|
this.vector.y *= this.bounce;
|
2011-03-04 12:38:38 +01:00
|
|
|
}
|
2011-02-21 03:37:35 +01:00
|
|
|
|
2013-03-02 20:58:56 +01:00
|
|
|
var max = Point.max(this.radius, this.point + this.vector);
|
|
|
|
this.item.position = this.point = Point.min(max, size - this.radius);
|
|
|
|
this.item.rotate(this.vector.x);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var balls = [];
|
2011-03-04 12:38:38 +01:00
|
|
|
for (var i = 0; i < 10; i++) {
|
2011-06-19 17:34:32 +02:00
|
|
|
var position = Point.random() * view.size,
|
|
|
|
vector = (Point.random() - [0.5, 0]) * [50, 100],
|
|
|
|
ball = new Ball(position, vector);
|
2011-03-04 12:38:38 +01:00
|
|
|
balls.push(ball);
|
|
|
|
}
|
|
|
|
|
2013-03-02 20:58:56 +01:00
|
|
|
var textItem = new PointText({
|
|
|
|
point: [20, 30],
|
|
|
|
fillColor: 'black',
|
|
|
|
content: 'Click, drag and release to add balls.'
|
|
|
|
});
|
2011-06-19 17:34:32 +02:00
|
|
|
|
2011-03-04 12:38:38 +01:00
|
|
|
var lastDelta;
|
2011-03-04 12:44:02 +01:00
|
|
|
function onMouseDrag(event) {
|
|
|
|
lastDelta = event.delta;
|
|
|
|
}
|
2011-06-19 17:34:32 +02:00
|
|
|
|
2011-03-04 12:44:02 +01:00
|
|
|
function onMouseUp(event) {
|
|
|
|
var ball = new Ball(event.point, lastDelta);
|
|
|
|
balls.push(ball);
|
|
|
|
lastDelta = null;
|
|
|
|
}
|
2011-02-21 03:37:35 +01:00
|
|
|
|
2011-05-06 15:34:27 +01:00
|
|
|
function onFrame() {
|
|
|
|
for (var i = 0, l = balls.length; i < l; i++)
|
2011-03-04 12:38:38 +01:00
|
|
|
balls[i].iterate();
|
2011-02-17 00:35:10 +01:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
2011-06-29 13:44:06 +02:00
|
|
|
<canvas id="canvas" resize></canvas>
|
2011-05-31 00:27:39 +02:00
|
|
|
</body>
|
2014-04-06 13:44:19 +02:00
|
|
|
</html>
|