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">
|
|
|
|
<title>Example</title>
|
2011-03-07 18:11:41 -05:00
|
|
|
<script type="text/javascript">var loadBase = '../../'</script>
|
|
|
|
<script type="text/javascript" src="../../src/load.js"></script>
|
2011-03-04 06:38:38 -05:00
|
|
|
<script type="text/paperscript" canvas="canvas">
|
|
|
|
var balls = [];
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
var circlePath = new Path.Circle([0, 0], 10);
|
|
|
|
circlePath.fillColor = new GradientColor(null, [0, 0], [0, 12], [0, 2]);
|
|
|
|
circlePath.fillColor.gradient.type = 'radial';
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
var symbol = new Symbol(circlePath);
|
|
|
|
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 {
|
|
|
|
this.vector = vector * 1.5;
|
|
|
|
}
|
|
|
|
this.point = point;
|
|
|
|
this.dampen = 0.4;
|
|
|
|
this.gravity = 3;
|
|
|
|
this.bounce = -0.6;
|
|
|
|
this.radius = 50 * Math.random() + 10;
|
|
|
|
this.item = new PlacedSymbol(symbol);
|
|
|
|
this.item.position = point;
|
2011-03-06 09:52:38 -05:00
|
|
|
this.item.scale(this.radius / 10);
|
2011-03-06 09:10:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
group.appendTop(this.item);
|
|
|
|
},
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
contrainPoint: function() {
|
|
|
|
var x = this.point.x;
|
|
|
|
var y = this.point.y;
|
|
|
|
var radius = this.radius;
|
|
|
|
this.point.x = Math.min(Math.max(radius, x), document.size.width - radius);
|
|
|
|
this.point.y = Math.min(Math.max(radius, y), document.size.height - radius);
|
|
|
|
},
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
iterate: function() {
|
|
|
|
this.vector.y += this.gravity;
|
|
|
|
this.vector.x *= 0.99;
|
|
|
|
var pre = this.point + this.vector;
|
|
|
|
if (pre.x < this.radius || pre.x > document.size.width - this.radius)
|
|
|
|
this.vector.x *= -this.dampen;
|
|
|
|
if (pre.y < this.radius || pre.y > document.size.height - this.radius) {
|
|
|
|
if (Math.abs(this.vector.x) < 3) {
|
|
|
|
this.vector.x = Math.random() * 150 - 75;
|
|
|
|
this.vector.y = Math.random() * 100 + 20;
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
2011-03-04 06:38:38 -05:00
|
|
|
this.vector.y *= this.bounce;
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
2011-03-04 06:38:38 -05:00
|
|
|
this.point = this.point + this.vector;
|
|
|
|
this.contrainPoint();
|
|
|
|
this.item.position = this.point;
|
|
|
|
}
|
|
|
|
});
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
var position = Point.random() * document.size;
|
|
|
|
var vector = (Point.random() - [0.5, 0]) * [50, 100];
|
|
|
|
var ball = new Ball(position, vector);
|
|
|
|
balls.push(ball);
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastDelta;
|
2011-03-04 06:44:02 -05:00
|
|
|
function onMouseDrag(event) {
|
|
|
|
lastDelta = event.delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseUp(event) {
|
|
|
|
var ball = new Ball(event.point, lastDelta);
|
|
|
|
balls.push(ball);
|
|
|
|
lastDelta = null;
|
|
|
|
}
|
2011-03-04 06:38:38 -05:00
|
|
|
setInterval(draw, 30);
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
var boundPath = new Path.Rectangle([-10, -10], [-11, -11]);
|
|
|
|
boundPath.strokeColor = 'red';
|
|
|
|
boundPath.strokeWidth = 1;
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-03-04 06:38:38 -05:00
|
|
|
function draw() {
|
|
|
|
for (var i = 0, l = balls.length; i < l; i++) {
|
|
|
|
balls[i].iterate();
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
2011-03-04 06:38:38 -05:00
|
|
|
boundPath.bounds = group.bounds;
|
|
|
|
document.redraw();
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p>Click to add circles:</p>
|
|
|
|
<canvas id='canvas' width=1024 height=768></canvas>
|
|
|
|
</body>
|