Your first graphic
This chapter gets a graphic on the screen: two handles you can drag and a line between them. On the way you meet the three things every VisQuill program starts with: the graphic, its canvas, and the CSS prefix.
import { VisQuill } from "@visquill/visquill-gdk"
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "vqg-")
const canvas = rvg.canvas
canvas.handles.defaultZIndex = 1
const a = canvas.handles.disk("handle", [180, 190])
const b = canvas.handles.disk("handle", [460, 90])
canvas.visuals.segment("line", a, b)
}Install
npm install @visquill/visquill-gdkThe GDK is an ES module with its own type definitions. Any bundler works; the examples in this guide use Vite.
A div to draw into
VisQuill draws into an SVG element that it creates inside a div of yours. The SVG fills the
div, so the div needs a size:
<div id="graphic" style="width: 640px; height: 280px"></div>Then hand the div to VisQuill.create:
const rvg = VisQuill.create(div, "vqg-")
const canvas = rvg.canvaswhere div is that element, for example document.getElementById("graphic").
VisQuill.create returns the graphic. You rarely need the graphic itself; what you work with
is its canvas, the top-level group. Everything you create comes from one of its factories:
| Factory | Creates |
|---|---|
canvas.values |
reactive values: numbers, flags, texts, points, pins |
canvas.shapes |
geometry that is not drawn |
canvas.visuals |
shapes that are drawn |
canvas.handles |
points the user can drag |
canvas.text |
labels |
Coordinates are pixels of the SVG. The origin is the top-left corner, x grows to the right and y grows downwards, as everywhere in SVG and CSS.
Handles and a line
const a = canvas.handles.disk("handle", [180, 190])
const b = canvas.handles.disk("handle", [460, 90])
canvas.visuals.segment("line", a, b)A handle is a point you can drag. The segment is built from the two handles, not from copies of their positions, so it is always drawn between them. You didn’t write any code to make the line follow; Shapes and visuals explains why that works.
Handles are drawn above everything else in this guide because of one more line,
canvas.handles.defaultZIndex = 1. Without it, things are drawn in the order you create them,
and the line, created after the handles, would lie on top of them and catch the pointer.
Styling with CSS
The first argument of every factory is the style. In the simplest case it is a class name.
VisQuill puts the prefix from VisQuill.create in front, so "handle" becomes the class
vqg-handle:
.vqg-handle { fill: #C2994C; stroke: #1C1A17; stroke-width: 1.5; cursor: grab; }
.vqg-line { stroke: #3B372F; stroke-width: 2; }The prefix keeps your class names from clashing with the rest of the page, and it lets two graphics on one page look different. More about the style string in Shapes and visuals.
The attribution label
Without a licence key, every graphic shows a small “Made with VisQuill” label. A commercial licence comes with a key that removes it:
const rvg = VisQuill.create(div, { cssPrefix: "vqg-", licenseKey: "your key" })Removing a graphic
rvg.dispose() removes the graphic from the page and releases everything it created.
VisQuill.disposeAll() does that for all graphics, for example before a single-page app
navigates away.
