Published
Edited
Sep 20, 2021
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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more