# How to use SaveDefinitions when using Rasterized Frames for a ListAnimate?

In another post (Is it possible to prerender animation in Wolfram Mathematica?) I got a good way to prerender frames in order to get a smooth animation using ListAnimate. I need to save the Animation in a CDF document so it can be viewed by people using Wolfram CDF Player and not needing to run cells, so I use SaveDefinitions->True within the ListAnimate command. The problem is, when using rasterized frames, ListAnimate never runs and apparently causes a memory overflow.

How can I solve this?

I use this code for example (which doesn't run when SaveDefinitions is set to True):

exampleFrames =
Table[
DensityPlot[
Evaluate[
Exp[-((x - Cos[t])^2 + (y - Sin[t])^2)/.025]
],
{x, -1.5, 1.5}, {y, -1.5, 1.5},
ColorFunction -> GrayLevel,
PlotRange -> All,
PlotPoints -> 30,
Frame -> None,
],
{t, Pi/50, 2 Pi, Pi/50}
];

rasterizedFrames = Map[Image, exampleFrames];

ListAnimate[rasterizedFrames, SaveDefinitions -> True]


I need this for my dissertation so any help would be really appreciated.

I have the same memory issue as you do. Try Compress/Uncompress with Animate:

exampleFrames =
Table[DensityPlot[
Evaluate[Exp[-((x - Cos[t])^2 + (y - Sin[t])^2)/.025]], {x, -1.5, 1.5}, {y, -1.5, 1.5},
ColorFunction -> GrayLevel, PlotRange -> All, PlotPoints -> 30, Frame -> None, PlotRangePadding -> None],
{t, Pi/50, 2 Pi, Pi/50}];

rasterizedFrames = Map[Image, exampleFrames];

compressedFrames = Compress /@ rasterizedFrames;

Animate[Uncompress[compressedFrames[[i]]], {i, 1, Length@compressedFrames, 1},
SaveDefinitions -> True]


It seems to run smoothly (to me). It's much smaller, probably because there's so much black in your plot:

ByteCount /@ {exampleFrames, rasterizedFrames, compressedFrames}
(* -> {9698688, 38924896, 1175328} *)

• Thanks for the hint about Compress! This saves a huge amount of memory and makes the prerendering technique usable. Jun 7, 2013 at 19:23

If I understand correctly, you want to use SaveDefinitions->True because the animation is supposed to be ready for playing when a CDF is loaded. I did get the CDF to work with ListAnimate, so the memory problem may be specific to your system.

However, since the ListAnimate command is also very slow, you may be interested in this alternative approach which works much faster:

makeAnimation[list_, delayList_: {.03}] :=
DynamicModule[{l = Length[list], delays = Abs@Flatten[{delayList}],
times, totalTime, delta = .03, frames},
times = Round[.5 + PadRight[delays, l, delays]/delta];
frames =
Flatten@Table[Table[list[[i]], {times[[i]]}], {i, Length[times]}];
totalTime = Length[frames];
EventHandler[
Dynamic[frames[[Clock[{1, totalTime, 1}, totalTime delta]]],
TrackedSymbols -> {}], {"MouseUp", 2} :> Null]]

exampleFrames =
Table[DensityPlot[
Evaluate[Exp[-((x - Cos[t])^2 + (y - Sin[t])^2)/.025]], {x, -1.5,
1.5}, {y, -1.5, 1.5}, ColorFunction -> GrayLevel,
PlotRange -> All, PlotPoints -> 30, Frame -> None,
PlotRangePadding -> None], {t, Pi/50, 2 Pi, Pi/50}];

rasterizedFrames = Map[Image, exampleFrames];

makeAnimation[rasterizedFrames]


The output is an animation without controls, much like a GIF89 movie. You can select its cell bracket and copy it to another notebook without the source code, and it will still work. This includes notebooks which you deploy as standalone CDF files.

There is a second argument in makeAnimation that's optional. It must be in the form of a List, and each entry is a frame duration in seconds. If there's just one element in the lest, that will be used as the duration for all frames. Otherwise the durations of consecutive frames will cycle through the list. The default is a constant duration of 0.03.

If the memory is the problem:

In that case, you may want to add the option ImageSize -> 150 (or some suitably small number) to the DensityPlot command. This will also speed up the ListAnimate initialization.