Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active October 16, 2018 11:57
Show Gist options
  • Save mattdesl/25a5334b09f156efdd476a00bded94f1 to your computer and use it in GitHub Desktop.
Save mattdesl/25a5334b09f156efdd476a00bded94f1 to your computer and use it in GitHub Desktop.
regl + canvas sketch + hot reloading

canvas-sketch + regl + hot reloading

To quick start:

# move to an empty folder
mkdir my-folder && cd my-folder

# paste regl.js file
pbpaste > regl.js

# auto-install & run
npx canvas-sketch-cli regl.js --open --hot

To re-run without installing each time, install it once like so:

npm i canvas-sketch-cli --save-dev
npx canvas-sketch regl.js --hot
const canvasSketch = require('canvas-sketch');
const createRegl = require('regl');
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: 'webgl',
// Turn on MSAA
attributes: { antialias: true }
};
const sketch = ({ gl }) => {
// Setup REGL with our canvas context
const regl = createRegl({ gl });
// Regl GL draw commands
// ...
// Return the renderer function
return {
render ({ time }) {
// Update regl sizes
regl.poll();
// Clear back buffer
regl.clear({
color: [ 0, 0, 0, 1 ]
});
// Draw meshes to scene
// ...
},
unload () {
// Unload sketch for hot reloading
regl.destroy();
}
}
};
canvasSketch(sketch, settings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment