MAKIO135 / svg5.js

A tiny JS library to generate static SVGs for plotters, lasercutting, embroidery and more, based on Processing/p5js syntax.
MIT License
138 stars 8 forks source link

svg5.js

svg5 is a tiny JS library to generate static SVGs for plotters, lasercutting, embroidery and more, based on Processing/p5js syntax.

Example

<script src="https://unpkg.com/svg5/svg5.min.js"></script>
<script>
    // Create an SVG element
    createSVG(500, 500)

    // Add content / elements
    background('red')
    stroke(0)
    noFill()
    for(let y = -150; y <= height + 150; y += 5){
        beginShape()
        vertex(-10, height + 100)
        for(let x = -10; x <= width + 10; x += 10){
            vertex(x, y + noise(x/500, y/200) * 50)
        }
        vertex(width + 10, height + 100)
        endShape(CLOSE)
    }

    // Finally, render the svg
    render() 
</script>

More examples here:

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:

Global variables

Shapes

Styling

The color parameter for background, fill and stroke functions can be passed either as:

Vertex shapes

Groups

Math helpers

Matrix transformations

Render SVG

Save SVG file

SVG helpers

Custom methods for modularity

NPM

svg5 is also available on NPM: npm i svg5

const svg5 = require('svg5')
svg5.createSVG(500, 500)
svg5.background(0)
svg5.fill(255)
svg5.noStroke()
svg5.circle(svg5.width/2, svg5.height/2, 200)

Examples:

Dependencies

No dependencies but svg5 is using code from simplex-noise.js from Jonas Wagner and Alea from Johannes Baagøe, packaged by Richard Hoffman

License

Copyright (c) 2020 Lionel RADISSON, licensed under the MIT License (enclosed)