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

Macro fps support #4594

Merged
merged 6 commits into from
Feb 2, 2023
Merged
Changes from 2 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
68 changes: 46 additions & 22 deletions src/animation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,40 +199,49 @@ function _animate(forloop::Expr, args...; type::Symbol = :none)
freqassert = :()
block = forloop.args[2]

# create filter
n = length(args)
filterexpr = if n == 0
# no filter... every iteration gets a frame
true

elseif args[1] === :every
# filter every `freq` frames (starting with the first frame)
@assert n == 2
freq = args[2]
freqassert = :(@assert isa($freq, Integer) && $freq > 0)
:(mod1($countersym, $freq) == 1)

elseif args[1] === :when
# filter on custom expression
@assert n == 2
args[2]

else
error("Unsupported animate filter: $args")
end
parameters = Dict{Symbol,Any}()
filterexpr=true

n = length(args)
i=1
# create filter and read parameters
while i<=n
if args[i] == :when
@assert i<n
filterexpr == true || error("Can only specify one filterexpression (one of 'when' or 'every')")
filterexpr = args[i+1]
i+=1
elseif args[i] == :every
@assert i<n
filterexpr == true || error("Can only specify one filterexpression (one of 'when' or 'every')")
freq = args[i+1]
freqassert = :(@assert isa($freq, Integer) && $freq > 0)
filterexpr = :(mod1($countersym, $freq) == 1)
i+=1
elseif args[i] isa Expr && args[i].head==Symbol("=") && args[i].args[1] isa Symbol
parameters[args[i].args[1]]=args[i].args[2]
else
error("Parameter specification not understood: $(args[i])")
end
i+=1
end
push!(block.args, :(
if $filterexpr
Plots.frame($animsym)
end
))
push!(block.args, :($countersym += 1))


animationsArgs = Any[animsym,]
animationsKwargs = Any[]
haskey(parameters, :fps) && push!(animationsKwargs, :(fps=$(parameters[:fps])))
# add a final call to `gif(anim)`?
retval = if type === :gif
:(Plots.gif($animsym))
:(Plots.gif($(animationsArgs...);$(animationsKwargs...)))
elseif type === :apng
:(Plots.apng($animsym))
:(Plots.apng($(animationsArgs...);$(animationsKwargs...)))
else
animsym
end
Expand All @@ -259,6 +268,11 @@ Example:
push!(p, 1, sin(x))
end
```
This macro supports additional parameters, that may be added after the main loop body.
- Add `fps=n` with positive Integer n, to specify the desired frames per second.
- Add `every n` with positive Integer n, to take only one frame every nth iteration.
- Add `when <cond>` where `<cond>` is an Expression resulting in a Boolean, to take a
frame only when `<cond>` returns `true`. Is incompatible with `every`.
"""
macro gif(forloop::Expr, args...)
_animate(forloop, args...; type = :gif)
Expand All @@ -275,6 +289,11 @@ Example:
push!(p, 1, sin(x))
end
```
This macro supports additional parameters, that may be added after the main loop body.
- Add `fps=n` with positive Integer n, to specify the desired frames per second.
- Add `every n` with positive Integer n, to take only one frame every nth iteration.
- Add `when <cond>` where `<cond>` is an Expression resulting in a Boolean, to take a
frame only when `<cond>` returns `true`. Is incompatible with `every`.
"""
macro apng(forloop::Expr, args...)
Comment on lines +289 to 295
Copy link
Member

@t-bltg t-bltg Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use @doc (@doc gif) macro apng to repeat a documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this, with

let moreDocumentation = """This macro supports ... """
    @doc string(@doc @gif)*moreDocumentation :(@gif)
    @doc string(@doc @apng)*moreDocumentation :(@apng)
    @doc string(@doc @animate)*moreDocumentation :(@animate)
end

but instead of extending the documentation it added a whole new one. It was not very pretty.

_animate(forloop, args...; type = :apng)
Expand All @@ -292,6 +311,11 @@ anim = @animate for x=0:0.1:5
end
gif(anim)
```
This macro supports additional parameters, that may be added after the main loop body.
- Add `fps=n` with positive Integer n, to specify the desired frames per second.
- Add `every n` with positive Integer n, to take only one frame every nth iteration.
- Add `when <cond>` where `<cond>` is an Expression resulting in a Boolean, to take a
frame only when `<cond>` returns `true`. Is incompatible with `every`.
"""
macro animate(forloop::Expr, args...)
_animate(forloop, args...)
Expand Down