|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +'use client'; |
| 4 | + |
| 5 | +import {createAnimator} from '@astryxdesign/core'; |
| 6 | + |
| 7 | +const DECK = [ |
| 8 | + {face: 'A\u2660', name: 'Ace of spades'}, |
| 9 | + {face: 'K\u2665', name: 'King of hearts'}, |
| 10 | + {face: 'Q\u2663', name: 'Queen of clubs'}, |
| 11 | + {face: 'J\u2666', name: 'Jack of diamonds'}, |
| 12 | +]; |
| 13 | +/** A release faster than this (px/s) throws the card away. */ |
| 14 | +const THROW_SPEED = 1000; |
| 15 | +/** The slowest throw, so a thrown card always leaves the table. */ |
| 16 | +const MIN_THROW = 1600; |
| 17 | + |
| 18 | +/** |
| 19 | + * A deck on a table, in plain JavaScript: no React below this line. It |
| 20 | + * returns a function that takes it down again. |
| 21 | + */ |
| 22 | +function mountDeck(table: HTMLElement, discardButton: HTMLButtonElement) { |
| 23 | + let top = 0; |
| 24 | + const card = document.createElement('div'); |
| 25 | + card.textContent = DECK[0].face; |
| 26 | + // An image's name is all a screen reader announces, so it names the card. |
| 27 | + card.setAttribute('role', 'img'); |
| 28 | + card.setAttribute('aria-label', DECK[0].name); |
| 29 | + Object.assign(card.style, { |
| 30 | + width: '120px', |
| 31 | + height: '170px', |
| 32 | + display: 'grid', |
| 33 | + placeItems: 'center', |
| 34 | + font: 'bold 40px system-ui, sans-serif', |
| 35 | + // The theme's surface and text, so the rank reads in light and dark. |
| 36 | + borderRadius: 'var(--radius-container)', |
| 37 | + backgroundColor: 'var(--color-background-surface)', |
| 38 | + color: 'var(--color-text-primary)', |
| 39 | + boxShadow: '0 2px 8px var(--color-shadow)', |
| 40 | + touchAction: 'none', |
| 41 | + cursor: 'grab', |
| 42 | + userSelect: 'none', |
| 43 | + }); |
| 44 | + // Announces each discard to screen readers. |
| 45 | + const status = document.createElement('div'); |
| 46 | + status.setAttribute('aria-live', 'polite'); |
| 47 | + Object.assign(status.style, { |
| 48 | + position: 'absolute', |
| 49 | + width: '1px', |
| 50 | + height: '1px', |
| 51 | + overflow: 'hidden', |
| 52 | + clipPath: 'inset(50%)', |
| 53 | + whiteSpace: 'nowrap', |
| 54 | + }); |
| 55 | + table.append(card, status); |
| 56 | + const animator = createAnimator(card); |
| 57 | + // One discard at a time: a press while a card leaves deals no second card. |
| 58 | + let isDiscarding = false; |
| 59 | + |
| 60 | + // Announces the discard and deals the next card in place of the old one. |
| 61 | + async function dealNext() { |
| 62 | + status.textContent = `${DECK[top % DECK.length].name} discarded`; |
| 63 | + top += 1; |
| 64 | + const next = DECK[top % DECK.length]; |
| 65 | + card.textContent = next.face; |
| 66 | + card.setAttribute('aria-label', next.name); |
| 67 | + animator.set({x: 0, y: 0, rotate: 0, scale: 0.9, opacity: 0}); |
| 68 | + isDiscarding = false; |
| 69 | + await animator.animate({scale: 1, opacity: 1}); |
| 70 | + } |
| 71 | + |
| 72 | + async function discard(vx: number, vy: number) { |
| 73 | + if (isDiscarding) { |
| 74 | + return; |
| 75 | + } |
| 76 | + isDiscarding = true; |
| 77 | + const speed = Math.hypot(vx, vy); |
| 78 | + const boost = speed < MIN_THROW ? MIN_THROW / Math.max(speed, 1) : 1; |
| 79 | + // The card coasts on while it fades; once it is gone, the next card is |
| 80 | + // dealt in place, which ends the coast. A coast continues the person's |
| 81 | + // throw, so Reduce Motion keeps it. |
| 82 | + void animator.decay({x: speed < 1 ? MIN_THROW : vx * boost, y: vy * boost}); |
| 83 | + await animator.animate({opacity: 0}); |
| 84 | + await dealNext(); |
| 85 | + } |
| 86 | + |
| 87 | + // The drag: follow the pointer, then read the 80 ms before the release, so |
| 88 | + // a pointer that held still before letting go releases at rest. |
| 89 | + let origin: {x: number; y: number} | null = null; |
| 90 | + let samples: {t: number; x: number; y: number}[] = []; |
| 91 | + function onDown(event: PointerEvent) { |
| 92 | + card.setPointerCapture(event.pointerId); |
| 93 | + animator.stop(); |
| 94 | + const {x, y} = animator.get(); |
| 95 | + origin = {x: event.clientX - x, y: event.clientY - y}; |
| 96 | + samples = []; |
| 97 | + } |
| 98 | + function onMove(event: PointerEvent) { |
| 99 | + if (origin == null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + const x = event.clientX - origin.x; |
| 103 | + const y = event.clientY - origin.y; |
| 104 | + samples.push({t: event.timeStamp, x, y}); |
| 105 | + animator.set({x, y, rotate: x / 18}); |
| 106 | + } |
| 107 | + // A cancelled pointer was not let go, so the card springs back. |
| 108 | + function onCancel() { |
| 109 | + if (origin == null) { |
| 110 | + return; |
| 111 | + } |
| 112 | + origin = null; |
| 113 | + void animator.animate({x: 0, y: 0, rotate: 0}); |
| 114 | + } |
| 115 | + function onUp(event: PointerEvent) { |
| 116 | + if (origin == null) { |
| 117 | + return; |
| 118 | + } |
| 119 | + origin = null; |
| 120 | + const recent = samples.filter(s => event.timeStamp - s.t <= 80); |
| 121 | + const first = recent[0]; |
| 122 | + const last = recent.at(-1); |
| 123 | + const seconds = last && first ? (last.t - first.t) / 1000 : 0; |
| 124 | + const vx = seconds > 0 && last && first ? (last.x - first.x) / seconds : 0; |
| 125 | + const vy = seconds > 0 && last && first ? (last.y - first.y) / seconds : 0; |
| 126 | + if (Math.abs(vx) > THROW_SPEED) { |
| 127 | + void discard(vx, vy); |
| 128 | + } else { |
| 129 | + void animator.animate( |
| 130 | + {x: 0, y: 0, rotate: 0}, |
| 131 | + {velocity: {x: vx, y: vy}}, |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + // The button moves the card off on the state-change motion: a planned |
| 136 | + // move, which Reduce Motion turns into a fade where the card is. |
| 137 | + async function discardWithButton() { |
| 138 | + if (isDiscarding) { |
| 139 | + return; |
| 140 | + } |
| 141 | + isDiscarding = true; |
| 142 | + await animator.animate({x: 400, opacity: 0}); |
| 143 | + await dealNext(); |
| 144 | + } |
| 145 | + const onDiscard = () => void discardWithButton(); |
| 146 | + |
| 147 | + card.addEventListener('pointerdown', onDown); |
| 148 | + card.addEventListener('pointermove', onMove); |
| 149 | + card.addEventListener('pointerup', onUp); |
| 150 | + card.addEventListener('pointercancel', onCancel); |
| 151 | + discardButton.addEventListener('click', onDiscard); |
| 152 | + return () => { |
| 153 | + discardButton.removeEventListener('click', onDiscard); |
| 154 | + animator.destroy(); |
| 155 | + card.remove(); |
| 156 | + status.remove(); |
| 157 | + }; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Mounts the deck when the wrapper attaches, and takes it down when it |
| 162 | + * detaches: its cleanup is the callback ref's. |
| 163 | + */ |
| 164 | +function mountTable(wrapper: HTMLDivElement | null) { |
| 165 | + if (wrapper == null) { |
| 166 | + return undefined; |
| 167 | + } |
| 168 | + const table = wrapper.firstElementChild as HTMLElement; |
| 169 | + const button = wrapper.querySelector('button') as HTMLButtonElement; |
| 170 | + return mountDeck(table, button); |
| 171 | +} |
| 172 | + |
| 173 | +export default function AnimatorPlayingCardsVanilla() { |
| 174 | + return ( |
| 175 | + <div |
| 176 | + ref={mountTable} |
| 177 | + style={{display: 'grid', gap: 16, padding: 16, maxWidth: 360}}> |
| 178 | + <div style={{height: 200, display: 'grid', placeItems: 'center'}} /> |
| 179 | + <button type="button">Discard the top card</button> |
| 180 | + </div> |
| 181 | + ); |
| 182 | +} |
0 commit comments