Animations

Something the plugin draws as a loop runs counts as an animation of the project.

The animation window plays it, and every export of the animation (a GIF, a video, a sprite sheet, a plugin's own kind of file) writes it.

animations: [{
  key: 'pulse', name: 'Pulse',
  frames: (doc) => (doc.pulse ? doc.pulse_frames ?? 12 : 0),      // 0 or 1: this project is not moved by it
  resize: (doc, frames) => void (doc.pulse_frames = frames),
  draw(doc, at) {
    const canvas = document.createElement('canvas');
    canvas.width = at.w;
    canvas.height = at.h;
    const pen = canvas.getContext('2d')!;
    pen.globalAlpha = 0.5 + 0.5 * Math.sin(at.t * Math.PI * 2);
    pen.fillStyle = '#ffffff';
    pen.fillRect(0, 0, at.w, at.h);
    return canvas;
  },
}],
import math

from rassam import canvas


def frames(doc):
    return doc.get('pulse_frames', 12) if doc.get('pulse') else 0      # 0 or 1: this project is not moved by it


def resize(doc, count):
    doc['pulse_frames'] = count


@plugin.animation('Pulse', frames=frames, resize=resize)
def pulse(doc, at):
    sheet = canvas(at.w, at.h)
    pen = sheet.getContext('2d')
    pen.globalAlpha = 0.5 + 0.5 * math.sin(at.t * math.tau)
    pen.fillStyle = '#ffffff'
    pen.fillRect(0, 0, at.w, at.h)
    return sheet
KeyWhat it does
frames(doc)How many frames the loop has in a project. A picture with frames of its own keeps its own count; one without takes yours.
draw(doc, at)The picture at a moment of the loop. at.t runs from 0 to 1, at.frame is the frame's number, at.w and at.h the picture's size, at.layer(id) a layer as it is drawn.
resize(doc, frames)Lets the animation window set the number of frames.
nameLabels the loop in the animation window.

What draw gives

In Python, canvas(width, height) from rassam makes a new picture to draw on, the layer form is a dict ({'canvas': sheet, 'layer': id}), and frames may be a plain number when it does not depend on the project.