forked from williamngan/pt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.field.js
More file actions
228 lines (176 loc) · 7.96 KB
/
Copy pathvector.field.js
File metadata and controls
228 lines (176 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This is generated from ES6 javascript source file.
// See original ES6 source code at https://github.com/williamngan/pt/tree/master/demo/es6/src
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
window.demoDescription = "If this is a sphere, it would visualize Hairy Ball Theorem, where you can't comb a hairy ball flat without creating a cowlick. This demo was done in javascript ES6 and compiled with babel.";
var space = new CanvasSpace("demo", "#f5ead6").display();
var form = new Form(space);
var center = space.size.$divide(2);
// A Circle that follows mouse and comb the VectorLines
var Comb = (function (_Circle) {
_inherits(Comb, _Circle);
function Comb() {
_classCallCheck(this, Comb);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_get(Object.getPrototypeOf(Comb.prototype), "constructor", this).apply(this, args);
this.lastPos = new Vector();
this.offset = new Vector();
this.radius = 50;
}
// create comb and add to space
_createClass(Comb, [{
key: "move",
value: function move(x, y) {
this.lastPos.set(this);
this.set(x, y);
this.offset = this.$subtract(this.lastPos).normalize();
this.lastPos.set(x, y);
}
}, {
key: "animate",
value: function animate(time, frame, ctx) {}
// follow mouse movement
}, {
key: "onMouseAction",
value: function onMouseAction(type, x, y, evt) {
if (type == "move") {
this.move(x, y);
}
}
}]);
return Comb;
})(Circle);
var comb = new Comb();
space.add(comb);
// VectorLine is a hair-like point that can be "combed"
var VectorLine = (function (_Vector) {
_inherits(VectorLine, _Vector);
function VectorLine() {
_classCallCheck(this, VectorLine);
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
_get(Object.getPrototypeOf(VectorLine.prototype), "constructor", this).apply(this, args);
this.direction = new Vector(); // hair direction
this.pointer = new Vector(); // hair
this.pulled = false; // has it been pulled by comb
this.moved = false; // has it been touched and not yet reset
this.pull_power = 9;
this.mag = 20;
this.intensity = .3;
this.resetTimer = -1;
}
// SamplePoints is a kind of PointSet in pt.ext. It distributes a set of points and optimizes for uniform distance.
_createClass(VectorLine, [{
key: "initVec",
value: function initVec() {
var _direction, _pointer;
(_direction = this.direction).set.apply(_direction, arguments).normalize();
(_pointer = this.pointer).set.apply(_pointer, arguments).multiply(this.mag / 4);
return this;
}
}, {
key: "animate",
value: function animate(time, frame, ctx) {
var c = this.checkRange();
form.stroke(c).line(new Line(this).to(this.pointer).relative());
// reset to initial direction (when clicked)
if (this.resetTimer >= 0) this.resetTimer += frame;
if (this.resetTimer > 0 && this.resetTimer < 2000) {
var ang = center.$subtract(this).angle();
var dir = new Vector(Math.cos(ang), Math.sin(ang));
this.direction.set(dir);
this.pointer.add(dir).divide(1.2);
}
}
// track mouse up
}, {
key: "onMouseAction",
value: function onMouseAction(type, x, y, evt) {
if (type == "up" && this.moved) {
this.resetTimer = 0;
this.moved = false;
}
}
}, {
key: "checkRange",
value: function checkRange() {
var color = undefined;
// pull it when intersecting with comb
if (comb.intersectPoint(this.x, this.y)) {
this.intensity = 0.5;
var dm = 1 - this.distance(comb) / comb.radius;
this.pull_power = this.pull_power > 0 ? this.pull_power - 0.25 : 0;
this.pointer.add(comb.offset.$multiply(dm * this.pull_power));
this.direction.set(this.pointer).normalize();
this.pulled = true;
this.moved = true;
color = "rgba(" + Math.ceil(this.pointer.y / comb.radius * 100 + 150) + ", 50, " + Math.ceil(this.pointer.x / comb.radius * 100 + 150);
// not pulled
} else {
this.pull_power = 9; // reset pull power
if (this.pulled) {
// transition back to maximum magnitude value
var _mag = this.pointer.magnitude();
if (_mag > this.mag) {
this.pointer.set(this.direction.$multiply(_mag * 0.95));
} else {
this.pulled = false;
}
this.intensity = Math.min(0.4, Math.max(5, _mag / (this.mag * 5)));
}
color = this.pointer.y > 0 ? "rgba(255,0,50" : "rgba(50,0,20";
}
return color + "," + this.intensity + ")";
}
}]);
return VectorLine;
})(Vector);
var samples = new SamplePoints();
samples.setBounds(new Rectangle().size(space.size.x, space.size.y), true);
samples.poissonSampler(5); // target 5px radius
var lastTime = 0;
var counter = 0;
// fill canvas in white initially and no refresh
space.refresh(false);
space.clear("#f5ead6");
space.add({
animate: function animate(time, frameTime, ctx) {
if (counter > 3000) {
// to complete at 3000 points
for (var i = 0; i < samples.count(); i++) {
form.point(samples.getAt(i), 2, true);
var vecline = new VectorLine(samples.getAt(i));
var dir = center.$subtract(samples.getAt(i)).angle();
vecline.initVec(Math.cos(dir), Math.sin(dir));
space.add(vecline);
}
// remove this actor when done
space.remove(this);
space.refresh(true);
}
// add 50 points per 25ms
var count = 0;
while (time - lastTime < 25 && count++ < 50) {
var s = samples.sample(10, 'poisson');
ctx.fillStyle = '#fff';
if (s) {
form.fill("rgba(50,0,20,.3)").stroke(false).point(s, 1);
// add to sample point set and increment counter
samples.to(s);
counter++;
}
}
// fade out effect
form.fill("rgba(245,234,214,.1").rect(new Rectangle().to(space.size));
lastTime = time;
}
});
// 4. Start playing
space.bindMouse();
space.play();