Render Queue

Stages

StagePurpose
READ_1Initial DOM read phase.
WRITE_1First DOM write phase.
READ_2Second DOM read phase.
WRITE_2Second DOM write phase. Animation processing follows this stage.
READ_3Final DOM read phase.
WRITE_3Final DOM write phase. Collision and debug rendering follow this stage.

Scheduling Work

object.schedule(callback, {
  stage: "READ_1",
  queueId: "measure",
});

If a queueId is reused for the same object, the newest queued task replaces the earlier task for that id.

object.schedule(() => {
  const width = object.element?.offsetWidth ?? 0;
  object.state.width = width;
}, { stage: "READ_1", queueId: "measure-width" });

object.schedule(() => {
  object.style = { width: `${object.state.width + 10}px` };
  object.writeDom();
}, { stage: "WRITE_1", queueId: "write-width" });

Writing DOM State

object.style = { backgroundColor: "red", padding: "10px" };
object.classList = ["active", "highlighted"];
object.dataAttribute = { state: "selected" };

object.schedule(() => {
  object.writeDom();
}, { stage: "WRITE_1" });

Writing Transforms

object.worldTransform = { x: 100, y: 200 };

object.schedule(() => {
  object.writeTransform();
}, { stage: "WRITE_1" });

Transform writing supports these modes:

ModeDescription
directWrites the object’s world transform directly.
relativeWrites a transform relative to the last read DOM position.
originWrites relative to transformOrigin, or the parent when unset.
noneClears SnapEngine’s generated transform.
object.transformMode = "relative";
object.transformOrigin = parentObject;

Reading DOM State

TypeScript

TypeScript

TypeScript

TypeScript

object.schedule(() => {
  object.readDom();
  const props = object.getDomProperty("READ_1");

  console.log(props.screenX, props.screenY, props.width, props.height);
}, { stage: "READ_1" });

FLIP Helper

object.requestFLIP(
  () => {
    object.style = { width: "240px" };
    object.writeDom();
  },
  () => {
    object.writeTransform();
  },
);

Public API

APIDescription
schedule(callback, { stage, queueId? })Queues read or write work.
FrameTask.cancel()Cancels a queued task callback.
readDom(config?, stage?)Reads DOM geometry in a read stage.
readDomRecursive(config?)Reads this object and child element objects recursively.
getDomProperty(stage?)Returns a cached DOM property object.
copyDomProperty(fromStage, toStage)Copies cached geometry between read stages.
saveDomProperety(stage?)Saves read geometry into world transform position.
writeDom()Applies pending style, classes, and data attributes.
writeTransform()Applies transform state as CSS transform.
requestFLIP(writeCallback, transformCallback)Queues a two-read/two-write FLIP sequence.