Imports

import { BaseObject, ElementObject } from "@snap-engine/core";

Base Objects

import { Engine, BaseObject } from "@snap-engine/core";

const engine = new Engine();
const object = new BaseObject(engine);

Objects receive a globally unique id when they are created.

Element Objects

import { Engine, ElementObject } from "@snap-engine/core";

const engine = new Engine();
const object = new ElementObject(engine);
const element = document.getElementById("box");

if (element) {
  object.element = element;
}

Parent and Child Objects

const parent = new BaseObject(engine);
const child = new BaseObject(engine, parent);

parent.appendChild(child);
parent.removeChild(child);

child.parent = parent;
child.parent = null;

Transforms

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

console.log(object.worldTransform.x, object.worldTransform.y);
console.log(object.localTransform.x, object.localTransform.y);

When a parent moves, child world transforms update from the preserved local offset.

const parent = new BaseObject(engine);
const child = new BaseObject(engine, parent);

child.localTransform = { x: 50, y: 50 };
parent.worldTransform = { x: 100, y: 100 };

console.log(child.worldTransform.x, child.worldTransform.y);

Coordinate Helpers

object.worldTransform = { x: 100, y: 200 };
object.cameraPosition = [50, 100];
object.screenPosition = [300, 400];

ElementObject DOM APIs

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

object.schedule(() => {
  object.writeDom();
}, { stage: "WRITE_1" });
object.schedule(() => {
  const box = object.readDom();
  console.log(box.x, box.y, box.width, box.height, box.padding.left);
  object.saveWorldPosition("READ_1");
}, { stage: "READ_1" });

Element Boxes

readDom() measures the element and returns an ElementBox (see Geometry):

FieldDescription
x, y, width, heightThe border box in world units (CSS pixels inside the camera layer, at any zoom).
margin, padding, borderComputed CSS edge thicknesses, also world units.
screenThe transformed viewport rectangle, exactly as getBoundingClientRect() reports it.

Every read produces a new frozen snapshot, so a box you keep never changes afterwards. elementObject.box always returns the latest one.

readDom({ unapplyTransform: true }) removes the element’s own inline translate and scale (honoring its computed transform-origin) to report its untransformed layout box; screen still reports what is rendered.

Public API

APIDescription
new BaseObject(engine, parent?)Creates an engine object.
new ElementObject(engine, parent?)Creates an object backed by a DOM element.
object.idUnique object id.
object.parentParent object or null.
object.childrenChild objects.
object.worldTransformWorld transform state.
object.localTransformLocal transform state.
object.cameraPositionPosition converted through the engine camera.
object.screenPositionPosition converted to viewport coordinates.
object.schedule(callback, config)Queues frame work.
object.destroy()Removes engine registrations and related resources.
elementObject.elementDOM element assigned to an element object.
elementObject.readDom(config?, stage?)Measures the element and returns a new frozen ElementBox.
elementObject.boxThe most recent measured ElementBox.
elementObject.getStageBox(stage?)The box captured by the last read in a read stage.
elementObject.copyStageBox(fromStage, toStage)Makes one read stage report another stage’s box.
elementObject.saveWorldPosition(stage?)Copies a stage box’s position into the object’s world transform.
elementObject.writeDom()Applies style, class, and data attribute state.
elementObject.writeTransform()Applies transform state to the DOM element.