Repository navigation
Expand file tree
/
Copy pathvector.field.js
More file actions
181 lines (139 loc) · 4.82 KB
/
Copy pathvector.field.js
File metadata and controls
181 lines (139 loc) · 4.82 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
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
class Comb extends Circle {
constructor( ...args ) {
super( ...args );
this.lastPos = new Vector();
this.offset = new Vector();
this.radius = 50;
}
move( x, y ) {
this.lastPos.set( this );
this.set( x, y );
this.offset = this.$subtract( this.lastPos ).normalize();
this.lastPos.set( x, y );
}
animate( time, frame, ctx ) {
}
// follow mouse movement
onMouseAction( type, x, y, evt ) {
if (type == "move") {
this.move( x, y );
}
}
}
// create comb and add to space
var comb = new Comb();
space.add( comb );
// VectorLine is a hair-like point that can be "combed"
class VectorLine extends Vector {
constructor( ...args ) {
super( ...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;
}
initVec( ...args ) {
this.direction.set( ...args ).normalize();
this.pointer.set( ...args ).multiply( this.mag / 4 );
return this;
}
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
onMouseAction( type, x, y, evt ) {
if (type == "up" && this.moved) {
this.resetTimer = 0;
this.moved = false;
}
}
checkRange() {
let color;
// pull it when intersecting with comb
if (comb.intersectPoint( this.x, this.y )) {
this.intensity = 0.5;
let 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
let _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})`;
}
}
// SamplePoints is a kind of PointSet in pt.ext. It distributes a set of points and optimizes for uniform distance.
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(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 );
let vecline = new VectorLine( samples.getAt( i ) );
let 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) {
let 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();