UNPKG

forma-embedded-view-sdk

Version:

The Forma Embedded View SDK is a JavaScript library for creating custom extensions in Autodesk Forma (previously Spacemaker).

101 lines (100 loc) 3.28 kB
function loadImage(url) { return new Promise((resolve, reject) => { const img = new Image(); img.onload = () => { resolve(img); }; img.onerror = () => { reject(new Error("Failed to load image")); }; img.src = url; }); } async function canvasFromDataUrl(url, width, height) { const canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; const ctx = canvas.getContext("2d"); if (!ctx) { throw new Error("Failed to get 2d context from canvas"); } const imageElement = await loadImage(url); ctx.drawImage(imageElement, 0, 0); return canvas; } /** * Interact with the camera in the 3D scene of designmode. * * @remarks * Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.camera | camera}. */ export class CameraApi { #iframeMessenger; /** @hidden */ constructor(iframeMessenger) { this.#iframeMessenger = iframeMessenger; } /** * Move camera view to a new position. * * @example * // Move camera to view down on the center of the proposal from directly * // above, spending 1 second on the transition. * await Forma.camera.move({ * position: { x: 0, y: 0, z: 100 }, * target: { x: 0, y: 0, z: 0 }, * transitionTimeMs: 1000, * }) */ async move(request) { await this.#iframeMessenger.sendRequest("scene/camera/move", request); } /** * Toggle between perspective and orthographic camera. * * @example * await Forma.camera.switchPerspective() */ async switchPerspective() { await this.#iframeMessenger.sendRequest("scene/camera/switch-perspective"); } /** * Capture a screenshot of the current camera view as a canvas. * * @returns Canvas with the captured screenshot. * * @example * // Capture a 100x100 pixel screenshot of the current camera view. * const canvas = await Forma.camera.capture({ width: 100, height: 100 }) */ async capture(request) { return await canvasFromDataUrl(await this.#iframeMessenger.sendRequest("scene/camera/capture", request), request.width, request.height); } /** * Fetch the current camera state in the designmode scene. * * @returns Current camera state. * * @example * const currentCameraState = await Forma.camera.getCurrent() */ async getCurrent() { return await this.#iframeMessenger.sendRequest("scene/camera/get-current"); } /** * Subscribe to camera changes. * * @example * const { unsubscribe } = await Forma.camera.subscribe((cameraState: CameraState) => { * console.log("Camera was updated", cameraState) * }) * // Later, when you want to stop listening for changes: * unsubscribe() * * @param callback event handler for each camera change. Callback receives the new camera state as an argument. * @returns { unsubscribe: () => void } object with an `unsubscribe` method to stop listening for changes */ async subscribe(callback) { return await this.#iframeMessenger.createSubscription("camera/on-change", callback); } }