coatless / quarto-webr

Community developed Quarto Extension to Embed webR for HTML Documents, RevealJS, Websites, Blogs, and Books.
https://quarto-webr.thecoatlessprofessor.com/
395 stars 19 forks source link

Add download image button on hover #147

Closed coatless closed 5 months ago

coatless commented 10 months ago

Presently, one has to right-click to download the image locally.

Let's maybe allow an image button to be added on hover that grabs the canvas content.

function downloadCanvas(canvas) {
  // Get the canvas data as a data URL
  var canvasDataUrl = canvas.toDataURL();

  // Create a link element
  var link = document.createElement('a');
  link.href = canvasDataUrl;
  link.download = 'downloaded_canvas.png'; // switch to code block?

  // Append the link to the body
  document.body.appendChild(link);

  // Trigger a click event on the link
  link.click();

  // Remove the link from the body
  document.body.removeChild(link);
}

https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL