Navigating the canvas
The surface widget offers a number of methods you can use to assist your users in navigating their way around the canvas. You'll need to access the surface to use these methods programmatically - expand the section below for details.
How to access the surface
From your app component
To access the surface from inside a component that uses a SurfaceComponent, declare a ref of type SurfaceComponent and assign it in the template.
You can then access the model through the ref's value, which is of type SurfaceComponent
<script setup lang="ts">
import { SurfaceComponent} from "@visuallyjs/browser-ui-svelte"
let surfaceComponent: SurfaceComponent = $state(null)
function centerContent() {
surfaceComponent.getSurface().centerContent()
}
</script>
<template>
<SurfaceComponent bind:this={surfaceComponent}/>
<button onClick={() => centerContent()}>Center Content!</button>
</template>
From a different component
If you have a component somewhere in your tree from which you wish to interact with the surface, you can use the useSurface() hook in conjunction with a SurfaceProvider to access the surface. Your app component should look something like this:
<script setup lang="ts">
</script>
<template>
<SurfaceProvider>
<SurfaceComponent/>
<MyOtherComponent/>
</SurfaceProvider>
</template>
Then the implementation of MyOtherComponent can use the useSurface hook:
<script setup lang="ts">
import { useSurface } from "@visuallyjs/browser-ui-svelte"
import { Surface } from "@visuallyjs/browser-ui"
const surface:RefObject<Surface> = useSurface() // this is reactive state
function centerContent() {
surface.current.centerContent()
}
</script>
<template>
<div><button onClick={() => centerContent()}>Center content!</button></div>
</template>
Positioning
centerOn
| element | string | Element | Vertex | Array<string | Element | Vertex> | The element(s) to center. Can be a DOM element, vertex id, or a Node/Group, or an array of these. |
| doNotAnimate | boolean |
centerOnHorizontally
| element | string | Element | Vertex | The element to center. Can be a DOM element, vertex id, or a Node/Group |
centerOnVertically
| element | string | Element | Vertex | The element to center. Can be a DOM element, vertex id, or a Node/Group |
centerContent
| options | CenterContentOptions | Method parameters. |
pan
| dx | number | Amount to pan in X direction |
| dy | number | Amount to pan in Y direction |
| doNotAnimate | boolean | By default this operation uses animation. |
setPan
| left | number | Position in pixels of the left edge of the panned content. |
| top | number | Position in pixels of the top edge of the panned content. |
| animate | boolean | Whether or not to animate the pan. Defaults to false. |
| onComplete | (p:PointXY) => any | If is set to true, an optional callback for the end of the pan |
alignContent
| options | AlignContentOptions |
alignContentLeft
| options | AlignContentToFaceOptions |
alignContentRight
| options | AlignContentToFaceOptions |
alignContentTop
| options | AlignContentToFaceOptions |
alignContentBottom
| options | AlignContentToFaceOptions |
Zooming
You can zoom the canvas in a number of different ways:
zoomToFit
doNotZoomIfVisible:true on the parameters to this method.| params | ZoomToFitOptions |
zoomToFitIfNecessary
| params | ZoomToFitIfNecessaryOptions |
zoomToElements
| zParams | ZoomToElementsOptions<BrowserElement> |
zoomToExtents
| zParams | ZoomToExtentsOptions | Options for the zoom |
zoomToDecorator
decorator:string | Decorator,
doNotAnimate:boolean,
doNotFirePanEvent:boolean,
doNotZoomIfVisible:boolean,
fill:number,
onComplete:(p:PointXY) => any,
onStep:() => any
})
| zParams | {
decorator:string | Decorator, doNotAnimate:boolean, doNotFirePanEvent:boolean, doNotZoomIfVisible:boolean, fill:number, onComplete:(p:PointXY) => any, onStep:() => any } |
Finding elements
Several methods are available to assist you to find elements in the canvas.
findIntersectingVertices
origin and dimensions.| options | IntersectingVerticesOptions | Options for the find operation. |
Mapping coordinates
Every now and then you'll likely want to map between the surface's coordinate system and the page coordinate system. The surface offers a few methods to assist with this.
isInViewport
| x | number | X location of point to test |
| y | number | Y location of point to test |
fromPageLocation
This takes into account the offset of the canvas in the page so that what you get back is the mapped position
relative to the target element's [left,top] corner
| left | number | X location |
| top | number | Y location |
| roundValues | boolean | If true, the location is returned as integers for x/y. |
toPageLocation
page in this method takes scroll into account. If you wish to map to just the visible section of the browser, use toWindowLocation.| left | number | X location |
| top | number | Y location |
fromWindowLocation
window, and is not the same as the page, because of scroll.
| left | number | |
| top | number | |
| roundValues | boolean |