Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Animations.jl #125

Merged
merged 18 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

## Unreleased
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"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
Expand All @@ -12,6 +13,7 @@ ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Animations = "0.4"
FFMPEG = "0.3, 0.4"
LaTeXStrings = "1.1"
LightXML = "0.9"
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
138 changes: 127 additions & 11 deletions src/Javis.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Javis

using Animations
TheCedarPrince marked this conversation as resolved.
Show resolved Hide resolved
using FFMPEG
using LaTeXStrings
using LightXML
Expand All @@ -10,6 +11,18 @@ using Random

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 @@ -133,6 +146,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 @@ -141,13 +155,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 @@ -172,7 +225,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 @@ -205,7 +258,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 @@ -268,6 +325,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 @@ -280,6 +338,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 @@ -338,6 +397,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 @@ -373,6 +435,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.
"""
Wikunia marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -391,6 +499,7 @@ function Action(
frames,
id::Union{Nothing,Symbol},
func::Function,
anim::Animation,
transitions::Transition...;
kwargs...,
)
Expand All @@ -401,7 +510,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 @@ -712,9 +831,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 @@ -741,9 +858,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 @@ -1213,7 +1328,8 @@ 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

# 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