svg5 is a tiny JS library to generate static SVGs for plotters, lasercutting, embroidery and more, based on Processing/p5js syntax.
npm install svg5html
`More examples here:
- https://editor.p5js.org/makio135/sketches/wquU-A1DQ
- https://editor.p5js.org/makio135/sketches/vwqbI49mr
- https://editor.p5js.org/makio135/sketches/J13KRFIJM
- https://editor.p5js.org/makio135/sketches/OazqbYiMn
Bonus: hatched shapes using the Polygon class from https://observablehq.com/@makio135/utilities
https://editor.p5js.org/makio135/sketches/VHsFuHzRb
Documentation
Start by creating an SVG element using the createSVG function:
- createSVG(width, height)
Then, simply draw your elements like a Processing sketch!$3
- width: width of the SVG.
- height: height of the SVG.
- CLOSE: used to specify if a path should be closed.$3
- circle(centerX, centerY, radius)
- ellipse(centerX, centerY, width, height)
- rect(x, y, width, height)
- square(x, y, width)
- line(x1, y1, x2, y2)
- polyline(x1, y1, x2, y2, x3, y3 [, …, xn, yn])
- triangle(x1, y1, x2, y2, x3, y3)
- quad(x1, y1, x2, y2, x3, y3, x4, y4)
- polygon(x1, y1, x2, y2, x3, y3 [, …, xn, yn])
- regularPolygon(centerX, centerY, radius [, startAngle]): optional startAngle is in degrees
- arc(centerX, centerY, width, height, startAngle, endAngle): angles are in degrees
- spline(x1, y1, x2, y2, x3, y3 [, …, xn, yn [, smoothness [, isClosed]]]): optional smoothness from 0 to 1 (default: 1) and optional boolean isClosed (default: false)$3
- background(color)
- clear()
- opacity(amount) where amount goes from 0 to 1.
- fill(color),
- noFill()
- stroke(color)
- strokeWidth(n) specifies the width in pixels of the stroke
- strokeWeight(n) alias for strokeWidth to keep p5js naming.
- strokeCap(style) style can be either butt, square or round
- strokeJoin(style) style can be either miter, round or bevel
- strokeDashArray(n1, n2 [, …, n])
- noStroke()The
color parameter for background, fill and stroke functions can be passed either as:
- gray level between 0 (black) and 255 (white): fill(255) (white)
- gray and alpha levels between 0 and 255: fill(255, 100) (white with alpha)
- red, green and blue levels between 0 and 255: fill(255, 0, 0) (red)
- red, green, blue and alpha levels between 0 and 255: fill(255, 0, 0, 100) (red with alpha)
- a CSS color string, ie: fill("red"), fill("#ff0000"), fill("rgb(255, 0, 0)"), fill("rgba(0, 0, 0, 0.5)"), fill("hsl(30, 100%, 50%)"), …$3
- beginShape() See doc on p5js
- vertex(x, y) See doc on p5js
- bezierVertex(x1, y1, x2, y2, x, y) See doc on p5js
- cubicVertex(x2, y2, x, y) See doc on p5js
- quadraticVertex(x1, y1, x, y) See doc on p5js
- lineTo(x, y)
- moveTo(x, y)
- endShape([CLOSE]) See doc on p5js$3
- beginGroup() starts an SVG group . Transformations are added to the group (-> cleaner SVG).
- endGroup() closes an SVG group $3
- lerp(a, b, t) calculates value between a and b at a specific increment between 0 and 1.
- map(n, start1, end1, start2, end2) re-maps a number from one range to another.
- constrain(value, min, max) constrains a value between a minimum and maximum value.
- radians(degrees) converts a value in degrees to radians.
- degrees(radians) converts a value in radians to degrees.
- random() random(max), random(min, max) or random(array).
- randomSeed(seed) sets the seed value for random().
- noise1D(x) computes a value using simplex noise 1D.
- noise2D(x, y) computes a value using simplex noise 2D.
- noise3D(x, y, z) computes a value using simplex noise 3D.
- noise4D(x, y, z, w) computes a value using simplex noise 4D.
- noise(x [, y , z , w]) computes a value using simplex noise 1D to 4D, depending on number of arguments.
- noiseSeed(seed) sets the seed value for noise.$3
- translate(x, y)
- rotate(degrees)
- scale() scale(n) for uniform scale or scale(x, y).
- push() start a new drawing state.
- pop() restore previous state.$3
- render([parentCSSSelector])
You can specify a CSS selector as third parameter to let the library know where to append the SVG. By default, SVG is appended to the body of the page.
You can simply right click the SVG to download it 🙂$3
- save()$3
- precision(n) sets the number of digits wanted after floating point
- getHTML() returns the SVG as an HTML String$3
- addStyle creates a