paper.js/examples/Scripts/BouncingBalls.html
2011-05-05 20:08:13 +01:00

100 lines
No EOL
2.8 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript">var root = '../../'</script>
<script type="text/javascript" src="../../src/load.js"></script>
<script type="text/paperscript" canvas="canvas">
var balls = [];
var circlePath = new Path.Circle([0, 0], 10);
circlePath.fillColor = new GradientColor(null, [0, 0], [0, 12], [0, 2]);
circlePath.fillColor.gradient.type = 'radial';
var symbol = new Symbol(circlePath);
var group = new Group();
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;
this.item.scale(this.radius / 10);
group.appendTop(this.item);
},
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);
},
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;
}
this.vector.y *= this.bounce;
}
this.point = this.point + this.vector;
this.contrainPoint();
this.item.position = this.point;
}
});
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;
function onMouseDrag(event) {
lastDelta = event.delta;
}
function onMouseUp(event) {
var ball = new Ball(event.point, lastDelta);
balls.push(ball);
lastDelta = null;
}
setInterval(draw, 30);
var boundPath = new Path.Rectangle([-10, -10], [-11, -11]);
boundPath.strokeColor = 'red';
boundPath.strokeWidth = 1;
function draw() {
for (var i = 0, l = balls.length; i < l; i++) {
balls[i].iterate();
}
boundPath.bounds = group.bounds;
document.redraw();
}
</script>
</head>
<body>
<p>Click to add circles:</p>
<canvas id='canvas' resize></canvas>
</body>