Skip to content

Commit beb8776

Browse files
authored
Add download canvas image on hover (#214)
* Add image download option on hover * Add release note
1 parent e8c2a8b commit beb8776

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

_extensions/webr/qwebr-compute-engine.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ globalThis.qwebrLogCodeToHistory = function(codeToRun, options) {
3131
);
3232
}
3333

34+
// Function to attach a download button onto the canvas
35+
// allowing the user to download the image.
36+
function qwebrImageCanvasDownloadButton(canvas, canvasContainer) {
37+
38+
// Create the download button
39+
const downloadButton = document.createElement('button');
40+
downloadButton.className = 'qwebr-canvas-image-download-btn';
41+
downloadButton.textContent = 'Download Image';
42+
canvasContainer.appendChild(downloadButton);
43+
44+
// Trigger a download of the image when the button is clicked
45+
downloadButton.addEventListener('click', function() {
46+
const image = canvas.toDataURL('image/png');
47+
const link = document.createElement('a');
48+
link.href = image;
49+
link.download = 'qwebr-canvas-image.png';
50+
link.click();
51+
});
52+
}
53+
54+
3455
// Function to parse the pager results
3556
globalThis.qwebrParseTypePager = async function (msg) {
3657

@@ -198,14 +219,20 @@ globalThis.qwebrComputeEngine = async function(
198219

199220
// Determine if we have graphs to display
200221
if (result.images.length > 0) {
222+
201223
// Create figure element
202-
const figureElement = document.createElement('figure');
224+
const figureElement = document.createElement("figure");
225+
figureElement.className = "qwebr-canvas-image";
203226

204227
// Place each rendered graphic onto a canvas element
205228
result.images.forEach((img) => {
229+
206230
// Construct canvas for object
207231
const canvas = document.createElement("canvas");
208232

233+
// Add an image download button
234+
qwebrImageCanvasDownloadButton(canvas, figureElement);
235+
209236
// Set canvas size to image
210237
canvas.width = img.width;
211238
canvas.height = img.height;
@@ -226,17 +253,19 @@ globalThis.qwebrComputeEngine = async function(
226253

227254
// Append canvas to figure output area
228255
figureElement.appendChild(canvas);
229-
});
230256

257+
});
258+
231259
if (options['fig-cap']) {
232260
// Create figcaption element
233261
const figcaptionElement = document.createElement('figcaption');
234262
figcaptionElement.innerText = options['fig-cap'];
235263
// Append figcaption to figure
236264
figureElement.appendChild(figcaptionElement);
237265
}
238-
266+
239267
elements.outputGraphDiv.appendChild(figureElement);
268+
240269
}
241270

242271
// Display the pager data

_extensions/webr/qwebr-styling.css

+24
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ body.quarto-dark .qwebr-button:hover {
169169
text-decoration: none !important;
170170
}
171171

172+
/* Styling to download image that is created */
173+
174+
.qwebr-canvas-image {
175+
position: relative;
176+
display: inline-block;
177+
margin: 10px;
178+
}
179+
180+
.qwebr-canvas-image-download-btn {
181+
position: absolute;
182+
top: 10px;
183+
right: 10px;
184+
padding: 5px 10px;
185+
background-color: #007BFF;
186+
color: white;
187+
border: none;
188+
cursor: pointer;
189+
display: none;
190+
}
191+
192+
figure:hover .qwebr-canvas-image-download-btn {
193+
display: block;
194+
}
195+
172196
/* Custom styling for RevealJS Presentations*/
173197

174198
/* Reset the style of the interactive area */

docs/qwebr-release-notes.qmd

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Features listed under the `-dev` version have not yet been solidified and may ch
2525

2626
- Added ability to download the R history of commands executed through a new global menu in the right column and underneath Revealjs' "Tools" menu. ([#148](https://github.com/coatless/quarto-webr/issues/148))
2727

28+
- Added a mouse over button to allow for downloading an image when generated. ([#147](https://github.com/coatless/quarto-webr/issues/147))
29+
2830
- Added `cell-options` document-level option to specify global defaults for `{webr-r}` options ([#173](https://github.com/coatless/quarto-webr/pulls/173), thanks [ute](https://github.com/ute)!)
2931

3032
- Added `version` document-level option to specify what version of webR should be used. ([#211](https://github.com/coatless/quarto-webr/issues/211))

0 commit comments

Comments
 (0)