Objects

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

TypeScript

TypeScript

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

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

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?)Reads and caches DOM geometry.
elementObject.getDomProperty(stage?)Returns cached DOM geometry.
elementObject.copyDomProperty(fromStage, toStage)Copies cached DOM geometry between read stages.
elementObject.saveDomProperety(stage?)Saves cached DOM position into the object transform.
elementObject.writeDom()Applies style, class, and data attribute state.
elementObject.writeTransform()Applies transform state to the DOM element.