paper.js/examples/Paperjs.org/FutureSplash.html

123 lines
4 KiB
HTML
Raw Normal View History

2011-06-30 07:55:35 -04:00
<!DOCTYPE html>
<html>
<head>
2014-08-16 13:24:54 -04:00
<meta charset="UTF-8">
<title>Future Splash</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper-full.js"></script>
<script type="text/paperscript" canvas="canvas">
// Code ported to Paper.js from http://the389.com/9/1/
// with permission.
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
var values = {
friction: 0.8,
timeStep: 0.01,
amount: 15,
mass: 2,
count: 0
};
values.invMass = 1 / values.mass;
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
var path, springs;
var size = view.size * [1.2, 1];
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
var Spring = function(a, b, strength, restLength) {
this.a = a;
this.b = b;
this.restLength = restLength || 80;
this.strength = strength ? strength : 0.55;
this.mamb = values.invMass * values.invMass;
};
2013-03-09 17:37:56 -05:00
2014-08-16 13:24:54 -04:00
Spring.prototype.update = function() {
var delta = this.b - this.a;
var dist = delta.length;
var normDistStrength = (dist - this.restLength) /
(dist * this.mamb) * this.strength;
delta.y *= normDistStrength * values.invMass * 0.2;
if (!this.a.fixed)
this.a.y += delta.y;
if (!this.b.fixed)
this.b.y -= delta.y;
};
2011-07-07 10:09:02 -04:00
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
function createPath(strength) {
var path = new Path({
fillColor: 'black'
});
springs = [];
for (var i = 0; i <= values.amount; i++) {
var segment = path.add(new Point(i / values.amount, 0.5) * size);
var point = segment.point;
if (i == 0 || i == values.amount)
point.y += size.height;
point.px = point.x;
point.py = point.y;
// The first two and last two points are fixed:
point.fixed = i < 2 || i > values.amount - 2;
if (i > 0) {
var spring = new Spring(segment.previous.point, point, strength);
springs.push(spring);
}
}
path.position.x -= size.width / 4;
return path;
}
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
function onResize() {
if (path)
path.remove();
size = view.bounds.size * [2, 1];
path = createPath(0.1);
}
2011-06-30 08:01:18 -04:00
2014-08-16 13:24:54 -04:00
function onMouseMove(event) {
var location = path.getNearestLocation(event.point);
var segment = location.segment;
var point = segment.point;
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
if (!point.fixed && location.distance < size.height / 4) {
var y = event.point.y;
point.y += (y - point.y) / 6;
2015-03-07 05:05:57 -05:00
var previous = segment.previous && segment.previous.point;
var next = segment.next && segment.next.point;
if (previous && !previous.fixed)
2014-08-16 13:24:54 -04:00
previous.y += (y - previous.y) / 24;
2015-03-07 05:05:57 -05:00
if (next && !next.fixed)
2014-08-16 13:24:54 -04:00
next.y += (y - next.y) / 24;
}
}
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
function onFrame(event) {
updateWave(path);
}
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
function updateWave(path) {
var force = 1 - values.friction * values.timeStep * values.timeStep;
for (var i = 0, l = path.segments.length; i < l; i++) {
var point = path.segments[i].point;
var dy = (point.y - point.py) * force;
point.py = point.y;
point.y = Math.max(point.y + dy, 0);
}
2011-06-30 07:55:35 -04:00
2014-08-16 13:24:54 -04:00
for (var j = 0, l = springs.length; j < l; j++) {
springs[j].update();
}
path.smooth({ type: 'continuous' });
2014-08-16 13:24:54 -04:00
}
2011-06-30 08:01:18 -04:00
2014-08-16 13:24:54 -04:00
function onKeyDown(event) {
if (event.key == 'space') {
path.fullySelected = !path.fullySelected;
path.fillColor = path.fullySelected ? null : 'black';
}
}
</script>
2011-06-30 07:55:35 -04:00
</head>
<body>
2014-08-16 13:24:54 -04:00
<canvas id="canvas" resize hidpi="off"></canvas>
2011-06-30 07:55:35 -04:00
</body>
</html>