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 and then use the getSurface() method on it:
import { SurfaceComponent, SurfaceComponentRef } from "@visuallyjs/browser-ui-react"
import "@visuallyjs/browser-ui/css/visuallyjs.css"
import {useRef} from "react"
export default function App() {
const surfaceRef = useRef<SurfaceComponentRef>(null)
const data = { ... }
const renderOptions = {...}
function doSomething() {
const surface = surfaceRef.current.getSurface()
// now you can invoke methods on "surface"
}
return <div style={{width:"100%", height:"500px"}}>
<SurfaceComponent data={data} renderOptions={renderOptions} ref={surfaceRef}/>
</div>
}
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:
import { useRef } from "react"
import { SurfaceComponent } from "@visuallyjs/browser-ui-react"
export default function MyComponent() {
return <SurfaceProvider>
<SurfaceComponent ref={surfaceRef}/>
<MyOtherComponent/>
</SurfaceProvider>
}
Then the implementation of MyOtherComponent can use the useSurface hook:
import { useRef } from "react"
import { SurfaceComponent, useSurface } from "@visuallyjs/browser-ui-react"
export default function MyOtherComponent() {
const surfaceRef = useRef<Surface>(null)
useSurface().then(s => surfaceRef.current = s)
function centerContent() {
surfaceRef.current.centerContent()
}
return <div><button onClick={() => centerContent()}>Center content!</button></div>
}
Note the difference between the ref type in the above example from the first example: when you use the useSurface hook the value passed into the promise is a Surface, not a SurfaceComponentRef.
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 |