This guide demonstrates changing which part of an image is visible in an Editor.
Related APIs:
Command
that moves/zooms/rotates the canvas.Command
.Viewport.transformBy
how to move/scale/rotate.
Mat33.scaling2D(2)
zooms in by a factor of 2.For example, to zoom out by a factor of 4,
Above:
document.body
.transformBy
command is created. In this case, the command scales the viewport by a factor of 4.
1/4
with 1/2
. This should zoom out by a factor of 2.1/4
with 4
. This should zoom in by a factor of 4.command
to the editor.
transformBy
command is unapplied.In the example above, pressing "undo" unapplies the zoom command. There are at least two ways to prevent this:
editor.dispatch(command)
with command.apply(editor)
.
command
was done for accessibility tools.editor.dispatch(command)
with editor.dispatch(command, false)
.See also Editor.dispatchNoAnnounce.
Let's start by creating an editor and adding it to the document:
Next, to make it clear that the editor is moving, let's give the editor a repeating background:
Next, let's change the position of the canvas in a loop:
Above, the Vec2.of(-1, 0)
gives the direction to move the canvas.
Things to try:
Vec2.of(-1, 0)
with Vec2.of(0, 1)
. How is the viewport updated?.apply
twice or use Mat33.rightMul to combine the two transformations.The above update loop has a problem — on some devices, the viewport moves faster than on others.
To fix this, we determine the time elapsed between each animation frame and use this to scale the position change: