canvas-sketch
canvas-sketch
is a loose collection of tools, modules and resources for creating generative art in JavaScript and the browser.
example of canvas-sketch
running in Chrome
↓
Quick Start with Node.js & npm
To jump directly into canvas-sketch
, try the following terminal commands with node@8.x
and npm@5.x
or newer.
npm install canvas-sketch-cli -g
mkdir my-sketches
cd my-sketches
canvas-sketch sketch.js --new --open
While in the browser, hit Cmd + S
or Ctrl + S
to export a high-resolution PNG of your artwork to your ~/Downloads
folder.
Some other commands to try:
canvas-sketch src/foobar.js --output=./tmp/
canvas-sketch --new --template=three --open
canvas-sketch src/foobar.js --build
canvas-sketch src/foobar.js --hot
For more features and details, see the Documentation.
Code Example
Once you have the CLI tool running, you can try this example of an A4 print artwork.
const canvasSketch = require('canvas-sketch');
const settings = {
dimensions: 'a4',
pixelsPerInch: 300,
units: 'in'
};
const sketch = () => {
return ({ context, width, height }) => {
const margin = 1 / 4;
context.fillStyle = 'hsl(0, 0%, 98%)';
context.fillRect(0, 0, width, height);
const fill = context.createLinearGradient(0, 0, width, height);
fill.addColorStop(0, 'cyan');
fill.addColorStop(1, 'orange');
context.fillStyle = fill;
context.fillRect(margin, margin, width - margin * 2, height - margin * 2);
};
};
canvasSketch(sketch, settings);
When exporting the image in browser with Cmd + S
or Ctrl + S
keystrokes, the saved PNG file matches 21 x 29.7 cm at 300 DPI, and can be printed with archival ink on quality paper.
Resulting image looks something like this:
Note: The above PNG file has been scaled/optimized for web.
Roadmap
There are many features still outstanding, such as:
- API & CLI Docs
- Easy & beginner-friendly examples
- Website/frontend
- HUD/GUI controls
- "Gallery Mode" for viewing many local sketches
- External Module for utilities (randomness, geometry, etc)
- Unit tests
- More??
License
MIT, see LICENSE.md for details.