PageSourceSearch

https://dribnet.github.io/sewn/p5App/block.js

js dribnet.github.io collected 2026-10-03 09:04:07 UTC 11,113 bytes, 438 lines download raw bytes

1// this decay can be disabled by the main program
2var decay_is_active = true;
3
4// split type
5const VERTICAL = 0;
6const HORIZONTAL = 1;
7
8// animation types
9const ANIM_NONE = 0;
10const LB_IN = 1;
11const LB_OUT = 2;
12const LT_IN = 3;
13const LT_OUT = 4;
14const RB_IN = 5;
15const RB_OUT = 6;
16const RT_IN = 7;
17const RT_OUT = 8;
18const TL_IN = 9;
19const TL_OUT = 10;
20const TR_IN = 11;
21const TR_OUT = 12;
22const BL_IN = 13;
23const BL_OUT = 14;
24const BR_IN = 15;
25const BR_OUT = 16;
26const LL_IN = 17;
27const LL_OUT = 18;
28const RR_IN = 19;
29const RR_OUT = 20;
30const BB_IN = 21;
31const BB_OUT = 22;
32const TT_IN = 23;
33const TT_OUT = 24;
34const TP_IN = 25;
35const TP_OUT = 26;
36const LP_IN = 27;
37const LP_OUT = 28;
38const BP_IN = 29;
39const BP_OUT = 30;
40const RP_IN = 31;
41const RP_OUT = 32;
42
43// color constants
44const pullVr = 0.4;
45const pullVg = 0.0;
46const pullVb = 0.0;
47const pullHr = 0.8;
48const pullHg = 0.6;
49const pullHb = 0.0;
50
51// millisecond based timer used to unsplit blocks after 30 seconds
52let theTime = 0;
53
54// OG lerp function (different argument order)
55function acuLerpf(A, l1, l2) {
56  return lerp(l1, l2, A);
57}
58
59function Block() {
60  this.hasChildren = false;
61  this.parent = null;
62  this.childA = null;
63  this.childB = null;
64  this.split = null;
65  this.splitPercent = 0;
66  this.L = 0;
67  this.R = 0;
68  this.B = 0;
69  this.T = 0;
70  this.cr = 0.5;
71  this.cg = 0.5;
72  this.cb = 0.5;
73  this.animateTime = 0.0;
74  this.complete = 1.0;
75  this.unsplitCountdown = 0;
76  this.unsplitBegun = false;
77  this.animType = ANIM_NONE;
78
79  this.Draw = function() {
80    if (this.parent == null) {
81      // only root sets time
82      theTime = millis();
83    }
84    if (this.hasChildren) {
85      this.childA.Draw();
86      this.childB.Draw();
87    }
88    else {
89      fill(this.cr * 255, this.cg * 255, this.cb * 255);
90      noStroke();
91      rect(this.L, this.B, this.R, this.T);
92    }
93  }
94
95  // lines get drawn after the rectangles
96  this.drawLines = function() {
97    if (this.hasChildren) {
98      this.childA.drawLines();
99      this.childB.drawLines();
100    }
101    else {
102      noFill();
103      stroke(int(0.2*255));
104      rect(this.L, this.T, this.R, this.B);
105    }
106
107    if ( this.hasChildren && ! this.unsplitBegun ) {
108      if ( this.childA.hasChildren || this.childB.hasChildren )
109        this.unsplitCountdown = theTime;
110      else {
111        if ( decay_is_active && theTime - this.unsplitCountdown > 30000 ) {
112          // 30 second delay
113          this.UnsplitBegin();
114        }
115      }
116    }
117  }
118
119  this.Split = function(S, P, extra_hint) {
120    if(this.hasChildren) {
121      // don't try to split twice
122      return;
123    }
124
125    this.unsplitCountdown = theTime;
126    this.hasChildren = true;
127    this.split = S;
128    // splitPercent will be computed
129    this.splitPercent = P;
130    this.childA = new Block();
131    this.childB = new Block();
132    this.childA.parent = this;
133    this.childB.parent = this;
134
135    let choose = random([false, true]);
136    if (extra_hint !== undefined) {
137      choose = extra_hint;
138    }
139    this.animateTime = 0.0;
140
141    if (this.split == VERTICAL) {
142      if (choose) {
143        this.animType = BP_IN;
144      }
145      else {
146        this.animType = TP_IN;
147      }
148    }
149    if (this.split == HORIZONTAL) {
150      if (choose) {
151        this.animType = LP_IN;
152      }
153      else {
154        this.animType = RP_IN;
155      }
156    }
157  }
158
159  this.UnsplitBegin = function() {
160    if (! this.hasChildren) {
161      return;
162    }
163
164    if (this.unsplitBegun) {
165      return;
166    }
167
168    this.unsplitBegun = true;
169
170    this.childA.UnsplitBegin();
171    this.childB.UnsplitBegin();
172
173    this.animateTime = 0.0;
174    let choose = random([false, true]);
175    if ( this.split==VERTICAL ) {
176      if (choose) {
177        this.animType = BP_OUT;
178      }
179      else {
180        this.animType = TP_OUT;
181      }
182    }
183    if ( this.split==HORIZONTAL ) { 
184      if (choose) {
185        this.animType = LP_OUT;
186      }
187      else {
188        this.animType = RP_OUT;
189      }
190    }
191  }
192
193  this.UnsplitFinish = function() {
194    if (! this.hasChildren) {
195      return;
196    }
197
198    this.unsplitBegun = false;
199    this.hasChildren = false;
200    this.complete = 1.0;
201    this.childA = null;
202    this.childB = null;
203    this.animType = ANIM_NONE;
204  }
205
206  // sub funciton implements goto logic in original
207  // returns true if calling function should also return
208  this.update_subroutine = function(P) {
209    if ( this.animType != ANIM_NONE ) {
210      this.childA.complete = this.animateTime;
211      this.childB.complete = this.animateTime;
212      if ( this.animateTime >= 1.0 ) {
213        if ( this.animType==LB_OUT || this.animType==LT_OUT ||
214             this.animType==RB_OUT || this.animType==RT_OUT ||
215             this.animType==TL_OUT || this.animType==TR_OUT ||
216             this.animType==BL_OUT || this.animType==BR_OUT ||
217             this.animType==LL_OUT || this.animType==RR_OUT ||
218             this.animType==BB_OUT || this.animType==TT_OUT || 
219             this.animType==TP_OUT || this.animType==LP_OUT || 
220             this.animType==BP_OUT || this.animType==RP_OUT ) {
221            this.UnsplitFinish();
222            this.animType = ANIM_NONE;
223            return true;
224        }
225        else {
226            this.animType = ANIM_NONE;
227            return false;
228        }
229      }
230      // note: animations run for 20 frames independent of wall time
231      this.animateTime += 0.05;
232    
233      let A = 0.0;
234      if ( this.animType==LB_OUT || this.animType==LT_OUT ||
235        this.animType==RB_OUT || this.animType==RT_OUT ||
236        this.animType==TL_OUT || this.animType==TR_OUT ||
237        this.animType==BL_OUT || this.animType==BR_OUT ||
238        this.animType==LL_OUT || this.animType==RR_OUT ||
239        this.animType==BB_OUT || this.animType==TT_OUT ||
240        this.animType==TP_OUT || this.animType==LP_OUT ||
241        this.animType==BP_OUT || this.animType==RP_OUT ) {
242
243        A = 1.0 - this.animateTime;
244      }
245      else {
246        A = this.animateTime;
247      }
248      if ( A > 1.0 ) {
249        A = 1.0;
250      }
251      if ( A < 0.0 ) {
252        A = 0.0;
253      }
254
255      if ( this.split == VERTICAL ) {
256        this.childA.setColor( acuLerpf( A, this.cr, (this.cr+pullVr)/2.0 ), 
257              acuLerpf( A, this.cg, (this.cg+pullVg)/2.0 ), 
258              acuLerpf( A, this.cb, (this.cb+pullVb)/2.0 ) );
259        this.childB.setColor( acuLerpf( A, this.cr, (this.cr+(1.0-pullVr))/2.0 ), 
260              acuLerpf( A, this.cg, (this.cg+(1.0-pullVg))/2.0 ), 
261              acuLerpf( A, this.cb, (this.cb+(1.0-pullVb))/2.0 ) );
262      }
263      if ( this.split == HORIZONTAL ) {
264        this.childA.setColor( acuLerpf( A, this.cr, (this.cr+pullHr)/2.0 ), 
265              acuLerpf( A, this.cg, (this.cg+pullHg)/2.0 ), 
266              acuLerpf( A, this.cb, (this.cb+pullHb)/2.0 ) );
267        this.childB.setColor( acuLerpf( A, this.cr, (this.cr+(1.0-pullHr))/2.0 ), 
268              acuLerpf( A, this.cg, (this.cg+(1.0-pullHg))/2.0 ), 
269              acuLerpf( A, this.cb, (this.cb+(1.0-pullHb))/2.0 ) );
270      }
271
272      let L = this.L;
273      let R = this.R;
274      let T = this.T;
275      let B = this.B;
276
277      switch( this.animType ) {
278        case TP_IN:
279        case TP_OUT:
280          this.childB.setLerpRect( A, L, T, R, T,
281             L, (T-B)*P+B, R, T );
282          this.childA.setLerpRect( A, L, B, R, T,
283             L, B, R, (T-B)*P+B );
284          break;
285        case BP_IN:
286        case BP_OUT:
287          this.childA.setLerpRect( A, L, B, R, B,
288             L, B, R, (T-B)*P+B );
289          this.childB.setLerpRect( A, L, B, R, T,
290             L, (T-B)*P+B, R, T );
291          break;
292        case LP_IN:
293        case LP_OUT:
294          this.childA.setLerpRect( A, L, B, L, T,
295                                  L, B, L+(R-L)*P, T );
296          this.childB.setLerpRect( A, L, B, R, T,
297                                  L+(R-L)*P, B, R, T );
298          break;
299        case RP_IN:
300        case RP_OUT:
301          this.childB.setLerpRect( A, R, B, R, T,
302                L+(R-L)*P, B, R, T );
303          this.childA.setLerpRect( A, L, B, R, T,
304                L, B, L+(R-L)*P, T );
305          break;
306      }
307    }
308    return false;
309  }
310
311  this.UpdateChildren = function() {
312    if(! this.hasChildren) {
313      return;
314    }
315
316    let P = this.splitPercent;
317    if ( P > 1.0 ) {
318      P = 1.0;
319    }
320    if ( P < 0.0 ) {
321      P = 0.0;
322    }
323
324    let do_return = this.update_subroutine(P);
325    if (do_return) {
326      return;
327    }
328  
329    let L = this.L;
330    let R = this.R;
331    let T = this.T;
332    let B = this.B;
333
334    if ( this.animType==ANIM_NONE ) {
335      // don't animate, just set in place
336      // P = 0.5;
337      if ( this.split == VERTICAL ) {
338        this.childA.setColor( (this.cr+pullVr)/2.0,
339              (this.cg+pullVg)/2.0, 
340              (this.cb+pullVb)/2.0  );
341        this.childB.setColor( (this.cr+(1.0-pullVr))/2.0,
342              (this.cg+(1.0-pullVg))/2.0,
343              (this.cb+(1.0-pullVb))/2.0  );
344        this.childA.setRect( L,B,R,(T-B)*P+B );
345        this.childB.setRect( L,(T-B)*P+B,R,T );
346        // childA.setRect( L,B,R,(T-B)*P+B );
347        // childB.setRect( L,(T-B)*P+B,R,T );
348      }
349      if ( this.split == HORIZONTAL ) {
350        this.childA.setColor( (this.cr+pullHr)/2.0,
351                          (this.cg+pullHg)/2.0, 
352                          (this.cb+pullHb)/2.0  );
353        this.childB.setColor( (this.cr+(1.0-pullHr))/2.0,
354                          (this.cg+(1.0-pullHg))/2.0,
355                          (this.cb+(1.0-pullHb))/2.0  );
356        this.childA.setRect( L,B,L+(R-L)*P,T );
357        this.childB.setRect( L+(R-L)*P,B,R,T );
358      }
359    }
360
361    this.childA.UpdateChildren();
362    this.childB.UpdateChildren();
363  }
364
365  this.setRect = function(l, b, r, t) {
366    this.L = l;
367    this.B = b;
368    this.R = r;
369    this.T = t;
370  }
371
372  this.setLerpRect = function(A, 
373                   l1, b1, r1, t1,
374                   l2, b2, r2, t2 ) {
375    A = (A<0) ? 0 : A;
376    A = (A>1) ? 1 : A;
377    A = Math.pow( A, 0.7 );
378    this.L = acuLerpf( A, l1, l2 );
379    this.T = acuLerpf( A, t1, t2 );
380    this.R = acuLerpf( A, r1, r2 );
381    this.B = acuLerpf( A, b1, b2 );
382  }
383
384  this.setColor = function(red, green, blue) {
385    this.cr = red;
386    this.cg = green;
387    this.cb = blue;
388  }
389
390  this.MouseInBlock = function(X, Y) {
391    if(this.hasChildren) {
392      let b1 = this.childA.MouseInBlock(X, Y);
393      if (b1 != null) {
394        return b1;
395      }
396      b1 = this.childB.MouseInBlock(X, Y);
397      if (b1 != null) {
398        return b1;
399      }
400      return null;
401    }
402    if ( X>=this.L && X<=this.R && Y<=this.T && Y>=this.B ) {
403      return this;
404    }
405    else {
406      return null;
407    }
408  }
409
410  this.getNumLeaves = function() {
411    if(! this.hasChildren) {
412      return 1;
413    }
414    else {
415      let l1 = this.childA.getNumLeaves();
416      let l2 = this.childB.getNumLeaves();
417      return l1 + l2;
418    }
419  }
420
421  this.BalanceSplit = function() {
422    if(! this.hasChildren) {
423      return;
424    }
425  
426    this.childA.BalanceSplit();
427    this.childB.BalanceSplit();
428  
429    let na = this.childA.getNumLeaves();
430    let nb = this.childB.getNumLeaves();
431    let D = (na+nb);
432    if ( D == 0 ) {
433      D = 1;
434    }
435
436    this.splitPercent = 0.95*this.splitPercent + 0.05*(na/(D));
437  }
438}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.