Giter VIP home page Giter VIP logo

p5.js-svg's Introduction

p5.js-svg

The main goal of p5.SVG is to provide a SVG runtime for p5.js, so that we can draw using p5's powerful API in <svg>, save things to svg file and manipulating existing SVG file without rasterization.

Getting Started

Add this line in your projects index.html :

<script src="https://unpkg.com/[email protected]"></script>

(p5.js-svg v1.5.x is compatible with p5.js v1.6.x)

Open your sketch.js and edit it:

function setup() {
  createCanvas(100, 100, SVG);
  background(255);
  fill(150);
  stroke(150);
}

function draw() {
  var r = frameCount % 200 * Math.sqrt(2);
  background(255);
  ellipse(0, 0, r, r);
}

Then you can open your html file, and view the result. It's <svg>!

SVG Gettting Started

Examples

SVG Renderer vs Canvas2D Renderer

The major difference is that SVG Renderer is based on SVG Document Object Model while Canvas 2D Renderer is based on pixels. Therefore, the performance may not be as good as canvas, but SVG-format vector images can be rendered at any size without loss of quality.

Note that not all drawing results are exactly same in pixel-level.For example, the round rects below are almost same, but there are some pixels different.

round rect

As for filters, gray(), invert(), threshold(), opaque() did have same behavior as Canvas2D Renderer. But blur(), erode(), dilate() didn't.

To implement blur, feGaussianBlur was used, which is different from Processing's blur. blur

As for erode() and dilate(), they were implemnted using feOffset and feBlend. So, the result is not exactly same. erode

You can view all the pixels based diff on the online tests.

Browser Compatibility

[email protected] was tested and should work on:

  • Chromium 90 (Debian 11.0, LXQt 0.16)
  • Safari (iPadOS 14)

How it works

p5.RendererSVG is a class which extends p5.Renderer2D. A mocked <canvas> element and a CanvasRenderingContext2D api are provided using svgcanvas, which is JavaScript Object that syncs proprieties and draws on <svg> element.

Known issue

Too many child elements

Since SVG is XML-based, every call of the draw function will insert elements into it, and these elements keep existing even if they are not visible. So, long-time running will result in too many child elements. We recommend calling clear() in your draw function, which will trigger internal context.__clearCanvas() to remove elements.

function draw() {
  clear();
  // draw
}

See #32

blendMode is not implemented yet.

Building dist

To build dist files after cloning repo, you can run:

npm install
npm run build

Tests

p5.SVG was driven by tests. We use Karma and mocha. Most tests are based on pixel-diff. There are still some p5's methods not covered with unit tests. But Rendering and Shape API are already covered with tests and should work.

If you found a bug, feel free to open a issue or pull a request.

All tests can be found here: https://github.com/zenozeng/p5.js-svg/tree/master/test/unit

You can also run the online test yourself: https://zenozeng.github.io/p5.js-svg/test/

p5.js-svg's People

Contributors

dependabot[bot] avatar miracle2k avatar misterkangaroo avatar zenozeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

p5.js-svg's Issues

TypeError: Cannot read property '1' of null in bezierPoint's unit test

in bezierPoint's unit test:

TypeError: Cannot read property '1' of null
    at ctx.__applyStyleToCurrentElement (http://localhost:8000/dist/p5.svg.js:338:47)
    at ctx.stroke (http://localhost:8000/dist/p5.svg.js:578:22)
    at methods.forEach._this.(anonymous function) [as stroke] (http://localhost:8000/dist/p5.svg.js:985:35)
    at eval (eval at <anonymous> (test-render.js?_=1432915454074:44:22), <anonymous>:10:32)
    at test-render.js?_=1432915454074:44:17
    at Array.forEach (native)
    at _testRender (test-render.js?_=1432915454074:34:27)
    at HTMLDocument.<anonymous> (test-render.js?_=1432915454074:172:13)

Mover API design

A Mover is a wrapped element in svg

m = new Mover();
m.translate(args);
m.rotate(args);

A Circle Mover's position should not based on its transform prop.

circle.r = 123; // should find <circle> and update its r

A Mover should provide event API.

Draw svg on current canvas

function setup() {
    createCanvas(width, height);
}
var myshape;
function preload() {
    myshape = loadImage('myshape.svg');
}
var x = 0,
    y = 0;
function draw() {
    background(0);
    shapeMode(CORNERS);
    shape(myshape, x, y, w, h); // draw svg on current canvas
    x++;
    y++;
}

Basic shapes in p5.js

function setup () {
    ellipse = new Ellipse(x, y, w, h);
}
function draw () {
    ellipse.width++;
    ellipse.update();
}

Exporting SVG

var svg;
function setup() {
    svg = createSVG(width, height);
}
function draw() {
    if (some condition) {
        // draw something
    } else {
        noLoop();
        var dataURL = svg.toDataURL();
    }
}

shape(myshape, x, y, w, h)

function setup() {
    createCanvas(width, height);
}
var myshape;
function preload() {
    myshape = loadImage('myshape.svg');
}
var x = 0,
    y = 0;
function draw() {
    background(0);
    shapeMode(CORNERS);
    shape(myshape, x, y, w, h); // draw svg on current canvas
    x++;
    y++;
}

new SVGShape API Design

var mover = beginGroup();
circle(0, 0, 50, 50);
rect(1, 2, 3, 4);
endGroup();
mover.filter = blur;
mover.translate.x += 4; // move x += 4

shape.filter

Set a filter for this object. Should allow SVG filters and provide some simple filters.

See also: https://developer.mozilla.org/en-US/docs/Web/CSS/filter

```javascript
var r = 0,
    ellipse;
function setup () {
    ellipse = new Ellipse(x, y, w, h);
}
function draw () {
    var myfilter = new SVG.Filter.Blur('5px');
    ellipse.filter = myfilter;
}
```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.