| 1 | /* pointer.js — Slides' half of the light: Docs' brand/pointer.js |
| 2 | (design/richer-ui), on Slides' opt-in attributes. brand/light.css |
| 3 | says what each attribute does; this only tells it where the pointer |
| 4 | is. |
| 5 | |
| 6 | What it writes, on the one lit element under the pointer, and |
| 7 | nothing else: --rst-mx and --rst-my, the pointer in the element's own |
| 8 | pixels (rastrillo light.js's names and units, so one stylesheet |
| 9 | vocabulary lights both), and --slides-tx and --slides-ty, the same |
| 10 | position from -1 to 1 across the element, for a lifted tile's lean. |
| 11 | It clears them when the pointer leaves. A press writes where it |
| 12 | landed as --rst-px and --rst-py on the control, light.js's names |
| 13 | again, which the bloom is centred on. Every property has a default in |
| 14 | the stylesheet, so deleting this file and its tag leaves the still |
| 15 | design. |
| 16 | |
| 17 | Lit is [slides-lit], [slides-lift], [slides-press] and [rst-btn], |
| 18 | and any element that is lit through a parent is lit as that parent: |
| 19 | it takes the nearest. It never runs for touch, under reduced motion |
| 20 | or forced colours, or without a hovering pointer; one animation |
| 21 | frame per move at most. */ |
| 22 | (function () { |
| 23 | "use strict"; |
| 24 | if (!window.matchMedia || !window.requestAnimationFrame) return; |
| 25 | var LIT = "[slides-lit],[slides-lift],[slides-press],[rst-btn]"; |
| 26 | var PROPS = ["--rst-mx", "--rst-my", "--slides-tx", "--slides-ty"]; |
| 27 | var quiet = [matchMedia("(prefers-reduced-motion: reduce)"), matchMedia("(forced-colors: active)")]; |
| 28 | var hover = matchMedia("(hover: hover)"); |
| 29 | var lit = null, pending = null, raf = 0, listening = false; |
| 30 | |
| 31 | function mouse(e) { |
| 32 | return !e.pointerType || e.pointerType === "mouse" || e.pointerType === "pen"; |
| 33 | } |
| 34 | function litFrom(target) { |
| 35 | return target && target.closest ? target.closest(LIT) : null; |
| 36 | } |
| 37 | function clear() { |
| 38 | if (!lit) return; |
| 39 | for (var i = 0; i < PROPS.length; i++) lit.style.removeProperty(PROPS[i]); |
| 40 | lit = null; |
| 41 | } |
| 42 | function step() { |
| 43 | raf = 0; |
| 44 | var e = pending; |
| 45 | pending = null; |
| 46 | if (!e) return; |
| 47 | var el = litFrom(e.target); |
| 48 | if (el !== lit) clear(); |
| 49 | if (!el) return; |
| 50 | lit = el; |
| 51 | var r = el.getBoundingClientRect(); |
| 52 | if (!r.width || !r.height) return; |
| 53 | var x = e.clientX - r.left, y = e.clientY - r.top; |
| 54 | el.style.setProperty("--rst-mx", x.toFixed(0) + "px"); |
| 55 | el.style.setProperty("--rst-my", y.toFixed(0) + "px"); |
| 56 | el.style.setProperty("--slides-tx", Math.max(-1, Math.min(1, (x / r.width) * 2 - 1)).toFixed(3)); |
| 57 | el.style.setProperty("--slides-ty", Math.max(-1, Math.min(1, (y / r.height) * 2 - 1)).toFixed(3)); |
| 58 | } |
| 59 | function move(e) { |
| 60 | if (!mouse(e)) return; |
| 61 | pending = e; |
| 62 | if (!raf) raf = requestAnimationFrame(step); |
| 63 | } |
| 64 | function out(e) { |
| 65 | if (!e.relatedTarget) clear(); |
| 66 | } |
| 67 | function press(e) { |
| 68 | if (e.button !== 0 || !mouse(e)) return; |
| 69 | var el = litFrom(e.target); |
| 70 | if (!el || el.disabled) return; |
| 71 | var r = el.getBoundingClientRect(); |
| 72 | el.style.setProperty("--rst-px", (e.clientX - r.left).toFixed(0) + "px"); |
| 73 | el.style.setProperty("--rst-py", (e.clientY - r.top).toFixed(0) + "px"); |
| 74 | } |
| 75 | function sync() { |
| 76 | var want = hover.matches && !quiet[0].matches && !quiet[1].matches; |
| 77 | if (want === listening) return; |
| 78 | listening = want; |
| 79 | if (want) { |
| 80 | document.addEventListener("pointermove", move, { passive: true }); |
| 81 | document.addEventListener("pointerout", out, { passive: true }); |
| 82 | document.addEventListener("pointerdown", press, { passive: true }); |
| 83 | } else { |
| 84 | document.removeEventListener("pointermove", move); |
| 85 | document.removeEventListener("pointerout", out); |
| 86 | document.removeEventListener("pointerdown", press); |
| 87 | clear(); |
| 88 | } |
| 89 | } |
| 90 | [hover, quiet[0], quiet[1]].forEach(function (m) { |
| 91 | if (m.addEventListener) m.addEventListener("change", sync); |
| 92 | else if (m.addListener) m.addListener(sync); |
| 93 | }); |
| 94 | sync(); |
| 95 | })(); |
| 96 | |