Skip to content

Commit

Permalink
Using Animations.jl (#125)
Browse files Browse the repository at this point in the history
* first version of Animations.jl
  • Loading branch information
Wikunia authored Sep 7, 2020
1 parent 3532015 commit ed48738
Show file tree
Hide file tree
Showing 25 changed files with 641 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
Manifest.toml
.vscode/
test.png
test/current/
test/current/
.commit
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Javis.jl - Changelog

## Unreleased v0.2
- Ability to use [Animations.jl](https://github.com/jkrumbiegel/Animations.jl)
- for Transformations and `appear` and `disappear`
- Show progress of rendering using [ProgressMeter.jl](https://github.com/timholy/ProgressMeter.jl)
- Use [VideoIO](https://github.com/JuliaIO/VideoIO.jl) for faster rendering without temporary images

Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Ole Kröger <[email protected]> and Jacob Zelko <jacobszelko@gm
version = "0.1.2"

[deps]
Animations = "27a7e980-b3e6-11e9-2bcd-0b925532e340"
FFMPEG = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Expand All @@ -14,6 +15,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
VideoIO = "d6d074c3-1acf-5d4c-9a43-ef38773959a2"

[compat]
Animations = "0.4"
FFMPEG = "0.3, 0.4"
Images = "0.22"
LaTeXStrings = "1.1"
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ graph TD

- [x] 56 OPEN ProgressMeter enhancement 2020-08-09 22:21:42 +0000 UTC

- [ ] 42 OPEN Using Animations.jl enhancement, question 2020-08-05 13:42:17 +0000 UTC
- [x] 42 OPEN Using Animations.jl enhancement, question 2020-08-05 13:42:17 +0000 UTC

- [x] 23 OPEN fontsize and color in Javis instead of Luxor enhancement, question 2020-08-18 14:59:14 +0000 UTC

Expand Down
139 changes: 128 additions & 11 deletions src/Javis.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Javis

using Animations
using FFMPEG
using Images
using LaTeXStrings
Expand All @@ -12,6 +13,18 @@ using VideoIO

const FRAMES_SYMBOL = [:same]

struct ReversedEasing
easing::Easing
end

"""
rev(e::Easing)
Reverse an easing function such that `easing_to_animation` maps it to `[1.0, 0.0]` instead of `[0.0, 1.0]`.
An example can be seen in [`rotate`](@ref)
"""
rev(e::Easing) = ReversedEasing(e)

"""
Video
Expand Down Expand Up @@ -135,6 +148,7 @@ A SubAction should not be created by hand but instead by using one of the constr
# Fields
- `frames::Frames`: the frames relative to the parent [`Action`](@ref)
- `anim::Animation`: defines the interpolation function for the transitions
- `func::Function`: the function that gets called in each of those frames.
Takes the following arguments: `video, action, subaction, rel_frame`
- `transitions::Vector{Transition}`: A list of transitions like [`Translation`](@ref)
Expand All @@ -143,13 +157,52 @@ A SubAction should not be created by hand but instead by using one of the constr
"""
mutable struct SubAction <: AbstractAction
frames::Frames
anim::Animation
func::Function
transitions::Vector{Transition}
internal_transitions::Vector{InternalTransition}
end

"""
SubAction(frames::UnitRange, func::Function)
SubAction(frames, easing::Union{ReversedEasing, Easing}, args...)
A `SubAction` can be defined with frames an
[easing function](https://jkrumbiegel.github.io/Animations.jl/stable/#Easings-1) and either
a function or transformation(s).
# Example
In the following example a filled circle with radius 50 appears in the first 20 frames,
which means the opacity is increased from 0 to 1.0. The interpolation function used here is
sineio from [`Animations.jl`](https://github.com/jkrumbiegel/Animations.jl).
Then it stays at full opacity and disappears the same way in the last 20 frames using a
linear decay.
```julia
javis(demo, [
BackgroundAction(1:100, ground),
Action((args...)->circle(O, 50, :fill); subactions = [
SubAction(1:20, sineio(), appear(:fade)),
SubAction(81:100, disappear(:fade))
])
])
```
# Arguments
- `frames`: A list of frames for which the function should be called.
- The frame numbers are relative to the parent [`Action`](@ref).
- `easing::Union{ReversedEasing, Easing}`: The easing function for `args...`
- `args...`: Either a function like [`appear`](@ref) or a Transformation
like [`Translation`](@ref)
"""
SubAction(frames, easing::Union{ReversedEasing,Easing}, args...) =
SubAction(frames, easing_to_animation(easing), args...)

SubAction(frames, anim::Animation, transition::Transition...) =
SubAction(frames, anim, (args...) -> 1, transition...)

"""
SubAction(frames, func::Function)
A `SubAction` can be defined with frames and a function
inside the `subactions` kwarg of an [`Action`](@ref).
Expand All @@ -174,7 +227,7 @@ javis(demo, [
- For [`appear`](@ref) and [`disappear`](@ref) a closure exists,
such that `appear(:fade)` works.
"""
SubAction(frames, func::Function) = SubAction(frames, func, [], [])
SubAction(frames, func::Function) = SubAction(frames, easing_to_animation(linear()), func)

"""
SubAction(frames, trans::Transition...)
Expand Down Expand Up @@ -207,7 +260,11 @@ javis(demo, [
- `trans::Transition...`: A list of transitions that shall be performed.
"""
SubAction(frames, trans::Transition...) =
SubAction(frames, (args...) -> 1, collect(trans), [])
SubAction(frames, easing_to_animation(linear()), (args...) -> 1, trans...)


SubAction(frames, anim::Animation, func::Function, transitions::Transition...) =
SubAction(frames, anim, func::Function, collect(transitions), [])

"""
ActionSetting
Expand Down Expand Up @@ -270,6 +327,7 @@ Defines what is drawn in a defined frame range.
- `id::Union{Nothing, Symbol}`: An id which can be used to save the result of `func`
- `func::Function`: The drawing function which draws something on the canvas.
It gets called with the arguments `video, action, frame`
- `anim::Animation`: defines the interpolation function for the transitions
- `transitions::Vector{Transition}` a list of transitions
which can be performed before the function gets called.
- `internal_transitions::Vector{InternalTransition}`:
Expand All @@ -282,6 +340,7 @@ mutable struct Action <: AbstractAction
frames::Frames
id::Union{Nothing,Symbol}
func::Function
anim::Animation
transitions::Vector{Transition}
internal_transitions::Vector{InternalTransition}
subactions::Vector{SubAction}
Expand Down Expand Up @@ -340,6 +399,9 @@ The current action can be accessed using CURRENT_ACTION[1]
"""
const CURRENT_ACTION = Array{Action,1}()

easing_to_animation(easing) = Animation(0.0, 0.0, easing, 1.0, 1.0)
easing_to_animation(rev_easing::ReversedEasing) =
Animation(0.0, 1.0, rev_easing.easing, 1.0, 0.0)

"""
Action(frames, func::Function, args...)
Expand Down Expand Up @@ -375,6 +437,52 @@ Similar to the above but uses the same as frames as the action above.
Action(func::Function, args...; kwargs...) =
Action(:same, nothing, func, args...; kwargs...)

"""
Action(frames, id::Union{Nothing,Symbol}, func::Function,
transitions::Transition...; kwargs...)
Fallback constructor for an Action which doesn't define an animation.
A linear animation is assumed.
"""
function Action(
frames,
id::Union{Nothing,Symbol},
func::Function,
transitions::Transition...;
kwargs...,
)

Action(frames, id, func, easing_to_animation(linear()), transitions...; kwargs...)
end

"""
Action(frames, id::Union{Nothing,Symbol}, func::Function, easing::Union{ReversedEasing, Easing},
args...; kwargs...)
Fallback constructor for an Action which does define an animation using an easing function.
# Example
```
javis(
video, [
BackgroundAction(1:100, ground),
Action((args...)->t(), sineio(), Translation(250, 0))
]
)
```
"""
function Action(
frames,
id::Union{Nothing,Symbol},
func::Function,
easing::Union{ReversedEasing,Easing},
args...;
kwargs...,
)

Action(frames, id, func, easing_to_animation(easing), args...; kwargs...)
end

"""
Action(frames, id::Union{Nothing,Symbol}, func::Function,
transitions::Transition...; kwargs...)
Expand All @@ -393,6 +501,7 @@ function Action(
frames,
id::Union{Nothing,Symbol},
func::Function,
anim::Animation,
transitions::Transition...;
kwargs...,
)
Expand All @@ -403,7 +512,17 @@ function Action(
subactions = opts[:subactions]
delete!(opts, :subactions)
end
Action(frames, id, func, collect(transitions), [], subactions, ActionSetting(), opts)
Action(
frames,
id,
func,
anim,
collect(transitions),
[],
subactions,
ActionSetting(),
opts,
)
end

"""
Expand Down Expand Up @@ -714,9 +833,7 @@ function compute_transition!(
action::AbstractAction,
frame,
)
t = (frame - first(get_frames(action))) / (length(get_frames(action)) - 1)
# makes sense to only allow 0 ≤ t ≤ 1
t = min(1.0, t)
t = get_interpolation(action, frame)
from, to, center = rotation.from, rotation.to, rotation.center

center isa Symbol && (center = pos(center))
Expand All @@ -743,9 +860,7 @@ function compute_transition!(
action::AbstractAction,
frame,
)
t = (frame - first(get_frames(action))) / (length(get_frames(action)) - 1)
# makes sense to only allow 0 ≤ t ≤ 1
t = min(1.0, t)
t = get_interpolation(action, frame)
from, to = translation.from, translation.to

from isa Symbol && (from = pos(from))
Expand Down Expand Up @@ -1253,7 +1368,9 @@ export Video, Action, BackgroundAction, SubAction, Rel
export Line, Translation, Rotation, Transformation, Scaling
export val, pos, ang, get_value, get_position, get_angle
export projection, morph
export appear, disappear
export appear, disappear, rotate_around
export rev
export scaleto

# custom override of luxor extensions
export setline, setopacity, fontsize, get_fontsize, scale
Expand Down
2 changes: 2 additions & 0 deletions src/luxor_overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ function scale(scl_x, scl_y)
# println("cs.current_scale: $(cs.current_scale)")
end

scaleto(xy) = scaleto(xy, xy)

"""
scaleto(x, y)
Expand Down
Loading

0 comments on commit ed48738

Please sign in to comment.