|
| 1 | +/** |
| 2 | + * The picture script: it lets the profile's picture form choose which square of the |
| 3 | + * chosen image auth keeps. The image moves and zooms under a circle, as apps show |
| 4 | + * it, and the square under the circle, in the image's pixels, fills the form's |
| 5 | + * hidden inputs. Without the script they stay empty and auth keeps the centred |
| 6 | + * square. It exports nothing, and nothing compiles it: auth embeds this file as it |
| 7 | + * is, under a path naming its content, and `tsc` only checks its JSDoc types. |
| 8 | + */ |
| 9 | + |
| 10 | +/** How far one arrow key moves the image, in screen pixels. */ |
| 11 | +const STEP = 8; |
| 12 | + |
| 13 | +/** |
| 14 | + * The cropper of one form: its image, its zoom, and the square it keeps, held as |
| 15 | + * the square's centre in the image's pixels. |
| 16 | + */ |
| 17 | +class Cropper { |
| 18 | + /** @param {HTMLFormElement} form */ |
| 19 | + constructor(form) { |
| 20 | + this.file = query(form, 'input[type="file"]', HTMLInputElement); |
| 21 | + this.cropper = query(form, '[data-cropper]', HTMLElement); |
| 22 | + this.stage = query(form, '[data-crop-stage]', HTMLElement); |
| 23 | + this.image = query(form, '[data-crop-image]', HTMLImageElement); |
| 24 | + this.zoom = query(form, '[data-crop-zoom]', HTMLInputElement); |
| 25 | + /** The hidden inputs of the square's left edge, top edge and side. */ |
| 26 | + this.fields = ['x', 'y', 'size'].map((part) => |
| 27 | + query(form, `[data-crop="${part}"]`, HTMLInputElement) |
| 28 | + ); |
| 29 | + /** The image's size and the square's centre, in its pixels. */ |
| 30 | + this.width = 0; |
| 31 | + this.height = 0; |
| 32 | + this.centreX = 0; |
| 33 | + this.centreY = 0; |
| 34 | + /** @type {{ id: number; x: number; y: number } | null} The pointer dragging the image. */ |
| 35 | + this.drag = null; |
| 36 | + /** @type {string | null} The URL of the file shown, revoked once replaced. */ |
| 37 | + this.url = null; |
| 38 | + |
| 39 | + this.file.addEventListener('change', () => { |
| 40 | + this.choose(); |
| 41 | + }); |
| 42 | + this.image.addEventListener('load', () => { |
| 43 | + this.loaded(); |
| 44 | + }); |
| 45 | + this.image.addEventListener('error', () => { |
| 46 | + this.reset(); |
| 47 | + }); |
| 48 | + this.zoom.addEventListener('input', () => { |
| 49 | + this.render(); |
| 50 | + }); |
| 51 | + this.stage.addEventListener('pointerdown', (event) => { |
| 52 | + this.grab(event); |
| 53 | + }); |
| 54 | + this.stage.addEventListener('pointermove', (event) => { |
| 55 | + this.move(event); |
| 56 | + }); |
| 57 | + for (const end of /** @type {const} */ (['pointerup', 'pointercancel'])) { |
| 58 | + this.stage.addEventListener(end, (event) => { |
| 59 | + if (event.pointerId === this.drag?.id) this.drag = null; |
| 60 | + }); |
| 61 | + } |
| 62 | + this.stage.addEventListener('keydown', (event) => { |
| 63 | + this.key(event); |
| 64 | + }); |
| 65 | + // The stage changes size with the window, and has none while its dialog is |
| 66 | + // closed. |
| 67 | + new ResizeObserver(() => { |
| 68 | + this.render(); |
| 69 | + }).observe(this.stage); |
| 70 | + } |
| 71 | + |
| 72 | + /** Shows the file just chosen, or nothing once none is. */ |
| 73 | + choose() { |
| 74 | + // The last image's square must not go with the next one. |
| 75 | + this.reset(); |
| 76 | + if (this.url) URL.revokeObjectURL(this.url); |
| 77 | + this.url = null; |
| 78 | + const file = this.file.files?.[0]; |
| 79 | + if (!file) return; |
| 80 | + this.url = URL.createObjectURL(file); |
| 81 | + this.image.src = this.url; |
| 82 | + } |
| 83 | + |
| 84 | + /** Starts on the image's centred square, at no zoom. */ |
| 85 | + loaded() { |
| 86 | + this.width = this.image.naturalWidth; |
| 87 | + this.height = this.image.naturalHeight; |
| 88 | + this.centreX = this.width / 2; |
| 89 | + this.centreY = this.height / 2; |
| 90 | + this.zoom.value = '1'; |
| 91 | + this.cropper.hidden = false; |
| 92 | + this.render(); |
| 93 | + } |
| 94 | + |
| 95 | + /** Hides the cropper and leaves the square to auth, until an image shows. */ |
| 96 | + reset() { |
| 97 | + this.width = 0; |
| 98 | + this.cropper.hidden = true; |
| 99 | + for (const field of this.fields) field.value = ''; |
| 100 | + } |
| 101 | + |
| 102 | + /** The stage's side, in screen pixels. */ |
| 103 | + side() { |
| 104 | + return this.stage.clientWidth; |
| 105 | + } |
| 106 | + |
| 107 | + /** Screen pixels per image pixel: the image covers the stage, times the zoom. */ |
| 108 | + scale() { |
| 109 | + return (this.side() / Math.min(this.width, this.height)) * Number(this.zoom.value); |
| 110 | + } |
| 111 | + |
| 112 | + /** Places the image so the square stays inside it, and writes the square. */ |
| 113 | + render() { |
| 114 | + const side = this.side(); |
| 115 | + if (!this.width || !side) return; |
| 116 | + const scale = this.scale(); |
| 117 | + const half = side / scale / 2; |
| 118 | + this.centreX = clamp(this.centreX, half, this.width - half); |
| 119 | + this.centreY = clamp(this.centreY, half, this.height - half); |
| 120 | + const left = side / 2 - this.centreX * scale; |
| 121 | + const top = side / 2 - this.centreY * scale; |
| 122 | + this.image.style.width = `${String(this.width * scale)}px`; |
| 123 | + this.image.style.transform = `translate(${String(left)}px, ${String(top)}px)`; |
| 124 | + const size = Math.max(1, Math.round(side / scale)); |
| 125 | + const square = [ |
| 126 | + clamp(Math.round(this.centreX - size / 2), 0, this.width - size), |
| 127 | + clamp(Math.round(this.centreY - size / 2), 0, this.height - size), |
| 128 | + size |
| 129 | + ]; |
| 130 | + this.fields.forEach((field, index) => (field.value = String(square[index]))); |
| 131 | + } |
| 132 | + |
| 133 | + /** Starts dragging with the primary pointer's main button. @param {PointerEvent} event */ |
| 134 | + grab(event) { |
| 135 | + if (!event.isPrimary || event.button !== 0) return; |
| 136 | + this.stage.setPointerCapture(event.pointerId); |
| 137 | + this.drag = { id: event.pointerId, x: event.clientX, y: event.clientY }; |
| 138 | + } |
| 139 | + |
| 140 | + /** Moves the image with the pointer dragging it, and no other. @param {PointerEvent} event */ |
| 141 | + move(event) { |
| 142 | + const drag = this.drag; |
| 143 | + if (drag?.id !== event.pointerId) return; |
| 144 | + this.pan(event.clientX - drag.x, event.clientY - drag.y); |
| 145 | + this.drag = { id: event.pointerId, x: event.clientX, y: event.clientY }; |
| 146 | + } |
| 147 | + |
| 148 | + /** Moves the image with the arrow keys. @param {KeyboardEvent} event */ |
| 149 | + key(event) { |
| 150 | + /** @type {Partial<Record<string, [number, number]>>} */ |
| 151 | + const moves = { |
| 152 | + ArrowLeft: [-STEP, 0], |
| 153 | + ArrowRight: [STEP, 0], |
| 154 | + ArrowUp: [0, -STEP], |
| 155 | + ArrowDown: [0, STEP] |
| 156 | + }; |
| 157 | + const by = moves[event.key]; |
| 158 | + if (!by) return; |
| 159 | + event.preventDefault(); |
| 160 | + this.pan(...by); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Moves the image by `dx` and `dy` screen pixels, so the square moves the other way. |
| 165 | + * @param {number} dx |
| 166 | + * @param {number} dy |
| 167 | + */ |
| 168 | + pan(dx, dy) { |
| 169 | + const scale = this.scale(); |
| 170 | + this.centreX -= dx / scale; |
| 171 | + this.centreY -= dy / scale; |
| 172 | + this.render(); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * The element of `form` that `selector` names, which the page always renders, as a |
| 178 | + * `kind`. |
| 179 | + * @template {Element} T |
| 180 | + * @param {HTMLFormElement} form |
| 181 | + * @param {string} selector |
| 182 | + * @param {{ new (): T }} kind |
| 183 | + * @returns {T} |
| 184 | + */ |
| 185 | +function query(form, selector, kind) { |
| 186 | + const element = form.querySelector(selector); |
| 187 | + if (!(element instanceof kind)) throw new Error(`The picture form has no ${selector}.`); |
| 188 | + return element; |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * `value`, within `low` and `high`; `low` when the range is empty. |
| 193 | + * @param {number} value |
| 194 | + * @param {number} low |
| 195 | + * @param {number} high |
| 196 | + */ |
| 197 | +function clamp(value, low, high) { |
| 198 | + return Math.max(low, Math.min(value, high)); |
| 199 | +} |
| 200 | + |
| 201 | +for (const stage of document.querySelectorAll('[data-crop-stage]')) { |
| 202 | + const form = stage.closest('form'); |
| 203 | + if (form) new Cropper(form); |
| 204 | +} |
0 commit comments