Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Created September 30, 2018 14:28
Show Gist options
  • Save mattdesl/dd950edbb6a20f998a3a9024c9459c06 to your computer and use it in GitHub Desktop.
Save mattdesl/dd950edbb6a20f998a3a9024c9459c06 to your computer and use it in GitHub Desktop.
example of declarative GUI support in canvas-sketch library
const canvasSketch = require('canvas-sketch');
const settings = {
dimensions: [ 2048, 2048 ],
params: {
size: {
min: 50,
max: 400,
step: 1,
value: 200
},
lineWidth: {
min: 1,
max: 20,
step: 0.01,
value: 10
},
stroke: false,
text: 'test',
foreground: 'white',
background: '#F8C9B7'
}
};
const sketch = () => {
return ({ context, width, height, params }) => {
context.clearRect(0, 0, width, height);
context.fillStyle = params.background;
context.fillRect(0, 0, width, height);
context.font = `${params.size}px "Andale Mono"`;
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = context.strokeStyle = params.foreground;
context.lineWidth = params.lineWidth;
context[params.stroke ? 'strokeText' : 'fillText'](params.text, width / 2, height / 2);
};
};
canvasSketch(sketch, settings);
@dackdel
Copy link

dackdel commented Sep 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment