canvas-sketch
— API Docs
Below are the docs for the canvas-sketch
JavaScript library. It should work in Node.js and the browser, including browserify, Webpack, Rollup, and similar build tools.
:bulb: If you are just getting started, you may want to look at some of the guides first, such as the Installation guide, or A “Hello, World” Sketch.
Contents
Importing
The default export is a function, canvasSketch
which can be imported like so:
// Node.js/CommonJS
const canvasSketch = require('canvas-sketch');
// ES Modules
import canvasSketch from 'canvas-sketch';
CommonJS is recommended for better compatibility with Node.js (i.e. for generating super-high resolution prints).
Methods
promise = canvasSketch(sketch, [settings])
The library exports the default canvasSketch
function, which takes in a sketch
function and optional settings
object.
The sketch
function can be sync or async (i.e. returns a promise), and is expected to have a signature similar to:
// Sync + ES5
function sketch (initialProps) {
// ... load & setup content
return function (renderProps) {
// ... render content
};
}
// Async + ES6
const sketch = async () => {
await someLoader();
return ({ width, height }) => {
// ... Render...
};
}
See the props for details about what is contained in the object passed to these functions.
You can also return a renderer object for more advanced functionality, for example to react to canvas resize events.
const sketch = () => {
return {
render ({ frame }) {
console.log('Rendering frame #%d', frame);
},
resize ({ width, height }) {
console.log('Canvas has resized to %d x %d', width, height);
}
};
}
The return value of canvasSketch
is a promise, resolved to a SketchManager
instance.
Settings
The settings
object often looks like this:
// 11 x 7 inches artwork
const settings = {
dimensions: [ 11, 7 ],
units: 'in'
};
#### Size Settings
parameter | type | default | description
--- | --- | --- | ---
`dimensions` | Array \| String | *window size* | The dimensions of your sketch in `[width, height]` units. If not specified, the sketch will fill the browser window. You can also specify a [preset string](/canvas-sketch/docs/physical-units.html#paper-size-presets) such as `"A4"` or `"Letter"`.
`units` | String | `"px"` | The working units if `dimensions` is specified, can be `"in"`, `"cm"`, `"px"`, `"ft"`, `"m"`, `"mm"`.
`pixelsPerInch` | Number | 72 | When `units` is a physical measurement (e.g. inches), this value will be used as the resolution to convert inches to pixels for exporting and rendering.
`orientation` | String | `"initial"` | If `"landscape"` or `"portrait"` are specified, the dimensions will be rotated to the respective orientation, otherwise no change will be made. Useful alongside [`dimensions` presets](/canvas-sketch/docs/physical-units.html#paper-size-presets).
`scaleToFit` | Boolean | true | When true, scales down the canvas to fit within the browser window.
`scaleToView` | Boolean | false | When true, scales up or down the canvas so that it is no larger or smaller than it needs to be based on the window size. This makes rendering more crisp and performant, but may not accurately represent the exported image. This is ignored during export.
`bleed` | Number | 0 | You can pad the dimensions of your artwork by `bleed` units, e.g. for print trim and safe zones.
`pixelRatio` | Number | device ratio | The pixel ratio of the canvas for rendering and export. Defaults to `window.devicePixelRatio`.
`exportPixelRatio` | Number | `pixelRatio` | The pixel ratio to use when exporting, defaults to `pixelRatio`. Not affected by `maxPixelRatio`.
`maxPixelRatio` | Number | Infinity | A maximum value for pixel ratio, in order to clamp the density for Retina displays.
`scaleContext` | Boolean | true | When true, 2D contexts will be scaled to account for the difference between `width` / `height` (physical measurements) and `canvasWidth` / `canvasHeight` (in-browser measurements).
`resizeCanvas` | Boolean | true | When true, canvas width and height will be set. You can stop the canvas from being resized by setting this to false.
`styleCanvas` | Boolean | true | When true, canvas style width and height will be added to account for pixelRatio scaling. Disable this by setting it to false.
#### DOM Settings
parameter | type | default | description
--- | --- | --- | ---
`canvas` | `