@20baht/stickers 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +48 -0
- package/README.md +173 -2
- package/dist/all.d.ts +4 -0
- package/dist/all.js +7 -0
- package/dist/element.d.ts +103 -0
- package/dist/element.js +181 -0
- package/dist/graph.d.ts +172 -0
- package/dist/graph.js +374 -0
- package/dist/icons/access-point.d.ts +2 -0
- package/dist/icons/access-point.js +34 -0
- package/dist/icons/ai-agent.d.ts +2 -0
- package/dist/icons/ai-agent.js +50 -0
- package/dist/icons/alarm-clock.d.ts +2 -0
- package/dist/icons/alarm-clock.js +46 -0
- package/dist/icons/all-in-one.d.ts +2 -0
- package/dist/icons/all-in-one.js +37 -0
- package/dist/icons/angpao.d.ts +2 -0
- package/dist/icons/angpao.js +46 -0
- package/dist/icons/badge-reader.d.ts +2 -0
- package/dist/icons/badge-reader.js +51 -0
- package/dist/icons/baht-note.d.ts +2 -0
- package/dist/icons/baht-note.js +42 -0
- package/dist/icons/bamboo.d.ts +2 -0
- package/dist/icons/bamboo.js +57 -0
- package/dist/icons/banana-leaf.d.ts +2 -0
- package/dist/icons/banana-leaf.js +87 -0
- package/dist/icons/banana-tree.d.ts +2 -0
- package/dist/icons/banana-tree.js +75 -0
- package/dist/icons/bear.d.ts +2 -0
- package/dist/icons/bear.js +39 -0
- package/dist/icons/blade-chassis.d.ts +2 -0
- package/dist/icons/blade-chassis.js +43 -0
- package/dist/icons/bond.d.ts +2 -0
- package/dist/icons/bond.js +48 -0
- package/dist/icons/books.d.ts +2 -0
- package/dist/icons/books.js +44 -0
- package/dist/icons/bts-card.d.ts +2 -0
- package/dist/icons/bts-card.js +47 -0
- package/dist/icons/budget-jar.d.ts +2 -0
- package/dist/icons/budget-jar.js +43 -0
- package/dist/icons/cactus.d.ts +2 -0
- package/dist/icons/cactus.js +42 -0
- package/dist/icons/calculator.d.ts +2 -0
- package/dist/icons/calculator.js +46 -0
- package/dist/icons/calendar-month.d.ts +2 -0
- package/dist/icons/calendar-month.js +66 -0
- package/dist/icons/campfire.d.ts +2 -0
- package/dist/icons/campfire.js +63 -0
- package/dist/icons/cctv.d.ts +2 -0
- package/dist/icons/cctv.js +38 -0
- package/dist/icons/certificate.d.ts +2 -0
- package/dist/icons/certificate.js +50 -0
- package/dist/icons/cha-yen.d.ts +2 -0
- package/dist/icons/cha-yen.js +68 -0
- package/dist/icons/chedi.d.ts +2 -0
- package/dist/icons/chedi.js +56 -0
- package/dist/icons/classic-phone.d.ts +2 -0
- package/dist/icons/classic-phone.js +32 -0
- package/dist/icons/clipboard.d.ts +2 -0
- package/dist/icons/clipboard.js +61 -0
- package/dist/icons/cloud.d.ts +2 -0
- package/dist/icons/cloud.js +28 -0
- package/dist/icons/coins.d.ts +2 -0
- package/dist/icons/coins.js +34 -0
- package/dist/icons/compare.d.ts +2 -0
- package/dist/icons/compare.js +33 -0
- package/dist/icons/confetti.d.ts +2 -0
- package/dist/icons/confetti.js +72 -0
- package/dist/icons/convenience-bag.d.ts +2 -0
- package/dist/icons/convenience-bag.js +51 -0
- package/dist/icons/cooling.d.ts +2 -0
- package/dist/icons/cooling.js +46 -0
- package/dist/icons/copy-files.d.ts +2 -0
- package/dist/icons/copy-files.js +37 -0
- package/dist/icons/core-switch.d.ts +2 -0
- package/dist/icons/core-switch.js +48 -0
- package/dist/icons/cron.d.ts +2 -0
- package/dist/icons/cron.js +86 -0
- package/dist/icons/cursor.d.ts +2 -0
- package/dist/icons/cursor.js +27 -0
- package/dist/icons/database.d.ts +2 -0
- package/dist/icons/database.js +24 -0
- package/dist/icons/desktop-tower.d.ts +2 -0
- package/dist/icons/desktop-tower.js +41 -0
- package/dist/icons/disc-image.d.ts +2 -0
- package/dist/icons/disc-image.js +54 -0
- package/dist/icons/dollar-note.d.ts +2 -0
- package/dist/icons/dollar-note.js +44 -0
- package/dist/icons/electric-bill.d.ts +2 -0
- package/dist/icons/electric-bill.js +51 -0
- package/dist/icons/email.d.ts +2 -0
- package/dist/icons/email.js +57 -0
- package/dist/icons/empty-state.d.ts +2 -0
- package/dist/icons/empty-state.js +75 -0
- package/dist/icons/expense.d.ts +2 -0
- package/dist/icons/expense.js +36 -0
- package/dist/icons/export-file.d.ts +2 -0
- package/dist/icons/export-file.js +35 -0
- package/dist/icons/eyedropper.d.ts +2 -0
- package/dist/icons/eyedropper.js +69 -0
- package/dist/icons/face-id.d.ts +2 -0
- package/dist/icons/face-id.js +51 -0
- package/dist/icons/file.d.ts +2 -0
- package/dist/icons/file.js +15 -0
- package/dist/icons/finance-b-coins.d.ts +8 -0
- package/dist/icons/finance-b-coins.js +14 -0
- package/dist/icons/finance-tray.d.ts +11 -0
- package/dist/icons/finance-tray.js +41 -0
- package/dist/icons/fingerprint.d.ts +2 -0
- package/dist/icons/fingerprint.js +45 -0
- package/dist/icons/firewall.d.ts +2 -0
- package/dist/icons/firewall.js +76 -0
- package/dist/icons/floating-ip.d.ts +2 -0
- package/dist/icons/floating-ip.js +59 -0
- package/dist/icons/flower.d.ts +2 -0
- package/dist/icons/flower.js +49 -0
- package/dist/icons/food.d.ts +6 -0
- package/dist/icons/food.js +18 -0
- package/dist/icons/freelance.d.ts +2 -0
- package/dist/icons/freelance.js +51 -0
- package/dist/icons/fuel.d.ts +2 -0
- package/dist/icons/fuel.js +46 -0
- package/dist/icons/goal.d.ts +2 -0
- package/dist/icons/goal.js +51 -0
- package/dist/icons/gold-bar.d.ts +2 -0
- package/dist/icons/gold-bar.js +60 -0
- package/dist/icons/hard-drive.d.ts +2 -0
- package/dist/icons/hard-drive.js +42 -0
- package/dist/icons/highlighter.d.ts +2 -0
- package/dist/icons/highlighter.js +75 -0
- package/dist/icons/home-router.d.ts +2 -0
- package/dist/icons/home-router.js +32 -0
- package/dist/icons/hot-drink.d.ts +2 -0
- package/dist/icons/hot-drink.js +33 -0
- package/dist/icons/hsm.d.ts +2 -0
- package/dist/icons/hsm.js +54 -0
- package/dist/icons/id-card.d.ts +2 -0
- package/dist/icons/id-card.js +36 -0
- package/dist/icons/income.d.ts +2 -0
- package/dist/icons/income.js +28 -0
- package/dist/icons/index.d.ts +187 -0
- package/dist/icons/index.js +370 -0
- package/dist/icons/interest.d.ts +2 -0
- package/dist/icons/interest.js +66 -0
- package/dist/icons/iot-sensor.d.ts +2 -0
- package/dist/icons/iot-sensor.js +35 -0
- package/dist/icons/ip-phone.d.ts +2 -0
- package/dist/icons/ip-phone.js +47 -0
- package/dist/icons/ipad-gen8.d.ts +2 -0
- package/dist/icons/ipad-gen8.js +58 -0
- package/dist/icons/iphone-17.d.ts +2 -0
- package/dist/icons/iphone-17.js +37 -0
- package/dist/icons/iso.d.ts +2 -0
- package/dist/icons/iso.js +3 -0
- package/dist/icons/kanban.d.ts +2 -0
- package/dist/icons/kanban.js +85 -0
- package/dist/icons/kaprao.d.ts +2 -0
- package/dist/icons/kaprao.js +70 -0
- package/dist/icons/key-pair.d.ts +2 -0
- package/dist/icons/key-pair.js +54 -0
- package/dist/icons/key.d.ts +3 -0
- package/dist/icons/key.js +37 -0
- package/dist/icons/keyboard.d.ts +2 -0
- package/dist/icons/keyboard.js +35 -0
- package/dist/icons/kite.d.ts +2 -0
- package/dist/icons/kite.js +53 -0
- package/dist/icons/kvm.d.ts +2 -0
- package/dist/icons/kvm.js +53 -0
- package/dist/icons/lamp.d.ts +2 -0
- package/dist/icons/lamp.js +49 -0
- package/dist/icons/laptop.d.ts +2 -0
- package/dist/icons/laptop.js +44 -0
- package/dist/icons/load-balancer.d.ts +2 -0
- package/dist/icons/load-balancer.js +50 -0
- package/dist/icons/lock-screen.d.ts +2 -0
- package/dist/icons/lock-screen.js +44 -0
- package/dist/icons/long-tail-boat.d.ts +2 -0
- package/dist/icons/long-tail-boat.js +66 -0
- package/dist/icons/lottery.d.ts +2 -0
- package/dist/icons/lottery.js +51 -0
- package/dist/icons/lotus.d.ts +2 -0
- package/dist/icons/lotus.js +70 -0
- package/dist/icons/mac-mini.d.ts +2 -0
- package/dist/icons/mac-mini.js +31 -0
- package/dist/icons/macbook-air.d.ts +2 -0
- package/dist/icons/macbook-air.js +41 -0
- package/dist/icons/mailbox-thai.d.ts +2 -0
- package/dist/icons/mailbox-thai.js +66 -0
- package/dist/icons/mango-sticky-rice.d.ts +2 -0
- package/dist/icons/mango-sticky-rice.js +41 -0
- package/dist/icons/marigold.d.ts +2 -0
- package/dist/icons/marigold.js +69 -0
- package/dist/icons/market-basket.d.ts +2 -0
- package/dist/icons/market-basket.js +69 -0
- package/dist/icons/media-player.d.ts +2 -0
- package/dist/icons/media-player.js +71 -0
- package/dist/icons/metrics.d.ts +2 -0
- package/dist/icons/metrics.js +58 -0
- package/dist/icons/mini-pc.d.ts +2 -0
- package/dist/icons/mini-pc.js +26 -0
- package/dist/icons/modem.d.ts +2 -0
- package/dist/icons/modem.js +37 -0
- package/dist/icons/monitor.d.ts +2 -0
- package/dist/icons/monitor.js +40 -0
- package/dist/icons/monstera.d.ts +2 -0
- package/dist/icons/monstera.js +47 -0
- package/dist/icons/moon.d.ts +2 -0
- package/dist/icons/moon.js +55 -0
- package/dist/icons/motorbike-taxi.d.ts +2 -0
- package/dist/icons/motorbike-taxi.js +42 -0
- package/dist/icons/music-note.d.ts +2 -0
- package/dist/icons/music-note.js +63 -0
- package/dist/icons/nas-2bay.d.ts +2 -0
- package/dist/icons/nas-2bay.js +49 -0
- package/dist/icons/nas.d.ts +2 -0
- package/dist/icons/nas.js +41 -0
- package/dist/icons/nature-iso.d.ts +29 -0
- package/dist/icons/nature-iso.js +74 -0
- package/dist/icons/noodle-soup.d.ts +2 -0
- package/dist/icons/noodle-soup.js +52 -0
- package/dist/icons/notebook.d.ts +2 -0
- package/dist/icons/notebook.js +44 -0
- package/dist/icons/object-drum.d.ts +18 -0
- package/dist/icons/object-drum.js +63 -0
- package/dist/icons/otp.d.ts +2 -0
- package/dist/icons/otp.js +46 -0
- package/dist/icons/over-budget.d.ts +2 -0
- package/dist/icons/over-budget.js +48 -0
- package/dist/icons/pad-see-ew.d.ts +2 -0
- package/dist/icons/pad-see-ew.js +56 -0
- package/dist/icons/palm-tree.d.ts +2 -0
- package/dist/icons/palm-tree.js +52 -0
- package/dist/icons/paper-boat.d.ts +2 -0
- package/dist/icons/paper-boat.js +67 -0
- package/dist/icons/paper.d.ts +3 -0
- package/dist/icons/paper.js +45 -0
- package/dist/icons/password.d.ts +2 -0
- package/dist/icons/password.js +45 -0
- package/dist/icons/patch-panel.d.ts +2 -0
- package/dist/icons/patch-panel.js +41 -0
- package/dist/icons/pdu.d.ts +2 -0
- package/dist/icons/pdu.js +40 -0
- package/dist/icons/phone-topup.d.ts +2 -0
- package/dist/icons/phone-topup.js +55 -0
- package/dist/icons/photo.d.ts +2 -0
- package/dist/icons/photo.js +48 -0
- package/dist/icons/phuang-malai.d.ts +2 -0
- package/dist/icons/phuang-malai.js +59 -0
- package/dist/icons/pie-chart.d.ts +2 -0
- package/dist/icons/pie-chart.js +36 -0
- package/dist/icons/piggy-bank.d.ts +2 -0
- package/dist/icons/piggy-bank.js +64 -0
- package/dist/icons/plant-kit.d.ts +23 -0
- package/dist/icons/plant-kit.js +112 -0
- package/dist/icons/plastic-stool.d.ts +2 -0
- package/dist/icons/plastic-stool.js +67 -0
- package/dist/icons/pointer.d.ts +1 -0
- package/dist/icons/pointer.js +12 -0
- package/dist/icons/pos.d.ts +2 -0
- package/dist/icons/pos.js +59 -0
- package/dist/icons/potted-plant.d.ts +2 -0
- package/dist/icons/potted-plant.js +31 -0
- package/dist/icons/power-pole.d.ts +2 -0
- package/dist/icons/power-pole.js +60 -0
- package/dist/icons/power-station.d.ts +2 -0
- package/dist/icons/power-station.js +56 -0
- package/dist/icons/printer.d.ts +2 -0
- package/dist/icons/printer.js +46 -0
- package/dist/icons/qr-code.d.ts +2 -0
- package/dist/icons/qr-code.js +45 -0
- package/dist/icons/rack.d.ts +2 -0
- package/dist/icons/rack.js +39 -0
- package/dist/icons/rain-cloud.d.ts +2 -0
- package/dist/icons/rain-cloud.js +31 -0
- package/dist/icons/rainbow.d.ts +2 -0
- package/dist/icons/rainbow.js +46 -0
- package/dist/icons/receipt.d.ts +2 -0
- package/dist/icons/receipt.js +56 -0
- package/dist/icons/redmi-note-9s-pro.d.ts +2 -0
- package/dist/icons/redmi-note-9s-pro.js +37 -0
- package/dist/icons/register.d.ts +2 -0
- package/dist/icons/register.js +40 -0
- package/dist/icons/remote-desktop.d.ts +2 -0
- package/dist/icons/remote-desktop.js +62 -0
- package/dist/icons/remote-folder.d.ts +2 -0
- package/dist/icons/remote-folder.js +55 -0
- package/dist/icons/rent.d.ts +2 -0
- package/dist/icons/rent.js +50 -0
- package/dist/icons/rock.d.ts +2 -0
- package/dist/icons/rock.js +80 -0
- package/dist/icons/router.d.ts +2 -0
- package/dist/icons/router.js +59 -0
- package/dist/icons/salary.d.ts +2 -0
- package/dist/icons/salary.js +54 -0
- package/dist/icons/saving-streak.d.ts +2 -0
- package/dist/icons/saving-streak.js +66 -0
- package/dist/icons/scan-slip.d.ts +2 -0
- package/dist/icons/scan-slip.js +53 -0
- package/dist/icons/scissors.d.ts +2 -0
- package/dist/icons/scissors.js +52 -0
- package/dist/icons/server-cabinet.d.ts +2 -0
- package/dist/icons/server-cabinet.js +49 -0
- package/dist/icons/server.d.ts +2 -0
- package/dist/icons/server.js +41 -0
- package/dist/icons/shared.d.ts +8 -0
- package/dist/icons/shared.js +18 -0
- package/dist/icons/shield.d.ts +3 -0
- package/dist/icons/shield.js +28 -0
- package/dist/icons/shooting-star.d.ts +2 -0
- package/dist/icons/shooting-star.js +37 -0
- package/dist/icons/shopping-box.d.ts +2 -0
- package/dist/icons/shopping-box.js +46 -0
- package/dist/icons/sky-kit.d.ts +19 -0
- package/dist/icons/sky-kit.js +72 -0
- package/dist/icons/smart-tv.d.ts +2 -0
- package/dist/icons/smart-tv.js +39 -0
- package/dist/icons/smartphone.d.ts +2 -0
- package/dist/icons/smartphone.js +30 -0
- package/dist/icons/sms.d.ts +2 -0
- package/dist/icons/sms.js +45 -0
- package/dist/icons/snapshots.d.ts +2 -0
- package/dist/icons/snapshots.js +45 -0
- package/dist/icons/snip.d.ts +2 -0
- package/dist/icons/snip.js +63 -0
- package/dist/icons/spirit-house.d.ts +2 -0
- package/dist/icons/spirit-house.js +73 -0
- package/dist/icons/ssl-certificate.d.ts +2 -0
- package/dist/icons/ssl-certificate.js +56 -0
- package/dist/icons/star.d.ts +2 -0
- package/dist/icons/star.js +16 -0
- package/dist/icons/stock.d.ts +2 -0
- package/dist/icons/stock.js +62 -0
- package/dist/icons/storage-array.d.ts +2 -0
- package/dist/icons/storage-array.js +40 -0
- package/dist/icons/storm.d.ts +2 -0
- package/dist/icons/storm.js +43 -0
- package/dist/icons/street-food-cart.d.ts +2 -0
- package/dist/icons/street-food-cart.js +66 -0
- package/dist/icons/street-kit.d.ts +19 -0
- package/dist/icons/street-kit.js +67 -0
- package/dist/icons/street-sign.d.ts +2 -0
- package/dist/icons/street-sign.js +42 -0
- package/dist/icons/studio-monitors.d.ts +2 -0
- package/dist/icons/studio-monitors.js +67 -0
- package/dist/icons/sun.d.ts +2 -0
- package/dist/icons/sun.js +40 -0
- package/dist/icons/support.d.ts +2 -0
- package/dist/icons/support.js +48 -0
- package/dist/icons/switch.d.ts +2 -0
- package/dist/icons/switch.js +34 -0
- package/dist/icons/tablet.d.ts +2 -0
- package/dist/icons/tablet.js +44 -0
- package/dist/icons/tape-library.d.ts +2 -0
- package/dist/icons/tape-library.js +48 -0
- package/dist/icons/temple-donation.d.ts +2 -0
- package/dist/icons/temple-donation.js +60 -0
- package/dist/icons/tent.d.ts +2 -0
- package/dist/icons/tent.js +66 -0
- package/dist/icons/terminal.d.ts +2 -0
- package/dist/icons/terminal.js +56 -0
- package/dist/icons/thai-house.d.ts +2 -0
- package/dist/icons/thai-house.js +85 -0
- package/dist/icons/thai-lantern.d.ts +2 -0
- package/dist/icons/thai-lantern.js +61 -0
- package/dist/icons/thinkpad-l380.d.ts +2 -0
- package/dist/icons/thinkpad-l380.js +54 -0
- package/dist/icons/timer.d.ts +2 -0
- package/dist/icons/timer.js +59 -0
- package/dist/icons/train-ticket.d.ts +2 -0
- package/dist/icons/train-ticket.js +57 -0
- package/dist/icons/trash.d.ts +2 -0
- package/dist/icons/trash.js +62 -0
- package/dist/icons/tree.d.ts +2 -0
- package/dist/icons/tree.js +40 -0
- package/dist/icons/tuk-tuk.d.ts +2 -0
- package/dist/icons/tuk-tuk.js +45 -0
- package/dist/icons/tulip.d.ts +2 -0
- package/dist/icons/tulip.js +56 -0
- package/dist/icons/tunnel.d.ts +2 -0
- package/dist/icons/tunnel.js +50 -0
- package/dist/icons/two-factor.d.ts +2 -0
- package/dist/icons/two-factor.js +81 -0
- package/dist/icons/ups.d.ts +2 -0
- package/dist/icons/ups.js +37 -0
- package/dist/icons/usb-drive.d.ts +2 -0
- package/dist/icons/usb-drive.js +35 -0
- package/dist/icons/verified.d.ts +2 -0
- package/dist/icons/verified.js +32 -0
- package/dist/icons/virtual-machine.d.ts +2 -0
- package/dist/icons/virtual-machine.js +66 -0
- package/dist/icons/volume.d.ts +6 -0
- package/dist/icons/volume.js +35 -0
- package/dist/icons/vpn-gateway.d.ts +2 -0
- package/dist/icons/vpn-gateway.js +51 -0
- package/dist/icons/wake-on-lan.d.ts +2 -0
- package/dist/icons/wake-on-lan.js +40 -0
- package/dist/icons/wallet.d.ts +3 -0
- package/dist/icons/wallet.js +55 -0
- package/dist/icons/water-jar.d.ts +2 -0
- package/dist/icons/water-jar.js +72 -0
- package/dist/icons/wooden-window.d.ts +2 -0
- package/dist/icons/wooden-window.js +65 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/dist/iso.d.ts +32 -0
- package/dist/iso.js +64 -0
- package/dist/load.d.ts +199 -0
- package/dist/load.js +199 -0
- package/dist/motions.d.ts +68 -0
- package/dist/motions.js +446 -0
- package/dist/sticker.d.ts +269 -0
- package/dist/sticker.js +1009 -0
- package/package.json +101 -4
package/dist/sticker.js
ADDED
|
@@ -0,0 +1,1009 @@
|
|
|
1
|
+
// baht-sticker runtime: slab stickers drawn on canvas, keyed the way lottie-web plays After Effects keys.
|
|
2
|
+
// Derived from fun-isometric by Kelvin Adithya (MIT, see LICENSE); palettes follow the 20baht tokens.
|
|
3
|
+
export const ACCENTS = ['main', 'gold', 'tint', 'ok', 'hot', 'deep'];
|
|
4
|
+
/** ฿20, the default ticket-wallet theme: blue action, carrier navy, amber, success green, red. */
|
|
5
|
+
export const BAHT = {
|
|
6
|
+
main: '#2e4a9e',
|
|
7
|
+
gold: '#fbbf24',
|
|
8
|
+
tint: '#e8ecf6',
|
|
9
|
+
ok: '#1f7a4d',
|
|
10
|
+
hot: '#c8202f',
|
|
11
|
+
deep: '#1c2a55',
|
|
12
|
+
white: '#ffffff',
|
|
13
|
+
grey: '#d4d4d4',
|
|
14
|
+
paper: '#fafafa',
|
|
15
|
+
ink: '#0a0a0a',
|
|
16
|
+
};
|
|
17
|
+
/** The banknote themes of apps/docs banknotes.css: main is the note's 500, deep its 800, tint its 100. */
|
|
18
|
+
export const PALETTES = {
|
|
19
|
+
baht: BAHT,
|
|
20
|
+
note50: { ...BAHT, main: '#3465a8', deep: '#173458', tint: '#d6e2f1' },
|
|
21
|
+
note100: { ...BAHT, main: '#bc3c29', deep: '#661e15', tint: '#f6d6d1', hot: '#d97706' },
|
|
22
|
+
note500: { ...BAHT, main: '#6f3fa4', deep: '#391d56', tint: '#e4d7f1' },
|
|
23
|
+
note1000: { ...BAHT, main: '#916a2c', deep: '#4b3618', tint: '#ebdcc8' },
|
|
24
|
+
};
|
|
25
|
+
/** Which way flat faces turn, as the layer tilt and icon extrude that draw it. `right` is the house view. */
|
|
26
|
+
export const VIEWS = {
|
|
27
|
+
right: { tilt: [-18, 36], extrude: [-4, 18] },
|
|
28
|
+
left: { tilt: [18, 36], extrude: [4, 18] },
|
|
29
|
+
front: { tilt: [0, 30], extrude: [0, 18] },
|
|
30
|
+
flat: { tilt: [-45, 55], extrude: [0, 20] },
|
|
31
|
+
};
|
|
32
|
+
export const FPS = 60;
|
|
33
|
+
const DEG = Math.PI / 180;
|
|
34
|
+
const mod = (a, n) => ((a % n) + n) % n;
|
|
35
|
+
const r2 = (v) => +v.toFixed(2);
|
|
36
|
+
const pt = (p) => `${String(r2(p[0]))} ${String(r2(p[1]))}`;
|
|
37
|
+
/** Stores into a cache, emptying it first once it passes `max`, so long sessions stay bounded. */
|
|
38
|
+
function remember(cache, key, value, max = 4000) {
|
|
39
|
+
if (cache.size > max)
|
|
40
|
+
cache.clear();
|
|
41
|
+
cache.set(key, value);
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
// ---------- Easing: bezier-easing as lottie-web solves it (Newton steps, bisection when flat) ----------
|
|
45
|
+
const curves = new Map();
|
|
46
|
+
export function bezier(x1, y1, x2, y2) {
|
|
47
|
+
const key = `${String(x1)},${String(y1)},${String(x2)},${String(y2)}`;
|
|
48
|
+
const hit = curves.get(key);
|
|
49
|
+
if (hit)
|
|
50
|
+
return hit;
|
|
51
|
+
const [ax, bx, cx] = [1 + 3 * x1 - 3 * x2, 3 * x2 - 6 * x1, 3 * x1];
|
|
52
|
+
const [ay, by, cy] = [1 + 3 * y1 - 3 * y2, 3 * y2 - 6 * y1, 3 * y1];
|
|
53
|
+
const sampleX = (s) => ((ax * s + bx) * s + cx) * s;
|
|
54
|
+
const sampleY = (s) => ((ay * s + by) * s + cy) * s;
|
|
55
|
+
const slopeX = (s) => (3 * ax * s + 2 * bx) * s + cx;
|
|
56
|
+
const ease = (x) => {
|
|
57
|
+
if (x <= 0)
|
|
58
|
+
return 0;
|
|
59
|
+
if (x >= 1)
|
|
60
|
+
return 1;
|
|
61
|
+
if (x1 === y1 && x2 === y2)
|
|
62
|
+
return x;
|
|
63
|
+
let s = x;
|
|
64
|
+
for (let i = 0; i < 8; i++) {
|
|
65
|
+
const err = sampleX(s) - x;
|
|
66
|
+
const slope = slopeX(s);
|
|
67
|
+
if (Math.abs(err) < 1e-7)
|
|
68
|
+
return sampleY(s);
|
|
69
|
+
if (Math.abs(slope) < 1e-6)
|
|
70
|
+
break;
|
|
71
|
+
s -= err / slope;
|
|
72
|
+
}
|
|
73
|
+
let [lo, hi] = [0, 1];
|
|
74
|
+
s = x;
|
|
75
|
+
for (let i = 0; i < 40 && Math.abs(sampleX(s) - x) > 1e-7; i++) {
|
|
76
|
+
if (sampleX(s) < x)
|
|
77
|
+
lo = s;
|
|
78
|
+
else
|
|
79
|
+
hi = s;
|
|
80
|
+
s = (lo + hi) / 2;
|
|
81
|
+
}
|
|
82
|
+
return sampleY(s);
|
|
83
|
+
};
|
|
84
|
+
return remember(curves, key, ease);
|
|
85
|
+
}
|
|
86
|
+
// ---------- Interpolation ----------
|
|
87
|
+
const NUM = /-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi;
|
|
88
|
+
const parsed = new Map();
|
|
89
|
+
/** Path data split into the text between numbers and the numbers, so it morphs number by number. */
|
|
90
|
+
function parse(d) {
|
|
91
|
+
return parsed.get(d) ?? remember(parsed, d, [d.split(NUM), (d.match(NUM) ?? []).map(Number)]);
|
|
92
|
+
}
|
|
93
|
+
/** Path data morphed number by number; the command letters come from the left key. */
|
|
94
|
+
function mixPath(a, b, y) {
|
|
95
|
+
if (y === 0 || a === b)
|
|
96
|
+
return a;
|
|
97
|
+
const [bits, na] = parse(a);
|
|
98
|
+
const nb = parse(b)[1];
|
|
99
|
+
let out = bits[0] ?? '';
|
|
100
|
+
na.forEach((n, i) => {
|
|
101
|
+
out += String(+(n + ((nb[i] ?? n) - n) * y).toFixed(3)) + (bits[i + 1] ?? '');
|
|
102
|
+
});
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
function mix(a, b, y) {
|
|
106
|
+
if (typeof a === 'number')
|
|
107
|
+
return (a + (b - a) * y);
|
|
108
|
+
if (typeof a === 'string')
|
|
109
|
+
return mixPath(a, b, y);
|
|
110
|
+
const [va, vb] = [a, b];
|
|
111
|
+
return [va[0] + (vb[0] - va[0]) * y, va[1] + (vb[1] - va[1]) * y];
|
|
112
|
+
}
|
|
113
|
+
const isTrack = (v) => Array.isArray(v) && Array.isArray(v[0]);
|
|
114
|
+
const ARC_STEPS = 150;
|
|
115
|
+
const arcs = new WeakMap();
|
|
116
|
+
/** A curved segment walked by distance, as lottie-web does: eased progress is a share of its length. */
|
|
117
|
+
function along(key, a, b, y) {
|
|
118
|
+
let hit = arcs.get(key);
|
|
119
|
+
if (!hit) {
|
|
120
|
+
const [to, ti] = key[3] ?? [
|
|
121
|
+
[0, 0],
|
|
122
|
+
[0, 0],
|
|
123
|
+
];
|
|
124
|
+
const c1 = [a[0] + to[0], a[1] + to[1]];
|
|
125
|
+
const c2 = [b[0] + ti[0], b[1] + ti[1]];
|
|
126
|
+
const pts = [];
|
|
127
|
+
const lens = [];
|
|
128
|
+
let prev = a;
|
|
129
|
+
let total = 0;
|
|
130
|
+
for (let i = 0; i <= ARC_STEPS; i++) {
|
|
131
|
+
const s = i / ARC_STEPS;
|
|
132
|
+
const u = 1 - s;
|
|
133
|
+
const [w0, w1, w2, w3] = [u * u * u, 3 * u * u * s, 3 * u * s * s, s * s * s];
|
|
134
|
+
const p = [
|
|
135
|
+
w0 * a[0] + w1 * c1[0] + w2 * c2[0] + w3 * b[0],
|
|
136
|
+
w0 * a[1] + w1 * c1[1] + w2 * c2[1] + w3 * b[1],
|
|
137
|
+
];
|
|
138
|
+
total += i ? Math.hypot(p[0] - prev[0], p[1] - prev[1]) : 0;
|
|
139
|
+
pts.push(p);
|
|
140
|
+
lens.push(total);
|
|
141
|
+
prev = p;
|
|
142
|
+
}
|
|
143
|
+
hit = { pts, lens };
|
|
144
|
+
arcs.set(key, hit);
|
|
145
|
+
}
|
|
146
|
+
const { pts, lens } = hit;
|
|
147
|
+
const total = lens[ARC_STEPS] ?? 0;
|
|
148
|
+
const goal = total * y;
|
|
149
|
+
if (goal <= 0)
|
|
150
|
+
return pts[0] ?? a;
|
|
151
|
+
if (goal >= total)
|
|
152
|
+
return pts[ARC_STEPS] ?? b;
|
|
153
|
+
let i = 1;
|
|
154
|
+
while ((lens[i] ?? total) < goal)
|
|
155
|
+
i++;
|
|
156
|
+
const [l0, l1] = [lens[i - 1] ?? 0, lens[i] ?? total];
|
|
157
|
+
const [p0, p1] = [pts[i - 1] ?? a, pts[i] ?? b];
|
|
158
|
+
const k = (goal - l0) / (l1 - l0 || 1);
|
|
159
|
+
return [p0[0] + (p1[0] - p0[0]) * k, p0[1] + (p1[1] - p0[1]) * k];
|
|
160
|
+
}
|
|
161
|
+
/** A track's value at frame f: held before the first key and after the last, eased per segment between. */
|
|
162
|
+
export function at(v, f) {
|
|
163
|
+
if (!isTrack(v))
|
|
164
|
+
return v;
|
|
165
|
+
const first = v[0];
|
|
166
|
+
const last = v[v.length - 1];
|
|
167
|
+
if (!first || !last)
|
|
168
|
+
throw new Error('baht-sticker: empty track');
|
|
169
|
+
if (f <= first[0])
|
|
170
|
+
return first[1];
|
|
171
|
+
for (let i = 0; i < v.length - 1; i++) {
|
|
172
|
+
const key = v[i];
|
|
173
|
+
const next = v[i + 1];
|
|
174
|
+
if (!key || !next || f >= next[0])
|
|
175
|
+
continue;
|
|
176
|
+
const [t0, a, e, bend] = key;
|
|
177
|
+
if (e === 0)
|
|
178
|
+
return a;
|
|
179
|
+
const x = (f - t0) / (next[0] - t0);
|
|
180
|
+
const y = e ? bezier(e[0], e[1], e[2], e[3])(x) : x;
|
|
181
|
+
return bend ? along(key, a, next[1], y) : mix(a, next[1], y);
|
|
182
|
+
}
|
|
183
|
+
return last[1];
|
|
184
|
+
}
|
|
185
|
+
/** Shifts a track along the timeline, for staggering one preset across parts. */
|
|
186
|
+
export const shift = (track, by) => track.map(([t, v, e, bend]) => [t + by, v, e, bend]);
|
|
187
|
+
const mul = (m, n) => [
|
|
188
|
+
m[0] * n[0] + m[2] * n[1],
|
|
189
|
+
m[1] * n[0] + m[3] * n[1],
|
|
190
|
+
m[0] * n[2] + m[2] * n[3],
|
|
191
|
+
m[1] * n[2] + m[3] * n[3],
|
|
192
|
+
m[0] * n[4] + m[2] * n[5] + m[4],
|
|
193
|
+
m[1] * n[4] + m[3] * n[5] + m[5],
|
|
194
|
+
];
|
|
195
|
+
/** A tilt's 2x2: spin in plane, then squash by the lean. */
|
|
196
|
+
function lean([spin, by]) {
|
|
197
|
+
const [a, k] = [spin * DEG, Math.cos(by * DEG)];
|
|
198
|
+
return [Math.cos(a), Math.sin(a) * k, -Math.sin(a), Math.cos(a) * k, 0, 0];
|
|
199
|
+
}
|
|
200
|
+
/** Where a point drawn on a face with this tilt lands in the layer's parent, for pivots and sparkles. */
|
|
201
|
+
export function onFace(tilt, [x, y]) {
|
|
202
|
+
const t = lean(tilt);
|
|
203
|
+
return [r2(t[0] * x + t[2] * y), r2(t[1] * x + t[3] * y)];
|
|
204
|
+
}
|
|
205
|
+
function motionsOf({ m }) {
|
|
206
|
+
if (!m)
|
|
207
|
+
return [];
|
|
208
|
+
return Array.isArray(m) ? m : [m];
|
|
209
|
+
}
|
|
210
|
+
function poseOf(L, f, frames) {
|
|
211
|
+
let p = at(L.p ?? [0, 0], f);
|
|
212
|
+
let r = at(L.r ?? 0, f);
|
|
213
|
+
let s = at(L.s ?? [100, 100], f);
|
|
214
|
+
let o = at(L.o ?? 100, f);
|
|
215
|
+
let x = 0;
|
|
216
|
+
for (const m of motionsOf(L)) {
|
|
217
|
+
const g = mod(f - (m.at ?? 0), m.loop ?? frames);
|
|
218
|
+
const k = m.k ?? 1;
|
|
219
|
+
if (m.r)
|
|
220
|
+
r += at(m.r, g) * k;
|
|
221
|
+
if (m.p) {
|
|
222
|
+
const d = at(m.p, g);
|
|
223
|
+
p = [p[0] + d[0] * k, p[1] + d[1] * k];
|
|
224
|
+
}
|
|
225
|
+
if (m.s) {
|
|
226
|
+
const d = at(m.s, g);
|
|
227
|
+
s = [(s[0] * (100 + (d[0] - 100) * k)) / 100, (s[1] * (100 + (d[1] - 100) * k)) / 100];
|
|
228
|
+
}
|
|
229
|
+
if (m.o)
|
|
230
|
+
o *= at(m.o, g) / 100;
|
|
231
|
+
if (m.x)
|
|
232
|
+
x += at(m.x, g) * k;
|
|
233
|
+
}
|
|
234
|
+
const a = L.a ?? [0, 0];
|
|
235
|
+
const [cos, sin, sx, sy] = [Math.cos(r * DEG), Math.sin(r * DEG), s[0] / 100, s[1] / 100];
|
|
236
|
+
// translate(p) rotate(r) scale(s) tilt translate(-a): the order After Effects composes a layer in, tilt added
|
|
237
|
+
let m = [cos * sx, sin * sx, -sin * sy, cos * sy, 0, 0];
|
|
238
|
+
const k = L.tilt ? Math.cos(L.tilt[1] * DEG) : 1;
|
|
239
|
+
if (L.tilt)
|
|
240
|
+
m = mul(m, lean(L.tilt));
|
|
241
|
+
m[4] = p[0] - (m[0] * a[0] + m[2] * a[1]);
|
|
242
|
+
m[5] = p[1] - (m[1] * a[0] + m[3] * a[1]);
|
|
243
|
+
return { m, o, x, k };
|
|
244
|
+
}
|
|
245
|
+
// ---------- Drawing ----------
|
|
246
|
+
const paths = new Map();
|
|
247
|
+
const pathOf = (d) => paths.get(d) ?? remember(paths, d, new Path2D(d));
|
|
248
|
+
const samples = new Map();
|
|
249
|
+
/** An open path's on-screen length, for trims: sampled once, then measured through the layer matrix. */
|
|
250
|
+
function lengthOf(d, m) {
|
|
251
|
+
let pts = samples.get(d);
|
|
252
|
+
if (!pts) {
|
|
253
|
+
const el = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
254
|
+
el.setAttribute('d', d);
|
|
255
|
+
const l = el.getTotalLength();
|
|
256
|
+
pts = remember(samples, d, Array.from({ length: 65 }, (_, i) => {
|
|
257
|
+
const q = el.getPointAtLength((l * i) / 64);
|
|
258
|
+
return [q.x, q.y];
|
|
259
|
+
}));
|
|
260
|
+
}
|
|
261
|
+
let sum = 0;
|
|
262
|
+
pts.forEach((q, i) => {
|
|
263
|
+
const prev = pts[i - 1];
|
|
264
|
+
if (!prev)
|
|
265
|
+
return;
|
|
266
|
+
const [dx, dy] = [q[0] - prev[0], q[1] - prev[1]];
|
|
267
|
+
sum += Math.hypot(m[0] * dx + m[2] * dy, m[1] * dx + m[3] * dy);
|
|
268
|
+
});
|
|
269
|
+
return sum;
|
|
270
|
+
}
|
|
271
|
+
const ids = new WeakMap();
|
|
272
|
+
/** An icon's layers by id, for parents and mattes: built once per layer list, not once per frame. */
|
|
273
|
+
function layersById(layers) {
|
|
274
|
+
let byId = ids.get(layers);
|
|
275
|
+
if (!byId) {
|
|
276
|
+
byId = new Map();
|
|
277
|
+
for (const L of layers)
|
|
278
|
+
if (L.id)
|
|
279
|
+
byId.set(L.id, L);
|
|
280
|
+
ids.set(layers, byId);
|
|
281
|
+
}
|
|
282
|
+
return byId;
|
|
283
|
+
}
|
|
284
|
+
function scene(icon, frame) {
|
|
285
|
+
const f = mod(frame, icon.frames);
|
|
286
|
+
const byId = layersById(icon.layers);
|
|
287
|
+
const memo = new Map();
|
|
288
|
+
const world = (L) => {
|
|
289
|
+
let hit = memo.get(L);
|
|
290
|
+
if (!hit) {
|
|
291
|
+
hit = poseOf(L, f, icon.frames);
|
|
292
|
+
const up = L.parent ? byId.get(L.parent) : undefined;
|
|
293
|
+
if (up) {
|
|
294
|
+
const parent = world(up);
|
|
295
|
+
hit = { ...hit, m: mul(parent.m, hit.m), k: parent.k * hit.k };
|
|
296
|
+
}
|
|
297
|
+
memo.set(L, hit);
|
|
298
|
+
}
|
|
299
|
+
return hit;
|
|
300
|
+
};
|
|
301
|
+
const parts = [];
|
|
302
|
+
for (const L of icon.layers) {
|
|
303
|
+
const hidden = (L.ip !== undefined && f < L.ip) || (L.op !== undefined && f >= L.op);
|
|
304
|
+
if (hidden || !L.shapes.length)
|
|
305
|
+
continue;
|
|
306
|
+
const pose = world(L);
|
|
307
|
+
if (pose.o <= 0)
|
|
308
|
+
continue;
|
|
309
|
+
parts.push({
|
|
310
|
+
L,
|
|
311
|
+
pose,
|
|
312
|
+
ds: L.shapes.map((S) => at(S.d, f)),
|
|
313
|
+
trims: L.shapes.map((S) => (S.trim === undefined ? 1 : at(S.trim, f))),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return { f, ext: at(icon.extrude ?? [-4, 14], f), parts, world, byId };
|
|
317
|
+
}
|
|
318
|
+
/** What a scene would paint, as text, so a player skips the frames of a still hold. */
|
|
319
|
+
function fingerprint({ ext, parts }) {
|
|
320
|
+
let out = `${ext[0].toFixed(2)},${ext[1].toFixed(2)}`;
|
|
321
|
+
for (const { L, pose, ds, trims } of parts) {
|
|
322
|
+
out += `|${pose.m.map((v) => v.toFixed(2)).join()},${pose.o.toFixed(1)},${pose.x.toFixed(2)}`;
|
|
323
|
+
L.shapes.forEach((S, i) => {
|
|
324
|
+
if (typeof S.d !== 'string')
|
|
325
|
+
out += `;${ds[i] ?? ''}`;
|
|
326
|
+
if (S.trim !== undefined)
|
|
327
|
+
out += `;${(trims[i] ?? 1).toFixed(3)}`;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
return out;
|
|
331
|
+
}
|
|
332
|
+
/** A filled path with its outline: stroked at 2w then filled, which leaves one w of ink outside the edge. */
|
|
333
|
+
function fillOutlined(pen, path, w, fill) {
|
|
334
|
+
const { ctx } = pen;
|
|
335
|
+
if (w) {
|
|
336
|
+
ctx.strokeStyle = pen.ink;
|
|
337
|
+
ctx.lineWidth = w * 2;
|
|
338
|
+
ctx.stroke(path);
|
|
339
|
+
}
|
|
340
|
+
ctx.fillStyle = fill;
|
|
341
|
+
ctx.fill(path);
|
|
342
|
+
}
|
|
343
|
+
/** Creases down the corners where two visible side faces meet, which is what makes a slab read solid. */
|
|
344
|
+
function drawCreases(pen, S, d, m, v, w) {
|
|
345
|
+
const auto = S.crease === false ? undefined : corners.get(d);
|
|
346
|
+
if (!w || (!S.edges && !auto))
|
|
347
|
+
return;
|
|
348
|
+
const { ctx } = pen;
|
|
349
|
+
const [vx, vy] = v;
|
|
350
|
+
const vl = Math.hypot(vx, vy) || 1;
|
|
351
|
+
const flip = Math.sign(m[0] * m[3] - m[1] * m[2]);
|
|
352
|
+
// edge normals go through the inverse transpose, so they stay true on a sheared face
|
|
353
|
+
const faces = (nx, ny) => {
|
|
354
|
+
const wx = (m[3] * nx - m[1] * ny) * flip;
|
|
355
|
+
const wy = (m[0] * ny - m[2] * nx) * flip;
|
|
356
|
+
return (wx * vx + wy * vy) / (Math.hypot(wx, wy) * vl) > 0.05;
|
|
357
|
+
};
|
|
358
|
+
const lines = [...(S.edges ?? [])];
|
|
359
|
+
for (const [cx, cy, ax, ay, bx, by] of auto ?? []) {
|
|
360
|
+
if (faces(ax, ay) && faces(bx, by))
|
|
361
|
+
lines.push([cx, cy]);
|
|
362
|
+
}
|
|
363
|
+
ctx.strokeStyle = pen.ink;
|
|
364
|
+
ctx.lineWidth = w;
|
|
365
|
+
ctx.beginPath();
|
|
366
|
+
for (const [ex, ey] of lines) {
|
|
367
|
+
const [px, py] = [m[0] * ex + m[2] * ey + m[4], m[1] * ex + m[3] * ey + m[5]];
|
|
368
|
+
ctx.moveTo(px, py);
|
|
369
|
+
ctx.lineTo(px + vx, py + vy);
|
|
370
|
+
}
|
|
371
|
+
ctx.stroke();
|
|
372
|
+
}
|
|
373
|
+
/** The slab: the face swept along the extrusion, copies under a device pixel apart so it reads solid. */
|
|
374
|
+
function drawSlab(pen, S, d, path, pose, w) {
|
|
375
|
+
const kk = (S.depth ?? 0) + pose.x / pen.len;
|
|
376
|
+
const v = [pen.ext[0] * kk, pen.ext[1] * kk];
|
|
377
|
+
const n = Math.min(64, Math.max(2, Math.ceil((Math.hypot(v[0], v[1]) * pen.dev) / 0.8)));
|
|
378
|
+
const sweep = new Path2D();
|
|
379
|
+
for (let j = n; j >= 0; j--) {
|
|
380
|
+
pen.step.e = (v[0] * j) / n;
|
|
381
|
+
pen.step.f = (v[1] * j) / n;
|
|
382
|
+
sweep.addPath(path, pen.step);
|
|
383
|
+
}
|
|
384
|
+
fillOutlined(pen, sweep, w, pen.colour(S.side ?? S.fill));
|
|
385
|
+
drawCreases(pen, S, d, pose.m, v, w);
|
|
386
|
+
}
|
|
387
|
+
/** An open path's ink, trimmed to its drawn share. */
|
|
388
|
+
function drawStroke(pen, path, d, m, w, trim) {
|
|
389
|
+
if (!w || trim <= 0)
|
|
390
|
+
return;
|
|
391
|
+
const { ctx } = pen;
|
|
392
|
+
ctx.strokeStyle = pen.ink;
|
|
393
|
+
ctx.lineWidth = w;
|
|
394
|
+
if (trim < 1) {
|
|
395
|
+
const l = lengthOf(d, m);
|
|
396
|
+
ctx.setLineDash([l * trim, l]);
|
|
397
|
+
}
|
|
398
|
+
ctx.stroke(path);
|
|
399
|
+
ctx.setLineDash([]);
|
|
400
|
+
}
|
|
401
|
+
function drawShape(pen, S, d, trim, pose, width, scale = 1) {
|
|
402
|
+
const { ctx } = pen;
|
|
403
|
+
const { m, k } = pose;
|
|
404
|
+
// paths go to canvas space before stroking, so a leaning face keeps even outlines that still scale
|
|
405
|
+
const grow = Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]) / Math.abs(k || 1));
|
|
406
|
+
const path = new Path2D();
|
|
407
|
+
path.addPath(pathOf(d), new DOMMatrix(m));
|
|
408
|
+
const w = (S.line === false ? 0 : typeof S.line === 'number' ? S.line * scale : width) * grow;
|
|
409
|
+
ctx.setTransform(pen.base);
|
|
410
|
+
ctx.lineCap = S.round ? 'round' : 'butt';
|
|
411
|
+
ctx.lineJoin = S.round ? 'round' : 'miter';
|
|
412
|
+
const { fill } = S;
|
|
413
|
+
if (fill === undefined) {
|
|
414
|
+
drawStroke(pen, path, d, m, w, trim);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (S.depth)
|
|
418
|
+
drawSlab(pen, { ...S, fill }, d, path, pose, w);
|
|
419
|
+
fillOutlined(pen, path, w, pen.colour(fill));
|
|
420
|
+
}
|
|
421
|
+
/** Clips to a matte layer's shapes, like an After Effects track matte. */
|
|
422
|
+
function clipTo(pen, mask, sc) {
|
|
423
|
+
const region = new Path2D();
|
|
424
|
+
const mm = new DOMMatrix(sc.world(mask).m);
|
|
425
|
+
for (const s of mask.shapes)
|
|
426
|
+
region.addPath(pathOf(at(s.d, sc.f)), mm);
|
|
427
|
+
pen.ctx.save();
|
|
428
|
+
pen.ctx.setTransform(pen.base);
|
|
429
|
+
pen.ctx.clip(region);
|
|
430
|
+
}
|
|
431
|
+
function render(ctx, icon, sc, palette, line = 1) {
|
|
432
|
+
const base = ctx.getTransform();
|
|
433
|
+
const colour = (c) => (c.startsWith('#') ? c : palette[c]);
|
|
434
|
+
const pen = {
|
|
435
|
+
ctx,
|
|
436
|
+
base,
|
|
437
|
+
ink: colour(icon.ink ?? 'ink'),
|
|
438
|
+
colour,
|
|
439
|
+
ext: sc.ext,
|
|
440
|
+
len: Math.hypot(sc.ext[0], sc.ext[1]) || 1,
|
|
441
|
+
dev: Math.sqrt(Math.abs(base.a * base.d - base.b * base.c)) || 1,
|
|
442
|
+
step: new DOMMatrix(),
|
|
443
|
+
};
|
|
444
|
+
ctx.save();
|
|
445
|
+
ctx.miterLimit = 4;
|
|
446
|
+
for (const { L, pose, ds, trims } of sc.parts) {
|
|
447
|
+
ctx.globalAlpha = Math.min(1, pose.o / 100);
|
|
448
|
+
const mask = L.clip ? sc.byId.get(L.clip) : undefined;
|
|
449
|
+
if (mask)
|
|
450
|
+
clipTo(pen, mask, sc);
|
|
451
|
+
L.shapes.forEach((S, i) => {
|
|
452
|
+
drawShape(pen, S, ds[i] ?? '', trims[i] ?? 1, pose, (icon.line ?? 4) * line, line);
|
|
453
|
+
});
|
|
454
|
+
if (mask)
|
|
455
|
+
ctx.restore();
|
|
456
|
+
}
|
|
457
|
+
ctx.restore();
|
|
458
|
+
}
|
|
459
|
+
const themed = new Map();
|
|
460
|
+
/** A partial palette laid over BAHT, kept per distinct palette so a running sticker does not rebuild it. */
|
|
461
|
+
function resolve(p) {
|
|
462
|
+
if (!p)
|
|
463
|
+
return BAHT;
|
|
464
|
+
const key = JSON.stringify(p);
|
|
465
|
+
return themed.get(key) ?? remember(themed, key, { ...BAHT, ...p }, 200);
|
|
466
|
+
}
|
|
467
|
+
/** Paints frame f of an icon into ctx, whose current transform maps icon units (0..size) to its box; `line` scales every outline. */
|
|
468
|
+
export function draw(ctx, icon, frame, palette, line = 1) {
|
|
469
|
+
render(ctx, icon, scene(icon, frame), resolve(palette), line);
|
|
470
|
+
}
|
|
471
|
+
const lin = (c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
|
|
472
|
+
const gam = (c) => (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055);
|
|
473
|
+
function lch(color) {
|
|
474
|
+
const full = color.length === 4 ? color.replace(/[0-9a-f]/gi, (c) => c + c) : color;
|
|
475
|
+
const n = parseInt(full.slice(1, 7), 16);
|
|
476
|
+
const [r, g, b] = [
|
|
477
|
+
lin(((n >> 16) & 255) / 255),
|
|
478
|
+
lin(((n >> 8) & 255) / 255),
|
|
479
|
+
lin((n & 255) / 255),
|
|
480
|
+
];
|
|
481
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
482
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
483
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
484
|
+
const A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s;
|
|
485
|
+
const B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s;
|
|
486
|
+
return [
|
|
487
|
+
0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
488
|
+
Math.hypot(A, B),
|
|
489
|
+
Math.atan2(B, A) / DEG,
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
/** Back to hex, easing chroma down until the colour fits sRGB. */
|
|
493
|
+
function hex([L, C, H]) {
|
|
494
|
+
const rgb = (c) => {
|
|
495
|
+
const [A, B] = [c * Math.cos(H * DEG), c * Math.sin(H * DEG)];
|
|
496
|
+
const l = (L + 0.3963377774 * A + 0.2158037573 * B) ** 3;
|
|
497
|
+
const m = (L - 0.1055613458 * A - 0.0638541728 * B) ** 3;
|
|
498
|
+
const s = (L - 0.0894841775 * A - 1.291485548 * B) ** 3;
|
|
499
|
+
return [
|
|
500
|
+
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
|
|
501
|
+
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
|
|
502
|
+
-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
|
|
503
|
+
];
|
|
504
|
+
};
|
|
505
|
+
const fits = (c) => rgb(c).every((v) => v >= -1e-4 && v <= 1 + 1e-4);
|
|
506
|
+
let [lo, hi] = [0, C];
|
|
507
|
+
if (fits(C))
|
|
508
|
+
lo = C;
|
|
509
|
+
else {
|
|
510
|
+
for (let i = 0; i < 24; i++) {
|
|
511
|
+
const mid = (lo + hi) / 2;
|
|
512
|
+
if (fits(mid))
|
|
513
|
+
lo = mid;
|
|
514
|
+
else
|
|
515
|
+
hi = mid;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const byte = (v) => Math.round(Math.min(1, Math.max(0, gam(v))) * 255)
|
|
519
|
+
.toString(16)
|
|
520
|
+
.padStart(2, '0');
|
|
521
|
+
return `#${rgb(lo).map(byte).join('')}`;
|
|
522
|
+
}
|
|
523
|
+
/** Every accent turned `deg` around the hue wheel, each keeping its own lightness and strength. */
|
|
524
|
+
export function hue(deg, from = BAHT) {
|
|
525
|
+
const out = { ...from };
|
|
526
|
+
for (const k of ACCENTS) {
|
|
527
|
+
const [L, C, H] = lch(from[k]);
|
|
528
|
+
out[k] = hex([L, C, H + deg]);
|
|
529
|
+
}
|
|
530
|
+
return out;
|
|
531
|
+
}
|
|
532
|
+
/** Two brand colours fill every accent: primary on bodies, tints and hearts; secondary on dark parts, coins and checks. */
|
|
533
|
+
export function brand(primary, secondary, from = BAHT) {
|
|
534
|
+
const [, C, H] = lch(primary);
|
|
535
|
+
const two = secondary ?? hex([0.42, C, H]);
|
|
536
|
+
const [, C2, H2] = lch(two);
|
|
537
|
+
return {
|
|
538
|
+
...from,
|
|
539
|
+
main: primary,
|
|
540
|
+
tint: hex([0.93, Math.min(C, 0.06), H]),
|
|
541
|
+
hot: hex([0.6, C, H]),
|
|
542
|
+
deep: two,
|
|
543
|
+
gold: hex([0.86, Math.min(C2, 0.12), H2]),
|
|
544
|
+
ok: hex([0.7, C2, H2]),
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
const MONO_STEPS = {
|
|
548
|
+
tint: 0.94,
|
|
549
|
+
gold: 0.81,
|
|
550
|
+
ok: 0.72,
|
|
551
|
+
main: 0.64,
|
|
552
|
+
hot: 0.55,
|
|
553
|
+
deep: 0.45,
|
|
554
|
+
};
|
|
555
|
+
/** One hue at six lightnesses; parts stay apart by lightness and the outlines do the rest. */
|
|
556
|
+
export function mono(color, from = BAHT) {
|
|
557
|
+
const [, C, H] = lch(color);
|
|
558
|
+
const out = { ...from };
|
|
559
|
+
for (const k of ACCENTS)
|
|
560
|
+
out[k] = hex([MONO_STEPS[k], Math.min(C, lch(from[k])[1]), H]);
|
|
561
|
+
return out;
|
|
562
|
+
}
|
|
563
|
+
/** The --baht-* token each accent reads, so a sticker follows the page's banknote theme and dark mode. */
|
|
564
|
+
export const TOKENS = {
|
|
565
|
+
main: '--baht-primary',
|
|
566
|
+
deep: '--baht-neutral',
|
|
567
|
+
tint: '--baht-color-primary-tint',
|
|
568
|
+
gold: '--baht-gold-400',
|
|
569
|
+
ok: '--baht-success',
|
|
570
|
+
hot: '--baht-error',
|
|
571
|
+
};
|
|
572
|
+
let probing;
|
|
573
|
+
/** The span and the 1px canvas every token read goes through, made once. */
|
|
574
|
+
function probeKit() {
|
|
575
|
+
if (probing)
|
|
576
|
+
return probing;
|
|
577
|
+
const probe = document.createElement('span');
|
|
578
|
+
probe.style.display = 'none';
|
|
579
|
+
const cv = document.createElement('canvas');
|
|
580
|
+
cv.width = cv.height = 1;
|
|
581
|
+
probing = { probe, ctx: cv.getContext('2d', { willReadFrequently: true }) };
|
|
582
|
+
return probing;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* The accents as `el` sees the --baht-* tokens right now, as hex. Unset tokens fall back to BAHT.
|
|
586
|
+
* Colours are resolved by the browser (var(), color-mix()) and read back through a 1px canvas.
|
|
587
|
+
*/
|
|
588
|
+
export function paletteFromTokens(el) {
|
|
589
|
+
const { probe, ctx } = probeKit();
|
|
590
|
+
el.appendChild(probe);
|
|
591
|
+
const out = { ...BAHT };
|
|
592
|
+
for (const k of ACCENTS) {
|
|
593
|
+
probe.style.color = '';
|
|
594
|
+
probe.style.color = `var(${TOKENS[k]}, ${BAHT[k]})`;
|
|
595
|
+
const css = getComputedStyle(probe).color;
|
|
596
|
+
if (!ctx || !css)
|
|
597
|
+
continue;
|
|
598
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
599
|
+
ctx.fillStyle = BAHT[k];
|
|
600
|
+
ctx.fillStyle = css;
|
|
601
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
602
|
+
const [r = 0, g = 0, b = 0] = ctx.getImageData(0, 0, 1, 1).data;
|
|
603
|
+
out[k] = `#${[r, g, b].map((v) => v.toString(16).padStart(2, '0')).join('')}`;
|
|
604
|
+
}
|
|
605
|
+
probe.remove();
|
|
606
|
+
return out;
|
|
607
|
+
}
|
|
608
|
+
// ---------- Shape kit: every builder returns SVG path data, so a part is one string and a morph key ----------
|
|
609
|
+
export const rect = (x, y, w, h, r = 0) => poly([
|
|
610
|
+
[x, y],
|
|
611
|
+
[x + w, y],
|
|
612
|
+
[x + w, y + h],
|
|
613
|
+
[x, y + h],
|
|
614
|
+
], r);
|
|
615
|
+
export const pill = (x, y, w, h) => rect(x, y, w, h, Math.min(w, h) / 2);
|
|
616
|
+
export const ellipse = (cx, cy, rx, ry = rx) => `M${pt([cx - rx, cy])}A${String(r2(rx))} ${String(r2(ry))} 0 1 1 ${pt([cx + rx, cy])}` +
|
|
617
|
+
`A${String(r2(rx))} ${String(r2(ry))} 0 1 1 ${pt([cx - rx, cy])}Z`;
|
|
618
|
+
// Never emptied, unlike the caches behind remember(): poly() runs once, when an icon's module is evaluated, so
|
|
619
|
+
// nothing would put an evicted shape's creases back.
|
|
620
|
+
const corners = new Map();
|
|
621
|
+
/** One filleted corner of a polygon: its path segment, and its crease when the corner is convex. */
|
|
622
|
+
function fillet(P, A, B, rad, area) {
|
|
623
|
+
const ua = [A[0] - P[0], A[1] - P[1]];
|
|
624
|
+
const ub = [B[0] - P[0], B[1] - P[1]];
|
|
625
|
+
const [la, lb] = [Math.hypot(ua[0], ua[1]), Math.hypot(ub[0], ub[1])];
|
|
626
|
+
const cos = (ua[0] * ub[0] + ua[1] * ub[1]) / (la * lb);
|
|
627
|
+
const half = Math.acos(Math.max(-1, Math.min(1, cos))) / 2;
|
|
628
|
+
const t = Math.min(rad && half > 1e-3 ? rad / Math.tan(half) : 0, la / 2, lb / 2);
|
|
629
|
+
const rr = t * Math.tan(half);
|
|
630
|
+
const T1 = [P[0] + (ua[0] / la) * t, P[1] + (ua[1] / la) * t];
|
|
631
|
+
const T2 = [P[0] + (ub[0] / lb) * t, P[1] + (ub[1] / lb) * t];
|
|
632
|
+
const cross = ua[0] * ub[1] - ua[1] * ub[0];
|
|
633
|
+
const seg = `${pt(T1)}A${String(r2(rr))} ${String(r2(rr))} 0 0 ${String(cross < 0 ? 1 : 0)} ${pt(T2)}`;
|
|
634
|
+
// a corner rounded into its neighbour (a pill end) turns smoothly and has no crease
|
|
635
|
+
const round = rad > 0 && t >= Math.min(la, lb) / 2 - 1e-6;
|
|
636
|
+
if (Math.sign(cross) !== -Math.sign(area) || round)
|
|
637
|
+
return { seg };
|
|
638
|
+
const bl = Math.hypot(ua[0] / la + ub[0] / lb, ua[1] / la + ub[1] / lb) || 1;
|
|
639
|
+
const [bx, by] = [(ua[0] / la + ub[0] / lb) / bl, (ua[1] / la + ub[1] / lb) / bl];
|
|
640
|
+
const k = half > 1e-3 ? rr / Math.sin(half) - rr : 0;
|
|
641
|
+
// each edge's normal, turned away from the bisector, which points inside
|
|
642
|
+
const out = (u, l) => {
|
|
643
|
+
const [nx, ny] = [-u[1] / l, u[0] / l];
|
|
644
|
+
return nx * bx + ny * by > 0 ? [-nx, -ny] : [nx, ny];
|
|
645
|
+
};
|
|
646
|
+
const [na, nb] = [out(ua, la), out(ub, lb)];
|
|
647
|
+
return { seg, mark: [P[0] + bx * k, P[1] + by * k, na[0], na[1], nb[0], nb[1]] };
|
|
648
|
+
}
|
|
649
|
+
/** Closed polygon with circular fillets, r per corner or one for all: the workhorse for sharp, friendly slabs. */
|
|
650
|
+
export function poly(points, r = 0) {
|
|
651
|
+
const n = points.length;
|
|
652
|
+
const pointAt = (i) => points[mod(i, n)] ?? [0, 0];
|
|
653
|
+
let area = 0;
|
|
654
|
+
for (let i = 0; i < n; i++) {
|
|
655
|
+
const [P, Q] = [pointAt(i), pointAt(i + 1)];
|
|
656
|
+
area += P[0] * Q[1] - Q[0] * P[1];
|
|
657
|
+
}
|
|
658
|
+
let d = '';
|
|
659
|
+
const marks = [];
|
|
660
|
+
for (let i = 0; i < n; i++) {
|
|
661
|
+
const rad = typeof r === 'number' ? r : (r[i] ?? 0);
|
|
662
|
+
const { seg, mark } = fillet(pointAt(i), pointAt(i - 1), pointAt(i + 1), rad, area);
|
|
663
|
+
d += (i ? 'L' : 'M') + seg;
|
|
664
|
+
if (mark)
|
|
665
|
+
marks.push(mark);
|
|
666
|
+
}
|
|
667
|
+
d += 'Z';
|
|
668
|
+
if (marks.length)
|
|
669
|
+
corners.set(d, marks);
|
|
670
|
+
return d;
|
|
671
|
+
}
|
|
672
|
+
/** Four point sparkle with concave sides; pinch 0 is a bare cross, about .3 a fat star. Morphs at any pinch. */
|
|
673
|
+
export function spark(cx, cy, r, pinch = 0.28) {
|
|
674
|
+
const q = r * pinch;
|
|
675
|
+
return (`M${pt([cx, cy - r])}Q${pt([cx + q, cy - q])} ${pt([cx + r, cy])}` +
|
|
676
|
+
`Q${pt([cx + q, cy + q])} ${pt([cx, cy + r])}Q${pt([cx - q, cy + q])} ${pt([cx - r, cy])}` +
|
|
677
|
+
`Q${pt([cx - q, cy - q])} ${pt([cx, cy - r])}Z`);
|
|
678
|
+
}
|
|
679
|
+
/** Scalloped rosette, the verified badge outline: n bumps bulging out from a ring of radius r. */
|
|
680
|
+
export function rosette(cx, cy, r, n = 8, bulge = 0.62) {
|
|
681
|
+
const chord = 2 * r * Math.sin(Math.PI / n);
|
|
682
|
+
const bump = String(r2(chord * bulge));
|
|
683
|
+
let d = '';
|
|
684
|
+
for (let i = 0; i <= n; i++) {
|
|
685
|
+
const a = (i / n) * Math.PI * 2 - Math.PI / 2;
|
|
686
|
+
const p = [cx + Math.cos(a) * r, cy + Math.sin(a) * r];
|
|
687
|
+
d += i ? `A${bump} ${bump} 0 0 1 ${pt(p)}` : `M${pt(p)}`;
|
|
688
|
+
}
|
|
689
|
+
return d + 'Z';
|
|
690
|
+
}
|
|
691
|
+
/** Ring: outer edge clockwise, hole counter clockwise, so the hole stays open and its far wall shows. */
|
|
692
|
+
export const ring = (cx, cy, r, hole) => ellipse(cx, cy, r) +
|
|
693
|
+
`M${pt([cx - hole, cy])}A${String(r2(hole))} ${String(r2(hole))} 0 1 0 ${pt([cx + hole, cy])}` +
|
|
694
|
+
`A${String(r2(hole))} ${String(r2(hole))} 0 1 0 ${pt([cx - hole, cy])}Z`;
|
|
695
|
+
/** n point star with lightly rounded tips. */
|
|
696
|
+
export const star = (cx, cy, r, inner = 0.48, n = 5, round = 3) => poly(Array.from({ length: n * 2 }, (_, i) => {
|
|
697
|
+
const a = (i / (n * 2)) * Math.PI * 2 - Math.PI / 2;
|
|
698
|
+
const k = i % 2 ? inner : 1;
|
|
699
|
+
return [cx + Math.cos(a) * r * k, cy + Math.sin(a) * r * k];
|
|
700
|
+
}), round);
|
|
701
|
+
/** Orthographic view of a model (x forward, y right, z up), turned yaw and seen from pitch above: poly() its faces. */
|
|
702
|
+
export function view(yaw, pitch) {
|
|
703
|
+
const [a, e] = [yaw * DEG, pitch * DEG];
|
|
704
|
+
return ([x, y, z]) => [
|
|
705
|
+
r2(x * Math.cos(a) - y * Math.sin(a)),
|
|
706
|
+
r2(-((x * Math.sin(a) + y * Math.cos(a)) * Math.sin(e) + z * Math.cos(e))),
|
|
707
|
+
];
|
|
708
|
+
}
|
|
709
|
+
export const line = (...points) => points.map((p, i) => `${i ? 'L' : 'M'}${pt(p)}`).join('');
|
|
710
|
+
/** Open circular arc, degrees clockwise from 3 o'clock. */
|
|
711
|
+
export function arc(cx, cy, r, from, to) {
|
|
712
|
+
const [a, b] = [from * DEG, to * DEG];
|
|
713
|
+
const large = Math.abs(to - from) > 180 ? 1 : 0;
|
|
714
|
+
const clockwise = to > from ? 1 : 0;
|
|
715
|
+
return (`M${pt([cx + Math.cos(a) * r, cy + Math.sin(a) * r])}` +
|
|
716
|
+
`A${String(r2(r))} ${String(r2(r))} 0 ${String(large)} ${String(clockwise)} ` +
|
|
717
|
+
pt([cx + Math.cos(b) * r, cy + Math.sin(b) * r]));
|
|
718
|
+
}
|
|
719
|
+
// ---------- Player: one clock for every sticker on the page, 60 frames a second times speed ----------
|
|
720
|
+
let frame = 0;
|
|
721
|
+
/** The page clock. Writing `frame` moves every sticker there, also while the loop is idle. */
|
|
722
|
+
export const clock = {
|
|
723
|
+
get frame() {
|
|
724
|
+
return frame;
|
|
725
|
+
},
|
|
726
|
+
set frame(to) {
|
|
727
|
+
frame = to;
|
|
728
|
+
wake();
|
|
729
|
+
},
|
|
730
|
+
speed: 1,
|
|
731
|
+
paused: false,
|
|
732
|
+
};
|
|
733
|
+
/** The sticker entrance: scale .2 and -90 degrees to rest over 1s, overshooting to 1.1424. */
|
|
734
|
+
const BOUNCE = [
|
|
735
|
+
[0, 0],
|
|
736
|
+
[0.076, 0.5737],
|
|
737
|
+
[0.1187, 0.8382],
|
|
738
|
+
[0.1419, 0.9463],
|
|
739
|
+
[0.1654, 1.0292],
|
|
740
|
+
[0.1897, 1.0886],
|
|
741
|
+
[0.2153, 1.1258],
|
|
742
|
+
[0.2297, 1.137],
|
|
743
|
+
[0.2448, 1.1424],
|
|
744
|
+
[0.261, 1.1423],
|
|
745
|
+
[0.2786, 1.1366],
|
|
746
|
+
[0.3101, 1.1165],
|
|
747
|
+
[0.3862, 1.0507],
|
|
748
|
+
[0.4257, 1.0219],
|
|
749
|
+
[0.4699, 0.9995],
|
|
750
|
+
[0.5163, 0.9872],
|
|
751
|
+
[0.5877, 0.9842],
|
|
752
|
+
[0.8126, 1.0011],
|
|
753
|
+
[1, 1],
|
|
754
|
+
];
|
|
755
|
+
export function popBounce(t) {
|
|
756
|
+
if (t <= 0)
|
|
757
|
+
return 0;
|
|
758
|
+
if (t >= 1)
|
|
759
|
+
return 1;
|
|
760
|
+
const i = BOUNCE.findIndex(([x]) => x >= t);
|
|
761
|
+
const [x0, y0] = BOUNCE[i - 1] ?? [0, 0];
|
|
762
|
+
const [x1, y1] = BOUNCE[i] ?? [1, 1];
|
|
763
|
+
return y0 + ((y1 - y0) * (t - x0)) / (x1 - x0);
|
|
764
|
+
}
|
|
765
|
+
/** Lays the entrance over a scene and paints it at the canvas's css size and device resolution. */
|
|
766
|
+
function present(canvas, icon, sc, e, palette, css, line = 1) {
|
|
767
|
+
const dpr = Math.min(3, window.devicePixelRatio || 1);
|
|
768
|
+
const px = Math.max(1, Math.round(css * dpr));
|
|
769
|
+
if (canvas.width !== px || canvas.height !== px)
|
|
770
|
+
canvas.width = canvas.height = px;
|
|
771
|
+
const ctx = canvas.getContext('2d');
|
|
772
|
+
if (!ctx)
|
|
773
|
+
return;
|
|
774
|
+
const size = icon.size ?? 500;
|
|
775
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
776
|
+
ctx.clearRect(0, 0, px, px);
|
|
777
|
+
const b = popBounce(e);
|
|
778
|
+
if (b <= 0)
|
|
779
|
+
return;
|
|
780
|
+
ctx.globalAlpha = Math.min(1, b);
|
|
781
|
+
ctx.translate(px / 2, px / 2);
|
|
782
|
+
ctx.rotate(-90 * (1 - b) * DEG);
|
|
783
|
+
ctx.scale(0.2 + 0.8 * b, 0.2 + 0.8 * b);
|
|
784
|
+
ctx.translate(-px / 2, -px / 2);
|
|
785
|
+
ctx.scale(px / size, px / size);
|
|
786
|
+
render(ctx, icon, sc, palette, line);
|
|
787
|
+
}
|
|
788
|
+
/** Paints one frame into a canvas sized by css, at device resolution, with the entrance at progress e (1 is rest). */
|
|
789
|
+
export function paint(canvas, icon, frame, e = 1, palette, line = 1) {
|
|
790
|
+
present(canvas, icon, scene(icon, frame), e, resolve(palette), canvas.clientWidth, line);
|
|
791
|
+
}
|
|
792
|
+
const live = new Set();
|
|
793
|
+
let raf = 0;
|
|
794
|
+
let last = 0;
|
|
795
|
+
let reduce;
|
|
796
|
+
let density;
|
|
797
|
+
function reducedMotion() {
|
|
798
|
+
if (reduce === undefined) {
|
|
799
|
+
reduce =
|
|
800
|
+
typeof matchMedia === 'undefined' ? null : matchMedia('(prefers-reduced-motion: reduce)');
|
|
801
|
+
}
|
|
802
|
+
return reduce?.matches ?? false;
|
|
803
|
+
}
|
|
804
|
+
/** Frames since a sticker's own start, and whether it still moves: a play-once runs to the still frame, or the whole loop when the still is frame 0. */
|
|
805
|
+
function progress({ icon, o, start }) {
|
|
806
|
+
const t = frame - (start ?? 0) + (o.offset ?? 0);
|
|
807
|
+
const still = icon.still ?? 0;
|
|
808
|
+
return { t, moving: o.loop !== false || t < (still > 0 ? still : icon.frames) };
|
|
809
|
+
}
|
|
810
|
+
/** A loop rides the page clock so every sticker stays in step; a replay or a play-once counts from its own start. */
|
|
811
|
+
function frameOf(l) {
|
|
812
|
+
const still = l.icon.still ?? 0;
|
|
813
|
+
if (reducedMotion())
|
|
814
|
+
return still;
|
|
815
|
+
const { t, moving } = progress(l);
|
|
816
|
+
return moving ? t : still;
|
|
817
|
+
}
|
|
818
|
+
/** Whether a sticker needs the next frame: on screen and unpainted, mid entrance, or moving. */
|
|
819
|
+
function busy(l) {
|
|
820
|
+
if (!l.shown)
|
|
821
|
+
return false;
|
|
822
|
+
if (!l.key || l.polled)
|
|
823
|
+
return true;
|
|
824
|
+
return !reducedMotion() && progress(l).moving;
|
|
825
|
+
}
|
|
826
|
+
/** Starts the loop if it is idle. Whatever can change what a resting sticker shows calls this. */
|
|
827
|
+
function wake() {
|
|
828
|
+
if (raf || !live.size)
|
|
829
|
+
return;
|
|
830
|
+
last = 0;
|
|
831
|
+
raf = requestAnimationFrame(tick);
|
|
832
|
+
}
|
|
833
|
+
function onDensity() {
|
|
834
|
+
watchDensity(true);
|
|
835
|
+
wake();
|
|
836
|
+
}
|
|
837
|
+
/** devicePixelRatio has no event of its own: a resolution query that stops matching is the cue, then a new one is set. */
|
|
838
|
+
function watchDensity(on) {
|
|
839
|
+
density?.removeEventListener('change', onDensity);
|
|
840
|
+
density =
|
|
841
|
+
on && typeof matchMedia !== 'undefined'
|
|
842
|
+
? matchMedia(`(resolution: ${String(window.devicePixelRatio || 1)}dppx)`)
|
|
843
|
+
: undefined;
|
|
844
|
+
density?.addEventListener('change', onDensity);
|
|
845
|
+
}
|
|
846
|
+
/** Listens, while any sticker plays, for what repaints a resting one from outside: reduced motion and pixel density. */
|
|
847
|
+
function listen(on) {
|
|
848
|
+
reducedMotion();
|
|
849
|
+
if (on)
|
|
850
|
+
reduce?.addEventListener('change', wake);
|
|
851
|
+
else
|
|
852
|
+
reduce?.removeEventListener('change', wake);
|
|
853
|
+
watchDensity(on);
|
|
854
|
+
}
|
|
855
|
+
/** Takes a sticker's palette option: an object is laid over BAHT now, a function waits for the next frame on screen. */
|
|
856
|
+
function repalette(l) {
|
|
857
|
+
const from = l.o.palette;
|
|
858
|
+
l.stale = typeof from === 'function';
|
|
859
|
+
if (typeof from !== 'function')
|
|
860
|
+
l.pal = resolve(from);
|
|
861
|
+
}
|
|
862
|
+
/** Reads a stale palette function; a different colour asks for a repaint, the same colours cost none. */
|
|
863
|
+
function reread(l) {
|
|
864
|
+
const from = l.o.palette;
|
|
865
|
+
l.stale = false;
|
|
866
|
+
if (typeof from !== 'function')
|
|
867
|
+
return;
|
|
868
|
+
const next = resolve(from());
|
|
869
|
+
const slots = Object.keys(next);
|
|
870
|
+
if (next === l.pal || slots.every((k) => next[k] === l.pal[k]))
|
|
871
|
+
return;
|
|
872
|
+
l.pal = next;
|
|
873
|
+
l.key = '';
|
|
874
|
+
}
|
|
875
|
+
/** One sticker's frame: paints only when the pose, size or colours changed since its last paint. */
|
|
876
|
+
function step(l, now) {
|
|
877
|
+
if (l.stale)
|
|
878
|
+
reread(l);
|
|
879
|
+
if (!l.seen)
|
|
880
|
+
l.seen = now;
|
|
881
|
+
if (l.o.loop === false && l.start === undefined)
|
|
882
|
+
l.start = frame;
|
|
883
|
+
const e = l.o.enter === undefined || reducedMotion() ? 1 : (now - l.seen - l.o.enter * 1000) / 1000;
|
|
884
|
+
const sc = scene(l.icon, frameOf(l));
|
|
885
|
+
const css = l.css || l.canvas.clientWidth;
|
|
886
|
+
// nothing moved and nothing resized (set() clears the key): a still hold costs no paint
|
|
887
|
+
const key = e < 1 ? '' : `${String(css)}|${String(window.devicePixelRatio)}|${fingerprint(sc)}`;
|
|
888
|
+
if (key && key === l.key)
|
|
889
|
+
return;
|
|
890
|
+
present(l.canvas, l.icon, sc, e, l.pal, css, l.o.line ?? 1);
|
|
891
|
+
l.key = key;
|
|
892
|
+
}
|
|
893
|
+
function tick(now) {
|
|
894
|
+
const dt = last ? Math.min(100, now - last) : 0;
|
|
895
|
+
last = now;
|
|
896
|
+
if (!clock.paused)
|
|
897
|
+
frame += (dt / 1000) * FPS * clock.speed;
|
|
898
|
+
let again = false;
|
|
899
|
+
for (const l of live) {
|
|
900
|
+
if (!l.shown)
|
|
901
|
+
continue;
|
|
902
|
+
try {
|
|
903
|
+
step(l, now);
|
|
904
|
+
again ||= busy(l);
|
|
905
|
+
}
|
|
906
|
+
catch (err) {
|
|
907
|
+
console.error(`[baht-sticker] ${l.icon.name} stopped`, err);
|
|
908
|
+
l.stop();
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
// nothing on screen is moving: the loop rests until wake()
|
|
912
|
+
raf = again ? requestAnimationFrame(tick) : 0;
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Plays an icon in a canvas sized by css, on the page clock, until stop(); off-screen stickers skip their paints.
|
|
916
|
+
* The loop, and the clock with it, rests while no sticker on screen is moving.
|
|
917
|
+
*/
|
|
918
|
+
export function play(canvas, icon, opts = {}) {
|
|
919
|
+
const l = {
|
|
920
|
+
canvas,
|
|
921
|
+
icon,
|
|
922
|
+
o: { ...opts },
|
|
923
|
+
pal: BAHT,
|
|
924
|
+
stale: false,
|
|
925
|
+
start: undefined,
|
|
926
|
+
seen: 0,
|
|
927
|
+
shown: true,
|
|
928
|
+
polled: typeof ResizeObserver === 'undefined',
|
|
929
|
+
css: 0,
|
|
930
|
+
key: '',
|
|
931
|
+
stop: () => {
|
|
932
|
+
io?.disconnect();
|
|
933
|
+
ro?.disconnect();
|
|
934
|
+
if (live.delete(l) && !live.size)
|
|
935
|
+
listen(false);
|
|
936
|
+
},
|
|
937
|
+
};
|
|
938
|
+
const io = typeof IntersectionObserver === 'undefined'
|
|
939
|
+
? null
|
|
940
|
+
: new IntersectionObserver(([en]) => {
|
|
941
|
+
l.shown = en?.isIntersecting ?? l.shown;
|
|
942
|
+
if (l.shown)
|
|
943
|
+
wake();
|
|
944
|
+
});
|
|
945
|
+
const ro = l.polled
|
|
946
|
+
? null
|
|
947
|
+
: new ResizeObserver(([en]) => {
|
|
948
|
+
l.css = en?.contentRect.width ?? l.css;
|
|
949
|
+
wake();
|
|
950
|
+
});
|
|
951
|
+
if (io) {
|
|
952
|
+
l.shown = false;
|
|
953
|
+
io.observe(canvas);
|
|
954
|
+
}
|
|
955
|
+
ro?.observe(canvas);
|
|
956
|
+
repalette(l);
|
|
957
|
+
live.add(l);
|
|
958
|
+
if (live.size === 1)
|
|
959
|
+
listen(true);
|
|
960
|
+
wake();
|
|
961
|
+
return {
|
|
962
|
+
stop: l.stop,
|
|
963
|
+
replay: () => {
|
|
964
|
+
l.start = frame;
|
|
965
|
+
l.key = '';
|
|
966
|
+
wake();
|
|
967
|
+
},
|
|
968
|
+
set: ({ icon: next, ...o }) => {
|
|
969
|
+
if (next)
|
|
970
|
+
l.icon = next;
|
|
971
|
+
Object.assign(l.o, o);
|
|
972
|
+
repalette(l);
|
|
973
|
+
l.key = '';
|
|
974
|
+
wake();
|
|
975
|
+
},
|
|
976
|
+
retheme: () => {
|
|
977
|
+
l.stale = typeof l.o.palette === 'function';
|
|
978
|
+
wake();
|
|
979
|
+
},
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
// ---------- Registry: stickers by name, so markup can say <baht-sticker name="piggy-bank"> ----------
|
|
983
|
+
// On globalThis under a registered symbol, so every bundled copy of this module (an app's and a
|
|
984
|
+
// library's, two Native Federation remotes) shares one registry: a sticker one copy registers, the
|
|
985
|
+
// other's <baht-sticker name> finds.
|
|
986
|
+
const REGISTRY = Symbol.for('@20baht/stickers.registry');
|
|
987
|
+
function sharedRegistry() {
|
|
988
|
+
const shared = globalThis;
|
|
989
|
+
const found = shared[REGISTRY];
|
|
990
|
+
if (found)
|
|
991
|
+
return found;
|
|
992
|
+
const made = new Map();
|
|
993
|
+
shared[REGISTRY] = made;
|
|
994
|
+
return made;
|
|
995
|
+
}
|
|
996
|
+
const registry = sharedRegistry();
|
|
997
|
+
/** Makes icons available by `icon.name` to `<baht-sticker name>`; a later icon with the same name replaces it. */
|
|
998
|
+
export function registerSticker(...icons) {
|
|
999
|
+
for (const icon of icons)
|
|
1000
|
+
registry.set(icon.name, icon);
|
|
1001
|
+
}
|
|
1002
|
+
/** The registered icon with this name, if any. */
|
|
1003
|
+
export function getSticker(name) {
|
|
1004
|
+
return registry.get(name);
|
|
1005
|
+
}
|
|
1006
|
+
/** Names of every registered icon, in registration order. */
|
|
1007
|
+
export function stickerNames() {
|
|
1008
|
+
return [...registry.keys()];
|
|
1009
|
+
}
|