Add PNG download for the participant QR code in the admin
A button appears on hover/focus over the QR code (always visible on touch devices) and saves it as a 1024px PNG, rendered client-side from the SVG via canvas. No new dependency or server image library needed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9b14af8bf
commit
945fc94e77
4 files changed
+57
-3
No files matched your search
+46
-1
@@ -3,7 +3,8 @@
|
||||
*
|
||||
* Modules (auto-initialised by data attributes):
|
||||
* - [data-copy] copy the value of the sibling [data-copy-source]
|
||||
* - [data-share] native share sheet (Web Share API), fallback: copy like [data-copy]
|
||||
* - [data-qr-download] save the sibling QR <img> (SVG) as PNG via canvas
|
||||
* - [data-share] native share sheet (Web Share API), fallback: copy like [data-copy]
|
||||
* - [data-youtube-form] slide form: "Link prüfen" for YouTube slides (status + preview)
|
||||
* - [data-presenter] presenter screen, polls /admin/{token}/state;
|
||||
* with [data-embed]: one embedded slide (/e/{key}/{id}), goes live while visible
|
||||
@@ -1165,6 +1166,49 @@
|
||||
});
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ QR download
|
||||
|
||||
/**
|
||||
* [data-qr-download]: renders the SVG QR code (the sibling <img>) onto a canvas and saves it as PNG.
|
||||
* data-filename sets the file name. Hidden without JS / canvas support.
|
||||
*/
|
||||
function initQrDownload() {
|
||||
document.querySelectorAll('[data-qr-download]').forEach(function (button) {
|
||||
var img = button.parentNode.querySelector('img');
|
||||
if (!img || !document.createElement('canvas').toBlob) {
|
||||
return;
|
||||
}
|
||||
button.hidden = false;
|
||||
button.addEventListener('click', function () {
|
||||
var size = 1024;
|
||||
// The SVG has only a viewBox; an explicit size keeps Firefox from refusing to draw it.
|
||||
var source = new Image(size, size);
|
||||
source.onload = function () {
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.width = size;
|
||||
canvas.height = size;
|
||||
var ctx = canvas.getContext('2d');
|
||||
ctx.fillStyle = '#fff';
|
||||
ctx.fillRect(0, 0, size, size);
|
||||
ctx.drawImage(source, 0, 0, size, size);
|
||||
canvas.toBlob(function (blob) {
|
||||
if (!blob) {
|
||||
return;
|
||||
}
|
||||
var link = document.createElement('a');
|
||||
link.href = URL.createObjectURL(blob);
|
||||
link.download = button.getAttribute('data-filename') || 'qr-code.png';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
setTimeout(function () { URL.revokeObjectURL(link.href); }, 1000);
|
||||
}, 'image/png');
|
||||
};
|
||||
source.src = img.currentSrc || img.src;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ share
|
||||
|
||||
/**
|
||||
@@ -3121,6 +3165,7 @@
|
||||
document.querySelectorAll('[data-theme-switch]').forEach(initThemeSwitch);
|
||||
initWidgets(document);
|
||||
initShare();
|
||||
initQrDownload();
|
||||
initAdminDashboard();
|
||||
document.querySelectorAll('[data-presenter]').forEach(initPresenter);
|
||||
document.querySelectorAll('[data-participant]').forEach(initParticipant);
|
||||
|
||||
Reference in new issue
Block a user