Mark up step pictures: markers, boxes and burned-in blur (Refs #25)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
126
src/app.js
126
src/app.js
@@ -214,7 +214,7 @@
|
||||
for (const file of files) {
|
||||
try {
|
||||
const url = await processImageFile(file);
|
||||
if (opts.replace && !added) opts.replace[key] = url;
|
||||
if (opts.replace && !added) { opts.replace[key] = url; if (key === 'image') delete opts.replace.marks; }
|
||||
else add(file.name, url);
|
||||
added++;
|
||||
} catch (err) {
|
||||
@@ -255,9 +255,117 @@
|
||||
importFiles(rec, files, { replace: field && field.kind === 'image' ? field : null, pasted: true });
|
||||
});
|
||||
|
||||
function openLightbox(label, src) {
|
||||
// A step's picture with its markers and boxes laid over it. Positions are fractions
|
||||
// of the picture, so they stay put at any size, in print too.
|
||||
function markedImage(img, marks) {
|
||||
if (!marks || !marks.length) return img;
|
||||
let n = 0;
|
||||
return h('div', { class: 'marked' }, img, marks.map((m) => (m.type === 'box'
|
||||
? h('span', { class: 'mbox', 'aria-hidden': 'true', style: 'left:' + m.x * 100 + '%;top:' + m.y * 100 + '%;width:' + m.w * 100 + '%;height:' + m.h * 100 + '%' })
|
||||
: h('span', { class: 'mpin', 'aria-hidden': 'true', style: 'left:' + m.x * 100 + '%;top:' + m.y * 100 + '%' }, String(++n)))));
|
||||
}
|
||||
|
||||
// Mark up a step's picture: numbered markers and highlight boxes (kept as overlays, editable
|
||||
// later) and Blur out (burned into the pixels on Apply, so no readable copy is kept).
|
||||
async function openAnnotator(q, f) {
|
||||
const img = new Image();
|
||||
img.src = f.image;
|
||||
try { await img.decode(); } catch { return setStatus('That image could not be opened.'); }
|
||||
const W = img.naturalWidth, H = img.naturalHeight;
|
||||
const base = document.createElement('canvas');
|
||||
base.width = W; base.height = H;
|
||||
const bctx = base.getContext('2d', { willReadFrequently: true });
|
||||
bctx.drawImage(img, 0, 0);
|
||||
const marks = structuredClone(f.marks || []);
|
||||
const undo = [];
|
||||
let tool = 'marker', blurs = 0, drag = null;
|
||||
const view = h('canvas', { class: 'annot', width: W, height: H, 'aria-label': 'Picture to mark up' });
|
||||
const vctx = view.getContext('2d');
|
||||
const lw = Math.max(3, Math.round(W / 300)), pr = Math.max(14, Math.round(W / 60));
|
||||
|
||||
function draw() {
|
||||
vctx.drawImage(base, 0, 0);
|
||||
let n = 0;
|
||||
for (const m of marks) {
|
||||
if (m.type === 'box') {
|
||||
vctx.strokeStyle = '#e11'; vctx.lineWidth = lw;
|
||||
vctx.strokeRect(m.x * W, m.y * H, m.w * W, m.h * H);
|
||||
} else {
|
||||
vctx.beginPath(); vctx.arc(m.x * W, m.y * H, pr, 0, 2 * Math.PI);
|
||||
vctx.fillStyle = '#e11'; vctx.fill(); vctx.lineWidth = 2; vctx.strokeStyle = '#fff'; vctx.stroke();
|
||||
vctx.fillStyle = '#fff'; vctx.font = '700 ' + Math.round(pr * 1.2) + 'px system-ui, sans-serif';
|
||||
vctx.textAlign = 'center'; vctx.textBaseline = 'middle'; vctx.fillText(String(++n), m.x * W, m.y * H + 1);
|
||||
}
|
||||
}
|
||||
if (drag) {
|
||||
const r = normRect(drag.x0, drag.y0, drag.x1, drag.y1, 0);
|
||||
if (r) {
|
||||
vctx.setLineDash([8, 6]); vctx.lineWidth = lw; vctx.strokeStyle = tool === 'blur' ? '#000' : '#e11';
|
||||
vctx.strokeRect(r.x * W, r.y * H, r.w * W, r.h * H); vctx.setLineDash([]);
|
||||
}
|
||||
}
|
||||
}
|
||||
const at = (e) => {
|
||||
const b = view.getBoundingClientRect();
|
||||
return { x: Math.min(1, Math.max(0, (e.clientX - b.left) / b.width)), y: Math.min(1, Math.max(0, (e.clientY - b.top) / b.height)) };
|
||||
};
|
||||
view.addEventListener('pointerdown', (e) => {
|
||||
const p = at(e);
|
||||
if (tool === 'marker') { marks.push({ type: 'marker', x: p.x, y: p.y }); undo.push(() => marks.pop()); draw(); return; }
|
||||
view.setPointerCapture(e.pointerId);
|
||||
drag = { x0: p.x, y0: p.y, x1: p.x, y1: p.y };
|
||||
});
|
||||
view.addEventListener('pointermove', (e) => { if (drag) { const p = at(e); drag.x1 = p.x; drag.y1 = p.y; draw(); } });
|
||||
view.addEventListener('pointerup', (e) => {
|
||||
if (!drag) return;
|
||||
const p = at(e);
|
||||
const r = normRect(drag.x0, drag.y0, p.x, p.y);
|
||||
drag = null;
|
||||
if (r && tool === 'box') {
|
||||
marks.push({ type: 'box', x: r.x, y: r.y, w: r.w, h: r.h }); undo.push(() => marks.pop());
|
||||
} else if (r && tool === 'blur') {
|
||||
const snap = bctx.getImageData(0, 0, W, H);
|
||||
const id = bctx.getImageData(0, 0, W, H);
|
||||
pixelateRegion(id.data, W, H, { x: r.x * W, y: r.y * H, w: r.w * W, h: r.h * H });
|
||||
bctx.putImageData(id, 0, 0);
|
||||
blurs++; undo.push(() => { bctx.putImageData(snap, 0, 0); blurs--; });
|
||||
}
|
||||
draw();
|
||||
});
|
||||
|
||||
const tools = [['marker', 'Marker (click)'], ['box', 'Box (drag)'], ['blur', 'Blur out (drag)']];
|
||||
const radios = tools.map(([v, t]) => h('label', { class: 'radio' },
|
||||
h('input', { type: 'radio', name: 'annot-tool', value: v, checked: v === tool, onchange: () => { tool = v; } }), ' ' + t));
|
||||
const dlg = h('dialog', { class: 'card annotator', 'aria-label': 'Mark up picture' },
|
||||
h('h3', {}, 'Mark up this picture'),
|
||||
h('p', { class: 'muted small' }, 'Marker: click where the reader should click. Box: drag around something to point at. Blur out: drag over anything private, such as account numbers or balances. Blur is permanent once you Apply and Save; markers and boxes can be changed later.'),
|
||||
h('div', { class: 'row' }, radios,
|
||||
btn('Undo', () => { const u = undo.pop(); if (u) { u(); draw(); } }, 'small'),
|
||||
btn('Clear markers and boxes', () => { if (!marks.length) return; const copy = marks.splice(0); undo.push(() => marks.push(...copy)); draw(); }, 'small')),
|
||||
view,
|
||||
h('div', { class: 'row' },
|
||||
btn('Cancel', () => dlg.close()),
|
||||
btn('Apply', () => {
|
||||
if (blurs > 0) {
|
||||
let url = base.toDataURL(f.image.startsWith('data:image/png') ? 'image/png' : 'image/jpeg', JPEG_QUALITY);
|
||||
if (!isImageDataUrl(url)) url = base.toDataURL('image/jpeg', JPEG_QUALITY);
|
||||
if (!isImageDataUrl(url)) return setStatus('The blurred picture is too large to keep.');
|
||||
f.image = url;
|
||||
}
|
||||
if (marks.length) f.marks = marks; else delete f.marks;
|
||||
changed(q); // clears the status, so set it afterwards
|
||||
setStatus(blurs > 0 ? 'Picture marked up and blurred. Save to keep it.' : 'Markers and boxes updated.');
|
||||
dlg.close(); render();
|
||||
}, 'primary')));
|
||||
dlg.addEventListener('close', () => dlg.remove());
|
||||
document.body.append(dlg);
|
||||
dlg.showModal();
|
||||
draw();
|
||||
}
|
||||
|
||||
function openLightbox(label, src, marks) {
|
||||
const dlg = h('dialog', { class: 'card lightbox', 'aria-label': label },
|
||||
h('h3', {}, label), h('img', { class: 'bigimg', src, alt: label }),
|
||||
h('h3', {}, label), markedImage(h('img', { class: 'bigimg', src, alt: label }), marks),
|
||||
h('div', { class: 'row' }, btn('Close', () => dlg.close(), 'primary')));
|
||||
dlg.addEventListener('close', () => dlg.remove());
|
||||
document.body.append(dlg);
|
||||
@@ -348,8 +456,8 @@
|
||||
return h('div', { class: 'frame ' + layout },
|
||||
h('div', { class: 'fnum' }, 'Step ' + (k + 1)),
|
||||
hidden ? h('div', {}, h('span', { class: 'secret' }, '(hidden step)'), btn('Show', toggle, 'link', { 'aria-label': 'Show ' + label })) : [
|
||||
f.image ? h('img', { class: 'fimg', src: f.image, alt: label, tabIndex: 0, title: 'Click to enlarge',
|
||||
onclick: () => openLightbox(label, f.image), onkeydown: (e) => { if (e.key === 'Enter') openLightbox(label, f.image); } })
|
||||
f.image ? markedImage(h('img', { class: 'fimg', src: f.image, alt: label, tabIndex: 0, title: 'Click to enlarge',
|
||||
onclick: () => openLightbox(label, f.image, f.marks), onkeydown: (e) => { if (e.key === 'Enter') openLightbox(label, f.image, f.marks); } }), f.marks)
|
||||
: h('span', { class: 'muted' }, '(no image)'),
|
||||
layout !== 'none' ? h('p', { class: 'ftext' }, f.text) : null,
|
||||
viewUses(f),
|
||||
@@ -461,7 +569,7 @@
|
||||
ondragover: (e) => { if (hasFiles(e)) { e.preventDefault(); zone.classList.add('over'); } },
|
||||
ondragleave: () => zone.classList.remove('over'),
|
||||
ondrop: (e) => { e.preventDefault(); e.stopPropagation(); zone.classList.remove('over'); onFiles([...e.dataTransfer.files]); } }, attrs),
|
||||
obj[key] ? h('img', { class: 'fimg', src: obj[key], alt: label }) : null,
|
||||
obj[key] ? markedImage(h('img', { class: 'fimg', src: obj[key], alt: label }), key === 'image' ? obj.marks : null) : null,
|
||||
h('p', { class: 'muted small' }, obj[key]
|
||||
? formatBytes(dataUrlBytes(obj[key])) + '. Drop, paste or choose a file to replace it.'
|
||||
: 'Drop an image here, paste one while this box is selected, or choose a file.'),
|
||||
@@ -582,7 +690,9 @@
|
||||
return h('div', { class: 'field imagefield frameedit' },
|
||||
h('div', { class: 'row' }, h('strong', {}, 'Step ' + (k + 1)), layout, h('label', { class: 'check' }, secret, ' secret'), rowTools(q.frames, k, 'step', q)),
|
||||
imageZone(f, 'image', q.title + ', step ' + (k + 1), { 'data-frame': String(k) },
|
||||
(files) => importFiles(q, files, { key: 'image', replace: f, add: addFrameFrom(q) }), () => changed(q)),
|
||||
(files) => importFiles(q, files, { key: 'image', replace: f, add: addFrameFrom(q) }), () => { if (!f.image) delete f.marks; changed(q); }),
|
||||
f.image ? h('div', { class: 'row' }, btn('Mark up picture\u2026', () => openAnnotator(q, f), 'small', { 'aria-label': 'Mark up the picture for step ' + (k + 1) }),
|
||||
f.marks && f.marks.length ? h('span', { class: 'muted small' }, f.marks.length + (f.marks.length === 1 ? ' marker or box' : ' markers and boxes')) : null) : null,
|
||||
f.layout === 'none' ? null : h('label', {}, 'Text', input(f, 'text', { multiline: true, label: 'Text for step ' + (k + 1) }, q)),
|
||||
useEditor(q, f, k),
|
||||
h('label', {}, 'If it looks different or fails', input(f, 'ifStuck', { multiline: true, label: 'If step ' + (k + 1) + ' looks different or fails' }, q)),
|
||||
@@ -686,7 +796,7 @@
|
||||
return h('div', { class: 'pframe ' + v.layout },
|
||||
h('div', { class: 'pfnum' }, 'Step ' + (k + 1)),
|
||||
v.withheld ? h('p', { class: 'psecret' }, '[withheld]')
|
||||
: v.image ? h('img', { class: 'pimg', src: v.image, alt: 'Step ' + (k + 1) })
|
||||
: v.image ? markedImage(h('img', { class: 'pimg', src: v.image, alt: 'Step ' + (k + 1) }), f.marks)
|
||||
: h('p', {}, '(no image)'),
|
||||
v.layout !== 'none' ? h('p', { class: 'pftext' }, v.text) : null,
|
||||
v.withheld ? null : usesView(state.doc, f, includeSecrets).map((u) => h('p', { class: 'pftext' },
|
||||
|
||||
@@ -59,6 +59,10 @@ textarea { width: 100%; display: block; } label { display: block; margin-top: .5
|
||||
.fnum { font-weight: 700; color: var(--accent); } .ftext { white-space: pre-wrap; margin: 0; }
|
||||
li.seqitem { display: block; list-style: none; margin-left: -1.4rem; } .frameedit { border-top: 1px dashed var(--line); padding-top: .5rem; }
|
||||
.fnotes { grid-column: 1 / -1; display: grid; gap: .35rem; } .fnotes p { margin: 0; }
|
||||
.marked { position: relative; display: block; width: fit-content; max-width: 100%; line-height: 0; }
|
||||
.mbox { position: absolute; box-sizing: border-box; border: 3px solid #e11; box-shadow: 0 0 0 1px #fff; border-radius: 3px; pointer-events: none; }
|
||||
.mpin { position: absolute; box-sizing: border-box; transform: translate(-50%, -50%); width: 1.6rem; height: 1.6rem; border-radius: 50%; background: #e11; color: #fff; border: 2px solid #fff; font: 700 .85rem/1.2rem system-ui, sans-serif; text-align: center; pointer-events: none; }
|
||||
.annotator { max-width: min(95vw, 70rem); } .annot { display: block; max-width: 100%; max-height: 65vh; margin: .5rem auto; cursor: crosshair; touch-action: none; border: 1px solid var(--line); }
|
||||
.fuses { grid-column: 1 / -1; } .fuse { align-items: center; margin: .25rem 0; }
|
||||
.fstop { border: 2px solid var(--danger); color: var(--danger); border-radius: .35rem; padding: .35rem .6rem; }
|
||||
.fstuck { background: var(--warn-bg); color: var(--warn-fg); border-radius: .35rem; padding: .35rem .6rem; white-space: pre-wrap; }
|
||||
@@ -106,6 +110,7 @@ dialog.lightbox { max-width: 92vw; max-height: 92vh; overflow: auto; } .bigimg {
|
||||
.pframe { break-inside: avoid; display: grid; grid-template-columns: minmax(0, 1fr); gap: 3pt 12pt; align-items: start; margin: 0 0 10pt; }
|
||||
.pframe.right { grid-template-columns: 3.9in minmax(0, 1fr); } .pframe.right .pimg { max-height: 3.4in; }
|
||||
.pfnum { grid-column: 1 / -1; font: 700 9.5pt system-ui, sans-serif; break-after: avoid; } .pframe p { margin: 0; } .pftext { white-space: pre-wrap; }
|
||||
.marked { break-inside: avoid; } .mbox { border: 2pt solid #e11; box-shadow: none; } .mpin { width: 14pt; height: 14pt; font: 700 8pt/10pt system-ui, sans-serif; background: #e11; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
|
||||
.pstop { grid-column: 1 / -1; border: 1.5pt solid #000; padding: 2pt 5pt; }
|
||||
.psecret { font-family: ui-monospace, Consolas, monospace; }
|
||||
.ptag { font: 700 8pt system-ui, sans-serif; border: 1pt solid #000; padding: 0 4pt; margin-left: 8pt; border-radius: 2pt; vertical-align: middle; }
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// YYMMDD is the date, X the iteration that day (A first, B second, ...).
|
||||
// Bump with `npm run bump` (see bump-version.js). build.js strips `export`.
|
||||
|
||||
export const APP_VERSION = '261124E';
|
||||
export const APP_VERSION = '261124F';
|
||||
|
||||
const VERSION_RE = /^(\d{6})([A-Z])$/;
|
||||
export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v);
|
||||
|
||||
Reference in New Issue
Block a user