X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/8f91d2a391ec8c0b38a6f657cf3815143234d090..a93c5c61b18192c5f74256e3cb18e7f1cd2034d5:/word/editor.js diff --git a/word/editor.js b/word/editor.js index 5fc9854..4eb2382 100644 --- a/word/editor.js +++ b/word/editor.js @@ -123,16 +123,30 @@ document.addEventListener('DOMContentLoaded', () => { thumbpreview.onclick = null; // setup once const cropinput = document.getElementById('crop32'); const imgselect = thumbpreview.children[0]; - const border = [300,200]; - let crop = cropinput.value.split(/[^0-9]/).map(pos => pos / 1000); - let scale = 1 / (crop[2] - crop[0]) || 1; + const canvas = [thumbpreview.clientWidth, thumbpreview.clientHeight]; + const border = [canvas[0], canvas[0] * imgpreview.height / imgpreview.width]; + const minscale = Math.max(1, canvas[1] / border[1]); // 100% or fit width + let crop = cropinput.value.split(/[^0-9.]/).map(pos => pos / 1000); + let scale = 1 / (crop[2] - crop[0]) || minscale; crop.push(0); // defined y dimension crop.splice(2); // end coordinates applied to zoom crop = crop.map((rel, axis) => rel * border[axis % 2] * scale); - let drag = false; + let drag, pinch; function applydrag(e) { - const pos = [e.pageX, e.pageY]; + const touch = e.touches ? e.touches[0] : e; + let pos = [touch.pageX, touch.pageY]; + if (e.type === 'touchmove' && e.touches.length > 1) { + // distance to second point + pos[0] -= e.touches[1].pageX; + pos[1] -= e.touches[1].pageY; + const span = Math.sqrt(pos[0]**2 + pos[1]**2); + if (pinch) { + cropzoom(.01 * (span - pinch)); + } + pinch = span; + return; + } if (drag) { // apply drag delta to crop position crop[0] += drag[0] - pos[0]; @@ -141,52 +155,71 @@ document.addEventListener('DOMContentLoaded', () => { } drag = pos; } + function recrop() { [0, 1].forEach(axis => { - if (crop[axis] > border[axis] * (scale - 1)) { - crop[axis] = border[axis] * (scale - 1); + if (crop[axis] > border[axis] * scale - canvas[axis]) { + crop[axis] = border[axis] * scale - canvas[axis]; // max bound } if (crop[axis] < 0) { - crop[axis] = 0; + crop[axis] = 0; // min bound } }); imgselect.style.left = -crop[0]+'px'; imgselect.style.top = -crop[1]+'px'; imgselect.style.width = (scale * 100)+'%'; cropinput.value = [ - crop[0] / border[0], crop[1] / border[1], - crop[0] / border[0] + 1, crop[1] / border[1] + 1, + crop[0] / border[0], + crop[1] / border[1], + (crop[0] + canvas[0]) / border[0], + (crop[1] + canvas[1]) / border[1], ].map(pos => Math.round(1000 * pos / scale)); } - imgselect.src = imgpreview.children[0].src; + function cropzoom(delta) { + if (scale + delta < minscale) { + delta = minscale - scale; // scale = 1 + } + [0, 1].forEach(axis => { + // same area center at altered scale + crop[axis] += (crop[axis] + border[axis] / 2) / scale * delta; + }); + scale += delta; + recrop(); + } + + imgselect.src = imgpreview.src; + imgselect.style.cursor = 'grab'; imgselect.style.position = 'absolute'; recrop(); + imgselect.ontouchstart = imgselect.onmousedown = e => { e.preventDefault(); + drag = pinch = false; applydrag(e); + imgselect.style.cursor = 'grabbing'; + window.ontouchmove = + window.onmousemove = e => { + e.preventDefault(); + applydrag(e); + }; + window.ontouchend = + window.onmouseup = e => { + e.preventDefault(); + imgselect.style.cursor = 'grab'; + window.ontouchmove = window.ontouchend = + window.onmousemove = window.onmouseup = null; + }; }; - imgselect.onmouseup = e => { - e.preventDefault(); - drag = false; - }; - imgselect.onmousemove = e => { - if (!drag) return; - applydrag(e); - }; + imgselect.onwheel = e => { e.preventDefault(); - let zoom = e.deltaY * -.05 * scale; - if (scale + zoom < 1) { - zoom = 1 - scale; // scale = 1 + let delta = (-e.deltaY || e.wheelDelta) * .001 * scale; + if (e.deltaMode == 1) { // DOM_DELTA_LINE + delta *= 18; // convert number of lines to pixels } - [0, 1].forEach(axis => { - // same area center at altered scale - crop[axis] += (crop[axis] + border[axis] / 2) / scale * zoom; - }); - scale += zoom; - recrop(); + cropzoom(delta); }; }; }