Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion tensorboard/plugins/graph/tf_graph_common/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,24 @@ export class Minimap {
let $download = d3.select('#graphdownload');
this.download = <HTMLLinkElement>$download.node();
$download.on('click', d => {
this.download.href = this.downloadCanvas.toDataURL('image/png');
// Revoke the old URL, if any. Then, generate a new URL.
URL.revokeObjectURL(this.download.href);
// We can't use the `HTMLCanvasElement.toBlob` API because it does
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, for the scalar plot download buttons, we use a drop-down menu as the trigger to generate the URL so that it's a two-click process. If we did something similar for the graph it would be possible to use toBlob() and just disable the actual download click target until the synchronous call completes.

Maybe it's not worth it, but I have to imagine toBlob() might be faster than iterating over each character in the blob by hand.

// not have a synchronous variant, and we need to update this href
// synchronously. Instead, we create a blob manually from the data
// URL.
const dataUrl = this.downloadCanvas.toDataURL("image/png");
const prefix = dataUrl.slice(0, dataUrl.indexOf(","));
if (!prefix.endsWith(";base64")) {
console.warn(`non-base64 data URL (${prefix}); cannot use blob download`);
this.download.href = dataUrl;
return;
}
const data = atob(dataUrl.slice(dataUrl.indexOf(",") + 1));
const bytes =
new Uint8Array(data.length).map((_, i) => data.charCodeAt(i));
const blob = new Blob([bytes], {type: "image/png"});
this.download.href = URL.createObjectURL(blob);
});

let $svg = d3.select(this.svg);
Expand Down