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-03 11:56:31 -05:00
|
|
|
<script type="text/javascript" src="../../lib/bootstrap.js"></script>
|
2011-02-16 18:35:10 -05:00
|
|
|
<script type="text/javascript" src="../../src/Paper.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/tool/ToolEvent.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/tool/ToolHandler.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/tool/Tool.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/basic/Point.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/basic/Rectangle.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/basic/Size.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/basic/Matrix.js"></script>
|
2011-03-02 11:18:32 -05:00
|
|
|
<script type="text/javascript" src="../../src/document/DocumentView.js"></script>
|
2011-03-03 12:23:27 -05:00
|
|
|
<script type="text/javascript" src="../../src/document/Document.js"></script>
|
2011-02-20 17:28:38 -05:00
|
|
|
<script type="text/javascript" src="../../src/document/Symbol.js"></script>
|
2011-02-16 18:35:10 -05:00
|
|
|
<script type="text/javascript" src="../../src/item/Item.js"></script>
|
2011-02-20 17:28:38 -05:00
|
|
|
<script type="text/javascript" src="../../src/item/PlacedSymbol.js"></script>
|
2011-02-16 18:35:10 -05:00
|
|
|
<script type="text/javascript" src="../../src/item/Group.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/item/Layer.js"></script>
|
2011-03-02 20:25:45 -05:00
|
|
|
<script type="text/javascript" src="../../src/item/Raster.js"></script>
|
2011-02-16 18:35:10 -05:00
|
|
|
<script type="text/javascript" src="../../src/item/PathStyle.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/path/Segment.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/path/PathItem.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/path/Path.js"></script>
|
2011-03-02 20:25:45 -05:00
|
|
|
<script type="text/javascript" src="../../src/path/CompoundPath.js"></script>
|
2011-02-17 09:56:07 -05:00
|
|
|
<script type="text/javascript" src="../../src/path/Path.Constructors.js"></script>
|
2011-02-19 11:56:15 -05:00
|
|
|
<script type="text/javascript" src="../../src/color/Color.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/color/RGBColor.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/color/GrayColor.js"></script>
|
2011-02-20 17:28:38 -05:00
|
|
|
<script type="text/javascript" src="../../src/color/GradientColor.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/color/Gradient.js"></script>
|
|
|
|
<script type="text/javascript" src="../../src/color/GradientStop.js"></script>
|
2011-02-16 18:35:10 -05:00
|
|
|
<script>
|
|
|
|
window.onload = function() {
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 17:28:38 -05:00
|
|
|
var canvas = document.getElementById('canvas');
|
2011-03-03 12:23:27 -05:00
|
|
|
var doc = new Document(canvas);
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 17:28:38 -05:00
|
|
|
var balls = [];
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 17:28:38 -05:00
|
|
|
var circlePath = new Path.Circle([0, 0], 10);
|
2011-02-20 21:37:35 -05:00
|
|
|
circlePath.fillColor = new GradientColor(null, [0, 0], [0, 12], [0, 2]);
|
|
|
|
circlePath.fillColor.gradient.type = 'radial';
|
|
|
|
|
|
|
|
var symbol = new Symbol(circlePath);
|
2011-03-02 20:25:45 -05:00
|
|
|
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 17:28:38 -05:00
|
|
|
var group = new Group();
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-16 18:35:10 -05:00
|
|
|
var Ball = Base.extend({
|
|
|
|
initialize: function(point, vector) {
|
2011-02-21 09:32:59 -05:00
|
|
|
if (!vector || vector.isZero()) {
|
2011-02-20 17:28:38 -05:00
|
|
|
this.vector = Point.random().multiply(5);
|
|
|
|
} else {
|
2011-02-21 09:32:59 -05:00
|
|
|
this.vector = vector.multiply(1.5);
|
2011-02-20 17:28:38 -05:00
|
|
|
}
|
2011-02-16 18:35:10 -05:00
|
|
|
this.point = point;
|
|
|
|
this.dampen = 0.4;
|
|
|
|
this.gravity = 3;
|
2011-02-20 17:28:38 -05:00
|
|
|
this.bounce = -0.6;
|
2011-02-16 18:35:10 -05:00
|
|
|
this.radius = 50 * Math.random() + 10;
|
2011-02-20 17:28:38 -05:00
|
|
|
this.item = new PlacedSymbol(symbol);
|
|
|
|
this.item.position = point;
|
|
|
|
this.item.scale(this.radius / 10);
|
|
|
|
group.appendTop(this.item);
|
2011-02-16 18:35:10 -05:00
|
|
|
},
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 17:28: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), doc.size.width - radius);
|
|
|
|
this.point.y = Math.min(Math.max(radius, y), doc.size.height - radius);
|
|
|
|
},
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-16 18:35:10 -05:00
|
|
|
iterate: function() {
|
|
|
|
this.vector.y += this.gravity;
|
2011-02-20 17:28:38 -05:00
|
|
|
this.vector.x *= 0.99;
|
2011-02-16 18:35:10 -05:00
|
|
|
var pre = this.point.add(this.vector);
|
2011-02-20 17:28:38 -05:00
|
|
|
if (pre.x < this.radius || pre.x > doc.size.width - this.radius)
|
2011-02-16 18:35:10 -05:00
|
|
|
this.vector.x *= -this.dampen;
|
2011-02-20 17:28:38 -05:00
|
|
|
if (pre.y < this.radius || pre.y > doc.size.height - this.radius) {
|
2011-02-20 21:32:39 -05:00
|
|
|
if (Math.abs(this.vector.x) < 3) {
|
2011-02-20 17:28:38 -05:00
|
|
|
this.vector.x = Math.random() * 150 - 75;
|
|
|
|
this.vector.y = Math.random() * 100 + 20;
|
|
|
|
}
|
2011-02-16 18:35:10 -05:00
|
|
|
this.vector.y *= this.bounce;
|
|
|
|
}
|
2011-02-20 17:28:38 -05:00
|
|
|
this.point = this.point.add(this.vector);
|
|
|
|
this.contrainPoint();
|
|
|
|
this.item.position = this.point;
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
|
|
|
});
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 21:32:39 -05:00
|
|
|
for (var i = 0; i < 10; i++) {
|
2011-02-20 17:28:38 -05:00
|
|
|
var position = Point.random().multiply(doc.size);
|
|
|
|
var vector = Point.random().subtract(0.5, 0).multiply(50, 100);
|
|
|
|
var ball = new Ball(position, vector);
|
|
|
|
balls.push(ball);
|
|
|
|
}
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-21 09:32:59 -05:00
|
|
|
var lastDelta;
|
2011-02-16 18:35:10 -05:00
|
|
|
tool = new Tool({
|
|
|
|
onMouseDrag: function(event) {
|
2011-02-21 09:32:59 -05:00
|
|
|
lastDelta = event.delta;
|
2011-02-16 18:35:10 -05:00
|
|
|
},
|
|
|
|
onMouseUp: function(event) {
|
2011-02-21 09:32:59 -05:00
|
|
|
var ball = new Ball(event.point, lastDelta);
|
2011-02-20 17:28:38 -05:00
|
|
|
balls.push(ball);
|
2011-02-21 09:32:59 -05:00
|
|
|
lastDelta = null;
|
2011-02-16 18:35:10 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
setInterval(draw, 30);
|
2011-02-20 21:37:35 -05:00
|
|
|
|
2011-02-20 17:28: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-02-20 17:28:38 -05:00
|
|
|
function draw() {
|
|
|
|
for (var i = 0, l = balls.length; i < l; i++) {
|
|
|
|
balls[i].iterate();
|
|
|
|
}
|
|
|
|
boundPath.bounds = group.bounds;
|
2011-02-16 18:35:10 -05:00
|
|
|
doc.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p>Click to add circles:</p>
|
|
|
|
<canvas id='canvas' width=1024 height=768></canvas>
|
|
|
|
</body>
|