1 | <!DOCTYPE html>
|
---|
2 | <html>
|
---|
3 | <head>
|
---|
4 | <meta charset="UTF-8">
|
---|
5 | <title>Future Splash</title>
|
---|
6 | <link rel="stylesheet" href="../css/style.css">
|
---|
7 | <script type="text/javascript" src="../../dist/paper.js"></script>
|
---|
8 | <script type="text/paperscript" canvas="canvas">
|
---|
9 | // Code ported to Paper.js from http://the389.com/9/1/
|
---|
10 | // with permission.
|
---|
11 |
|
---|
12 | var values = {
|
---|
13 | friction: 0.8,
|
---|
14 | timeStep: 0.01,
|
---|
15 | amount: 15,
|
---|
16 | mass: 2,
|
---|
17 | count: 0
|
---|
18 | };
|
---|
19 | values.invMass = 1 / values.mass;
|
---|
20 |
|
---|
21 | var path, springs;
|
---|
22 | var size = view.size * [1.2, 1];
|
---|
23 |
|
---|
24 | var Spring = function(a, b, strength, restLength) {
|
---|
25 | this.a = a;
|
---|
26 | this.b = b;
|
---|
27 | this.restLength = restLength || 80;
|
---|
28 | this.strength = strength ? strength : 0.55;
|
---|
29 | this.mamb = values.invMass * values.invMass;
|
---|
30 | };
|
---|
31 |
|
---|
32 | Spring.prototype.update = function() {
|
---|
33 | var delta = this.b - this.a;
|
---|
34 | var dist = delta.length;
|
---|
35 | var normDistStrength = (dist - this.restLength) /
|
---|
36 | (dist * this.mamb) * this.strength;
|
---|
37 | delta.y *= normDistStrength * values.invMass * 0.2;
|
---|
38 | if (!this.a.fixed)
|
---|
39 | this.a.y += delta.y;
|
---|
40 | if (!this.b.fixed)
|
---|
41 | this.b.y -= delta.y;
|
---|
42 | };
|
---|
43 |
|
---|
44 |
|
---|
45 | function createPath(strength) {
|
---|
46 | var path = new Path({
|
---|
47 | fillColor: 'black'
|
---|
48 | });
|
---|
49 | springs = [];
|
---|
50 | for (var i = 0; i <= values.amount; i++) {
|
---|
51 | var segment = path.add(new Point(i / values.amount, 0.5) * size);
|
---|
52 | var point = segment.point;
|
---|
53 | if (i == 0 || i == values.amount)
|
---|
54 | point.y += size.height;
|
---|
55 | point.px = point.x;
|
---|
56 | point.py = point.y;
|
---|
57 | // The first two and last two points are fixed:
|
---|
58 | point.fixed = i < 2 || i > values.amount - 2;
|
---|
59 | if (i > 0) {
|
---|
60 | var spring = new Spring(segment.previous.point, point, strength);
|
---|
61 | springs.push(spring);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | path.position.x -= size.width / 4;
|
---|
65 | return path;
|
---|
66 | }
|
---|
67 |
|
---|
68 | function onResize() {
|
---|
69 | if (path)
|
---|
70 | path.remove();
|
---|
71 | size = view.bounds.size * [2, 1];
|
---|
72 | path = createPath(0.1);
|
---|
73 | }
|
---|
74 |
|
---|
75 | function onMouseMove(event) {
|
---|
76 | var location = path.getNearestLocation(event.point);
|
---|
77 | var segment = location.segment;
|
---|
78 | var point = segment.point;
|
---|
79 |
|
---|
80 | if (!point.fixed && location.distance < size.height / 4) {
|
---|
81 | var y = event.point.y;
|
---|
82 | point.y += (y - point.y) / 6;
|
---|
83 | if (segment.previous && !segment.previous.fixed) {
|
---|
84 | var previous = segment.previous.point;
|
---|
85 | previous.y += (y - previous.y) / 24;
|
---|
86 | }
|
---|
87 | if (segment.next && !segment.next.fixed) {
|
---|
88 | var next = segment.next.point;
|
---|
89 | next.y += (y - next.y) / 24;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | function onFrame(event) {
|
---|
95 | updateWave(path);
|
---|
96 | }
|
---|
97 |
|
---|
98 | function updateWave(path) {
|
---|
99 | var force = 1 - values.friction * values.timeStep * values.timeStep;
|
---|
100 | for (var i = 0, l = path.segments.length; i < l; i++) {
|
---|
101 | var point = path.segments[i].point;
|
---|
102 | var dy = (point.y - point.py) * force;
|
---|
103 | point.py = point.y;
|
---|
104 | point.y = Math.max(point.y + dy, 0);
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (var j = 0, l = springs.length; j < l; j++) {
|
---|
108 | springs[j].update();
|
---|
109 | }
|
---|
110 | path.smooth();
|
---|
111 | }
|
---|
112 |
|
---|
113 | function onKeyDown(event) {
|
---|
114 | if (event.key == 'space') {
|
---|
115 | path.fullySelected = !path.fullySelected;
|
---|
116 | path.fillColor = path.fullySelected ? null : 'black';
|
---|
117 | }
|
---|
118 | }
|
---|
119 | </script>
|
---|
120 | </head>
|
---|
121 | <body>
|
---|
122 | <canvas id="canvas" resize hidpi="off"></canvas>
|
---|
123 | </body>
|
---|
124 | </html> |
---|