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

object.schedule(() => {
  const box = object.readDom();

  console.log(box.screen.x, box.screen.y, box.width, box.height);
}, { stage: "READ_1" });
object.schedule(() => {
  object.readDom();
  object.copyStageBox("READ_2", "READ_3");
}, { stage: "READ_2" });
object.schedule(() => {
  object.readDom();
  object.saveWorldPosition("READ_1");
}, { stage: "READ_1" });
object.schedule(() => {
  object.readDom({ unapplyTransform: true });
  const box = object.getStageBox("READ_1");

  console.log(box.x, box.y);
}, { 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.
getStageBox(stage?)Returns the box captured by the last read in a read stage.
copyStageBox(fromStage, toStage)Makes one read stage report another stage’s box.
saveWorldPosition(stage?)Copies a stage box’s position into the world transform.
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.