Public
Edited
Feb 3, 2023
Importers
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
downloadHtmlAsImage(sampleHTML)
Insert cell
Insert cell
downloadHtmlAsImage(sampleHTML, {
label: "Download @4x PNG",
filename: "scaled@4x",

// options for html2canvas
scale: 4
})
Insert cell
Insert cell
Insert cell
function downloadHtmlAsImage(el, options) {
const { label, ...restOptions } = Object.assign(
{ label: "Download as PNG", scale: window.devicePixelRatio },
options
);
const ui = Inputs.button(label, {
value: null,
reduce: generateDownloader(el, restOptions)
});
return ui;
}
Insert cell
function generateDownloader(el, options) {
return async function () {
let resolve, reject;
const { filename = "untitled", ...html2canvasOptions } = options;
const canvas = await html2canvas(el, html2canvasOptions);
const blob = await canvas2blob(canvas);
FileSaver(blob, filename);
};
}
Insert cell
async function canvas2blob(canvas) {
let resolve, reject;
const promise = new Promise((y, n) => ((resolve = y), (reject = n)));
canvas.toBlob((blob) => {
if (blob == null) {
return reject();
}
resolve(blob);
});
return promise;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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