Interaction

Interaction in VisQuill works like everything else: something changes a value, and the relationships do the rest. Handles change their own position when they are dragged. For anything else (clicks, hovering, keys) you listen to DOM events and change a value yourself.

Handles

A handle is a point the user can drag, with mouse, pen or touch. You have used them in every chapter; three things are left to say.

Constraining. To keep a handle on a track, correct its position in a Reactive.do that is registered right after the handle (One update at a time).

Starting a drag from elsewhere. handle.active is a Bool that is true while the handle is dragged. Set it yourself, and the handle jumps to the pointer and is dragged from there. A slider uses that: press anywhere on the track, and the knob comes to you.

50
Drag the knob, or press anywhere on the track.
const track = canvas.visuals.box("shape clickable", [left, y - 8], [right, y + 8])
const knob = canvas.handles.disk("handle", [320, y])

// Keep the knob on the track
Reactive.do([knob], () => {
    knob.x = Math.min(right, Math.max(left, knob.x))
    knob.y = y
})

// The slider's value, 0 to 100
const value = canvas.values.real()
Reactive.do([knob], () => {
    Values.remap(knob.x, left, right, 0, 100, value)
})

// Pressing the track starts dragging the knob: it jumps to the pointer
Svg.get(track).addEventListener("pointerdown", () => {
    knob.active.value = true
})

Switching off. handle.isInteractive = false keeps a handle where it is: it can no longer be grabbed, but it still moves when you set its position.

DOM events

Every visual is an SVG element, and Svg.get gives it to you, so you can listen to any DOM event. Keep the listener small: it changes a value, and a Reactive.do turns the value into what the user sees.

Click a circle to select it, click it again to deselect it.
const items = [160, 320, 480].map(x => {
    const item = canvas.visuals.circle("shape clickable")
    Circles.circleAt([x, 140], 50, item)
    return item
})

// Which item is selected: a value like any other
const selected = canvas.values.item(null)
items.forEach(item => {
    Svg.get(item).addEventListener("click", () => {
        selected.value = selected.value === item ? null : item
    })
})

// The look follows the value
Reactive.do([selected], () => {
    for (const item of items)
        item.applyStyleDirective(item === selected.value ? "@class selected" : "@class-remove selected")
})

This split pays off as soon as there is more than one way to change something. A key press, a button outside the graphic or a URL parameter can set selected too, and the look follows, because it depends only on the value.

Changing the look

visual.applyStyleDirective(style) changes the style of an existing visual, with the same syntax as the style string of the factories (Styles). It adds what you give it, so use @class-remove name to take a class away again. Classes get the prefix of the visual’s group, as always.

Prefer this to Svg.get(visual).setAttribute("class", …): that throws away the classes and the prefixes VisQuill set, and VisQuill doesn’t know about the change.

Viewports: pan and zoom

A viewport is a window onto a larger drawing: its content is clipped to the window, and the user can move and zoom it. Drag the empty space, and scroll to zoom:

The dots are in the viewport’s content. The handle inside it still drags as usual.
const view = canvas.viewports.box([20, 20], [620, 260], { panning: true, zooming: true })

// Draw into the viewport's content; it is clipped to the box and moves with pan and zoom
for (let i = 0; i < 160; i++) {
    const x = 40 + (i * 97) % 560, y = 40 + (i * 53) % 200
    Circles.circleAt([x, y], 3 + (i % 5), view.content.visuals.circle("shape"))
}
view.content.handles.disk("handle", [320, 140])

canvas.viewports.box(p1, p2, options) makes a rectangular viewport, circleAt(center, radius, options) a round one. A viewport has two groups:

  • content is clipped to the window, and moves and zooms with it. Draw the drawing here.
  • frame is drawn on top and doesn’t move: for a border, a legend, buttons.

panning and zooming switch the two gestures on; zoomFactor is the zoom per wheel step. Handles in the content keep working: they are dragged, not panned.

How far the view is panned and zoomed is a pin, viewport.origin, in the same way as the anchor of a group. The helpers in Viewports read and change it in program code, for a “zoom in” button or a reset:

const zoom = Viewports.getZoom(view.origin).value   // 1 = not zoomed
Viewports.zoomBy(view.origin, 2, [320, 140], view.origin)  // zoom in around (320, 140)
Viewports.reset(view.origin)                         // back to the start