Skip to content

Interactions ​

The scene listens to pointer events, so mouse, touch and pen are all supported and trigger the same events.

EventWhen
mousedown / mouseupPointer pressed / released over the element
clickPressed then released without moving more than 10px
mousemovePointer moved over the element
hover / leavePointer starts / stops hovering the element
scrollup / scrolldown (zoomin / zoomout)Mouse wheel
contextmenu, dblclickSame as their DOM counterpart
keydown / keyupFired on the scene

Use MouseEvent.events and KeyboardEvent.events constants instead of strings.

Drag, rotate and resize ​

js
import "pencil.js/draggable";

const api = rectangle.draggable({
    y: false, // Only move horizontally
    constrain: [[0, 0], [500, 0]], // In the parent space
});

api.stop(); // Not draggable anymore

Dragging works whatever the transformations (rotation, scale …) of the element's ancestors.

js
import "pencil.js/rotatable";

knob.rotatable(); // Rotate around options.rotationCenter
js
import "pencil.js/resizable";

rectangle.resizable(); // Add a handle at the bottom-right corner

These interactions fire grab, drag, rotate, resize and drop events.

Coordinates ​

Pointer events give positions in the scene space. Convert them for any element:

js
scene.on("click", (event) => {
    const local = group.toLocal(event.position);
    const back = group.toGlobal(local);
});

getAbsolutePosition() and getAbsoluteMatrix() give the element's position and full transformation in the scene.

Released under the MIT License.