Published
Edited
Dec 21, 2020
4 forks
Importers
11 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// This function converts svg strings into base64 png urls
// which you can use for creating patterns in mapbox
function rasterize(svgString, size = 50, interpolation = 1) {
const svg = html`${svgString}`;
svg.setAttribute('width', size * interpolation);
svg.setAttribute('height', size * interpolation);

let resolve, reject;
const promise = new Promise((y, n) => ((resolve = y), (reject = n)));
const image = new Image();
image.onerror = reject;
image.onload = () => {
const context = DOM.context2d(size * interpolation, size * interpolation);
for (let i = 0; i < interpolation; i++) {
for (let j = 0; j < interpolation; j++) {
context.drawImage(image, i * size, j * size, size, size);
}
}
resolve(context.canvas.toDataURL("image/png"));
};
const serializedSVG = new XMLSerializer().serializeToString(svg);
const base64Data = window.btoa(serializedSVG);
const imgsrc = `data:image/svg+xml;base64,${base64Data}`;
image.src = imgsrc;

return promise;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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