2011-02-21 08:31:26 -05:00
|
|
|
<!DOCTYPE html>
|
2011-02-16 18:35:10 -05:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
2011-06-30 09:57:17 -04:00
|
|
|
<title>Bouncing Balls</title>
|
2011-05-05 11:25:17 -04:00
|
|
|
<link rel="stylesheet" href="../css/style.css">
|
2011-06-12 14:03:18 -04:00
|
|
|
<script type="text/javascript" src="../../dist/paper.js"></script>
|
2011-03-04 06:38:38 -05:00
|
|
|
<script type="text/paperscript" canvas="canvas">
|
2011-05-15 06:36:10 -04:00
|
|
|
var balls = [];
|
|
|
|
var group = new Group();
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
var Ball = Base.extend({
|
|
|
|
initialize: function(point, vector) {
|
|
|
|
if (!vector || vector.isZero()) {
|
|
|
|
this.vector = Point.random() * 5;
|
|
|
|
} else {
|
2011-06-19 11:34:32 -04:00
|
|
|
this.vector = vector * 2;
|
2011-03-04 06:38:38 -05:00
|
|
|
}
|
|
|
|
this.point = point;
|
|
|
|
this.dampen = 0.4;
|
|
|
|
this.gravity = 3;
|
|
|
|
this.bounce = -0.6;
|
2011-06-19 11:34:32 -04:00
|
|
|
this.radius = 50 * Math.random() + 30;
|
|
|
|
this.createPaths();
|
|
|
|
group.addChild(this.item);
|
|
|
|
},
|
|
|
|
|
|
|
|
createPaths: function() {
|
|
|
|
var overlayPos = this.point + this.radius / 8;
|
|
|
|
var compound = new CompoundPath([
|
|
|
|
new Path.Circle(this.point, this.radius),
|
|
|
|
new Path.Circle(overlayPos, this.radius / 2)
|
|
|
|
]);
|
2011-11-10 13:16:34 -05:00
|
|
|
var color = new HsbColor(Math.random() * 360, 1, 1);
|
2013-03-01 20:44:16 -05:00
|
|
|
var gradient = new RadialGradient(color, 'black');
|
2012-11-22 15:08:17 -05:00
|
|
|
compound.fillColor = new GradientColor(gradient, this.point,
|
2011-06-19 11:34:32 -04:00
|
|
|
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;
|
2013-03-01 20:44:16 -05:00
|
|
|
var overlayGradient = new LinearGradient(new RgbColor(1, 1, 1, 0.5), new RgbColor(1, 1, 1, 1));
|
2011-06-19 11:34:32 -04:00
|
|
|
overlay.fillColor = new GradientColor(overlayGradient, overlayPos, overlayPos + this.radius / 2);
|
2012-11-06 23:37:50 -05:00
|
|
|
this.item = new Group(compound, overlay);
|
2011-03-04 06:38:38 -05:00
|
|
|
},
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
iterate: function() {
|
2011-05-16 07:51:20 -04:00
|
|
|
var size = view.size;
|
2011-03-04 06:38:38 -05:00
|
|
|
this.vector.y += this.gravity;
|
|
|
|
this.vector.x *= 0.99;
|
|
|
|
var pre = this.point + this.vector;
|
2011-05-06 10:34:27 -04:00
|
|
|
if (pre.x < this.radius || pre.x > size.width - this.radius)
|
2011-07-07 10:09:02 -04:00
|
|
|
this.vector.x *= -this.dampen;
|
2011-05-06 10:34:27 -04:00
|
|
|
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];
|
2011-03-04 06:38:38 -05:00
|
|
|
this.vector.y *= this.bounce;
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
2011-06-19 11:34:32 -04:00
|
|
|
|
2011-05-06 10:34:27 -04:00
|
|
|
var max = Point.max(this.radius, this.point + this.vector);
|
|
|
|
this.item.position = this.point = Point.min(max, size - this.radius);
|
2011-05-08 10:44:19 -04:00
|
|
|
this.item.rotate(this.vector.x / 2);
|
2011-03-04 06:38:38 -05:00
|
|
|
}
|
|
|
|
});
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
for (var i = 0; i < 10; i++) {
|
2011-06-19 11:34:32 -04:00
|
|
|
var position = Point.random() * view.size,
|
|
|
|
vector = (Point.random() - [0.5, 0]) * [50, 100],
|
|
|
|
ball = new Ball(position, vector);
|
2011-03-04 06:38:38 -05:00
|
|
|
balls.push(ball);
|
|
|
|
}
|
|
|
|
|
2011-06-19 11:34:32 -04:00
|
|
|
var textItem = new PointText(20, 30);
|
|
|
|
textItem.fillColor = 'black';
|
|
|
|
textItem.content = 'Click, drag and release to add balls.'
|
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
var lastDelta;
|
2011-03-04 06:44:02 -05:00
|
|
|
function onMouseDrag(event) {
|
|
|
|
lastDelta = event.delta;
|
|
|
|
}
|
2011-06-19 11:34:32 -04:00
|
|
|
|
2011-03-04 06:44:02 -05:00
|
|
|
function onMouseUp(event) {
|
|
|
|
var ball = new Ball(event.point, lastDelta);
|
|
|
|
balls.push(ball);
|
|
|
|
lastDelta = null;
|
|
|
|
}
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-05-06 10:34:27 -04:00
|
|
|
function onFrame() {
|
|
|
|
for (var i = 0, l = balls.length; i < l; i++)
|
2011-03-04 06:38:38 -05:00
|
|
|
balls[i].iterate();
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
2011-06-29 07:44:06 -04:00
|
|
|
<canvas id="canvas" resize></canvas>
|
2011-05-30 18:27:39 -04:00
|
|
|
</body>
|
|
|
|
</html>
|