Getting started with SVG
Getting Started with SVG
Section titled “Getting Started with SVG”The SVG backend lets you generate scalable vector graphics by writing a
JavaScript function that returns an SVG string. The result is displayed in the
preview pane and downloadable as an .svg file — suitable for print, laser
cutting, or the web.
Your first script
Section titled “Your first script”Open the Modelling tab and click New project, choosing SVG as the backend. A starter file is created for you — hit ▶ Run to see it rendered.
function main() { return `<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400"> <circle cx="200" cy="200" r="150" fill="#e94560" stroke="#0f3460" stroke-width="6" /> </svg>`}
module.exports = { main }main() returns a plain SVG string. Whatever you put in the <svg> element
is what the viewer renders.
The SVG coordinate system
Section titled “The SVG coordinate system”SVG places the origin (0, 0) at the top-left corner. The x-axis runs
right and the y-axis runs downward — the opposite of most maths
conventions.
| Position | x | y |
|---|---|---|
| Top-left | 0 | 0 |
| Centre (400 × 400) | 200 | 200 |
| Bottom-right (400 × 400) | 400 | 400 |
The viewBox attribute defines the internal coordinate space; width and
height control the rendered size. Setting them equal means 1 SVG unit = 1 px.
Basic shapes
Section titled “Basic shapes”SVG has several built-in shape elements:
// Rectangle`<rect x="50" y="50" width="200" height="100" fill="#4fc3f7" />`
// Circle`<circle cx="200" cy="200" r="80" fill="none" stroke="#fff" stroke-width="2" />`
// Ellipse`<ellipse cx="200" cy="200" rx="120" ry="60" fill="#e94560" />`
// Line`<line x1="0" y1="0" x2="400" y2="400" stroke="#fff" stroke-width="1" />`
// Polygon (closed shape from point list)`<polygon points="200,20 350,380 50,380" fill="#0f3460" />`Fill and stroke
Section titled “Fill and stroke”The two most important style attributes are fill and stroke:
| Attribute | Effect |
|---|---|
fill |
Interior colour; "none" for transparent |
stroke |
Outline colour |
stroke-width |
Outline thickness in SVG units |
opacity |
Overall transparency from 0 to 1 |
fill-opacity |
Interior transparency only |
stroke-opacity |
Outline transparency only |
Colours can be any CSS colour value: "#e94560", "rgb(233,69,96)",
"hsl(349,80%,59%)", or a named colour like "tomato".
Grouping and transforms
Section titled “Grouping and transforms”Wrap shapes in <g> to apply transforms to all of them at once:
// Rotate a group of shapes 45° around the centre`<g transform="rotate(45, 200, 200)"> <rect x="100" y="100" width="200" height="200" fill="#e94560" /> <circle cx="200" cy="200" r="30" fill="#fff" /></g>`Common transform values:
| Transform | Example |
|---|---|
| Translate | translate(50, 20) |
| Rotate (around origin) | rotate(45) |
| Rotate (around point) | rotate(45, cx, cy) |
| Scale | scale(1.5) or scale(sx, sy) |
Multiple transforms are applied right-to-left: translate(200,200) rotate(45)
first rotates around the origin, then moves the result to (200, 200).
Path elements
Section titled “Path elements”For anything more complex than the built-in shapes, use <path> with an SVG
path string in the d attribute:
`<path d="M 100 200 L 300 200 L 200 50 Z" fill="#e94560" />`Key path commands:
| Command | Meaning |
|---|---|
M x y |
Move to (starts a new sub-path) |
L x y |
Line to |
A rx ry rot large-arc sweep x y |
Elliptical arc |
Z |
Close path (line back to start) |
Lowercase commands (m, l, a) use relative coordinates. Building complex
path strings by hand is error-prone — use d3-path instead (covered in the
next tutorial).
Parameters
Section titled “Parameters”Export a getParameterDefinitions() function to expose interactive controls.
The script re-runs automatically whenever a value changes:
const getParameterDefinitions = () => [ { name: 'size', type: 'number', widget: 'slider', caption: 'Canvas size', default: 400, min: 200, max: 1200 }, { name: 'color', type: 'color', caption: 'Fill colour', default: '#e94560' }, { name: 'sides', type: 'number', widget: 'slider', caption: 'Sides', default: 6, min: 3, max: 16 },]
function main({ size, color, sides }) { const cx = size / 2, cy = size / 2, r = size * 0.4 const pts = Array.from({ length: sides }, (_, i) => { const a = (i / sides) * Math.PI * 2 - Math.PI / 2 return `${cx + r * Math.cos(a)},${cy + r * Math.sin(a)}` }).join(' ')
return `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}"> <polygon points="${pts}" fill="${color}" /> </svg>`}
module.exports = { main, getParameterDefinitions }Exporting
Section titled “Exporting”Once the preview looks right, click ↓ Export in the viewer toolbar to
download the .svg file.
Available libraries
Section titled “Available libraries”Use Edit imports… in the editor toolbar to browse and add library imports:
| Library | Require path | What it provides |
|---|---|---|
| d3-path | 'd3-path' |
Programmatic SVG path building |
| d3-shape | 'd3-shape' |
Line, arc, pie, and other generators |
| chroma-js | 'chroma-js' |
Colour manipulation and scales |
| Font loader | '@javasculpt/lib/font-loader' |
Render font glyphs as <path> elements |
Next steps
Section titled “Next steps”- Try the Shapes, paths, and colour tutorial to learn d3-shape and chroma-js.
- Use Edit imports… to browse every available function from each library.