• Stars
    star
    672
  • Rank 66,797 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SVG runtime for p5.js.

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/

More Repositories

1

fonts.css

跨平台 CSS 中文字体解决方案
TypeScript
2,075
star
2

Free-Chinese-Fonts

免费中文字体
JavaScript
1,442
star
3

color-hash

Generate color based on the given string (using HSL color space and SHA256).
TypeScript
709
star
4

yafolding.el

Yet another folding extension for Emacs
Emacs Lisp
149
star
5

font-detect.js

Detect font without flash, simply using javascript & @font-face
JavaScript
80
star
6

node-yaqrcode

Pure Javascript QR Code Generator for NodeJS
HTML
62
star
7

p5.js-pdf

A simple PDF export module for p5.js.
JavaScript
53
star
8

apisix-javascript-plugin-runner

Node.js / Deno / WASM Plugin Runner for APISIX
TypeScript
33
star
9

css-eldoc

eldoc-mode plugin for CSS
Emacs Lisp
28
star
10

gitlab-changelog

A php script to generate changelog via gitlab api
PHP
16
star
11

cocolour

Color schemes generator based on machine learning
JavaScript
12
star
12

colors-clustering

Colors clustering based on K-means algorithm & CIE76
JavaScript
11
star
13

act.js

Create Adobe Color Table (.act) files using Uint8Array, Blob API and FileReader API.
JavaScript
10
star
14

php-eldoc

eldoc-mode plugin for PHP source code
Emacs Lisp
10
star
15

parallel-algorithm-notes

This is my notes for our Parallel Algorithms course.
HTML
9
star
16

electronic-wechat-deb

Deb package for Zhongyi Tong's awesome Electronic WeChat.
Shell
7
star
17

automatic-control-theory

自动控制理论
Python
6
star
18

colors

colors
JavaScript
5
star
19

qr-auth

QR Code Based 2-Step Authentication
CoffeeScript
4
star
20

colors-clustering-visualization

Visualization demo for Colors Clustering using RGB & K-means algorithm
JavaScript
4
star
21

Data-Visualization-Examples

一些数据可视化的例子
CSS
4
star
22

wp-cors

Yet another wordpress cross domain plugin
PHP
3
star
23

gsoc2015

My Proposals and Logs for Google Summer of Code 2015
JavaScript
3
star
24

rice-sorting

Rice sorting based on machine vision
JavaScript
2
star
25

.xmonad

My XMonad & XMobar Config
Haskell
2
star
26

The-Nature-of-Code

The Nature of Code (in processing-js)
Processing
2
star
27

upgradeneeded

浏览器更新提示。
CSS
2
star
28

interval.js

A simple interval manager with lifetime / start / stop / pause / resume / restart
CSS
2
star
29

ASM-80C51

80C51 汇编
Assembly
2
star
30

gene-pool

Genetic algorithms in a given gene pool
JavaScript
2
star
31

zju-cli

This repo was moved to https://github.com/QSCTech/zjunet.
Shell
2
star
32

apisix-plugin-waf

ModSecurity compatible web application firewall plugin for Apache APISIX
2
star
33

npmm

Install npm packages using mirrrors with package-lock.json resolved to registry.npmjs.org
Shell
2
star
34

skeleton

Detect theme colors of a webpage using phantomjs
JavaScript
1
star
35

service

互联网服务
1
star
36

dotxbindkeys

Xbindkeys Configuration Framework
Scheme
1
star
37

xbindkeys-chromemacs

Emacs keybindings for Chrome / Chromium using xbindkeys
Scheme
1
star
38

cold-plasma-rice-seeds

冷等离子处理的水稻种子吸水特性
Python
1
star
39

ASABE-Robotics-Competition

ASABE Robotics Competition
JavaScript
1
star
40

postino

C
1
star
41

socks-scripts

Scripts using red socks & socks to bgswlan or any other router or server
1
star
42

bingang-industrial-park

滨港工业城官方网站前端
CoffeeScript
1
star
43

MathJax

Self hosted MathJax plus some handy configuration.
JavaScript
1
star
44

simple-loading-bar

A simple loading bar bind on dom
JavaScript
1
star
45

chrome-time

a simple clock for chrome
JavaScript
1
star
46

Machine-Learning

Machine Learning Exercises
Clojure
1
star
47

iframe-url-binding

Iframe based url binding
JavaScript
1
star
48

Cache.coffee

Cache.coffee is a library written for data caching.
CoffeeScript
1
star
49

common-knowledge

常识
1
star
50

time

A clock Web App, using IndexedDB.
JavaScript
1
star
51

mdt

Markdown toolkit (pandoc, XeLaTeX, mermaid and LaTeX beamer slide show)
Shell
1
star
52

try-kicad

IDL
1
star
53

fs-storage

localStorage like, fileSystem based storage
CoffeeScript
1
star