/
animate.jl
590 lines (490 loc) · 16 KB
/
animate.jl
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
"""
The `Movie` and `Scene` types and the `animate()` function are designed to help you
create the frames that can be used to make an animated GIF or movie.
1 Provide width, height, title, and optionally a frame range to the Movie constructor:
demo = Movie(400, 400, "test", 1:500)
2 Define one or more scenes and scene-drawing functions.
3 Run the `animate()` function, calling those scenes.
Example
```
bang = Movie(400, 100, "bang")
backdrop(scene, framenumber) = background("black")
function frame1(scene, framenumber)
background("white")
sethue("black")
eased_n = scene.easingfunction(framenumber, 0, 1, scene.framerange.stop)
circle(O, 40 * eased_n, :fill)
end
animate(bang, [
Scene(bang, backdrop, 0:200),
Scene(bang, frame1, 0:200, easingfunction=easeinsine)],
creategif=true,
pathname="/tmp/animationtest.gif")
```
"""
struct Movie
width::Float64
height::Float64
movietitle::AbstractString
movieframerange::AbstractRange
end
"""
Movie(width, height, movietitle)
Define a movie, specifying the width, height, and a title. The title will be used to
make the output file name. The range defaults to `1:250`.
"""
Movie(width, height, movietitle::AbstractString) = Movie(width, height, movietitle, 1:250)
"""default linear transition - no easing, no acceleration"""
function lineartween(t, b, c, d)
return c * t/d + b
end
"""
The Scene type defines a function to be used to render a range of frames in a movie.
- the `movie` created by Movie()
- the `framefunction` is a function taking two arguments: the scene and the framenumber.
- the `framerange` determines which frames are processed by the function. Defaults to the entire movie.
- the optional `easingfunction` can be accessed by the framefunction to vary the transition speed
- the optional `opts` which is a single argument of an abstract type which can be accessed within the framefunction
"""
mutable struct Scene
movie::Movie
framefunction::Function
framerange::AbstractRange
easingfunction::Function
opts::Any
end
"""
Scene(movie, function, range;
easingfunction=easinoutquad,
optarg=nothing)
Use the Scene() constructor function to create a scene. Supply a movie, a function
to generate the scene, and a range of frames. Optionally you can supply an
easing function, and other information, in `optarg`, which can be accessed as
`scene.opts`.
### Example
```
function initial(scene, framenumber)
balls = scene.opts
...
end
animate(poolmovie, [
Scene(poolmovie, initial, optarg=balls, 1:20),
...
])
```
To use an easing function inside the frame-generating function, you can create a normalized
value with, for example:
```
eased_n = scene.easingfunction(framenumber, 0, 1, scene.framerange.stop)
```
Or, if the scene doesn't start at frame 1, calculate normalized easing function like this:
```
eased_n = scene.easingfunction(framenumber - scene.framerange.start,
0, 1, scene.framerange.stop - scene.framerange.start)
```
"""
Scene(movie::Movie, framefunction::Function;
easingfunction=lineartween, optarg=nothing) =
Scene(movie, framefunction, movie.movieframerange, easingfunction, optarg)
Scene(movie::Movie, framefunction::Function, framerange::AbstractRange;
easingfunction=lineartween, optarg=nothing) =
Scene(movie, framefunction, framerange, easingfunction, optarg)
"Wraps the location of an animated gif so that it can be displayed"
struct AnimatedGif
filename::String
end
"""
animate(movie::Movie, scenelist::Array{Scene, 1};
creategif=false,
framerate=30,
pathname="",
tempdirectory="",
usenewffmpeg=true)
Create the movie defined in `movie` by rendering the frames define in the array of scenes
in `scenelist`.
If `creategif` is `true`, the function attempts to call the `ffmpeg` utility on
the resulting frames to build a GIF animation. This will be stored in `pathname`
(an existing file will be overwritten; use a ".gif" suffix), or in
`(movietitle).gif` in a temporary directory. `ffmpeg` should be installed and
available, of course, if this is to work.
In suitable environments, the resulting animation is displayed in the Plots window.
### Example
```
animate(bang, [
Scene(bang, backdrop, 0:200),
Scene(bang, frame1, 0:200, easingfunction=easeinsine)],
creategif=true,
pathname="/tmp/animationtest.gif")
```
The `usenewffmpeg` option, `true` by default, uses single-pass palette
generation and more complex filtering provided by recent versions of the
`ffmpeg` utility, mainly to cope with transparent backgrounds. If set to
`false`, the behavior is the same as in previous versions of Luxor.
"""
function animate(movie::Movie, scenelist::Array{Scene, 1};
creategif=false,
framerate=30,
pathname="",
tempdirectory="",
usenewffmpeg=true)
if isdir(pathname)
suggestedpathname = joinpath(pathname, "myanimation.gif")
@error("Parameter pathname=$pathname points to a directory. Please pass a filename like '$suggestedpathname' as pathname parameter!")
return false
end
if tempdirectory == ""
tempdirectory = mktempdir()
else
if !isdir(tempdirectory)
@info "$(tempdirectory) does not exist, using an autogenerated one."
tempdirectory = mktempdir()
end
end
@info("Frames for animation \"$(movie.movietitle)\" are being stored in directory: \n\t $(tempdirectory)")
filecounter = 1
rangelist = 0:-1
for scene in scenelist
rangelist = vcat(rangelist, collect(scene.framerange))
end
rangelist = unique(rangelist) # remove shared frames
if rangelist[end] < movie.movieframerange.stop
@warn("Movie framerange is longer than scene frame range: \n\t $(movie.movieframerange) > $(rangelist[end])")
end
for currentframe in rangelist[1]:rangelist[end]
Drawing(movie.width, movie.height, "$(tempdirectory)/$(lpad(filecounter, 10, "0")).png")
origin()
# this frame needs doing, see if each of the scenes defines it
for scene in scenelist
if currentframe in scene.framerange
scene.framefunction(scene, currentframe)
end
end
finish()
filecounter += 1
end
@info("... $(filecounter-1) frames saved in directory:\n\t $(tempdirectory)")
if creategif == false
return true # we're done
end
# these two commands create a palette and then create animated GIF from the resulting images
if !usenewffmpeg
# old version of ffmpeg up to 2.1.3
# these two commands create a palette and then an animated GIF from the resulting images using the palette
run(`ffmpeg -loglevel panic -f image2 -i $(tempdirectory)/%10d.png -vf palettegen -y $(tempdirectory)/$(movie.movietitle)-palette.png`)
run(`ffmpeg -loglevel panic -framerate $(framerate) -f image2 -i $(tempdirectory)/%10d.png -i $(tempdirectory)/$(movie.movietitle)-palette.png -lavfi paletteuse -y $(tempdirectory)/$(movie.movietitle).gif`)
else
# the latest version of ffmpeg uses built-in palettes and allegedly does transparency using complex filters
run(`ffmpeg -loglevel panic -framerate $(framerate) -f image2 -i $(tempdirectory)/%10d.png -filter_complex "[0:v] split [a][b]; [a] palettegen=stats_mode=full:reserve_transparent=on:transparency_color=FFFFFF [p]; [b][p] paletteuse=new=1:alpha_threshold=128" -y $(tempdirectory)/$(movie.movietitle).gif`)
end
if ! isempty(pathname)
mv("$(tempdirectory)/$(movie.movietitle).gif", pathname, force=true)
@info("GIF is: $pathname")
giffn = pathname
else
@info("GIF is: $(tempdirectory)/$(movie.movietitle).gif")
giffn = tempdirectory * "/" * movie.movietitle * ".gif"
end
AnimatedGif(giffn)
end
"""
animate(movie::Movie, scene::Scene; creategif=false, framerate=30)
Create the movie defined in `movie` by rendering the frames define in `scene`.
"""
animate(movie::Movie, scene::Scene; kwargs...) = animate(movie, [scene]; kwargs...)
# write out the HTML to view the gif
function Base.show(io::IO, ::MIME"text/html", agif::AnimatedGif)
ext = last(splitext(agif.filename))
if ext == ".gif"
html = "<img src=\"data:image/gif;base64," * base64encode(read(agif.filename)) * "\" />"
elseif ext in (".mov", ".mp4")
mimetype = ext == ".mov" ? "video/quicktime" : "video/mp4"
html = "<video controls><source src=\"data:$mimetype;base64," *
base64encode(read(agif.filename)) *
"\" type = \"$mimetype\"></video>"
else
error("Cannot show animation with extension $ext: $agif")
end
write(io, html)
return nothing
end
# Only gifs can be shown via image/gif
Base.showable(::MIME"image/gif", agif::AnimatedGif) = lowercase(last(splitext(agif.filename))) == ".gif"
function Base.show(io::IO, ::MIME"image/gif", agif::AnimatedGif)
open(fio -> write(io, fio), agif.filename)
end
"""
easingflat(t, b, c, d)
A flat easing function, same as `lineartween()`.
For all easing functions, the four parameters are:
- `t` time, ie the current framenumber
- `b` beginning position or bottom value of the range
- `c` total change in position or top value of the range
- `d` duration, ie a framecount
1. `t/d` or `t/=d` normalizes `t` to between 0 and 1
2. `... * c` scales up to the required range value
3. `... + b` adds the initial offset
"""
function easingflat(t, b, c, d)
return c * t / d + b
end
"""
easeinquad(t, b, c, d)
quadratic easing in - accelerating from zero velocity
"""
function easeinquad(t, b, c, d)
t /= d
return c * t * t + b
end
"""
easeoutquad(t, b, c, d)
quadratic easing out - decelerating to zero velocity
"""
function easeoutquad(t, b, c, d)
t /= d
return -c * t * (t - 2) + b
end
"""
easeinoutquad(t, b, c, d)
quadratic easing in/out - acceleration until halfway, then deceleration
"""
function easeinoutquad(t, b, c, d)
t /= d/2
if t < 1
return (c/2) * t * t + b
end
t -= 1
return -(c/2) * (t * (t - 2) - 1) + b
end
"""
easeincubic(t, b, c, d)
cubic easing in - accelerating from zero velocity
"""
function easeincubic(t, b, c, d)
t /= d
return c * t * t * t + b
end
"""
easeoutcubic(t, b, c, d)
cubic easing out - decelerating to zero velocity
"""
function easeoutcubic(t, b, c, d)
t /= d
t -= 1
return c * (t * t * t + 1) + b
end
"""
easeinoutcubic(t, b, c, d)
cubic easing in/out - acceleration until halfway, then deceleration
"""
function easeinoutcubic(t, b, c, d)
t /= d/2
if t < 1
return c/2 * t * t * t + b
end
t -= 2
return c/2 * (t * t * t + 2) + b
end
"""
easeinquart(t, b, c, d)
quartic easing in - accelerating from zero velocity
"""
function easeinquart(t, b, c, d)
t /= d
return c * t * t * t * t + b
end
"""
easeoutquart(t, b, c, d)
quartic easing out - decelerating to zero velocity
"""
function easeoutquart(t, b, c, d)
t /= d
t -= 1
return -c * (t * t * t * t - 1) + b
end
"""
easeinoutquart(t, b, c, d)
quartic easing in/out - acceleration until halfway, then deceleration
"""
function easeinoutquart(t, b, c, d)
t /= d/2
if t < 1
return c/2 * t * t * t * t + b
end
t -= 2
return -c/2 * (t * t * t * t - 2) + b
end
"""
easeinquint(t, b, c, d)
quintic easing in - accelerating from zero velocity
"""
function easeinquint(t, b, c, d)
t /= d
return c * t * t * t * t * t + b
end
"""
easeoutquint(t, b, c, d)
quintic easing out - decelerating to zero velocity
"""
function easeoutquint(t, b, c, d)
t /= d
t -= 1
return c * (t * t * t * t * t + 1) + b
end
"""
easeinoutquint(t, b, c, d)
quintic easing in/out - acceleration until halfway, then deceleration
"""
function easeinoutquint(t, b, c, d)
t /= d/2
if t < 1
return c/2 * t * t * t * t * t + b
end
t -= 2
return c/2 * (t * t * t * t * t + 2) + b
end
"""
easeinsine(t, b, c, d)
sinusoidal easing in - accelerating from zero velocity
"""
function easeinsine(t, b, c, d)
return -c * cos(t/d * (pi/2)) + c + b
end
"""
easeoutsine(t, b, c, d)
sinusoidal easing out - decelerating to zero velocity
"""
function easeoutsine(t, b, c, d)
return c * sin(t/d * (pi/2)) + b
end
"""
easeinoutsine(t, b, c, d)
sinusoidal easing in/out - accelerating until halfway, then decelerating
"""
function easeinoutsine(t, b, c, d)
return -c/2 * (cos(pi * t/d) - 1) + b
end
"""
easeinexpo(t, b, c, d)
exponential easing in - accelerating from zero velocity
"""
function easeinexpo(t, b, c, d)
if isapprox(t, 0.0, atol=1e-3)
return 0.0
else
return c * 2 ^ (10 * (t/d - 1)) + b
end
end
"""
easeoutexpo(t, b, c, d)
exponential easing out - decelerating to zero velocity
"""
function easeoutexpo(t, b, c, d)
return c * (-(2 ^ (-10 * t/d)) + 1) + b
end
"""
easeinoutexpo(t, b, c, d)
exponential easing in/out - accelerating until halfway, then decelerating
"""
function easeinoutexpo(t, b, c, d)
t /= d/2
if t < 1
return c/2 * 2 ^ (10 * (t - 1)) + b
end
t -= 1
return c/2 * (-(2 ^ (-10 * t)) + 2) + b
end
"""
easeincirc(t, b, c, d)
circular easing in - accelerating from zero velocity
"""
function easeincirc(t, b, c, d)
t /= d
return -c * (sqrt(abs((1 - t * t))) - 1) + b
end
"""
easeoutcirc(t, b, c, d)
circular easing out - decelerating to zero velocity
"""
function easeoutcirc(t, b, c, d)
t /= d
t -= 1
return c * sqrt(abs((1 - t * t))) + b
end
"""
easeinoutcirc(t, b, c, d)
circular easing in/out - acceleration until halfway, then deceleration
"""
function easeinoutcirc(t, b, c, d)
t /= d/2
if (t < 1)
return -c/2 * ((sqrt(abs(1 - t * t))) - 1) + b
end
t -= 2
return c/2 * (sqrt(abs(1 - t * t)) + 1) + b
end
"""
easeinoutinversequad(t, b, c, d)
ease in, then slow down, then speed up, and ease out
"""
function easeinoutinversequad(t, b, c, d)
t /= d/2
if t <= 1
return -(c/2) * (t * (t - 2)) + b
end
t -= 1
return c/2 * (t * t * t + 1) + b
end
"""
easeinoutbezier(t, b, c, d, cpt1, cpt2)
This easing function takes six arguments, the usual `t`, `b`, `c`, and `d`, but
also two points. These are the normalized control points of a Bezier curve drawn
between `Point(0, 0)` to `Point(1.0, 1.0)`. The `y` value of the Bezier is the
eased value for `t`.
In your `frame()` generating function, if a Scene specifies the `easeinoutbezier` easing function, you can use this:
```
...
lineareasing = rescale(framenumber, 1, scene.framerange.stop)
beziereasing = scene.easingfunction(lineareasing, 0, 1, 1,
Point(0.25, 0.25), Point(0.75, 0.75))
...
```
These two control points lie on the line between `0/0` and `1/1`, so it's equivalent to a linear easing (`lineartween()` or `easingflat`).
However, in the next example, the two control points define a wave-like curve
that changes direction before changing back. When animating with this easing
function, an object will 'go retrograde' for a while.
```
lineareasing = rescale(framenumber, 1, scene.framerange.stop)
beziereasing = scene.easingfunction(lineareasing, 0, 1, 1,
Point(0.01, 1.99), Point(0.99, -1.5))
```
"""
function easeinoutbezier(t, b, c, d, cpt1::Point=Point(0.25, 0.25), cpt2::Point=Point(0.75, 0.75))
t /= d + b
bez = bezier(t, Point(0, 0), cpt1, cpt2, Point(1.0, 1.0))
return bez.y * c
end
easingfunctions = [lineartween,
easeinquad,
easeoutquad,
easeinoutquad,
easeincubic,
easeoutcubic,
easeinoutcubic,
easeinquart,
easeoutquart,
easeinoutquart,
easeinquint,
easeoutquint,
easeinoutquint,
easeinsine,
easeoutsine,
easeinoutsine,
easeinexpo,
easeoutexpo,
easeinoutexpo,
easeincirc,
easeoutcirc,
easeinoutcirc,
easingflat,
easeinoutinversequad,
easeinoutbezier]