forked from GalenIvanov/Animation-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimate.red
2297 lines (2098 loc) · 81 KB
/
Animate.red
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Red [
title: "Animation dialect tests"
author: "Galen Ivanov"
needs: view
]
; Docs and test at %https://github.com/GalenIvanov/Animation-tests
random/seed now
context [
st-time: now/precise
prev-t: 0.0 ;
text-data: make map! 20
draw-blocks-data: make map! 20
timeline: make map! 100 ; the timeline of effects key: value <- id: effect
time-map: make map! 100 ; for the named animations
text-fx-map: make map! 10
particles-map: make map! 10
curve-fx-map: make map! 10
curve-fx-init: make block! 10
scaled-fonts: copy []
stroke-path-map: make map! 10
morph-path-map: make map! 10
two-way-map: make map! 10 ; keeps a track of the way each two-way loop is running
named-animations: make map! 20 ;
dummy: 0
effect: make object! [
val1: 0.0 ; starting value to change
val2: 1.0 ; end value
start: 0.0 ; starting time
dur: 1.0 ; duration of the animation
delay: 0.0 ; delay between successive subanimations
loop-count: 1 ; repetitions of the effect in time
bi-dir: false ; does the animation runs backwards too?
dir: 'forth ; which way a two-way loop is going
started: false ; has the animation started?
paused: false ; true if the animation has been paused
elapsed: 0.0 ; elapsed time from the animation start
speed: 1.0 ; animation speed
on-start: [] ; actor
on-time: [] ; actor
on-exit: [] ; actor
ease: none ; easing function
]
text-effect: make object! [
id: none ;
text: "" ; text to render
font: none ; font to use
mode: 'chars ; how to split the text
from: 'center ; origin of scaling
posXY: 0x0 ; where to place the text
sp-x: 1.0 ; spacing factor for X direction
sp-y: 1.0 ; spacing factor for Y direction
start: 0.0 ; starting time
dur: 1.0 ; duration
delay: 0.1 ; delay between subanimations
expires: 0 ; when to remove the text primitives from the draw block
; if zero, uses the end of the last animation of the effect
random: false
]
;------------------------------------------------------------------------------------------------
; easing functions
; the argument must be in the range 0.0 - 1.0
;------------------------------------------------------------------------------------------------
easings: context [
ease-linear: func [x][x]
ease-steps: func [x n][round/to x 1 / n]
ease-in-sine: func [x][1 - cos x * pi / 2]
ease-out-sine: func [x][sin x * pi / 2]
ease-in-out-sine: func [x][(cos pi * x) - 1 / -2]
ease-in-out-power: func [x n][either x < 0.5 [x ** n * (2 ** (n - 1))][1 - (-2 * x + 2 ** n / 2)]]
ease-in-quad: func [x][x ** 2]
ease-out-quad: func [x][2 - x * x] ; shorter for [1 - (1 - x ** 2)]
ease-in-out-quad: func [x][ease-in-out-power x 2]
ease-in-cubic: func [x][x ** 3]
ease-out-cubic: func [x][1 - (1 - x ** 3)]
ease-in-out-cubic: func [x][ease-in-out-power x 3]
ease-in-quart: func [x][x ** 4]
ease-out-quart: func [x][1 - (1 - x ** 4)]
ease-in-out-quart: func [x][ease-in-out-power x 4]
ease-in-quint: func [x][x ** 5]
ease-out-quint: func [x][1 - (1 - x ** 5)]
ease-in-out-quint: func [x][ease-in-out-power x 5]
ease-in-expo: func [x][2 ** (10 * x - 10)]
ease-out-expo: func [x][1 - (2 ** (-10 * x))]
ease-in-out-expo: func [x][
either x < 0.5 [
2 ** (20 * x - 10) / 2
][
2 - (2 ** (-20 * x + 10)) / 2
]
]
ease-in-circ: func [x][1 - sqrt 1 - (x * x)]
ease-out-circ: func [x][sqrt 1 - (x - 1 ** 2)]
ease-in-out-circ: func [x][
either x < 0.5 [
(1 - sqrt 1 - (2 * x ** 2)) / 2
][
(sqrt 1 - (-2 * x + 2 ** 2)) + 1 / 2
]
]
ease-in-back: func [x /local c1 c3][
c1: 1.70158
c3: c1 + 1
x ** 3 * c3 - (c1 * x * x)
]
ease-out-back: func [x /local c1 c3][
c1: 1.70158
c3: c1 + 1
x - 1 ** 3 * c3 + 1 + (x - 1 ** 2 * c1)
]
ease-in-out-back: func [x /local c1 c2][
c1: 1.70158 ; why two constants?
c2: c1 * 1.525
either x < 0.5 [
2 * x ** 2 * (c2 + 1 * 2 * x - c2) / 2
][
2 * x - 2 ** 2 * (c2 + 1 * (x * 2 - 2) + c2) + 2 / 2
]
]
ease-in-elastic: func [x /local c][
c: 2 * pi / 3
negate 2 ** (10 * x - 10) * sin x * 10 - 10.75 * c
]
ease-out-elastic: func [x /local c][
c: 2 * pi / 3
(2 ** (-10 * x) * sin 10 * x - 0.75 * c) + 1
]
ease-in-out-elastic: func [x /local c][
c: 2 * pi / 4.5
either x < 0.5 [
2 ** ( 20 * x - 10) * (sin 20 * x - 11.125 * c) / -2
][
2 ** (-20 * x + 10) * (sin 20 * x - 11.125 * c) / 2 + 1
]
]
ease-in-bounce: func [x][1 - ease-out-bounce 1 - x]
ease-out-bounce: func [x /local n d][
n: 7.5625
d: 2.75
case [
x < (1.0 / d) [n * x * x]
x < (2.0 / d) [n * (x: x - (1.5 / d)) * x + 0.75]
x < (2.5 / d) [n * (x: x - (2.25 / d)) * x + 0.9375]
true [n * (x: x - (2.625 / d)) * x + 0.9984375]
]
]
ease-in-out-bounce: func [x][
either x < 0.5 [
(1 - ease-out-bounce -2 * x + 1) / 2
][
(1 + ease-out-bounce 2 * x - 1) / 2
]
]
]
;------------------------------------------------------------------------------------------------
set 'tween function [
{Interpolates a value at t using easing function}
val1 [number! pair! tuple!] {Value to interpolate from}
val2 [number! pair! tuple!] {Value to interpolate to}
t [float!] {float! from 0.0 to 1.0}
ease [any-word! function!] {Easing function}
][
if word? :ease [ease: get in easings ease]
t: ease t
to val1 add val1 * (1 - t) val2 * t
]
process-timeline: has [
t target v w d dt old-t opposite
][
t: to float! difference now/precise st-time
dt: to float! difference (old-t: now/precise) prev-t
prev-t: old-t
t-keep: t
foreach [key val] timeline [
w: val/2
unless w/paused [
if t > w/start[
w/elapsed: w/elapsed + dt
unless w/started [
do w/on-start
w/started: true
]
either t < (w/start + w/dur) [
tt: t - w/start / w/dur
if w/val1 <> w/val2 [set val/1 tween w/val1 w/val2 tt :w/ease]
bind w/on-time context compose [time: (t)] ; makes elapsed time visible to the caller as "time"
do w/on-time
id: key
if w/dir = 'back [
parse to-string id [copy id to ["_r" end]]
]
if w/bi-dir [two-way-map/:id: w/dir]
][
d: w/dur
w/elapsed: 0.0
if w/bi-dir [ ; two-way loop - reset after 2 x duration
d: d * 2
; Change the opposite direction's start
opposite: form key
either w/dir = 'back [
take/part/last opposite find/last opposite "_"
][
opposite: rejoin [form key "_r"]
]
timeline/:opposite/2/start: t
]
either w/loop-count = -1 [ ; loop forever
w/start: w/start + d
][
either w/loop-count > 1 [
w/loop-count: w/loop-count - 1
w/start: w/start + d
][
do w/on-exit
remove/key timeline key
if opposite [remove/key timeline opposite]
if named-animations/:key [remove/key named-animations key]
]
]
]
]
t: t-keep
]
]
dt-keep: dt
foreach [key effect] particles-map [
proto: effect/proto
dt: proto/t-speed * dt
if all [t >= proto/start not proto/paused][
either t <= (proto/start + proto/duration) [
particle/update-particles to-word key dt
][
if all [proto/expires <> 0 t > proto/expires] [
clear at get key 3
if named-animations/(form key) [remove/key named-animations form key print [key "removed"]]
remove/key particles-map key
]
]
]
dt: dt-keep
]
foreach [key v] curve-fx-map [
target: 0.0
if t >= v/start [
either all [t <= (v/start + v/dur * 1.01) not v/paused] [
if lit-word? :v/ease [v/ease: in easings :v/ease]
tt: t - v/start / v/dur
target: tween v/v1 v/v2 tt get v/ease
switch/default v/type [
text [text-along-curve v/id target]
block [block-along-curve v/id target]
][print "Unsupported effect type - must be text or block"]
][
if all [v/expires <> 0 t > v/expires not v/paused] [
remove/key curve-fx-map key
clear first get v/id ; clear the effect's draw block
if named-animations/:key [remove/key named-animations key]
]
]
]
t: t-keep
]
;clean-up text-fx
foreach [key v] text-fx-map [
if all [v <> 0 t > (1.02 * v)] [
clear pick get key 1
remove/key text-fx-map key
]
]
foreach [key v] stroke-path-map [
target: 0.0
if t >= v/start [
either t <= (v/start + v/duration) [
if not v/paused [
v/elapsed: v/elapsed + dt
if lit-word? :v/ease [v/ease: in easings :v/ease]
tt: t - v/start / v/duration
target: tween 0.0 1.0 tt get v/ease
trace-path to-word key target
]
][
unless v/paused [trace-path to-word key 1.0] ; stroke the entire path
if all [v/expires > 0 t > v/expires not v/paused] [
remove/key stroke-path-map key
clear first get key
remove/key named-animations form key
]
]
]
t: t-keep
]
foreach [key v] morph-path-map [
s: v/started
if all [t > v/start not s][
clear p: get to-path reduce [to-word key 1]
append p v/block-1
v/started: on
]
if all [t >= v/start s] [
if lit-word? :v/ease [v/ease: in easings :v/ease]
either t <= (v/start + v/duration) [
if not v/paused [
tt: t - v/start / v/duration
repeat n length? at v/block-1 2 [
k: n + 1
trgt: to-path reduce [to-word key 1 k]
set trgt tween v/block-1/:k v/block-2/:k tt get v/ease
]
]
][
if all [v/expires > 0 t > v/expires not v/paused] [
clear p
remove/key morph-path-map key
remove/key named-animations form key
]
]
]
t: t-keep
]
ani-start/2: 0.1 ; refresh the draw block in case only font or image parameters have been changed
]
set 'toggle-animation function [id][
t: to float! difference now/precise st-time
id: form id
id-txt: id
id-dummy: none
either none? type: named-animations/:id [
print [id "not found in the list of named animations"]
][
id: switch type [
simple [timeline/(id)/2]
particles [
id-dummy: timeline/(to-word id)/2 ; the same for all the effects except simple
particles-map/(to-word id)/proto
]
curve-fx [
id-dummy: timeline/(to-word id)/2
curve-fx-map/:id
]
stroke-path [
id-dummy: timeline/(to-word id)/2
stroke-path-map/(to-set-word id)
]
morph-path [
id-dummy: timeline/(to-word id)/2
morph-path-map/(to-set-word id)
]
]
bi?: id/bi-dir
id/paused: not id/paused
if id-dummy [
id-dummy/paused: not id-dummy/paused
unless id-dummy/paused [
id-dummy/start: t - id-dummy/elapsed
id/elapsed: id-dummy/elapsed
]
]
if bi? [
id_r: timeline/(rejoin [id-txt "_r"])/2
id_r/paused: not id_r/paused
]
unless id/paused [
id/start: t - id/elapsed
; calculate the new starts of the forth and back animations
if bi? [
either two-way-map/:id-txt = 'forth [
id_r/start: id/start + id/dur
][
id_r/start: t - id_r/elapsed
id/start: id_r/start + id/dur
]
]
]
]
]
;------------------------------------------------------------------------------------------------
nCk: function [
{Calculates the binomial coefficient. a.k.a n choose k }
n [integer!] "size of the set"
k [integer!] "size of the subset to get from n"
][
p: 1
repeat i k [p: n + 1 - i / i * p]
to-integer p
]
bezier-n: function [
{Calculates a point in the Bezier curve, defined by pts, at t}
pts [block!] {a set of pairs}
t [float!] {offset in the curve, from 0.0 to 1.0}
][
if t < 0.0 [return pts/1]
if t > 1.0 [return last pts]
n: (length? pts) - 1
bx: by: i: 0
foreach p pts [
c: (nCk n i) * ((1 - t) ** (n - i)) * (t ** i)
bx: c * p/x + bx
by: c * p/y + by
i: i + 1
]
as-pair bx by
]
bezier-tangent: function [ ; needs a better name!
{Calculates the tangent angle for a Bezier curve
defined with pts at point t}
pts [block!] {a set of pairs}
t [float!] {offset in the curve, from 0.0 to 1.0}
][
p1: bezier-n pts t
p2: bezier-n pts t + 0.01
arctangent2 p2/y - p1/y p2/x - p1/x
]
bezier-lengths: function [
{Returns a block of accumulated lengths of the linear segments
a bezier curve can be simplified to}
pts [block!] {a set of 2d points defining a Bezier curve}
seg-n [integer!] {number of linear segments to divide the curve into}
][
t: 0.0
length: 0.0
p0: bezier-n pts t
collect [
repeat n seg-n [
t: 1.0 * n / seg-n
p1: bezier-n pts t
keep length: length + sqrt p1/1 - p0/1 ** 2 + (p1/2 - p0/2 ** 2)
p0: p1
]
]
]
half: func [a b][to integer! a + b / 2]
b-search: function [
{Returns the index of the largest element of src
that is less than or equal to target}
src [block!] {block of numbers}
target [number!] {the number to be searched}
][
L: 1
R: length? src
M: half L R
while [L < R][
case [
src/:M = target [break]
src/:M < target [L: M + 1]
src/:M > target [R: M - 1]
]
M: half L R
]
M
]
bezier-lerp: function [
{Returns a point in a Bezier curve. The distance from the
starting point is linearly interpolated.}
u [float!] {parameter of the interpolation, from 0.0 to 1.0}
seg [block!] {a precalculated block of segment lengths}
][
; !!! The points around 0.0 and 1.00 are much sparsely located!
; This leads to uneven placement of objects along the curve !!!
len: to integer! u * last seg
either len = seg/(idx: b-search seg len) [
to float! idx / length? seg
][
if idx = length? seg [return 1.0]
l1: seg/:idx
l2: seg/(idx + 1)
seg-t: to float! len - l1 / (l2 - l1)
to float! (idx + seg-t / length? seg)
]
]
;------------------------------------------------------------------------------------------------
; Text-related functions
;------------------------------------------------------------------------------------------------
char-offsets: function [
{Calculates the offsets of the characters
in a text for a given font settings}
src [string!]
fnt [object!]
][
new-src: head append copy src "|" ; to find the last offset
size: as-pair fnt/size * length? new-src fnt/size
; as a general rule, never use make face!, only make-face
txt: make make-face 'rich-text compose [size: (size) text: (new-src)]
txt/font: copy fnt
next collect [repeat n length? new-src [keep caret-to-offset txt n]]
]
text-box-size: function [
{Calculates the size of the bounding box
of a text for a given font settings}
src [string!]
fnt [object!]
][
size: as-pair fnt/size * length? src fnt/size
txt: make face! compose [size: (size) type: 'text text: (src)]
txt/font: copy fnt
size-text txt
]
split-text: function [
{Splits src on characters, words (on spaces and newlines)
or lines (on newlines) and returns a block of blocks,
each consisting of position, size and substring}
src [string!] {Text to split}
fnt [object!] {Font to use for measurements}
mode [any-word!] {chars, words or lines}
pos [pair!] {coordinates of the starting point}
sx [number!] {x spacing factor}
sy [number!] {y spacing factor}
][
size: as-pair fnt/size * length? src fnt/size
txt: make make-face 'rich-text compose [size: (size) text: (src)]
txt/font: copy fnt
txt1: make face! compose [size: (size) type: 'text text: (src)]
txt1/font: copy fnt
rule: select [chars [skip] words [space | newline] lines [newline]] mode
collect [
parse src [
any [
p: copy t thru [rule | end]
(
c-offs: caret-to-offset txt index? p
c-offs/x: to integer! c-offs/x * sx
c-offs/y: to integer! c-offs/y * sy
c-offs: 10x10 * pos + c-offs
keep/only reduce [
c-offs ; position
size-text/with txt1 t ; size
t ; text
]
)
| skip
]
]
]
]
init-text-fx: function [
id [any-word!]
t-spec [block!]
delay [number!]
][
if not text-data/:id [ ;init
t-obj: make text-effect t-spec
t-obj/delay: delay
; t-obj/dur: duration
t-obj/font/size: t-obj/font/size * 10 ; upscale the provided font!
chunks: split-text t-obj/text t-obj/font t-obj/mode t-obj/posXY t-obj/sp-x t-obj/sp-y ; too many args
starts: collect [
st: 0.0
repeat n length? chunks [
keep st
st: round/to st + delay 0.001
]
]
if t-obj/random [random starts]
repeat n length? chunks [
fnt-name: rejoin [id "-" n]
insert chunks/:n fnt-name
append chunks/:n reduce [starts/:n t-obj/dur]
]
put text-data id compose/deep [chunks: [(chunks)] effect: (t-obj)]
collect [
foreach item chunks [
fnt-name: to-word rejoin [item/1 "-fnt"]
fnt: copy t-obj/font
set fnt-name fnt
fnt-id: to set-word! item/1
keep compose/deep [
(fnt-id) font (fnt-name)
translate (item/2) [
scale 1.0 1.0
rotate 0.0 (item/3 / 2)
text 0x0 (item/4)
]
]
]
]
]
]
; obsolete?
fade-in-text: function [
{Animates the text so that each element (character, word or line)
fades-in from transparent to the chosen font color}
id [any-word!] {identifier for the effect}
t [float!] {current time}
/init
t-spec [block!] {specification of the text effect}
/rand
][
either init [ ; initialize
t-obj: make text-effect t-spec
chunks: split-text t-obj/text t-obj/font t-obj/mode
starts: collect [
st: t-obj/start
repeat n length? chunks [
keep st
st: st + t-obj/delay
]
]
if rand [random starts]
repeat n length? chunks [
fnt-name: rejoin [id "-fnt-" n]
insert chunks/:n fnt-name
append chunks/:n reduce [starts/:n t-obj/dur]
]
put text-data id compose/deep [chunks: [(chunks)]]
collect [
foreach item chunks [
fnt-name: to-word rejoin [item/1 "_"]
set fnt-name copy t-obj/font
fnt-id: to set-word! item/1
posx: item/2/x * t-obj/sp-x
posy: item/2/y * t-obj/sp-y
p: as-pair posx posy
keep compose [
(fnt-id) font (get fnt-name)
text (t-obj/posXY + p) (item/4)
]
]
]
][ ; animate
foreach item text-data/:id/chunks [
fnt-id: get to word! rejoin [item/1 "_"]
name: get to word! item/1
name/4: name/4 ; refresh
;tween 'fnt-id/color/4 255 0 item/5 item/6 t :ease-in-out-quart
]
]
]
text-along-curve: function [
{Flow a text along Bezier curve}
id [word!] {effect identificator}
t [number!] {point on the curve}
/init
txt [string!] {text to be displayed}
fnt [object!] {font to use}
pts [block!] {point of the Bezier curve}
spacing [number!] {multiplier for the space between the characters}
][
either init [
txt-ofs: char-offsets txt fnt
len: last txt-ofs ; text length
txt-sz: 0x1 * text-box-size txt fnt ; only the text height
crv: copy pts
forall crv [crv/1: crv/1 * 10]
bez-segs: bezier-lengths crv 500 ; should it be an argument - for tuning performance / quality?
put text-data id compose/deep [ ; the map of id's and objects
txt-ofs: [(txt-ofs)]
len: (len)
txt-sz: (txt-sz)
crv: [(crv)]
bez-segs: [(bez-segs)]
spacing: (spacing)
]
draw-buf: make block! 10 * length? txt
append/only draw-buf collect [
repeat n length? txt [
id-t: to set-word! rejoin [id "-t-" n] ;translate
id-r: to set-word! rejoin [id "-r-" n] ; rotate
keep compose/deep [
(id-t) translate 10000x0 [
(id-r) rotate 0 0x0
text 0x0 (to-string txt/:n)
]
]
]
]
draw-buf
][
tt: t
d: d0: 0x0
obj: text-data/:id
txt-ofs: obj/txt-ofs
txt-sz: obj/txt-sz
crv: obj/crv
bez-segs: obj/bez-segs
len: last bez-segs
spacing: obj/spacing
repeat n length? txt-ofs [
d: txt-ofs/:n - d0 + txt-sz / 2
u: d/x / len * spacing + tt
;u: d/x / len + tt
ttt: bezier-lerp u bez-segs
if ttt > 0.999 [break]
id-t: to word! rejoin [id "-t-" n] ;translate
id-r: to word! rejoin [id "-r-" n] ; rotate
c-offs: bezier-n crv ttt
angle: round/to bezier-tangent crv ttt 0.01
change at get id-t 2 c-offs - d
change at get id-r 2 angle
change at get id-r 3 d
tt: (to-float txt-ofs/:n/x / len * spacing) + t
d0: txt-ofs/:n
]
]
]
block-along-curve: function [
{Flow a block of blocks of Draw commands along Bezier curve}
id [word!] {effect identificator}
t [number!] {point on the curve}
/init
blocks [block!] {a block of blocks to be rendered}
pts [block!] {point of the Bezier curve}
spacing [number!] {linear distance between the blocks on the curve, pixels}
][
either init [
crv: copy pts
forall crv [crv/1: crv/1 * 10]
bez-segs: bezier-lengths crv 500 ; should it be an argument - for tuning performance / quality?
draw-buf: make block! 3 * length? blocks
draw-buf: collect [
foreach block blocks [
keep [translate 0x0]
keep/only compose/deep [
translate 10000x0 [ ; outside the visible screen
rotate 0 0x0
(autoscale block)
]
]
]
]
put draw-blocks-data id compose/deep [ ; the map of id's and objects
blocks: [(draw-buf)]
crv: [(crv)]
bez-segs: [(bez-segs)]
spacing: (spacing)
]
draw-buf
][
t: max t 0.005
d: 0
obj: draw-blocks-data/:id
crv: obj/crv
bez-segs: obj/bez-segs
len: last bez-segs
spacing: 10 * obj/spacing
foreach [a b block] obj/blocks [
u: to-float d / len + t
ttt: bezier-lerp u bez-segs
if any [ttt < 0.001 ttt > 0.999] [break]
block/2: bezier-n crv ttt
block/3/2: round/to bezier-tangent crv ttt 0.01
d: d + spacing
]
]
]
particle: context [
speck: [ ; a default template for particles
[fill-pen 240.240.255.30 circle 0x0 5]
]
particle-base: make object! [
number: 100 ; how many particles
start: 1.0 ; start time of the effect
duration: 5.0 ; duration of the effect
elapsed: 0.0
shapes: speck ; a block of draw blocks (shapes to be used to render particles)
ffd: 0
forces: [] ; what forces affect the particles motion - a block of words
started: false
finished: false
expires: 0 ; when to clear the particle draw block
paused: false
bi-dir: false
on-start: []
on-time: []
on-exit: []
]
a-particle: context [
x: 0.0 ; X position
y: 0.0 ; Y position
dir: 45.0 ; movement direction in degrees
speed: 1.0 ; speed
scale-x: 1.0 ; X scale factor
scale-y: 1.0 ; Y scale factor
color: transparent ; fill-pen color
t: 0.0 ; elapsed time for the particle
shape: [] ; drawing commands
data: none
]
create-particle: func [
{Instantiates a sinlge particle using the prototype}
proto [object!]
/local p
][
p: proto/emitter
p: make a-particle p
p/x: 10.0 * p/x
p/y: 10.0 * p/y
p/speed: 10.0 * p/speed
p/shape: autoscale random/only proto/shapes
p
]
init-particles: func [
{Populates a named set of particles using a prototype}
id [word!] ; particles identifier
proto [object!] ; particle-base object
idx [integer!] ; unique identifier
/local
particles
particles-draw
d n p
][
particles: make block! 2 * n: proto/number
proto: make a-particle proto
append particles reduce [
'proto proto
'spec copy []
'draw copy []
]
particles-draw: make block! 3 * n: proto/number
append particles-draw compose [(to-set-word id ) translate 0x0]
loop n [
p: create-particle proto
append/only particles/spec p
d: compose/deep [
translate (as-pair to-integer p/x to-integer p/y) [
scale 1.0 1.0 fill-pen (p/color) (p/shape)
]
]
append particles/draw d
]
put particles-map id particles
put named-animations form id 'particles
append/only particles-draw particles/draw
loop 100 [update-particles id proto/ffd / 100.0] ; update the particle positions
particles-draw
]
prev-t: now/precise
update-particles: func [
id
dt [float!]
/local
i p ps pd p-id tmp new-p
][
p-id: particles-map/:id
ps: p-id/spec
pd: p-id/draw
repeat i length? ps [
p: ps/:i
p/t: p/t + dt ; force functions can use it
dist: dt * p/speed ; elapsed time times speed
; apply forces - they make changes in place
; forces should accept particles position, directin and speed!
p/x: p/x / 10.0
p/y: p/y / 10.0
foreach force p-id/proto/forces [p: do reduce [:force p]]
p/x: p/x * 10.0
p/y: p/y * 10.0
; calculate new position
p/x: dist * (cosine p/dir) + p/x
p/y: dist * ( sine p/dir) + p/y
p-copy: copy p
p-copy/x: 0.1 * p-copy/x
p-copy/y: 0.1 * p-copy/y
if p-id/proto/absorber p-copy [
new-p: make a-particle p-id/proto/emitter
p/x: 10.0 * new-p/x
p/y: 10.0 * new-p/y
p/dir: new-p/dir
p/speed: 10.0 * new-p/speed
p/color: do p-id/proto/color
p/scale-x: new-p/scale-x
p/scale-y: new-p/scale-y
p/t: 0.0
]
pd/2: as-pair to-integer p/x to-integer p/y
pd/3/2: p/scale-x
pd/3/3: p/scale-y
pd/3/5: p/color
pd: skip pd 3
]
]
]
do-not-scale: [
; this list might not be complete!
[circle 4]
[circle 5]
[arc 4]
[arc 5]
[rotate 2]
[scale 2]
[scale 3]
[skew 2]
[skew 3]
[transform 3]
[transform 4]
]
draw-words: split {line triangle box polygon circle ellipse arc curve spline
image text font pen fill-pen linear radial diamond pattern bitmap
line-width line-join line-cap anti-alias matrix reset-matrix
invert-matrix push rotate scale translate skew transform
clip shape move line arc curve curv qcurve qcurv hline vline} charset reduce [space newline]
draw-cmd: collect [
foreach w difference draw-words split "linear radial diamond pattern bitmap" sp [
keep to-lit-word w keep '|
]
]
append draw-cmd ['particles | 'text-fx | 'curve-fx | 'stroke-path | 'morph-path | 'parameter]
autoscale: function [
{Multiplies by 10 the linear sizes of draw block commands
Used for subpixel precision,
Returns a modified block}
src [block!]
][
dest: copy/deep src
target: none
offs: 1
k: 1