Published
Edited
Jan 20, 2021
2 stars
Also listed in…
API
Insert cell
Insert cell
Insert cell
Insert cell
{
let c = html`<canvas id="c"></canvas>`;
yield c; // this is the magic word. This pushes the canvas out before the code below needs it.
// I learned this magic from https://observablehq.com/@tmcw/p5
let ctx = c.getContext("2d");
ctx.canvas.width = 200;
ctx.canvas.height = 200;
// create a wrapper around native canvas element (with id="c")
var canvas = new fabric.Canvas("c");

// create a rectangle object
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'red',
width: 20,
height: 20
});
// "add" rectangle onto canvas
canvas.add(rect);
return c
}
Insert cell
{
let cc = html`<canvas id="cc"></canvas>`;
yield cc; // this is the magic word. This pushes the canvas out before the code below needs it.
let ctx = cc.getContext("2d");
ctx.canvas.width = 200;
ctx.canvas.height = 200;
// create a wrapper around native canvas element (with id="c")
var canvas = new fabric.Canvas("cc");

// create a rectangle object
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'red',
width: 20,
height: 20,
angle: 45 // only change

});
// "add" rectangle onto canvas
canvas.add(rect);
//change it a bit
rect.set('fill', 'green');
rect.set({ strokeWidth: 5, stroke: 'rgba(100,200,200,0.5)' });
rect.set('angle', 15).set('flipY', true);
return cc
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more