A library for the programming environment processing to record and save animated gifs.
Download the animatedgif library from the releases section of this repository.
unzip the zip file that you downloaded from above, then place the animatedgif folder into the libraries folder inside Processing's sketchbook folder. Where is the sketchbook folder? For osx users this folder by default is located at ~/Documents/Processing/libraries. For windows users this folder is probably located at c:/My Documents/Processing/libraries also see this tutorial. After putting the library into the libraries folder, restart processing to activate the library.
Initialize a GifRecorder by creating a new instance of GifRecorder, the recorder automatically attaches itself to the tail of your draw procedure so that it can record the screen when instructed to do so.
import sojamo.animatedgif.*;
GifRecorder gif;
void setup() {
gif = new GifRecorder(this);
}
void draw() {}
Create a new GifRecorder instance
GifRecorder gif = new GifRecorder(this); // this is of type PApplet
Use the following setter and getter functions to customize the seetings of a gif recording session.
Set the duration of a recording session in milliseconds. When gif.record()
is called, the length of the recording will correspond to the duration set with gif.setDuration(int)
gif.setDuration(5000); // sets the duration to 5 seconds
gif.getDuration(); // returns an int representing milliseconds
Set the filename under which an animated gif will be saved, by default gifs will be saved into the sketch folder if no other folder is specified. The GifRecorder does add a timestamp to the filename so that more than one gifs can be saved under the same name, differentiated by the timestamp suffix.
gif.setFileName("myGif.gif"); // myGif will be saved to the sketchfolder
gif.getFileName(); // returns a String
Set the frames per second that will be used during record.
gif.setFramesPerSecond(10); // sets the update rate to 100ms
gif.getFramesPerSecond(); // returns an int
Set the interval between frames in milliseconds during record.
gif.setMillisBetweenFrames(100); // sets the update rate to 100ms
gif.getMillisBetweenFrames(); // returns an int
Set the number of frames that will be recorded and calculates the duration based on the millis between frames.
gif.setNumberOfFrames(20); // limits the frames to be recorded to 20
gif.getMillisBetweenFrames(); // returns an int
Scales the animated gif when saved by default the scale is set to 1.0 to double the size, use 2.0, to half the size use 0.5.
gif.setScale(0.5); // shrinks the original size to half
gif.getScale(); // returns a float
Set the loop status of the currently recorded animated gif
gif.setLoop(true); // gif will loop
gif.isLoop(); // returns a boolean
Use the following commands to control the and save gif recordings.
Add a frame to the current array of frames, use gif.clear()
to reset the frame-array.
gif.add();
Clear the frame array of recorded frames
gif.clear(); // deletes all recorded frames
Start recording frames over time based on the frames-per-second and duration settings. After recording has finished, the animated gif will be saved to the file defined by setFileName(String)
.
gif.record(); // starts the recording procedure
gif.isRecording(); // returns a boolean
Pause the current active recording
gif.pause();
Resume the currently paused recording session
gif.resume();
Stop the currenlty active recording session. To start over again, use gif.record
gif.stop();
Save the current gif recording under the name set by gif.setFileName()
with timestamp-prefix
gif.save();
Save the current gif recording
gif.saveAs("newGif.gif");
Request the state of current settings
gif.settings(); // returns a HashMap including current settings
De/activates the pre-defined key controls for GifRecorder, by default the key controls are deactivated. When active, use
- the space bar to add a frame
- s to save the current frame-array
- backspace to clear the frame-array
gif.setKeyControl(true); // activates the pre-defined key control
gif.isKeyControl(); // returns a boolean
Use a Timestamp as a prefix for recorded and saved gifs to avoid overriding, this feature is active by default
gif.useTimeStamp(false); // deactivates the timestamp prefix
gif.isTimeStamp(); // returns a boolean
And some example code which you can also find inside the examples folder that comes with the library.
import sojamo.animatedgif.*;
GifRecorder gif;
void setup() {
size(500,200);
// create a new GifRecorder to record animated gifs
gif = new GifRecorder(this);
// set the update time per second, here 100ms
gif.setMillisBetweenFrames(100);
// set the duration of the gif
gif.setDuration(5000);
// loop the gif
gif.setLoop(true);
}
void draw() {
background(frameCount%255);
}
void keyPressed() {
switch(key) {
// press key 'r' to start recording
// after the recording has finished, the gif
// will be saved to disc automatically
case('r'):gif.record();break;
}
}
The GifSequenceWriter.java code was created by Elliot Kroo link
9 March 2015