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 vjs-surface, declare a ViewChild of type SurfaceComponent:
You can then use the surface member of your surface component to access the surface, which is of type Surface
import { ViewChild } from "@angular/core"
import { SurfaceComponent, SurfaceComponentRef } from "@visuallyjs/browser-ui-react"
@Component({
selector: 'app-my-app',
template: `<div>
<button (click)="centerContent">Center Content!</button>
<vjs-surface [renderOptions]="renderOptions">
</vjs-surface>
</div>
`,
standalone: true,
imports: [VisuallyJsModule]
})
export class MyComponent implements OnInit {
@ViewChild(SurfaceComponent) surfaceComponent!: SurfaceComponent;
centerContent() {
this.surfaceComponent.surface.centerContent()
}
}
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 VisuallyJsService, which exposes a surface() signal:
import { ViewChild, inject } from "@angular/core"
import { VisuallyJsService } from "@visuallyjs/browser-ui-react"
import { BrowserUIModel } from "@visuallyjs/browser-ui"
@Component({
selector: 'app-my-app',
template: `<div>
<button (click)="centerContent">Center Content!</button>
</div>
`,
standalone: true,
imports: [VisuallyJsModule]
})
export class MyComponent implements OnInit {
$vjs = inject(VisuallyJsService)
centerContent() {
this.$vjs.surface().addNode({id:"1", left:50, top:50})
}
}
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 |