magic-pill 9.0.0 → 9.1.0
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/CHANGELOG.md +44 -0
- package/README.md +259 -15
- package/dist/MagicPill/MagicPill.d.mts +7 -1
- package/dist/MagicPill/MagicPill.d.ts +7 -1
- package/dist/MagicPill/MagicPill.js +40 -17
- package/dist/MagicPill/MagicPill.mjs +42 -19
- package/dist/MagicPill/choreography.js +1 -1
- package/dist/MagicPill/choreography.mjs +1 -1
- package/dist/Pill/Pill.js +53 -17
- package/dist/Pill/Pill.mjs +54 -18
- package/dist/Pill/PillContext.js +3 -1
- package/dist/Pill/PillContext.mjs +3 -1
- package/dist/Pill/PillPanel.d.mts +5 -5
- package/dist/Pill/PillPanel.d.ts +5 -5
- package/dist/Pill/PillPanel.js +28 -20
- package/dist/Pill/PillPanel.mjs +28 -20
- package/dist/Pill/morph.js +25 -7
- package/dist/Pill/morph.mjs +22 -8
- package/dist/Pill/scene.js +90 -0
- package/dist/Pill/scene.mjs +89 -0
- package/dist/Pill/themed.js +36 -0
- package/dist/Pill/themed.mjs +34 -0
- package/dist/Pill/types.d.mts +50 -0
- package/dist/Pill/types.d.ts +50 -0
- package/dist/Pill/zone.js +160 -0
- package/dist/Pill/zone.mjs +159 -0
- package/dist/PillForm/Calendar.d.mts +15 -0
- package/dist/PillForm/Calendar.d.ts +16 -0
- package/dist/PillForm/Calendar.js +371 -0
- package/dist/PillForm/Calendar.mjs +370 -0
- package/dist/PillForm/FormContext.d.mts +2 -0
- package/dist/PillForm/FormContext.d.ts +2 -0
- package/dist/PillForm/FormContext.js +8 -0
- package/dist/PillForm/FormContext.mjs +8 -0
- package/dist/PillForm/PillCheckbox.d.mts +8 -0
- package/dist/PillForm/PillCheckbox.d.ts +8 -0
- package/dist/PillForm/PillCheckbox.js +45 -0
- package/dist/PillForm/PillCheckbox.mjs +45 -0
- package/dist/PillForm/PillChoices.d.mts +39 -0
- package/dist/PillForm/PillChoices.d.ts +39 -0
- package/dist/PillForm/PillChoices.js +103 -0
- package/dist/PillForm/PillChoices.mjs +103 -0
- package/dist/PillForm/PillCode.d.mts +12 -0
- package/dist/PillForm/PillCode.d.ts +12 -0
- package/dist/PillForm/PillCode.js +60 -0
- package/dist/PillForm/PillCode.mjs +60 -0
- package/dist/PillForm/PillColor.d.mts +10 -0
- package/dist/PillForm/PillColor.d.ts +10 -0
- package/dist/PillForm/PillColor.js +56 -0
- package/dist/PillForm/PillColor.mjs +56 -0
- package/dist/PillForm/PillDate.d.mts +44 -0
- package/dist/PillForm/PillDate.d.ts +44 -0
- package/dist/PillForm/PillDate.js +193 -0
- package/dist/PillForm/PillDate.mjs +193 -0
- package/dist/PillForm/PillField.d.mts +38 -0
- package/dist/PillForm/PillField.d.ts +38 -0
- package/dist/PillForm/PillField.js +81 -0
- package/dist/PillForm/PillField.mjs +80 -0
- package/dist/PillForm/PillFields.d.mts +11 -0
- package/dist/PillForm/PillFields.d.ts +11 -0
- package/dist/PillForm/PillFields.js +21 -0
- package/dist/PillForm/PillFields.mjs +21 -0
- package/dist/PillForm/PillFile.d.mts +8 -0
- package/dist/PillForm/PillFile.d.ts +8 -0
- package/dist/PillForm/PillFile.js +55 -0
- package/dist/PillForm/PillFile.mjs +55 -0
- package/dist/PillForm/PillForm.d.mts +29 -0
- package/dist/PillForm/PillForm.d.ts +29 -0
- package/dist/PillForm/PillForm.js +66 -0
- package/dist/PillForm/PillForm.mjs +65 -0
- package/dist/PillForm/PillFormPanel.d.mts +24 -0
- package/dist/PillForm/PillFormPanel.d.ts +24 -0
- package/dist/PillForm/PillFormPanel.js +53 -0
- package/dist/PillForm/PillFormPanel.mjs +53 -0
- package/dist/PillForm/PillInput.d.mts +20 -0
- package/dist/PillForm/PillInput.d.ts +20 -0
- package/dist/PillForm/PillInput.js +84 -0
- package/dist/PillForm/PillInput.mjs +84 -0
- package/dist/PillForm/PillSelect.d.mts +23 -0
- package/dist/PillForm/PillSelect.d.ts +23 -0
- package/dist/PillForm/PillSelect.js +87 -0
- package/dist/PillForm/PillSelect.mjs +87 -0
- package/dist/PillForm/PillSlider.d.mts +17 -0
- package/dist/PillForm/PillSlider.d.ts +17 -0
- package/dist/PillForm/PillSlider.js +61 -0
- package/dist/PillForm/PillSlider.mjs +61 -0
- package/dist/PillForm/PillStepper.d.mts +13 -0
- package/dist/PillForm/PillStepper.d.ts +13 -0
- package/dist/PillForm/PillStepper.js +84 -0
- package/dist/PillForm/PillStepper.mjs +84 -0
- package/dist/PillForm/PillSubmit.d.mts +14 -0
- package/dist/PillForm/PillSubmit.d.ts +14 -0
- package/dist/PillForm/PillSubmit.js +90 -0
- package/dist/PillForm/PillSubmit.mjs +89 -0
- package/dist/PillForm/PillSwitch.d.mts +5 -0
- package/dist/PillForm/PillSwitch.d.ts +5 -0
- package/dist/PillForm/PillSwitch.js +38 -0
- package/dist/PillForm/PillSwitch.mjs +38 -0
- package/dist/PillForm/PillTextarea.d.mts +5 -0
- package/dist/PillForm/PillTextarea.d.ts +5 -0
- package/dist/PillForm/PillTextarea.js +49 -0
- package/dist/PillForm/PillTextarea.mjs +49 -0
- package/dist/PillForm/dates.d.mts +1 -0
- package/dist/PillForm/dates.d.ts +1 -0
- package/dist/PillForm/dates.js +155 -0
- package/dist/PillForm/dates.mjs +139 -0
- package/dist/PillForm/glyphs.js +65 -0
- package/dist/PillForm/glyphs.mjs +56 -0
- package/dist/PillForm/keyboard.js +34 -0
- package/dist/PillForm/keyboard.mjs +34 -0
- package/dist/PillForm/popover.js +84 -0
- package/dist/PillForm/popover.mjs +83 -0
- package/dist/PillForm/send.d.mts +6 -0
- package/dist/PillForm/send.d.ts +6 -0
- package/dist/PillForm/send.js +44 -0
- package/dist/PillForm/send.mjs +44 -0
- package/dist/PillForm/validity.js +74 -0
- package/dist/PillForm/validity.mjs +72 -0
- package/dist/PillNavbar/PillNavbar.d.mts +29 -6
- package/dist/PillNavbar/PillNavbar.d.ts +29 -6
- package/dist/PillNavbar/PillNavbar.js +35 -11
- package/dist/PillNavbar/PillNavbar.mjs +36 -12
- package/dist/PillNavbar/room.js +84 -0
- package/dist/PillNavbar/room.mjs +83 -0
- package/dist/PillNotification/PillNotification.d.mts +6 -0
- package/dist/PillNotification/PillNotification.d.ts +6 -0
- package/dist/PillNotification/PillNotification.js +3 -0
- package/dist/PillNotification/PillNotification.mjs +3 -0
- package/dist/PillOptics/PillOptics.d.mts +8 -0
- package/dist/PillOptics/PillOptics.d.ts +8 -0
- package/dist/PillOptics/PillOptics.js +195 -0
- package/dist/PillOptics/PillOptics.mjs +195 -0
- package/dist/PillOptics/adaptive.js +212 -0
- package/dist/PillOptics/adaptive.mjs +208 -0
- package/dist/PillOptics/lens.js +77 -0
- package/dist/PillOptics/lens.mjs +73 -0
- package/dist/PillOptics/light.js +37 -0
- package/dist/PillOptics/light.mjs +36 -0
- package/dist/calendar.css +499 -0
- package/dist/forms.css +1452 -0
- package/dist/index.d.mts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +36 -0
- package/dist/index.mjs +19 -1
- package/dist/styles.css +169 -19
- package/dist/themes.css +449 -0
- package/dist/utils/visuallyHidden.js +18 -0
- package/dist/utils/visuallyHidden.mjs +18 -0
- package/package.json +4 -1
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// src/PillOptics/adaptive.ts
|
|
2
|
+
/** Past this, what's underneath no longer shows. */
|
|
3
|
+
const OPAQUE = .95;
|
|
4
|
+
/** A smaller change of colour than this (out of 255) isn't seen, and restyling the pill costs more than reading the page. */
|
|
5
|
+
const NOTICEABLE = 10;
|
|
6
|
+
/** Where the page is read, as parts of the pill's width and height. */
|
|
7
|
+
const ACROSS = [
|
|
8
|
+
.06,
|
|
9
|
+
.28,
|
|
10
|
+
.5,
|
|
11
|
+
.72,
|
|
12
|
+
.94
|
|
13
|
+
];
|
|
14
|
+
const DOWN = [.2, .8];
|
|
15
|
+
/** How wide a patch of a picture is read around each point, in px. */
|
|
16
|
+
const PATCH = 16;
|
|
17
|
+
/** Once per burst of scrolling events, and for what changes by itself (a new page, a video…). */
|
|
18
|
+
const SCROLL_DELAY = 100;
|
|
19
|
+
const INTERVAL = 500;
|
|
20
|
+
/** What layers look like on top of each other, front to back. */
|
|
21
|
+
function flatten(layers) {
|
|
22
|
+
const flat = [
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0
|
|
26
|
+
];
|
|
27
|
+
let alpha = 0;
|
|
28
|
+
for (const layer of layers) {
|
|
29
|
+
const share = (1 - alpha) * layer[3];
|
|
30
|
+
flat.forEach((_, channel) => flat[channel] += layer[channel] * share);
|
|
31
|
+
alpha += share;
|
|
32
|
+
}
|
|
33
|
+
return alpha === 0 ? [
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
0
|
|
38
|
+
] : [
|
|
39
|
+
flat[0] / alpha,
|
|
40
|
+
flat[1] / alpha,
|
|
41
|
+
flat[2] / alpha,
|
|
42
|
+
alpha
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
/** The mean of some colours, or null without any. */
|
|
46
|
+
function mean(colours) {
|
|
47
|
+
if (colours.length === 0) return null;
|
|
48
|
+
const average = (channel) => colours.reduce((sum, colour) => sum + colour[channel], 0) / colours.length;
|
|
49
|
+
return [
|
|
50
|
+
average(0),
|
|
51
|
+
average(1),
|
|
52
|
+
average(2),
|
|
53
|
+
average(3)
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
/** Whether two colours are far enough apart to be told apart on a pill. */
|
|
57
|
+
const differ = (one, other) => [
|
|
58
|
+
0,
|
|
59
|
+
1,
|
|
60
|
+
2
|
|
61
|
+
].some((channel) => Math.abs(one[channel] - other[channel]) >= NOTICEABLE);
|
|
62
|
+
const linear = (value) => {
|
|
63
|
+
const part = value / 255;
|
|
64
|
+
return part <= .04045 ? part / 12.92 : ((part + .055) / 1.055) ** 2.4;
|
|
65
|
+
};
|
|
66
|
+
/** Over this colour, a light pill or a dark one? With a margin around the limit, so it doesn't flicker. */
|
|
67
|
+
function pickScheme([red, green, blue], previous) {
|
|
68
|
+
return .2126 * linear(red) + .7152 * linear(green) + .0722 * linear(blue) > (previous === "light" ? .3 : .45) ? "light" : "dark";
|
|
69
|
+
}
|
|
70
|
+
let pixel = null;
|
|
71
|
+
function readPixel(draw) {
|
|
72
|
+
pixel ?? (pixel = document.createElement("canvas").getContext("2d", { willReadFrequently: true }));
|
|
73
|
+
if (!pixel) return null;
|
|
74
|
+
pixel.clearRect(0, 0, 1, 1);
|
|
75
|
+
try {
|
|
76
|
+
draw(pixel);
|
|
77
|
+
const [red, green, blue, alpha] = pixel.getImageData(0, 0, 1, 1).data;
|
|
78
|
+
return [
|
|
79
|
+
red,
|
|
80
|
+
green,
|
|
81
|
+
blue,
|
|
82
|
+
alpha / 255
|
|
83
|
+
];
|
|
84
|
+
} catch {
|
|
85
|
+
pixel = null;
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const KNOWN_COLOURS = 200;
|
|
90
|
+
const knownColours = /* @__PURE__ */ new Map();
|
|
91
|
+
function readColour(css) {
|
|
92
|
+
const known = knownColours.get(css);
|
|
93
|
+
if (known) return known;
|
|
94
|
+
const colour = readPixel((context) => {
|
|
95
|
+
context.fillStyle = css;
|
|
96
|
+
context.fillRect(0, 0, 1, 1);
|
|
97
|
+
});
|
|
98
|
+
if (knownColours.size >= KNOWN_COLOURS) knownColours.clear();
|
|
99
|
+
if (colour) knownColours.set(css, colour);
|
|
100
|
+
return colour;
|
|
101
|
+
}
|
|
102
|
+
const unreadable = /* @__PURE__ */ new WeakMap();
|
|
103
|
+
const sourceOf = (picture) => "currentSrc" in picture ? picture.currentSrc : "";
|
|
104
|
+
function sizeOf(picture) {
|
|
105
|
+
if (picture instanceof HTMLImageElement) return [picture.naturalWidth, picture.naturalHeight];
|
|
106
|
+
if (picture instanceof HTMLVideoElement) return [picture.videoWidth, picture.videoHeight];
|
|
107
|
+
return [picture.width, picture.height];
|
|
108
|
+
}
|
|
109
|
+
/** The mean colour of a picture around a point (as if it filled its box: `object-fit` isn't worth more). */
|
|
110
|
+
function readPicture(picture, x, y) {
|
|
111
|
+
if (unreadable.get(picture) === sourceOf(picture)) return null;
|
|
112
|
+
const [sourceWidth, sourceHeight] = sizeOf(picture);
|
|
113
|
+
const { left, top, width, height } = picture.getBoundingClientRect();
|
|
114
|
+
if (sourceWidth === 0 || sourceHeight === 0 || width === 0 || height === 0) return null;
|
|
115
|
+
const scaleX = sourceWidth / width;
|
|
116
|
+
const scaleY = sourceHeight / height;
|
|
117
|
+
const colour = readPixel((context) => context.drawImage(picture, (x - left - PATCH / 2) * scaleX, (y - top - PATCH / 2) * scaleY, PATCH * scaleX, PATCH * scaleY, 0, 0, 1, 1));
|
|
118
|
+
if (!colour) unreadable.set(picture, sourceOf(picture));
|
|
119
|
+
return colour;
|
|
120
|
+
}
|
|
121
|
+
const isPicture = (element) => element.matches("img, canvas, video");
|
|
122
|
+
/** The layers an element paints at a point, front to back. `url()` backgrounds can't be read. */
|
|
123
|
+
function layersOf(element, x, y) {
|
|
124
|
+
const { backgroundColor, backgroundImage } = getComputedStyle(element);
|
|
125
|
+
const stops = (backgroundImage.match(/(?:rgb|hsl|hwb|lab|lch|oklab|oklch|color)a?\([^()]*\)/g) ?? []).map(readColour);
|
|
126
|
+
return [
|
|
127
|
+
isPicture(element) ? readPicture(element, x, y) : null,
|
|
128
|
+
mean(stops.filter((stop) => stop !== null)),
|
|
129
|
+
readColour(backgroundColor)
|
|
130
|
+
].filter((layer) => layer !== null);
|
|
131
|
+
}
|
|
132
|
+
/** What the page shows where nothing else does. */
|
|
133
|
+
function canvasColour() {
|
|
134
|
+
for (const element of [document.documentElement, document.body]) {
|
|
135
|
+
const colour = readColour(getComputedStyle(element).backgroundColor);
|
|
136
|
+
if (colour && colour[3] > 0) return colour;
|
|
137
|
+
}
|
|
138
|
+
const scheme = getComputedStyle(document.documentElement).colorScheme ?? "";
|
|
139
|
+
return scheme.includes("dark") && (!scheme.includes("light") || matchMedia("(prefers-color-scheme: dark)").matches) ? [
|
|
140
|
+
18,
|
|
141
|
+
18,
|
|
142
|
+
18,
|
|
143
|
+
1
|
|
144
|
+
] : [
|
|
145
|
+
255,
|
|
146
|
+
255,
|
|
147
|
+
255,
|
|
148
|
+
1
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
/** The page's colour at a point, pills left out, or null out of the window. */
|
|
152
|
+
function colourAt(x, y) {
|
|
153
|
+
const elements = document.elementsFromPoint(x, y);
|
|
154
|
+
if (elements.length === 0) return null;
|
|
155
|
+
const layers = [];
|
|
156
|
+
for (const element of elements) {
|
|
157
|
+
if (element.closest("[data-mp=\"pill\"]")) continue;
|
|
158
|
+
layers.push(...layersOf(element, x, y));
|
|
159
|
+
if (flatten(layers)[3] > OPAQUE) return flatten(layers);
|
|
160
|
+
}
|
|
161
|
+
return flatten([...layers, canvasColour()]);
|
|
162
|
+
}
|
|
163
|
+
/** The mean colour of the page behind the pill, or null when none of it is in the window. */
|
|
164
|
+
function colourBehind(pill) {
|
|
165
|
+
const { left, top, width, height } = pill.getBoundingClientRect();
|
|
166
|
+
return mean(ACROSS.flatMap((across) => DOWN.map((down) => colourAt(left + width * across, top + height * down))).filter((colour) => colour !== null));
|
|
167
|
+
}
|
|
168
|
+
const isModalOpen = () => [...document.querySelectorAll("dialog[open]")].some((dialog) => dialog.matches(":modal"));
|
|
169
|
+
/** Keeps the pills' scheme and ambient colour up to date. Returns what stops it, and gives them their own colours back. */
|
|
170
|
+
function adapt(pills) {
|
|
171
|
+
let timer;
|
|
172
|
+
const shown = /* @__PURE__ */ new Map();
|
|
173
|
+
const update = () => {
|
|
174
|
+
if (document.hidden || isModalOpen()) return;
|
|
175
|
+
for (const pill of pills) {
|
|
176
|
+
if (pill.dataset.presence === "hidden") continue;
|
|
177
|
+
const colour = colourBehind(pill);
|
|
178
|
+
const last = shown.get(pill);
|
|
179
|
+
if (!colour || last && !differ(colour, last)) continue;
|
|
180
|
+
shown.set(pill, colour);
|
|
181
|
+
pill.dataset.scheme = pickScheme(colour, pill.dataset.scheme);
|
|
182
|
+
pill.style.setProperty("--_mp-ambient", `rgb(${colour.slice(0, 3).map(Math.round).join(" ")})`);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const onScroll = () => {
|
|
186
|
+
timer ?? (timer = setTimeout(() => {
|
|
187
|
+
timer = void 0;
|
|
188
|
+
update();
|
|
189
|
+
}, SCROLL_DELAY));
|
|
190
|
+
};
|
|
191
|
+
update();
|
|
192
|
+
document.addEventListener("scroll", onScroll, {
|
|
193
|
+
passive: true,
|
|
194
|
+
capture: true
|
|
195
|
+
});
|
|
196
|
+
const interval = setInterval(update, INTERVAL);
|
|
197
|
+
return () => {
|
|
198
|
+
document.removeEventListener("scroll", onScroll, { capture: true });
|
|
199
|
+
clearInterval(interval);
|
|
200
|
+
clearTimeout(timer);
|
|
201
|
+
for (const pill of pills) {
|
|
202
|
+
delete pill.dataset.scheme;
|
|
203
|
+
pill.style.removeProperty("--_mp-ambient");
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { adapt, differ, flatten, mean, pickScheme };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/PillOptics/lens.ts
|
|
2
|
+
const STEPS = 16;
|
|
3
|
+
/**
|
|
4
|
+
* How much the light bends, from the edge (first) to the flat top (last),
|
|
5
|
+
* 0 to 1: Snell's law through a rounded glass edge (refractive index 1.5).
|
|
6
|
+
*/
|
|
7
|
+
const BEND = (() => {
|
|
8
|
+
const bend = Array.from({ length: 17 }, (_, step) => {
|
|
9
|
+
const inner = 1 - Math.min(.999, Math.max(.012, step / STEPS));
|
|
10
|
+
const height = (1 - inner ** 4) ** .25;
|
|
11
|
+
const angle = Math.atan(inner ** 3 / (1 - inner ** 4) ** .75);
|
|
12
|
+
return Math.tan(angle - Math.asin(Math.sin(angle) / 1.5)) * (height + .35);
|
|
13
|
+
});
|
|
14
|
+
const most = Math.max(...bend);
|
|
15
|
+
return bend.map((value) => value / most);
|
|
16
|
+
})();
|
|
17
|
+
/** Rounded, so pills with about the same lens share one. */
|
|
18
|
+
function makeLens(radius, edge, refraction, fringe) {
|
|
19
|
+
const round = (value, per) => Math.round(value * per) / per;
|
|
20
|
+
const lens = {
|
|
21
|
+
radius: round(radius, 2),
|
|
22
|
+
edge: round(Math.min(edge, radius), 2),
|
|
23
|
+
depth: round(Math.max(0, refraction) * 90, 2),
|
|
24
|
+
fringe: round(Math.min(1, Math.max(0, fringe)), 100)
|
|
25
|
+
};
|
|
26
|
+
return lens.radius > 0 && lens.edge > 0 && lens.depth > 0 ? lens : null;
|
|
27
|
+
}
|
|
28
|
+
const lensId = ({ radius, edge, depth, fringe }) => `mp-lens-${[
|
|
29
|
+
radius,
|
|
30
|
+
edge,
|
|
31
|
+
depth,
|
|
32
|
+
fringe
|
|
33
|
+
].join("-").replace(/\./g, "_")}`;
|
|
34
|
+
const stop = (offset, colour, opacity = 1) => `<stop offset="${offset.toFixed(3)}" stop-color="${colour}" stop-opacity="${opacity.toFixed(3)}"/>`;
|
|
35
|
+
const NEUTRAL = "#808080";
|
|
36
|
+
/**
|
|
37
|
+
* The displacement map, as a data URI: red bends along x, green along y,
|
|
38
|
+
* #808080 doesn't bend. Every side points inwards, faded to neutral by how
|
|
39
|
+
* little the light bends at that depth. The corners' direction (x/r, y/r)
|
|
40
|
+
* is two ramps over a disc, so their fade is corrected by r.
|
|
41
|
+
*/
|
|
42
|
+
function lensMap({ radius, edge }) {
|
|
43
|
+
const size = 2 * radius;
|
|
44
|
+
const sideFade = BEND.map((bend, step) => stop(step / STEPS, NEUTRAL, 1 - bend)).join("");
|
|
45
|
+
const cornerFade = BEND.map((bend, step) => {
|
|
46
|
+
const r = 1 - step / STEPS * edge / radius;
|
|
47
|
+
return stop(r, NEUTRAL, 1 - Math.min(1, r > 0 ? bend / r : 0));
|
|
48
|
+
}).reverse().join("");
|
|
49
|
+
const linear = (id, x1, y1, x2, y2, stops) => `<linearGradient id="${id}" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">${stops}</linearGradient>`;
|
|
50
|
+
const rect = (attributes, fill) => `<rect ${attributes} fill="${fill}"/>`;
|
|
51
|
+
const side = (attributes, colour, fade) => rect(attributes, colour) + rect(attributes, `url(#${fade})`);
|
|
52
|
+
const at = (x, y, content) => `<svg x="${x * 100}%" y="${y * 100}%" overflow="visible">${content}</svg>`;
|
|
53
|
+
const corner = (x, y) => at(x, y, `<svg x="${-x * radius}" y="${-y * radius}" width="${radius}" height="${radius}" style="isolation:isolate"><use href="#d" x="${-x * radius}" y="${-y * radius}"/></svg>`);
|
|
54
|
+
const disc = `width="${size}" height="${size}"`;
|
|
55
|
+
return `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%"><defs>${linear("t", 0, 0, 0, 1, sideFade) + linear("b", 0, 1, 0, 0, sideFade) + linear("l", 0, 0, 1, 0, sideFade) + linear("r", 1, 0, 0, 0, sideFade) + linear("x", 0, 0, 1, 0, stop(0, "#f00") + stop(1, "#000")) + linear("y", 0, 0, 0, 1, stop(0, "#0f0") + stop(1, "#000"))}<radialGradient id="c">${cornerFade}</radialGradient><g id="d">${rect(disc, "url(#x)") + rect(`${disc} style="mix-blend-mode:screen"`, "url(#y)") + rect(disc, "url(#c)")}</g></defs>${rect(`width="100%" height="100%"`, NEUTRAL) + side(`width="100%" height="${edge}"`, "#80ff00", "t") + at(0, 1, side(`y="${-edge}" width="100%" height="${edge}"`, "#800000", "b")) + side(`width="${edge}" height="100%"`, "#ff8000", "l") + at(1, 0, side(`x="${-edge}" width="${edge}" height="100%"`, "#008000", "r")) + corner(0, 0) + corner(1, 0) + corner(0, 1) + corner(1, 1)}</svg>`)}`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Whether the browser draws SVG filters behind an element. Only Chromium
|
|
59
|
+
* does; Safari would drop the whole backdrop-filter, and @supports says yes
|
|
60
|
+
* everywhere, so the others are told by what only they know.
|
|
61
|
+
*/
|
|
62
|
+
function canRefract() {
|
|
63
|
+
try {
|
|
64
|
+
if (!CSS.supports("not ((-moz-appearance: none) or (font: -apple-system-body))")) return false;
|
|
65
|
+
const gl = document.createElement("canvas").getContext("webgl", { failIfMajorPerformanceCaveat: true });
|
|
66
|
+
gl?.getExtension("WEBGL_lose_context")?.loseContext();
|
|
67
|
+
return gl !== null;
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
exports.BEND = BEND;
|
|
74
|
+
exports.canRefract = canRefract;
|
|
75
|
+
exports.lensId = lensId;
|
|
76
|
+
exports.lensMap = lensMap;
|
|
77
|
+
exports.makeLens = makeLens;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/PillOptics/lens.ts
|
|
2
|
+
const STEPS = 16;
|
|
3
|
+
/**
|
|
4
|
+
* How much the light bends, from the edge (first) to the flat top (last),
|
|
5
|
+
* 0 to 1: Snell's law through a rounded glass edge (refractive index 1.5).
|
|
6
|
+
*/
|
|
7
|
+
const BEND = (() => {
|
|
8
|
+
const bend = Array.from({ length: 17 }, (_, step) => {
|
|
9
|
+
const inner = 1 - Math.min(.999, Math.max(.012, step / STEPS));
|
|
10
|
+
const height = (1 - inner ** 4) ** .25;
|
|
11
|
+
const angle = Math.atan(inner ** 3 / (1 - inner ** 4) ** .75);
|
|
12
|
+
return Math.tan(angle - Math.asin(Math.sin(angle) / 1.5)) * (height + .35);
|
|
13
|
+
});
|
|
14
|
+
const most = Math.max(...bend);
|
|
15
|
+
return bend.map((value) => value / most);
|
|
16
|
+
})();
|
|
17
|
+
/** Rounded, so pills with about the same lens share one. */
|
|
18
|
+
function makeLens(radius, edge, refraction, fringe) {
|
|
19
|
+
const round = (value, per) => Math.round(value * per) / per;
|
|
20
|
+
const lens = {
|
|
21
|
+
radius: round(radius, 2),
|
|
22
|
+
edge: round(Math.min(edge, radius), 2),
|
|
23
|
+
depth: round(Math.max(0, refraction) * 90, 2),
|
|
24
|
+
fringe: round(Math.min(1, Math.max(0, fringe)), 100)
|
|
25
|
+
};
|
|
26
|
+
return lens.radius > 0 && lens.edge > 0 && lens.depth > 0 ? lens : null;
|
|
27
|
+
}
|
|
28
|
+
const lensId = ({ radius, edge, depth, fringe }) => `mp-lens-${[
|
|
29
|
+
radius,
|
|
30
|
+
edge,
|
|
31
|
+
depth,
|
|
32
|
+
fringe
|
|
33
|
+
].join("-").replace(/\./g, "_")}`;
|
|
34
|
+
const stop = (offset, colour, opacity = 1) => `<stop offset="${offset.toFixed(3)}" stop-color="${colour}" stop-opacity="${opacity.toFixed(3)}"/>`;
|
|
35
|
+
const NEUTRAL = "#808080";
|
|
36
|
+
/**
|
|
37
|
+
* The displacement map, as a data URI: red bends along x, green along y,
|
|
38
|
+
* #808080 doesn't bend. Every side points inwards, faded to neutral by how
|
|
39
|
+
* little the light bends at that depth. The corners' direction (x/r, y/r)
|
|
40
|
+
* is two ramps over a disc, so their fade is corrected by r.
|
|
41
|
+
*/
|
|
42
|
+
function lensMap({ radius, edge }) {
|
|
43
|
+
const size = 2 * radius;
|
|
44
|
+
const sideFade = BEND.map((bend, step) => stop(step / STEPS, NEUTRAL, 1 - bend)).join("");
|
|
45
|
+
const cornerFade = BEND.map((bend, step) => {
|
|
46
|
+
const r = 1 - step / STEPS * edge / radius;
|
|
47
|
+
return stop(r, NEUTRAL, 1 - Math.min(1, r > 0 ? bend / r : 0));
|
|
48
|
+
}).reverse().join("");
|
|
49
|
+
const linear = (id, x1, y1, x2, y2, stops) => `<linearGradient id="${id}" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">${stops}</linearGradient>`;
|
|
50
|
+
const rect = (attributes, fill) => `<rect ${attributes} fill="${fill}"/>`;
|
|
51
|
+
const side = (attributes, colour, fade) => rect(attributes, colour) + rect(attributes, `url(#${fade})`);
|
|
52
|
+
const at = (x, y, content) => `<svg x="${x * 100}%" y="${y * 100}%" overflow="visible">${content}</svg>`;
|
|
53
|
+
const corner = (x, y) => at(x, y, `<svg x="${-x * radius}" y="${-y * radius}" width="${radius}" height="${radius}" style="isolation:isolate"><use href="#d" x="${-x * radius}" y="${-y * radius}"/></svg>`);
|
|
54
|
+
const disc = `width="${size}" height="${size}"`;
|
|
55
|
+
return `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%"><defs>${linear("t", 0, 0, 0, 1, sideFade) + linear("b", 0, 1, 0, 0, sideFade) + linear("l", 0, 0, 1, 0, sideFade) + linear("r", 1, 0, 0, 0, sideFade) + linear("x", 0, 0, 1, 0, stop(0, "#f00") + stop(1, "#000")) + linear("y", 0, 0, 0, 1, stop(0, "#0f0") + stop(1, "#000"))}<radialGradient id="c">${cornerFade}</radialGradient><g id="d">${rect(disc, "url(#x)") + rect(`${disc} style="mix-blend-mode:screen"`, "url(#y)") + rect(disc, "url(#c)")}</g></defs>${rect(`width="100%" height="100%"`, NEUTRAL) + side(`width="100%" height="${edge}"`, "#80ff00", "t") + at(0, 1, side(`y="${-edge}" width="100%" height="${edge}"`, "#800000", "b")) + side(`width="${edge}" height="100%"`, "#ff8000", "l") + at(1, 0, side(`x="${-edge}" width="${edge}" height="100%"`, "#008000", "r")) + corner(0, 0) + corner(1, 0) + corner(0, 1) + corner(1, 1)}</svg>`)}`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Whether the browser draws SVG filters behind an element. Only Chromium
|
|
59
|
+
* does; Safari would drop the whole backdrop-filter, and @supports says yes
|
|
60
|
+
* everywhere, so the others are told by what only they know.
|
|
61
|
+
*/
|
|
62
|
+
function canRefract() {
|
|
63
|
+
try {
|
|
64
|
+
if (!CSS.supports("not ((-moz-appearance: none) or (font: -apple-system-body))")) return false;
|
|
65
|
+
const gl = document.createElement("canvas").getContext("webgl", { failIfMajorPerformanceCaveat: true });
|
|
66
|
+
gl?.getExtension("WEBGL_lose_context")?.loseContext();
|
|
67
|
+
return gl !== null;
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { BEND, canRefract, lensId, lensMap, makeLens };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/PillOptics/light.ts
|
|
2
|
+
const PROPERTIES = [
|
|
3
|
+
"--_mp-angle",
|
|
4
|
+
"--_mp-sheen-x",
|
|
5
|
+
"--_mp-sheen-y"
|
|
6
|
+
];
|
|
7
|
+
/** Where the pointer is from the pill: the light's angle, and its highlight's place. Pure, so it can be tested. */
|
|
8
|
+
function aim(x, y, { left, top, width, height }) {
|
|
9
|
+
const dx = x - left;
|
|
10
|
+
const dy = y - top;
|
|
11
|
+
return [
|
|
12
|
+
`${Math.atan2(dy - height / 2, dx - width / 2)}rad`,
|
|
13
|
+
`${dx}px`,
|
|
14
|
+
`${dy}px`
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
/** Returns what stops it, and gives the pills their own light back. */
|
|
18
|
+
function followPointer(pills) {
|
|
19
|
+
let frame = 0;
|
|
20
|
+
const onMove = ({ clientX, clientY, pointerType }) => {
|
|
21
|
+
if (pointerType === "touch") return;
|
|
22
|
+
cancelAnimationFrame(frame);
|
|
23
|
+
frame = requestAnimationFrame(() => {
|
|
24
|
+
const aims = [...pills].map((pill) => [pill, aim(clientX, clientY, pill.getBoundingClientRect())]);
|
|
25
|
+
for (const [pill, values] of aims) PROPERTIES.forEach((property, index) => pill.style.setProperty(property, values[index]));
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
document.addEventListener("pointermove", onMove, { passive: true });
|
|
29
|
+
return () => {
|
|
30
|
+
document.removeEventListener("pointermove", onMove);
|
|
31
|
+
cancelAnimationFrame(frame);
|
|
32
|
+
for (const pill of pills) PROPERTIES.forEach((property) => pill.style.removeProperty(property));
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.aim = aim;
|
|
37
|
+
exports.followPointer = followPointer;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/PillOptics/light.ts
|
|
2
|
+
const PROPERTIES = [
|
|
3
|
+
"--_mp-angle",
|
|
4
|
+
"--_mp-sheen-x",
|
|
5
|
+
"--_mp-sheen-y"
|
|
6
|
+
];
|
|
7
|
+
/** Where the pointer is from the pill: the light's angle, and its highlight's place. Pure, so it can be tested. */
|
|
8
|
+
function aim(x, y, { left, top, width, height }) {
|
|
9
|
+
const dx = x - left;
|
|
10
|
+
const dy = y - top;
|
|
11
|
+
return [
|
|
12
|
+
`${Math.atan2(dy - height / 2, dx - width / 2)}rad`,
|
|
13
|
+
`${dx}px`,
|
|
14
|
+
`${dy}px`
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
/** Returns what stops it, and gives the pills their own light back. */
|
|
18
|
+
function followPointer(pills) {
|
|
19
|
+
let frame = 0;
|
|
20
|
+
const onMove = ({ clientX, clientY, pointerType }) => {
|
|
21
|
+
if (pointerType === "touch") return;
|
|
22
|
+
cancelAnimationFrame(frame);
|
|
23
|
+
frame = requestAnimationFrame(() => {
|
|
24
|
+
const aims = [...pills].map((pill) => [pill, aim(clientX, clientY, pill.getBoundingClientRect())]);
|
|
25
|
+
for (const [pill, values] of aims) PROPERTIES.forEach((property, index) => pill.style.setProperty(property, values[index]));
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
document.addEventListener("pointermove", onMove, { passive: true });
|
|
29
|
+
return () => {
|
|
30
|
+
document.removeEventListener("pointermove", onMove);
|
|
31
|
+
cancelAnimationFrame(frame);
|
|
32
|
+
for (const pill of pills) PROPERTIES.forEach((property) => pill.style.removeProperty(property));
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { aim, followPointer };
|