Viewers Sync
Synchronize pan and zoom across two Canvas viewers with the NPM SDK.
Use one @rasterex/viewer instance per Canvas iframe and coordinate them through viewer.viewSync. The host chooses the group, configures each viewer, aligns the initial snapshot, and relays later changes to the sibling viewer.
This page documents the NPM SDK integration. It does not use the PostMessage commands directly.
- 1Create both viewers and mount them.
- 2Wait for both
ready()calls to resolve. - 3Configure both viewers with the same
groupId, distinctinstanceIdvalues, andenabled: false. - 4Open the documents and wait for both
documents.open()calls to resolve. - 5Request a snapshot from the source with
getSnapshot()and apply it to the target withapplySnapshot(). - 6Enable both viewers and set up
changed,applied, andfailedsubscriptions. - 7Relay each accepted change to the sibling with
apply(change, { pan, zoom }).
The host keeps relay disabled until both viewers are ready and the target has applied the source snapshot. It stops subscriptions and destroys both viewers during cleanup. The example uses React lifecycle hooks; the same SDK sequence applies to other frameworks or a plain TypeScript host.
React host implementation
Set one mode for the group. panAndZoom synchronizes both operations, pan synchronizes pan, zoom synchronizes zoom, and off disables local view-sync events.
Use compatible documents and viewport geometry for snapshot alignment. When a viewer changes documents, stop relaying, configure synchronization off, reset sequence tracking, open the new document, align again, and only then enable relay.
configure(), getSnapshot(), and applySnapshot() wait for their results and reject on Canvas failure, invalid results, or timeout. apply() returns after sending the operation; listen for applied or failed to observe the Canvas result.
Canvas may ignore stale sequences without reporting an error. Do not treat a fire-and-forget apply() call as confirmation that the target finished applying the change.
- Configuration or snapshot failure: keep relay disabled, correct readiness or document alignment, and retry.
- Apply failure: stop relaying, disable both viewers, and establish a new initial snapshot.
- Feedback loop: verify that the deployed Canvas build does not emit a new local change for a remotely applied operation.
Subscribe to changed for source operations and applied or failed for remote application results. Remove every subscription and call destroy() for both viewers when the host unmounts.
