Move, rotate and scale a shape with one pin
Apply translation, rotation and uniform scale to a polygon from a single pin controlled by two handles. TypeScript with VisQuill.
import {
VisQuill, Reactive, Shapes, Boxes, Segments, Points, Pins, Vectors,
} from "@visquill/visquill-gdk"
import './styles.css'
// ─── Layout ───────────────────────────────────────────────────────────────
const cx = 0
const cy = 0
// Asymmetric arrow shape centred at origin so the pin transform is
// applied cleanly around (0, 0) before being placed on canvas.
const arrowVertices: [number, number][] = [
[ 80, 0 ],
[ 10, 40 ],
[ 10, 18 ],
[ -60, 18 ],
[ -60, -28 ],
[ 10, -28 ],
[ 10, -40 ],
].map(([x, y]) => [cx + x, cy + y])
// Initial pin: anchor at centre, tip 90px to the right (angle=0, scale=1).
const anchorStart: [number, number] = [cx, cy]
const tipStart: [number, number] = [cx + 90, cy]
/**
* Demonstrates `Shapes.transform()` — applying a similarity transform
* encoded in a `Pin` to a shape.
*
* A pin combines translation (its location), rotation (the angle of its
* orientation vector), and uniform scale (the length of its orientation
* vector). All three are controlled by two draggable handles:
*
* - **Anchor handle** (white) — sets the pin's location, i.e. the
* translation. The tip handle moves with it so rotation and scale
* are preserved.
* - **Tip handle** (purple) — sets the orientation vector endpoint.
* Dragging it changes both rotation (angle from anchor) and scale
* (distance from anchor, relative to the reference length).
*
* A ghost shows the original unmodified shape for reference.
*
* @param div - The div element that will contain the graphic.
*/
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-shapes-transform-")
const canvas = rvg.canvas
const content = canvas.layer()
// ─── Instruction label ────────────────────────────────────────────
const instructions = canvas.text.label(
"Drag anchor to translate · Drag tip to rotate and scale",
"instructions"
)
Reactive.do([rvg.frame], () => {
Points.moveDown(
Segments.midPoint(Boxes.topSide(rvg.frame)),
25,
instructions
)
Vectors.copy(rvg.center,content.anchor)
})
// ─── Handles ──────────────────────────────────────────────────────
const hAnchor = content.handles.disk("handle-anchor", anchorStart)
const hTip = content.handles.disk("handle-tip", tipStart)
// ─── Ghost shape ──────────────────────────────────────────────────
content.visuals.polygon("ghost", arrowVertices)
// ─── Transformed shape ────────────────────────────────────────────
const arrow = content.visuals.polygon("arrow")
// ─── Pin axis line (anchor → tip) ─────────────────────────────────
const axis = content.visuals.segment("axis")
// ─── Anchor dot (centre of pin location) ──────────────────────────
const anchorDot = content.visuals.point("anchor-dot")
// ─── Tip dot ──────────────────────────────────────────────────────
const tipDot = content.visuals.point("tip-dot")
// ─── Info labels (top-left) ───────────────────────────────────────
const infoLeft = 20
const infoTop = 55
const infoLineH = 20
const labelAngle = canvas.text.label("", "info-label")
const labelScale = canvas.text.label("", "info-label")
const labelTransX = canvas.text.label("", "info-label")
const labelTransY = canvas.text.label("", "info-label")
Reactive.do([rvg.frame], () => {
const tl = Boxes.topSide(rvg.frame).at(0)
Points.moveRight(Points.moveDown(tl, infoTop + 0 * infoLineH), infoLeft, labelAngle)
Points.moveRight(Points.moveDown(tl, infoTop + 1 * infoLineH), infoLeft, labelScale)
Points.moveRight(Points.moveDown(tl, infoTop + 2 * infoLineH), infoLeft, labelTransX)
Points.moveRight(Points.moveDown(tl, infoTop + 3 * infoLineH), infoLeft, labelTransY)
})
// ─── Reference pin length (scale = 1) ────────────────────────────
// The initial distance from anchor to tip defines "scale = 1".
const refLen = Points.distanceToPoint(anchorStart, tipStart).value
Pins.withLength(hAnchor,refLen,hAnchor)
// ─── Reactive block — anchor moved ────────────────────────────────
// When the anchor moves, shift the tip by the same delta so the
// orientation vector (and therefore rotation + scale) is unchanged.
Reactive.do([hAnchor], () => {
Pins.moveForward(hAnchor,Pins.len(hAnchor).value,hTip)
})
Reactive.do([hTip],()=>{
Pins.withOrientation(hAnchor,Vectors.direction(hAnchor,hTip),hAnchor)
})
// ─── Reactive block — full recompute ──────────────────────────────
Reactive.do([hAnchor, hTip], () => {
// Derive readable values.
const pin = Pins.withLength(hAnchor,Pins.len(hAnchor).value/refLen)
const angleDeg = (Pins.angle(pin).value * 180 / Math.PI).toFixed(1)
const scale = (Pins.len(pin).value).toFixed(2)
// Apply the similarity transform.
Shapes.transform(arrowVertices, pin, arrow)
// Axis line.
Points.copy(hAnchor, axis.at(0))
Points.copy(hTip, axis.at(1))
// Dots.
Points.copy(hAnchor, anchorDot)
Points.copy(hTip, tipDot)
// Info panel.
labelAngle.value = `angle: ${angleDeg}°`
labelScale.value = `scale: ${scale}×`
labelTransX.value = `trans x: ${hAnchor.x.toFixed(1)} px`
labelTransY.value = `trans y: ${hAnchor.y.toFixed(1)} px`
}, true)
}Shapes.transform moves, rotates and scales a shape in one step with a pin: position for the
translation, orientation for the rotation and its length for the scale.
How it works
Shapes.transform reads a pin as a transform: its position is the translation, its orientation's angle the rotation, and the orientation's length the scale factor. The arrow's vertices are given around (0, 0), so the pin places that origin.
The anchor handle is itself a pin. Dragging the tip changes its orientation:
Reactive.do([hTip],()=>{
Pins.withOrientation(hAnchor,Vectors.direction(hAnchor,hTip),hAnchor)
})
The main reaction divides the orientation's length by its start length, so the start position means scale 1, then transforms the vertices into the visual:
const pin = Pins.withLength(hAnchor,Pins.len(hAnchor).value/refLen)
// …
Shapes.transform(arrowVertices, pin, arrow)
A third reaction moves the tip along with the anchor, so dragging the anchor only translates.
API
Shapes.transform: applies a pin's translation, rotation and scale to a shapePins.withOrientation: sets a pin's orientation vectorPins.withLength,Pins.len: set and read the orientation's lengthPins.moveForward: places the tip at the end of the orientationPins.angle: the rotation shown in the info panel
See also
geometry/05-fittinggeometry/shapes-01-orientate




