Lenses

Spotlight lens with an SVG mask

Darken a chart and reveal a draggable circular area in full colour with a reactive SVG mask; adjust the radius.

import {VisQuill, Reactive, Attach, Animate, Boxes, Circles, Svg} from "@visquill/visquill-gdk"
import './styles.css'
import {createPoints} from "../../utils/decoration";

// Configuration
// The radius button cycles through these values.

const radii = [80, 100, 120, 140]

const dataTypes = ["red", "green", "blue", "orange"]
const numberOfDataPoints = 100

// Bounding box for scatter points. The lens moves freely within this area.
const pointBox = Boxes.box([150, 50], 800, 600)

/**
 * Creates a draggable lens that shows the scattered points through a hole in a
 * dark cover layer. The hole is a mask circle that follows the lens.
 *
 * @param div - HTML element where the graphic renders
 */
export function run(div: HTMLDivElement) {
    const rvg = VisQuill.create(div, "demos-")
    const canvas = rvg.layer("lenses-masked-background-")

    // Layer organization
    const pointLayer = canvas.layer()
    const cover = canvas.layer()

    // Lens in dedicated sub-layer for group movement
    const lens = canvas.layer()
    const lensRadius = lens.values.real(radii[0])

    // Controls last, so they sit above the cover
    const controls = canvas.layer()

    // Invisible handle provides drag target for lens
    const handle = canvas.handles.disk("handle", Boxes.center(pointBox))

    // Build scene
    createPoints(pointLayer, pointBox, numberOfDataPoints, dataTypes)
    createCover()
    createLens()

    createRadiusButton()

    function createCover() {
        const box = cover.visuals.box("@style fill:#000000AA")
        Reactive.do([rvg.frame], () => {
            Boxes.copy(rvg.frame, box)
        })
        const frameMask = cover.mask.box("white")
        Reactive.do([rvg.frame],()=>{
            Boxes.copy(rvg.frame, frameMask);
        })
        const circleMask = cover.mask.circle("black")
        Reactive.do([handle,lensRadius],()=>{
            Circles.circleAt(handle,lensRadius,circleMask)
        })
    }

    // A box with a label; each click animates the radius to the next value
    function createRadiusButton() {
        const button = controls.visuals.box("button", [10, 20], [150, 44])
        const label = controls.text.labelAt("", Boxes.center(button), "button-label")
        let index = 0
        const show = () => label.value = `Radius: ${radii[index]} px`
        show()
        Svg.get(button).addEventListener("click", () => {
            index = (index + 1) % radii.length
            show()
            Animate.real.eased(lensRadius, radii[index], 1000)
        })
    }

    function createLens() {
        // Circular boundary
        const ring = lens.visuals.circle("ring")
        Attach.circleRadiusToValue(ring, lensRadius)

        // Pin lens group to draggable handle
        Attach.pointToPoint(lens.anchor, handle)
    }
}

A dark cover over a chart with a circular hole that follows a draggable lens. The hole is an SVG mask: canvas.mask.box shows the cover, canvas.mask.circle hides it inside the lens.

How it works

The cover is a layer with one dark box over the whole frame. Its mask has two parts: a white box (show the cover) and a black circle on top of it (hide the cover). Where the circle is, the points below show through.

const box = cover.visuals.box("@style fill:#000000AA")
// …
const frameMask = cover.mask.box("white")
// …
const circleMask = cover.mask.circle("black")
Reactive.do([handle,lensRadius],()=>{
    Circles.circleAt(handle,lensRadius,circleMask)
})

The mask circle follows the drag handle and the radius option. The ring on top is a separate layer, attached once to the same handle and radius.

const ring = lens.visuals.circle("ring")
Attach.circleRadiusToValue(ring, lensRadius)

Attach.pointToPoint(lens.anchor, handle)

API

  • canvas.mask.box, canvas.mask.circle: mask shapes of a layer; white shows, black hides
  • Circles.circleAt: moves the mask circle to the handle
  • Attach.circleRadiusToValue: ties the ring's radius to the option
  • Attach.pointToPoint: moves the lens layer with the handle

Notes

The drag handle is transparent and has a radius of 50 px, so the lens can be dragged only near its centre.

See also

  • lenses/02-single-data-type
  • basics/04-layers

API used

Also used (13)