In [1]:
using PyPlot
using PyCall
@pyimport matplotlib.animation as anim
In [2]:
const n = 2^8
x = (1:n) / n
x -= 1/2
y = (1:n) / n
y -= 1/2
# x, y = Vector{Float64}(x),
Out[2]:
-0.49609375:0.00390625:0.5
In [3]:
struct Params
    x
    y
    sig::Float64
    a::Float64
    θ::Float64

    sf::Float64
    phase::Float64

    n::Int64
end
base_params = Params(x, y, 0.1, 1.4, π/32, 4, π/32, n)
Out[3]:
Params(-0.49609375:0.00390625:0.5, -0.49609375:0.00390625:0.5, 0.1, 1.4, 0.09817477042468103, 4.0, 0.09817477042468103, 256)
In [4]:
function draw!(x, y, Z, params::Params)
    R = [ cos(params.θ) sin(params.θ);
         -sin(params.θ) cos(params.θ);]
    for i in 1:params.n
        for j in 1:params.n
            x_rot, y_rot = R * [x[i], y[j]]
            if x_rot^2 + y_rot^2 < (4params.sig)^2
                Z[j, i] = exp(-(x_rot^2 + y_rot^2/params.a^2) / (2params.sig^2))
                Z[j, i] *= cos(2π*params.sf*x_rot - params.phase)
            end
        end
    end
end

function animate(i::Int64, base_params::Params)
    global ax
    
    params = Params(base_params.x, base_params.y,
        base_params.sig, base_params.a,
        base_params.θ*i, base_params.sf,
        base_params.phase*i, base_params.n)
    Z = zeros(Float64, base_params.n, base_params.n)
    draw!(base_params.x, base_params.y, Z, params)
    img = ax[:imshow](Z, vmin=-1, vmax=1, origin="lower", 
        extent=[x[1], x[end], y[1], y[end]])
    return (img)
end
Out[4]:
animate (generic function with 1 method)
In [5]:
fig, ax = subplots()
set_cmap("gray")
animate(1, base_params)
Out[5]:
PyObject <matplotlib.image.AxesImage object at 0x1212b5e10>
In [6]:
fig, ax = subplots()
set_cmap("gray")

myanim = anim.FuncAnimation(fig, animate, 
    frames=64, interval=100, fargs=(base_params,))
myanim[:save]("test1.mp4", bitrate=-1,
    extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"])
In [8]:
# Function for creating an embedded video given a filename
function html_video(filename)
    open(filename) do f
        base64_video = base64encode(f)
        """<video controls src="data:video/x-m4v;base64,$base64_video">"""
    end
end

# Display the movie in a Julia cell as follows. Note it has animation controls for the user.
display("text/html", html_video("test1.mp4"))