Published
Edited
Sep 20, 2021
Use face.svg() method to draw polygon in canvas - Reply to @ArtS3xy
Run point along polygon boundary
Insert cell
md`# Use face.svg() method to draw polygon in canvas - Reply to @ArtS3xy`
Insert cell
md`Face.svg() method in [flatten-js](https://github.com/alexbol99/flatten-js) library returns string that may be used to create new Path2D object. Path2D object is a stadard input for context *fill* and *stroke* methods`
Insert cell
{
const canvas = DOM.canvas(width, height);
const context = canvas.getContext("2d");

context.clearRect(0, 0, width, height);

for (let face of polygon.faces) {
const p = new Path2D(face.svg());
context.fillStyle = "lightgreen";
context.fill(p);
context.stroke(p);
}

return canvas;
}
Insert cell
height = 200
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Flatten = require("@flatten-js/core")
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