@spintax/dice-lite 0.0.0-stage → 0.2.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 +33 -0
- package/LICENSE +21 -0
- package/README.md +81 -2
- package/dice.d.ts +97 -0
- package/dice.js +819 -0
- package/dice.min.js +10 -0
- package/package.json +58 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@spintax/dice-lite`. The version is the contract: same (seed,
|
|
4
|
+
options) → identical bytes within a minor version; any change to the output bytes is a minor
|
|
5
|
+
bump.
|
|
6
|
+
|
|
7
|
+
## 0.2.0 — 2026-11-16
|
|
8
|
+
|
|
9
|
+
- The seams finish where the corner rounding starts. Both ends are trimmed — at the nose by
|
|
10
|
+
the tangent rule on each adjacent wedge, at the silhouette by intersection with the
|
|
11
|
+
rounded-corner Q-curve, the crossing chosen on the segment nearest the corner — and every
|
|
12
|
+
nose corner closes through a small arc, one per face, so every corner of every face reads
|
|
13
|
+
rounded alike. Each seam end stops one stroke-width short of the outline: an edge meets
|
|
14
|
+
the boundary and stops (ADR 007 addenda).
|
|
15
|
+
- `body` is part of the id key: paper and glass on one page never share an id.
|
|
16
|
+
- An arbitrary CSS colour pin on `die` frosts the glass verbatim, like `dieUnder` in paper.
|
|
17
|
+
- Library 8536 B gzipped, budget 8704 (ADR 006 addenda).
|
|
18
|
+
|
|
19
|
+
## 0.1.0 — 2026-11-16
|
|
20
|
+
|
|
21
|
+
- First release. A throw of dice on a table as an SVG string: the projected rounded cube
|
|
22
|
+
(three visible faces, exact projected pips, opposite sides sum to seven by construction),
|
|
23
|
+
seeded placement and values, the brand-coloured felt/rail/trim cascade with the light-theme
|
|
24
|
+
`line` escape, five results (`lucky7`, `snake`, `boxcars`, `matched`, `mixed`) as data,
|
|
25
|
+
`values` pins, the one-shot CSS settle with `prefers-reduced-motion` support, `palette()`,
|
|
26
|
+
`die()`, `init()` and `dice.d.ts`. The rail is a mode: `'auto'` (default) the dark surround,
|
|
27
|
+
`'brand'` the leftover brand colour, `'felt'` no edge, any colour pins it (ADR 004 addendum).
|
|
28
|
+
The dice gained edges: cut seams in quiet ink on the paper body, and `body: 'glass'` draws
|
|
29
|
+
frosted acrylic — silhouette fill at 55%, outline seams, solid pips, no gradients emitted
|
|
30
|
+
(ADR 007; the alpha measured by raster subtraction). The seams were then trimmed to stop
|
|
31
|
+
where the corner rounding starts — nose and silhouette alike — after the square-front-corner
|
|
32
|
+
defect was caught on the cover image (ADR 007 addendum). Library 8536 B gzipped (budget
|
|
33
|
+
8704, ADR 006 addendum); a `dice: 3` throw averages 9347 B raw / 2345 B gzip over 60 seeds.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 301ST (https://301.st)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,82 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dice-lite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**A throw of dice on a table, drawn by code.** One call returns an SVG string — in Node at
|
|
4
|
+
build time or in the browser — so a static site gets its hero art with no script on the page.
|
|
5
|
+
Two knobs span the variant space: **`seed`** throws the dice (how many, where they lie, what
|
|
6
|
+
they show), **`brand`** colours the table.
|
|
7
|
+
|
|
8
|
+
The dice are the classic ones — paper bodies, black pips, values one to six — each a rounded
|
|
9
|
+
cube projected at runtime from its own seeded orientation: three visible faces, exact
|
|
10
|
+
projected pips, opposite sides summing to seven by construction. A result such as a lucky
|
|
11
|
+
seven is a named preset that places values; nothing is ever scored. Zero dependencies, ~7 KB
|
|
12
|
+
minified and gzipped.
|
|
13
|
+
|
|
14
|
+
[](https://www.npmjs.com/package/@spintax/dice-lite)
|
|
15
|
+
[](LICENSE)
|
|
16
|
+
|
|
17
|
+
**[Live demo →](https://investblog.github.io/dice-lite/)** — the throw's options on controls:
|
|
18
|
+
seed stepping, a 24-seed contact sheet, every result, and the six faces.
|
|
19
|
+
|
|
20
|
+
[](https://investblog.github.io/dice-lite/)
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const Dice = require('@spintax/dice-lite');
|
|
24
|
+
|
|
25
|
+
Dice.toss({ seed: 'spintax.net' });
|
|
26
|
+
// → '<svg xmlns="http://www.w3.org/2000/svg" viewBox="…" …>…</svg>'
|
|
27
|
+
|
|
28
|
+
Dice.toss({
|
|
29
|
+
seed: 'hero', // a string or a 32-bit number; the default is 1
|
|
30
|
+
brand: ['#00abf3', '#d6af3c', '#a91455'], // hex or hex[] — the site's tokens
|
|
31
|
+
theme: 'dark', // 'dark' | 'light'
|
|
32
|
+
dice: 3, // 2–5; appends: dice 0..k-1 of k+1 are the k-dice throw
|
|
33
|
+
result: 'lucky7', // 'lucky7' | 'snake' | 'boxcars' | 'matched' | 'mixed'
|
|
34
|
+
values: [4, null, 2], // pin exact top faces; null lets the seed/result decide
|
|
35
|
+
motion: 'roll', // a one-shot settle; honours prefers-reduced-motion
|
|
36
|
+
size: 960, // width; the height follows the viewBox
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
Dice.die({ value: 5 }); // one die on its em — an icon
|
|
40
|
+
Dice.palette('#c2410c'); // → { felt, rail, trim, die, pip, ink, outline, … }
|
|
41
|
+
|
|
42
|
+
// browser: draw into an element and keep it drawn
|
|
43
|
+
const table = Dice.init('#hero', { seed: 'spintax.net' });
|
|
44
|
+
table.set({ result: 'snake' });
|
|
45
|
+
table.destroy();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Why it looks the way it looks
|
|
49
|
+
|
|
50
|
+
The brand's colours become the casino table: the first chromatic colour felts it, a gold-hue
|
|
51
|
+
colour tones the brass trim; the dice stay paper with near-black pips in every brand. The
|
|
52
|
+
table's edge wears that same near-black — a dark surround, never a colour — and `rail:
|
|
53
|
+
'brand'` / `rail: 'felt'` / any colour pin reshapes it (`'brand'` brings back the brand-coloured
|
|
54
|
+
pad). Under `theme: 'light'` the table defaults to a `line` drawing — a filled felt over a
|
|
55
|
+
white page was a pastel field in every sibling that tried it. The picture carries **no fixed
|
|
56
|
+
path data**: every coordinate is computed from the seed, so same (seed, options) → identical
|
|
57
|
+
bytes, always.
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
npm install @spintax/dice-lite
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
ES5, one file, UMD tail — CommonJS, ESM default import and a plain `<script>` (global
|
|
66
|
+
`Dice`) all work. `dice.d.ts` ships the types. **[Playground →](https://investblog.github.io/dice-lite/)**
|
|
67
|
+
|
|
68
|
+
## The family
|
|
69
|
+
|
|
70
|
+
dice-lite is a sibling of seven zero-dependency libraries by the same hands —
|
|
71
|
+
[slots-lite](https://github.com/investblog/slots-lite) (its closest relative — a slot machine
|
|
72
|
+
drawn whole, whose colour engine and seeding it shares),
|
|
73
|
+
[cards-lite](https://github.com/investblog/cards-lite),
|
|
74
|
+
[roulette-lite](https://github.com/investblog/roulette-lite),
|
|
75
|
+
[trigons-lite](https://github.com/investblog/trigons-lite),
|
|
76
|
+
[hexagons-lite](https://github.com/investblog/hexagons-lite),
|
|
77
|
+
[octagons](https://github.com/investblog/octagons) and
|
|
78
|
+
[qr-svg](https://github.com/investblog/qr-svg).
|
|
79
|
+
|
|
80
|
+
## Licence and credits
|
|
81
|
+
|
|
82
|
+
MIT. Made in [301](https://301.st) · for [spintax.net](https://spintax.net).
|
package/dice.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Types for dice-lite. The library ships as one ES5 file with a UMD tail (ADR 001), so this
|
|
2
|
+
// declares a single value and exports it with `export =` — `import Dice from
|
|
3
|
+
// '@spintax/dice-lite'` is the idiomatic use, and named imports are not the module's shape.
|
|
4
|
+
|
|
5
|
+
type Colour = string;
|
|
6
|
+
type Pin = Colour | 'auto';
|
|
7
|
+
/** rail: a colour pin, or a named mode — 'brand' the leftover brand colour, 'felt' no visible edge. */
|
|
8
|
+
type RailPin = Pin | 'brand' | 'felt';
|
|
9
|
+
|
|
10
|
+
declare namespace Dice {
|
|
11
|
+
/** A name that places values. Nothing is ever scored (ADR 003). */
|
|
12
|
+
type Result = 'lucky7' | 'snake' | 'boxcars' | 'matched' | 'mixed' | '';
|
|
13
|
+
type Lattice = 'auto' | 'trigon' | 'hex' | 'octagon' | 'none';
|
|
14
|
+
|
|
15
|
+
/** Shared by toss() and die(). */
|
|
16
|
+
interface Common {
|
|
17
|
+
/** A string (a domain name is fine) or a number taken as a 32-bit unsigned integer. */
|
|
18
|
+
seed?: number | string;
|
|
19
|
+
/** hex or hex[]; default the spintax triad. */
|
|
20
|
+
brand?: string | string[];
|
|
21
|
+
/** derived colours only. */
|
|
22
|
+
theme?: 'dark' | 'light';
|
|
23
|
+
/** the table style; default flat on dark, line on light. */
|
|
24
|
+
style?: 'line' | 'flat';
|
|
25
|
+
/** line weight multiplier. */
|
|
26
|
+
weight?: number;
|
|
27
|
+
/** the dice bodies: paper (default) or frosted glass (ADR 007). */
|
|
28
|
+
body?: 'paper' | 'glass';
|
|
29
|
+
felt?: Pin;
|
|
30
|
+
rail?: RailPin;
|
|
31
|
+
trim?: Pin;
|
|
32
|
+
die?: Pin;
|
|
33
|
+
pip?: Pin;
|
|
34
|
+
ink?: Pin;
|
|
35
|
+
outline?: Pin;
|
|
36
|
+
/** width; the height follows the viewBox. */
|
|
37
|
+
size?: number;
|
|
38
|
+
/** decimals for coordinates. */
|
|
39
|
+
precision?: number;
|
|
40
|
+
/** extra entropy for ids — the same picture twice on one page. */
|
|
41
|
+
salt?: string;
|
|
42
|
+
/** role="img" and an escaped aria-label; otherwise aria-hidden. */
|
|
43
|
+
title?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface TossOptions extends Common {
|
|
47
|
+
/** how many dice, 2–5, default 3; appends — dice 0..k-1 of k+1 are the k-dice throw. */
|
|
48
|
+
dice?: number;
|
|
49
|
+
/** names values, never scores; '' (default) lets the seed show. */
|
|
50
|
+
result?: Result;
|
|
51
|
+
/** pins exact top values per die; null or outside 1–6 leaves the die to the seed/result. */
|
|
52
|
+
values?: Array<number | null | undefined>;
|
|
53
|
+
lattice?: Lattice;
|
|
54
|
+
/** 'roll' or true — a one-shot settle (ADR 005 addendum); off by default. */
|
|
55
|
+
motion?: 'roll' | boolean;
|
|
56
|
+
/** divides the settle periods, default 1; 0 returns the static bytes exactly. */
|
|
57
|
+
speed?: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface DieOptions extends Common {
|
|
61
|
+
/** the top face, 1–6; the seed picks when absent. */
|
|
62
|
+
value?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface Palette {
|
|
66
|
+
felt: string;
|
|
67
|
+
rail: string;
|
|
68
|
+
trim: string;
|
|
69
|
+
die: string;
|
|
70
|
+
pip: string;
|
|
71
|
+
ink: string;
|
|
72
|
+
outline: string;
|
|
73
|
+
background: string;
|
|
74
|
+
halo: string;
|
|
75
|
+
stroke: string[];
|
|
76
|
+
[key: string]: string | string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface Handle {
|
|
80
|
+
el: Element;
|
|
81
|
+
get(): TossOptions;
|
|
82
|
+
set(opts: Partial<TossOptions>): void;
|
|
83
|
+
destroy(): void;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
declare const Dice: {
|
|
88
|
+
/** Pure: the whole throw — table + dice — as an SVG string. Node and browser. */
|
|
89
|
+
toss(opts?: Dice.TossOptions): string;
|
|
90
|
+
/** Pure: one die on its em — an icon. */
|
|
91
|
+
die(opts?: Dice.DieOptions): string;
|
|
92
|
+
palette(brand?: string | string[], opts?: { theme?: 'dark' | 'light'; rail?: RailPin }): Dice.Palette;
|
|
93
|
+
/** Browser only. Returns null when the target finds nothing. */
|
|
94
|
+
init(target: string | Element, opts?: Dice.TossOptions): Dice.Handle | null;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export = Dice;
|
package/dice.js
ADDED
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* dice-lite — a procedural throw of dice as an SVG string.
|
|
3
|
+
* MIT © 301ST (https://301.st) · for spintax.net
|
|
4
|
+
*
|
|
5
|
+
* One file, ES5, zero dependencies. Works as a <script> (defines `Dice`) and as a CommonJS
|
|
6
|
+
* module (Node, bundlers) — ADR 001. The spec is docs/README.md: change the doc before the code.
|
|
7
|
+
* The colour engine and the seed machinery are the family's, ported unchanged from cards-lite
|
|
8
|
+
* (by way of slots-lite); a fixture test pins that they agree.
|
|
9
|
+
*/
|
|
10
|
+
(function (root, factory) {
|
|
11
|
+
var api = factory();
|
|
12
|
+
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
13
|
+
else root.Dice = api;
|
|
14
|
+
})(typeof self !== 'undefined' ? self : this, function () {
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
var NS = 'http://www.w3.org/2000/svg';
|
|
18
|
+
var DEFAULT_BRAND = ['#00abf3', '#d6af3c', '#a91455'];
|
|
19
|
+
// machine space: origin at the centre of the table, y down (spec: Machine space). The
|
|
20
|
+
// constants are provisional — looked at on screen at M1/M2 and kept or changed there.
|
|
21
|
+
// machine space: origin at the centre of the table, y down. The die is a rounded cube,
|
|
22
|
+
// orthographic-baked (ADR 002 addendum); constants tuned to the reference at M1.
|
|
23
|
+
var H = 100; // the die's half-edge — the cube is 2H on a side
|
|
24
|
+
var PITCH = 0.91; // camera elevation ≈ 52°
|
|
25
|
+
var RS = 44, RF = 9; // silhouette rounding / face-quad rounding (projection units)
|
|
26
|
+
var RPIP = 19; // pip radius in 3D units — drawn as the exact projected ellipse
|
|
27
|
+
var HG = H + 2; // faces overlap 2 units at the seams, clipped by the silhouette
|
|
28
|
+
var YAW = [0.36, 0.08]; // yaw = sign × (0.36 + 0.08·u) — the readable range (Codex's math)
|
|
29
|
+
var SQRT3 = Math.sqrt(3), A_REG = (2 - Math.SQRT2) / 2; // 0.29289… exact regular octagon
|
|
30
|
+
var MARGIN = [90, 64]; // felt inset from the viewBox: long side, short side
|
|
31
|
+
var RAIL = [22, 34]; // the padded ring's width range, one seeded draw (table:rail)
|
|
32
|
+
var SLOT_A = 126; // canonical slot distance from the centre (0.9 × 140)
|
|
33
|
+
var JIT = 0.06; // positional scatter as a fraction of the slot distance
|
|
34
|
+
var HX = 145, HY = 176; // projected half-extents of a die (yaw range + jitter room)
|
|
35
|
+
|
|
36
|
+
// ── numbers and markup ──────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
// Round, then String: no exponent notation, and no "-0" (it is a different string from "0").
|
|
39
|
+
function n(v, d) {
|
|
40
|
+
var p = Math.pow(10, d || 0), r = Math.round(v * p) / p;
|
|
41
|
+
return String(r === 0 ? 0 : r);
|
|
42
|
+
}
|
|
43
|
+
function esc(s) {
|
|
44
|
+
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
45
|
+
}
|
|
46
|
+
// attrs is a flat [name, value, …] list; a null value drops the attribute
|
|
47
|
+
function el(tag, attrs, inner) {
|
|
48
|
+
var s = '<' + tag;
|
|
49
|
+
for (var i = 0; i < attrs.length; i += 2) if (attrs[i + 1] != null) s += ' ' + attrs[i] + '="' + attrs[i + 1] + '"';
|
|
50
|
+
return inner == null ? s + '/>' : s + '>' + inner + '</' + tag + '>';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ── seeds ───────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
function fnv(str) {
|
|
56
|
+
var h = 0x811c9dc5;
|
|
57
|
+
for (var i = 0; i < str.length; i++) h = Math.imul(h ^ str.charCodeAt(i), 0x01000193);
|
|
58
|
+
return h >>> 0;
|
|
59
|
+
}
|
|
60
|
+
function fmix(h) {
|
|
61
|
+
h = Math.imul(h ^ h >>> 16, 0x85ebca6b);
|
|
62
|
+
h = Math.imul(h ^ h >>> 13, 0xc2b2ae35);
|
|
63
|
+
return (h ^ h >>> 16) >>> 0;
|
|
64
|
+
}
|
|
65
|
+
function mulberry(a) {
|
|
66
|
+
return function () {
|
|
67
|
+
a = (a + 0x6D2B79F5) >>> 0;
|
|
68
|
+
var x = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
69
|
+
x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;
|
|
70
|
+
return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
// Every parameter reads its OWN stream, keyed by name. Nothing shares a cursor, so adding a
|
|
74
|
+
// parameter later never re-rolls an existing throw (the hexagons lesson).
|
|
75
|
+
function streams(seed) {
|
|
76
|
+
var s = typeof seed === 'number' ? seed >>> 0 : fnv(String(seed));
|
|
77
|
+
return function (key) { return mulberry(fmix(s ^ Math.imul(fnv(key), 0x9E3779B9))); };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ── auto-palette: sRGB <-> CIE LCh(ab), D65 — ported unchanged from hexagons ──
|
|
81
|
+
|
|
82
|
+
function parseColor(str) {
|
|
83
|
+
str = String(str).trim();
|
|
84
|
+
if (str.charAt(0) === '#') {
|
|
85
|
+
var h = str.slice(1);
|
|
86
|
+
if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
|
|
87
|
+
var v = parseInt(h, 16);
|
|
88
|
+
return [v >> 16 & 255, v >> 8 & 255, v & 255];
|
|
89
|
+
}
|
|
90
|
+
var m = str.match(/rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/);
|
|
91
|
+
return m ? [+m[1], +m[2], +m[3]] : [255, 255, 255];
|
|
92
|
+
}
|
|
93
|
+
function toHex(c) {
|
|
94
|
+
return '#' + ((1 << 24) + ((c[0] | 0) << 16) + ((c[1] | 0) << 8) + (c[2] | 0)).toString(16).slice(1);
|
|
95
|
+
}
|
|
96
|
+
function s2l(u) { u /= 255; return u <= 0.04045 ? u / 12.92 : Math.pow((u + 0.055) / 1.055, 2.4); }
|
|
97
|
+
function l2s(u) { return 255 * (u <= 0.0031308 ? u * 12.92 : 1.055 * Math.pow(u, 1 / 2.4) - 0.055); }
|
|
98
|
+
function fwd(t) { return t > 216 / 24389 ? Math.pow(t, 1 / 3) : (24389 / 27 * t + 16) / 116; }
|
|
99
|
+
function inv(t) { var c = t * t * t; return c > 216 / 24389 ? c : (116 * t - 16) * 27 / 24389; }
|
|
100
|
+
|
|
101
|
+
function rgb2lch(rgb) {
|
|
102
|
+
var r = s2l(rgb[0]), g = s2l(rgb[1]), b = s2l(rgb[2]);
|
|
103
|
+
var x = (0.41246 * r + 0.35758 * g + 0.18044 * b) / 0.95047;
|
|
104
|
+
var y = 0.21267 * r + 0.71515 * g + 0.07218 * b;
|
|
105
|
+
var z = (0.01933 * r + 0.11919 * g + 0.9503 * b) / 1.08883;
|
|
106
|
+
var fx = fwd(x), fy = fwd(y), fz = fwd(z);
|
|
107
|
+
var L = 116 * fy - 16, A = 500 * (fx - fy), B = 200 * (fy - fz);
|
|
108
|
+
var C = Math.sqrt(A * A + B * B);
|
|
109
|
+
var H = Math.atan2(B, A) * 180 / Math.PI;
|
|
110
|
+
return [L, C, (H + 360) % 360];
|
|
111
|
+
}
|
|
112
|
+
function lch2lin(L, C, H) {
|
|
113
|
+
var A = C * Math.cos(H * Math.PI / 180), B = C * Math.sin(H * Math.PI / 180);
|
|
114
|
+
var fy = (L + 16) / 116, fx = fy + A / 500, fz = fy - B / 200;
|
|
115
|
+
var x = inv(fx) * 0.95047, y = inv(fy), z = inv(fz) * 1.08883;
|
|
116
|
+
return [
|
|
117
|
+
3.24045 * x - 1.53714 * y - 0.49853 * z,
|
|
118
|
+
-0.96927 * x + 1.87601 * y + 0.04156 * z,
|
|
119
|
+
0.05564 * x - 0.20403 * y + 1.05723 * z
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
// Gamut policy: hold L and H, reduce C until inside sRGB. Never channel-clip — clipping
|
|
123
|
+
// shifts hue, and hue is the brand's identity.
|
|
124
|
+
function lch2rgb(L, C, H) {
|
|
125
|
+
var lin = lch2lin(L, C, H), lo = 0, hi = C, i;
|
|
126
|
+
if (!inGamut(lin)) {
|
|
127
|
+
for (i = 0; i < 20; i++) {
|
|
128
|
+
var mid = (lo + hi) / 2;
|
|
129
|
+
lin = lch2lin(L, mid, H);
|
|
130
|
+
if (inGamut(lin)) lo = mid; else hi = mid;
|
|
131
|
+
}
|
|
132
|
+
lin = lch2lin(L, lo, H);
|
|
133
|
+
}
|
|
134
|
+
return [clamp255(l2s(lin[0])), clamp255(l2s(lin[1])), clamp255(l2s(lin[2]))];
|
|
135
|
+
}
|
|
136
|
+
function inGamut(lin) {
|
|
137
|
+
for (var i = 0; i < 3; i++) if (lin[i] < -0.0005 || lin[i] > 1.0005) return false;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
function clamp255(v) { return Math.max(0, Math.min(255, Math.round(v))); }
|
|
141
|
+
function lum(rgb) { return 0.2126 * s2l(rgb[0]) + 0.7152 * s2l(rgb[1]) + 0.0722 * s2l(rgb[2]); }
|
|
142
|
+
function contrast(a, b) {
|
|
143
|
+
var x = lum(a), y = lum(b);
|
|
144
|
+
return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
|
|
145
|
+
}
|
|
146
|
+
// Repair by shifting L only (fidelity: hue and chroma are the brand's)
|
|
147
|
+
function ensureContrast(rgb, bgRgb, min, dir) {
|
|
148
|
+
if (contrast(rgb, bgRgb) >= min) return rgb;
|
|
149
|
+
var lch = rgb2lch(rgb);
|
|
150
|
+
for (var L = lch[0]; L >= 2 && L <= 98; L += dir * 2) {
|
|
151
|
+
var c = lch2rgb(L, lch[1], lch[2]);
|
|
152
|
+
if (contrast(c, bgRgb) >= min) return c;
|
|
153
|
+
}
|
|
154
|
+
return rgb;
|
|
155
|
+
}
|
|
156
|
+
// hexagons' derive(), unchanged: same input, same colours as Hexagons.palette().
|
|
157
|
+
function derive(brand, theme) {
|
|
158
|
+
var arr = (typeof brand === 'string' ? [brand] : brand).map(function (x) {
|
|
159
|
+
return rgb2lch(parseColor(x));
|
|
160
|
+
});
|
|
161
|
+
var chrom = [];
|
|
162
|
+
for (var i = 0; i < arr.length; i++) if (arr[i][1] >= 12) chrom.push(arr[i]);
|
|
163
|
+
var neutral = chrom.length === 0;
|
|
164
|
+
var prim = neutral ? [50, 0, 0] : chrom[0];
|
|
165
|
+
var acc = chrom.length > 1 ? chrom[1] : [0, prim[1], (prim[2] + 40) % 360];
|
|
166
|
+
var hotH = (chrom.length > 2 ? chrom[2] : prim)[2];
|
|
167
|
+
var C = prim[1], H = prim[2], light = theme === 'light';
|
|
168
|
+
|
|
169
|
+
var bg = light
|
|
170
|
+
? lch2rgb(97, neutral ? 0 : 3, H)
|
|
171
|
+
: lch2rgb(6, Math.min(C * 0.2, 10), H);
|
|
172
|
+
var halo = light
|
|
173
|
+
? lch2rgb(92, Math.min(C * 0.2, 8), H)
|
|
174
|
+
: lch2rgb(12, Math.min(C * 0.3, 15), H);
|
|
175
|
+
var Ls = light ? [72, 52, 32] : [32, 58, 82];
|
|
176
|
+
var dir = light ? -1 : 1;
|
|
177
|
+
var stops = [
|
|
178
|
+
ensureContrast(lch2rgb(Ls[0], C * 0.9, H), bg, 1.5, dir),
|
|
179
|
+
ensureContrast(lch2rgb(Ls[1], C, H), bg, 2.5, dir),
|
|
180
|
+
ensureContrast(lch2rgb(Ls[2], C * 0.55, H), bg, 5, dir)
|
|
181
|
+
];
|
|
182
|
+
var accent = ensureContrast(lch2rgb(light ? 45 : 70, acc[1], acc[2]), bg, 3, dir);
|
|
183
|
+
var hot = ensureContrast(
|
|
184
|
+
lch2rgb(light ? 28 : 92, neutral ? 0 : (light ? 24 : 12), hotH), bg, 7, dir);
|
|
185
|
+
|
|
186
|
+
return { colors: stops, accent: accent, hot: hot, background: bg, halo: halo };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ── colour: a casino table from the brand (ADR 004, provisional until M1) ──
|
|
190
|
+
|
|
191
|
+
// Dice claim almost no classic hues — the subject is paper and black — so the brand colours
|
|
192
|
+
// go where a casino table puts them: the felt takes the first chromatic brand colour, the
|
|
193
|
+
// trim is metal claiming a gold-hue colour (slots' formula). die, pip, ink and outline are
|
|
194
|
+
// derived, never brand colours — and neither is the rail's edge by default (ADR 004 addendum)
|
|
195
|
+
function roles(brand, theme, railMode) {
|
|
196
|
+
var list = brand == null ? DEFAULT_BRAND : typeof brand === 'string' ? [brand] : brand;
|
|
197
|
+
var chrom = [], grey = null, i;
|
|
198
|
+
for (i = 0; i < list.length; i++) {
|
|
199
|
+
var c = rgb2lch(parseColor(list[i]));
|
|
200
|
+
if (c[1] >= 12) chrom.push({ lch: c, hex: list[i] });
|
|
201
|
+
else if (!grey) grey = list[i];
|
|
202
|
+
}
|
|
203
|
+
var dk = theme === 'light' ? 'light' : 'dark';
|
|
204
|
+
// felt: the first chromatic brand colour; else the grey one; else the spintax blue
|
|
205
|
+
var feltHex = chrom.length ? chrom[0].hex : grey || '#00abf3';
|
|
206
|
+
var f = rgb2lch(parseColor(feltHex));
|
|
207
|
+
var bC = f[1], bH = f[2];
|
|
208
|
+
var felt = derive(feltHex, dk);
|
|
209
|
+
// the family's capture with the table's windows, each colour at most once: the felt
|
|
210
|
+
// takes the first chromatic colour; trim claims a gold-hue colour for the brass — not
|
|
211
|
+
// the felt's. The leftover colour is claimed only by rail: 'brand' (ADR 004)
|
|
212
|
+
var goldI = -1, goldD = Infinity;
|
|
213
|
+
for (i = 1; i < chrom.length; i++) {
|
|
214
|
+
var d = Math.abs(chrom[i].lch[2] - 85) % 360;
|
|
215
|
+
d = Math.min(d, 360 - d);
|
|
216
|
+
if (d < goldD) { goldD = d; goldI = i; }
|
|
217
|
+
}
|
|
218
|
+
if (goldD > 30) goldI = -1; // the window is ±30° around the gold centre
|
|
219
|
+
var gold = goldI >= 0 ? chrom[goldI].lch : null;
|
|
220
|
+
var trim = lch2rgb(66, grey || !chrom.length ? 0 : gold ? gold[1] * 0.5 : 25, gold ? gold[2] : 85);
|
|
221
|
+
// the dice are the classic: paper in both themes, near-black pips and shadows (cards'
|
|
222
|
+
// stock/spade/ink formulas, the paper as their contrast reference)
|
|
223
|
+
var die = lch2rgb(96.5, Math.min(bC * 0.08, 4), bH);
|
|
224
|
+
var pip = ensureContrast(lch2rgb(14, Math.min(bC * 0.15, 6), bH), die, 7, -1);
|
|
225
|
+
var ink = ensureContrast(lch2rgb(34, Math.min(bC * 0.25, 10), bH), die, 3, -1);
|
|
226
|
+
// the rail is a mode, not a colour (ADR 004 addendum): 'auto' — the pip family's
|
|
227
|
+
// near-black, the dark surround a casino table wears; 'brand' — the leftover chromatic
|
|
228
|
+
// colour, else a darker derive() stop of the felt; 'felt' — painted the felt, no edge
|
|
229
|
+
var railHex = null;
|
|
230
|
+
for (i = 1; i < chrom.length; i++) {
|
|
231
|
+
if (i !== goldI) { railHex = chrom[i].hex; break; }
|
|
232
|
+
}
|
|
233
|
+
var rail = railMode === 'brand' ? (railHex ? parseColor(railHex) : felt.colors[dk === 'light' ? 2 : 0])
|
|
234
|
+
: railMode === 'felt' ? parseColor(feltHex) : pip;
|
|
235
|
+
// the field the table sits on is the family's palette of the whole brand, so a throw on a
|
|
236
|
+
// sibling field reads as one picture (pinned by the parity fixture)
|
|
237
|
+
var field = derive(list, dk);
|
|
238
|
+
return {
|
|
239
|
+
felt: parseColor(feltHex),
|
|
240
|
+
rail: rail,
|
|
241
|
+
trim: trim,
|
|
242
|
+
die: die, pip: pip, ink: ink,
|
|
243
|
+
// a line table's rule: the felt hue's middle stop, which derive() holds ≥ 2.5
|
|
244
|
+
// against the page
|
|
245
|
+
outline: felt.colors[1],
|
|
246
|
+
stroke: field.colors, background: field.background, halo: field.halo
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function palette(brand, opts) {
|
|
251
|
+
var pin = opts && opts.rail, r = roles(brand, opts && opts.theme, pin), out = {};
|
|
252
|
+
for (var k in r) out[k] = k === 'stroke' ? r[k].map(toHex) : toHex(r[k]);
|
|
253
|
+
// pins win here as in the renderer: an arbitrary colour comes back verbatim (spec: Pins)
|
|
254
|
+
if (pin != null && pin !== 'auto' && pin !== 'brand' && pin !== 'felt') out.rail = pin;
|
|
255
|
+
return out;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// seeded ids: [a-z][a-z0-9]{5}, never repeated within one picture — cards-lite's, unchanged
|
|
259
|
+
function tokens(S, salt) {
|
|
260
|
+
var r = S('ids' + (salt || '')), used = {}, AZ = 'abcdefghijklmnopqrstuvwxyz', AZ09 = AZ + '0123456789';
|
|
261
|
+
return function () {
|
|
262
|
+
var tk;
|
|
263
|
+
do {
|
|
264
|
+
tk = AZ.charAt(Math.floor(r() * 26));
|
|
265
|
+
for (var i = 0; i < 5; i++) tk += AZ09.charAt(Math.floor(r() * 36));
|
|
266
|
+
} while (used[tk]);
|
|
267
|
+
used[tk] = 1;
|
|
268
|
+
return tk;
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ── the context ─────────────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
function context(o, what) {
|
|
275
|
+
var S = streams(o.seed == null ? 1 : o.seed), r = roles(o.brand, o.theme, o.rail), seen = {}, key = what;
|
|
276
|
+
var c = {
|
|
277
|
+
o: o, S: S, defs: '',
|
|
278
|
+
p: o.precision == null ? 0 : o.precision,
|
|
279
|
+
w: o.weight == null ? 1 : o.weight,
|
|
280
|
+
// the table style: the caller's, or flat on dark and line on light (ADR 004)
|
|
281
|
+
cab: o.style ? o.style !== 'line' : o.theme !== 'light',
|
|
282
|
+
col: function (role) {
|
|
283
|
+
var pin = o[role];
|
|
284
|
+
// rail's 'brand'/'felt' are modes resolved in roles(), never literal colours
|
|
285
|
+
return pin != null && pin !== 'auto' && !(role === 'rail' && (pin === 'brand' || pin === 'felt')) ? esc(pin) : toHex(r[role]);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
// Ids are keyed by WHAT IS DRAWN — the geometry and every colour a def carries — so two
|
|
289
|
+
// different throws under one seed on one page never share an id (cards-lite's lesson).
|
|
290
|
+
// salt remains for the same picture twice.
|
|
291
|
+
for (var k in r) key += k === 'stroke' ? '' : c.col(k);
|
|
292
|
+
// normalised, so passing a default ('auto', '', []) is byte-identical to leaving it out
|
|
293
|
+
key += c.cab + c.w + c.p + (o.lattice || 'auto') + (o.body === 'glass' ? 'g' : '');
|
|
294
|
+
c.key = key;
|
|
295
|
+
c.tk = tokens(S, (o.salt || '') + key);
|
|
296
|
+
c.add = function (k, make) {
|
|
297
|
+
if (!seen[k]) { var id = c.tk(), m; seen[k] = id; m = make(); c.defs += m.replace('"%"', '"' + id + '"'); }
|
|
298
|
+
return seen[k];
|
|
299
|
+
};
|
|
300
|
+
return c;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function wrap(o, c, box, inner) {
|
|
304
|
+
var a11y = o.title ? ['role', 'img', 'aria-label', esc(o.title)] : ['aria-hidden', 'true'];
|
|
305
|
+
var vb = box.map(function (v) { return n(v, c.p); }).join(' ');
|
|
306
|
+
return el('svg', ['xmlns', NS, 'viewBox', vb, 'width', o.size == null ? null : n(o.size),
|
|
307
|
+
'height', o.size == null ? null : n(o.size * box[3] / box[2])].concat(a11y),
|
|
308
|
+
(c.defs ? el('defs', [], c.defs) : '') + inner);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ── the lattice: the family's own tiles (cards-lite's back port) ────────
|
|
312
|
+
|
|
313
|
+
// The three `d` builders are the siblings' own, copied with their seam comments — both were
|
|
314
|
+
// earned by defects, and re-deriving them would re-earn them. Sources: trigons-lite.js:427,
|
|
315
|
+
// octagons.js:463, hexagons.js:870, by way of cards.js.
|
|
316
|
+
function lattice(kind, p2, flat) {
|
|
317
|
+
var d = [];
|
|
318
|
+
var q = function (v) { return Math.round(v * 1000) / 1000; };
|
|
319
|
+
var seg = function (x1, y1, x2, y2) {
|
|
320
|
+
// flat orientation is the same tile with x and y swapped (hexagons')
|
|
321
|
+
d.push(flat ? 'M' + q(y1) + ' ' + q(x1) + 'L' + q(y2) + ' ' + q(x2)
|
|
322
|
+
: 'M' + q(x1) + ' ' + q(y1) + 'L' + q(x2) + ' ' + q(y2));
|
|
323
|
+
};
|
|
324
|
+
if (kind === 'trigon') {
|
|
325
|
+
// border segments sit on both opposite edges so the half-clipped strokes of
|
|
326
|
+
// neighbouring tiles sum to full weight; butt caps keep the vertices sharp
|
|
327
|
+
seg(0, 0, p2, 0); seg(0, p2, p2, p2);
|
|
328
|
+
seg(0, 0, 0, p2); seg(p2, 0, p2, p2);
|
|
329
|
+
seg(p2, 0, 0, p2);
|
|
330
|
+
} else if (kind === 'octagon') {
|
|
331
|
+
var a = A_REG * p2;
|
|
332
|
+
// Flats sit exactly on the tile edge, so a stroke there is half-clipped. Drawing each on
|
|
333
|
+
// BOTH opposite edges lets the neighbouring tile supply the missing half.
|
|
334
|
+
seg(a, 0, p2 - a, 0); seg(a, p2, p2 - a, p2);
|
|
335
|
+
seg(0, a, 0, p2 - a); seg(p2, a, p2, p2 - a);
|
|
336
|
+
seg(p2 - a, 0, p2, a); seg(p2, p2 - a, p2 - a, p2);
|
|
337
|
+
seg(a, p2, 0, p2 - a); seg(0, a, a, 0);
|
|
338
|
+
} else {
|
|
339
|
+
var sz = p2 / SQRT3;
|
|
340
|
+
var cell = function (cx, cy, r) {
|
|
341
|
+
var w2 = SQRT3 / 2 * r;
|
|
342
|
+
var v = [[cx, cy - r], [cx + w2, cy - r / 2], [cx + w2, cy + r / 2],
|
|
343
|
+
[cx, cy + r], [cx - w2, cy + r / 2], [cx - w2, cy - r / 2]];
|
|
344
|
+
for (var k = 0; k < 6; k++) seg(v[k][0], v[k][1], v[(k + 1) % 6][0], v[(k + 1) % 6][1]);
|
|
345
|
+
};
|
|
346
|
+
// the centre cell's side walls sit on the tile edge, so both are drawn (the octagons' seam)
|
|
347
|
+
cell(p2 / 2, sz, sz);
|
|
348
|
+
// the one wall not on the centre cell: between the two half-cells
|
|
349
|
+
seg(p2 / 2, 2 * sz, p2 / 2, 3 * sz);
|
|
350
|
+
}
|
|
351
|
+
return d.join('');
|
|
352
|
+
}
|
|
353
|
+
var KINDS = ['trigon', 'octagon', 'hex'];
|
|
354
|
+
|
|
355
|
+
// the felt's lattice: one seeded tile in `col` at a low weight, as the pattern fill the felt
|
|
356
|
+
// carries — or none at all, `lattice: 'none'`
|
|
357
|
+
function tile(c, col, op) {
|
|
358
|
+
var S = c.S, o = c.o;
|
|
359
|
+
if (o.lattice === 'none') return ['fill', 'none'];
|
|
360
|
+
var kind = KINDS.indexOf(o.lattice) >= 0 ? o.lattice : KINDS[Math.floor(S('table:lattice')() * 3)];
|
|
361
|
+
var pitch = 22 + 22 * S('table:pitch')(), flatHex = kind === 'hex' && S('table:orient')() < 0.5;
|
|
362
|
+
var tall = kind === 'hex' ? 3 * (pitch / SQRT3) : pitch;
|
|
363
|
+
return ['fill', 'url(#' + c.add('tile' + col + op, function () {
|
|
364
|
+
return el('pattern', ['id', '%', 'patternUnits', 'userSpaceOnUse',
|
|
365
|
+
'width', n(flatHex ? tall : pitch, 2), 'height', n(flatHex ? pitch : tall, 2),
|
|
366
|
+
'patternTransform', 'translate(' + n(pitch * S('table:phase')(), 2) + ' ' + n(pitch * S('table:phase2')(), 2) +
|
|
367
|
+
') rotate(' + n(90 * S('table:turn')(), 2) + ')'],
|
|
368
|
+
// butt caps (the default, never set): round caps blunt the vertices (hexagons.js:918)
|
|
369
|
+
el('path', ['d', lattice(kind, pitch, flatHex), 'fill', 'none', 'stroke', col,
|
|
370
|
+
'stroke-width', n((1 + 1.4 * S('table:weight')()) * c.w, 2), 'stroke-opacity', op]));
|
|
371
|
+
}) + ')'];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ── the die: a rounded cube, projected at runtime ───────────────────────
|
|
375
|
+
|
|
376
|
+
// Nothing here is drawn in 3D — the cube, its faces and its pips are computed and baked
|
|
377
|
+
// flat, so the output still carries no path data the seed cannot change (ADR 002 addendum).
|
|
378
|
+
// The values live on a real net: opposite sides sum to seven by construction, because the
|
|
379
|
+
// die RESTS on the face opposite its top value (REST, six entries).
|
|
380
|
+
var rotY = function (p, a) { var c = Math.cos(a), s = Math.sin(a); return [p[0] * c + p[2] * s, p[1], -p[0] * s + p[2] * c]; };
|
|
381
|
+
var rotX = function (p, a) { var c = Math.cos(a), s = Math.sin(a); return [p[0], p[1] * c - p[2] * s, p[1] * s + p[2] * c]; };
|
|
382
|
+
var add3 = function (a, b, s) { return [a[0] + b[0] * (s == null ? 1 : s), a[1] + b[1] * (s == null ? 1 : s), a[2] + b[2] * (s == null ? 1 : s)]; };
|
|
383
|
+
var cross3 = function (a, b) { return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]; };
|
|
384
|
+
var norm3 = function (v) { var l = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) || 1; return [v[0] / l, v[1] / l, v[2] / l]; };
|
|
385
|
+
|
|
386
|
+
// the six faces: normal, centre, u axis, v axis, value (opposites sum to seven)
|
|
387
|
+
var CUBE = [
|
|
388
|
+
{ n: [0, 1, 0], c: [0, H, 0], u: [1, 0, 0], v: [0, 0, 1], val: 1 },
|
|
389
|
+
{ n: [0, -1, 0], c: [0, -H, 0], u: [1, 0, 0], v: [0, 0, -1], val: 6 },
|
|
390
|
+
{ n: [0, 0, 1], c: [0, 0, H], u: [1, 0, 0], v: [0, 1, 0], val: 2 },
|
|
391
|
+
{ n: [0, 0, -1], c: [0, 0, -H], u: [-1, 0, 0], v: [0, 1, 0], val: 5 },
|
|
392
|
+
{ n: [1, 0, 0], c: [H, 0, 0], u: [0, 0, -1], v: [0, 1, 0], val: 3 },
|
|
393
|
+
{ n: [-1, 0, 0], c: [-H, 0, 0], u: [0, 0, 1], v: [0, 1, 0], val: 4 }
|
|
394
|
+
];
|
|
395
|
+
var rotZ = function (p, a) { var c = Math.cos(a), s = Math.sin(a); return [p[0] * c - p[1] * s, p[0] * s + p[1] * c, p[2]]; };
|
|
396
|
+
// rest-rotations per top value: which face the die lies on
|
|
397
|
+
var REST = {
|
|
398
|
+
1: function (p) { return p; }, 6: function (p) { return rotX(p, Math.PI); },
|
|
399
|
+
2: function (p) { return rotX(p, -Math.PI / 2); }, 5: function (p) { return rotX(p, Math.PI / 2); },
|
|
400
|
+
3: function (p) { return rotZ(p, Math.PI / 2); }, 4: function (p) { return rotZ(p, -Math.PI / 2); }
|
|
401
|
+
};
|
|
402
|
+
// the value-to-pips table on the 3×3 lattice — face units (±1 = ±H/3)
|
|
403
|
+
var LAYOUT = {
|
|
404
|
+
1: [[0, 0]],
|
|
405
|
+
2: [[-1, -1], [1, 1]],
|
|
406
|
+
3: [[-1, -1], [0, 0], [1, 1]],
|
|
407
|
+
4: [[-1, -1], [1, -1], [-1, 1], [1, 1]],
|
|
408
|
+
5: [[-1, -1], [1, -1], [0, 0], [-1, 1], [1, 1]],
|
|
409
|
+
6: [[-1, -1.25], [1, -1.25], [-1, 0], [1, 0], [-1, 1.25], [1, 1.25]]
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// 2D helpers: convex hull (monotone chain) and a quad with rounded corners
|
|
413
|
+
function hull2(ps) {
|
|
414
|
+
var pts = ps.slice().sort(function (a, b) { return a[0] - b[0] || a[1] - b[1]; }), lo = [], hi = [], i;
|
|
415
|
+
var cr = function (o, a, b) { return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]); };
|
|
416
|
+
for (i = 0; i < pts.length; i++) { while (lo.length >= 2 && cr(lo[lo.length - 2], lo[lo.length - 1], pts[i]) <= 1e-8) lo.pop(); lo.push(pts[i]); }
|
|
417
|
+
for (i = pts.length - 1; i >= 0; i--) { while (hi.length >= 2 && cr(hi[hi.length - 2], hi[hi.length - 1], pts[i]) <= 1e-8) hi.pop(); hi.push(pts[i]); }
|
|
418
|
+
return lo.slice(0, -1).concat(hi.slice(0, -1));
|
|
419
|
+
}
|
|
420
|
+
function rquad2(pts, r) {
|
|
421
|
+
var d = '', i;
|
|
422
|
+
for (i = 0; i < pts.length; i++) {
|
|
423
|
+
var p0 = pts[(i - 1 + pts.length) % pts.length], p1 = pts[i], p2 = pts[(i + 1) % pts.length];
|
|
424
|
+
var l0 = Math.sqrt((p1[0] - p0[0]) * (p1[0] - p0[0]) + (p1[1] - p0[1]) * (p1[1] - p0[1]));
|
|
425
|
+
var l1 = Math.sqrt((p2[0] - p1[0]) * (p2[0] - p1[0]) + (p2[1] - p1[1]) * (p2[1] - p1[1]));
|
|
426
|
+
if (l0 < 1e-6 || l1 < 1e-6) continue;
|
|
427
|
+
var k = Math.min(r, l0 / 3, l1 / 3);
|
|
428
|
+
var a1 = [p1[0] - (p1[0] - p0[0]) / l0 * k, p1[1] - (p1[1] - p0[1]) / l0 * k];
|
|
429
|
+
var a2 = [p1[0] + (p2[0] - p1[0]) / l1 * k, p1[1] + (p2[1] - p1[1]) / l1 * k];
|
|
430
|
+
d += (i ? 'L' + n(a1[0], 1) + ' ' + n(a1[1], 1) : 'M' + n(a1[0], 1) + ' ' + n(a1[1], 1)) +
|
|
431
|
+
'Q' + n(p1[0], 1) + ' ' + n(p1[1], 1) + ' ' + n(a2[0], 1) + ' ' + n(a2[1], 1);
|
|
432
|
+
}
|
|
433
|
+
return d + 'Z';
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// face gradients from the die role colour and the pip gradient from `pip`: when a pin is
|
|
437
|
+
// a plain #hex/rgb() colour its LCh light and shade build the stops; any other string is
|
|
438
|
+
// used verbatim for every stop (flat), escaped like every pin — nothing else is parsed
|
|
439
|
+
function dieDefs(c) {
|
|
440
|
+
var colMix = function (pin, dl) {
|
|
441
|
+
var lch = rgb2lch(parseColor(pin));
|
|
442
|
+
return toHex(lch2rgb(Math.max(2, Math.min(99, lch[0] + dl)), lch[1], lch[2]));
|
|
443
|
+
};
|
|
444
|
+
// exactly the grammar the family's parseColor can read — anything else passes verbatim
|
|
445
|
+
var usable = function (x) {
|
|
446
|
+
return /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i.test(x) || /^rgba?\(\s*[\d.]+[\s,]+[\d.]+[\s,]+[\d.]+/i.test(x);
|
|
447
|
+
};
|
|
448
|
+
var die = c.o.die && c.o.die !== 'auto' ? c.o.die : c.col('die');
|
|
449
|
+
var pip = c.o.pip && c.o.pip !== 'auto' ? c.o.pip : c.col('pip');
|
|
450
|
+
var t = !usable(die), p = !usable(pip);
|
|
451
|
+
var stops = t ? [die, die, die] : [colMix(die, 14), colMix(die, 5), die];
|
|
452
|
+
var sl = t ? [die, die, die] : [colMix(die, 4), die, colMix(die, -11)];
|
|
453
|
+
var sr = t ? [die, die, die] : [die, colMix(die, -13), colMix(die, -21)];
|
|
454
|
+
var ps = p ? [pip, pip, pip, pip] : [colMix(pip, 24), colMix(pip, 9), pip, colMix(pip, -9)];
|
|
455
|
+
var g = function (id, list, a1, a2) {
|
|
456
|
+
var s = '', i;
|
|
457
|
+
for (i = 0; i < list.length; i++) s += el('stop', ['offset', i === 0 ? 0 : n(i / (list.length - 1), 2), 'stop-color', esc(list[i])]);
|
|
458
|
+
return el('linearGradient', ['id', id, 'x1', a1[0], 'y1', a1[1], 'x2', a2[0], 'y2', a2[1]], s);
|
|
459
|
+
};
|
|
460
|
+
var glass = c.o.body === 'glass';
|
|
461
|
+
return c.add('dice:defs', function () {
|
|
462
|
+
var gt = c.tk(), gl = c.tk(), gr = c.tk(), gp = c.tk(), ga = c.tk(), b14 = c.tk(), b5 = c.tk(), sf = c.tk();
|
|
463
|
+
c.die = { t: gt, l: gl, r: gr, p: gp, a: ga, b14: b14, b5: b5, sf: sf };
|
|
464
|
+
c.dieUnder = t ? die : colMix(die, -20);
|
|
465
|
+
var blur = el('filter', ['id', b14, 'x', '-60%', 'y', '-60%', 'width', '220%', 'height', '220%'], el('feGaussianBlur', ['stdDeviation', 14])) +
|
|
466
|
+
el('filter', ['id', b5, 'x', '-60%', 'y', '-60%', 'width', '220%', 'height', '220%'], el('feGaussianBlur', ['stdDeviation', 5]));
|
|
467
|
+
// a glass body draws no gradients: the frost is a flat fill, the pips solid — only
|
|
468
|
+
// the shadows' blurs come out of the defs (ADR 007)
|
|
469
|
+
if (glass) {
|
|
470
|
+
// an arbitrary CSS pin is the frost verbatim, like dieUnder in paper — only a
|
|
471
|
+
// parseable colour can be lightened (spec: Pins)
|
|
472
|
+
c.dieGlass = t ? die : colMix(die, 6);
|
|
473
|
+
return blur;
|
|
474
|
+
}
|
|
475
|
+
return g(gt, stops, ['.25', 0], ['.6', 1]) + g(gl, sl, ['.2', 0], ['.7', 1]) + g(gr, sr, ['.2', 0], ['.7', 1]) +
|
|
476
|
+
el('radialGradient', ['id', gp, 'cx', '.31', 'cy', '.27', 'r', '.78'],
|
|
477
|
+
el('stop', ['offset', 0, 'stop-color', esc(ps[0])]) + el('stop', ['offset', '.1', 'stop-color', esc(ps[1])]) +
|
|
478
|
+
el('stop', ['offset', '.35', 'stop-color', esc(ps[2])]) + el('stop', ['offset', 1, 'stop-color', esc(ps[3])])) +
|
|
479
|
+
el('radialGradient', ['id', ga, 'cx', '.34', 'cy', '.24', 'r', '.88'],
|
|
480
|
+
el('stop', ['offset', 0, 'stop-color', '#fff', 'stop-opacity', '.22']) +
|
|
481
|
+
el('stop', ['offset', '.48', 'stop-color', '#fff', 'stop-opacity', '.04']) +
|
|
482
|
+
el('stop', ['offset', '.72', 'stop-color', '#000', 'stop-opacity', 0]) +
|
|
483
|
+
el('stop', ['offset', 1, 'stop-color', '#000', 'stop-opacity', '.09'])) +
|
|
484
|
+
blur +
|
|
485
|
+
el('filter', ['id', sf, 'x', '-20%', 'y', '-20%', 'width', '140%', 'height', '140%'], el('feGaussianBlur', ['stdDeviation', 1.4]));
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// one die as {shadow, content} — no placement wrapper: the caller owns the transform, so
|
|
490
|
+
// the motion group can sit between the placement and the content (ADR 005 addendum)
|
|
491
|
+
function die(c, top, yaw) {
|
|
492
|
+
var pre = REST[top], F = [], i, glass = c.o.body === 'glass';
|
|
493
|
+
for (i = 0; i < 6; i++) {
|
|
494
|
+
var f = CUBE[i];
|
|
495
|
+
F.push({ c: pre(f.c), u: pre(f.u), v: pre(f.v), n: pre(f.n), val: f.val });
|
|
496
|
+
}
|
|
497
|
+
var pt = function (p) { var q = rotX(rotY(p, yaw), PITCH); return [q[0], -q[1]]; };
|
|
498
|
+
// pips/axes are computed in the yawed frame already — project with the pitch only
|
|
499
|
+
var pj = function (p) { var q = rotX(p, PITCH); return [q[0], -q[1]]; };
|
|
500
|
+
var corners = [];
|
|
501
|
+
for (i = 0; i < 8; i++) corners.push(pt(pre([i & 1 ? H : -H, i & 2 ? H : -H, i & 4 ? H : -H])));
|
|
502
|
+
var sil = rquad2(hull2(corners), RS);
|
|
503
|
+
var d = c.die;
|
|
504
|
+
// visible faces, far first (depth = H × normal.z)
|
|
505
|
+
var vis = [];
|
|
506
|
+
for (i = 0; i < 6; i++) {
|
|
507
|
+
var rn = rotX(rotY(F[i].n, yaw), PITCH);
|
|
508
|
+
if (rn[2] > 0.02) vis.push({ f: F[i], rn: rn, z: rn[2] });
|
|
509
|
+
}
|
|
510
|
+
vis.sort(function (a, b) { return a.z - b.z; });
|
|
511
|
+
var viewT = [0, Math.sin(PITCH), Math.cos(PITCH)];
|
|
512
|
+
var faces = '', face = '', seams = '', SEAMC = [];
|
|
513
|
+
for (i = 0; i < vis.length; i++) {
|
|
514
|
+
var fc = vis[i].f, cs = [], cs2 = [], j;
|
|
515
|
+
for (j = 0; j < 4; j++) {
|
|
516
|
+
var I = j === 1 || j === 2 ? 1 : -1, J = j >= 2 ? 1 : -1;
|
|
517
|
+
cs.push(pt(add3(add3(fc.c, fc.u, I * HG), fc.v, J * HG)));
|
|
518
|
+
// the true corners (±H) — endpoints for the seams; the quads above use HG = H + 2
|
|
519
|
+
// and overlap on purpose, so their corners must not serve as edges
|
|
520
|
+
cs2.push(pt(add3(add3(fc.c, fc.u, I * H), fc.v, J * H)));
|
|
521
|
+
}
|
|
522
|
+
SEAMC.push(cs2);
|
|
523
|
+
// a glass body has no face fills: the frost is the silhouette's; pips and seams
|
|
524
|
+
// ride the clip
|
|
525
|
+
face = glass ? '' : el('path', ['d', rquad2(cs, RF), 'fill', 'url(#' + (vis[i].rn[1] > 0.4 ? d.t : vis[i].rn[0] > 0 ? d.r : d.l) + ')']);
|
|
526
|
+
// the pips: circles in 3D on the face plane, drawn as their exact projection —
|
|
527
|
+
// major axis along face∩image-plane (true length), minor foreshortened
|
|
528
|
+
var nT = rotY(fc.n, yaw);
|
|
529
|
+
var dM = norm3(cross3(nT, viewT)), dm = norm3(cross3(nT, dM));
|
|
530
|
+
var L = LAYOUT[fc.val];
|
|
531
|
+
for (j = 0; j < L.length; j++) {
|
|
532
|
+
var pT = rotY(add3(add3(fc.c, fc.u, L[j][0] * H / 3), fc.v, L[j][1] * H / 3), yaw);
|
|
533
|
+
var cc = pj(pT), mj = pj(add3(pT, dM, RPIP)), mn = pj(add3(pT, dm, RPIP));
|
|
534
|
+
var ax = [mj[0] - cc[0], mj[1] - cc[1]];
|
|
535
|
+
face += el('ellipse', ['cx', n(cc[0], 1), 'cy', n(cc[1], 1),
|
|
536
|
+
'rx', n(Math.sqrt(ax[0] * ax[0] + ax[1] * ax[1]), 1),
|
|
537
|
+
'ry', n(Math.sqrt((mn[0] - cc[0]) * (mn[0] - cc[0]) + (mn[1] - cc[1]) * (mn[1] - cc[1])), 1),
|
|
538
|
+
'transform', 'rotate(' + n(Math.atan2(ax[1], ax[0]) * 180 / Math.PI, 1) + ' ' + n(cc[0], 1) + ' ' + n(cc[1], 1) + ')',
|
|
539
|
+
'fill', glass ? c.col('pip') : 'url(#' + d.p + ')']);
|
|
540
|
+
}
|
|
541
|
+
faces += face; // face and its pips stay one unit, far-to-near
|
|
542
|
+
}
|
|
543
|
+
var sid = c.tk(), cid = c.tk();
|
|
544
|
+
// the seams: each pair of visible faces shares one cube edge — two matching true corners
|
|
545
|
+
// (the drawn quads use HG = H + 2 and overlap on purpose, so they cannot serve). Open
|
|
546
|
+
// segments over the faces, both ends stopping where the corner rounding starts: a rounded
|
|
547
|
+
// cube's edges never reach the vertex, and lines that do make the front face read square
|
|
548
|
+
// against the rounded silhouette corners (the cover-image defect, ADR 007 addendum).
|
|
549
|
+
// At the nose the trim is rquad2's own tangent length on each adjacent wedge; at the
|
|
550
|
+
// silhouette the seam ends where it meets the rounded-corner Q-curve
|
|
551
|
+
var SP = [];
|
|
552
|
+
for (i = 0; i < SEAMC.length; i++) for (var j2 = i + 1; j2 < SEAMC.length; j2++) {
|
|
553
|
+
var A = SEAMC[i], B = SEAMC[j2], P = [];
|
|
554
|
+
for (var a2 = 0; a2 < 4; a2++) for (var b2 = 0; b2 < 4; b2++) {
|
|
555
|
+
var d2 = A[a2][0] - B[b2][0], d3 = A[a2][1] - B[b2][1];
|
|
556
|
+
if (d2 * d2 + d3 * d3 < 0.01) { P.push(A[a2]); break; }
|
|
557
|
+
}
|
|
558
|
+
if (P.length === 2) SP.push(P);
|
|
559
|
+
}
|
|
560
|
+
var nose = null;
|
|
561
|
+
if (SP.length === 3) for (var e0 = 0; e0 < 2 && !nose; e0++) {
|
|
562
|
+
var hits = 0;
|
|
563
|
+
for (var s0 = 0; s0 < 3; s0++) for (var e1 = 0; e1 < 2; e1++) {
|
|
564
|
+
var qx = SP[s0][e1][0] - SP[0][e0][0], qy = SP[s0][e1][1] - SP[0][e0][1];
|
|
565
|
+
if (qx * qx + qy * qy < 0.01) hits++;
|
|
566
|
+
}
|
|
567
|
+
if (hits === 3) nose = SP[0][e0];
|
|
568
|
+
}
|
|
569
|
+
var hp = hull2(corners), stops = [];
|
|
570
|
+
for (i = 0; i < SP.length; i++) {
|
|
571
|
+
var W = SP[i][0], V = SP[i][1];
|
|
572
|
+
if (nose) {
|
|
573
|
+
if (Math.abs(W[0] - nose[0]) + Math.abs(W[1] - nose[1]) > 0.1) { var sw = W; W = V; V = sw; }
|
|
574
|
+
var ux = V[0] - W[0], uy = V[1] - W[1], luv = Math.sqrt(ux * ux + uy * uy);
|
|
575
|
+
if (luv > 1e-6) {
|
|
576
|
+
var tn = Math.min(luv, RS);
|
|
577
|
+
for (var s1 = 0; s1 < 3; s1++) if (s1 !== i) {
|
|
578
|
+
var O = Math.abs(SP[s1][0][0] - W[0]) + Math.abs(SP[s1][0][1] - W[1]) < 0.1 ? SP[s1][1] : SP[s1][0];
|
|
579
|
+
var vx = O[0] - W[0], vy = O[1] - W[1];
|
|
580
|
+
var lw = Math.min(luv, Math.sqrt(vx * vx + vy * vy)) / 3;
|
|
581
|
+
if (lw < tn) tn = lw;
|
|
582
|
+
}
|
|
583
|
+
W = [W[0] + ux / luv * tn, W[1] + uy / luv * tn];
|
|
584
|
+
}
|
|
585
|
+
stops.push(W);
|
|
586
|
+
}
|
|
587
|
+
// the silhouette end: meet the rounded-corner Q-curve at V (rquad2's construction)
|
|
588
|
+
var vi = -1;
|
|
589
|
+
for (var h0 = 0; h0 < hp.length; h0++) {
|
|
590
|
+
var hx = hp[h0][0] - V[0], hy = hp[h0][1] - V[1];
|
|
591
|
+
if (hx * hx + hy * hy < 0.01) { vi = h0; break; }
|
|
592
|
+
}
|
|
593
|
+
if (vi >= 0) {
|
|
594
|
+
var pv = hp[(vi - 1 + hp.length) % hp.length], nx2 = hp[(vi + 1) % hp.length];
|
|
595
|
+
var ex = V[0] - pv[0], ey = V[1] - pv[1], fx = nx2[0] - V[0], fy = nx2[1] - V[1];
|
|
596
|
+
var le = Math.sqrt(ex * ex + ey * ey), lf = Math.sqrt(fx * fx + fy * fy);
|
|
597
|
+
var Dx = V[0] - W[0], Dy = V[1] - W[1], d2n = Dx * Dx + Dy * Dy;
|
|
598
|
+
if (le > 1e-6 && lf > 1e-6 && d2n > 1e-6) {
|
|
599
|
+
var kk = Math.min(RS, le / 3, lf / 3);
|
|
600
|
+
var m0 = [V[0] - ex / le * kk, V[1] - ey / le * kk], m1 = [V[0] + fx / lf * kk, V[1] + fy / lf * kk];
|
|
601
|
+
var c1 = m0[0], c2 = 2 * (V[0] - m0[0]), c3 = m0[0] - 2 * V[0] + m1[0];
|
|
602
|
+
var c4 = m0[1], c5 = 2 * (V[1] - m0[1]), c6 = m0[1] - 2 * V[1] + m1[1];
|
|
603
|
+
var qa = c3 * Dy - c6 * Dx, qb = c2 * Dy - c5 * Dx, qc = (c1 - W[0]) * Dy - (c4 - W[1]) * Dx;
|
|
604
|
+
var cand = [];
|
|
605
|
+
if (Math.abs(qa) < 1e-9) { if (Math.abs(qb) > 1e-9) { var t0 = -qc / qb; if (t0 > 0 && t0 < 1) cand.push(t0); } }
|
|
606
|
+
else {
|
|
607
|
+
var disc = qb * qb - 4 * qa * qc;
|
|
608
|
+
if (disc >= 0) {
|
|
609
|
+
var sq2 = Math.sqrt(disc);
|
|
610
|
+
for (var sg = -1; sg <= 1; sg += 2) {
|
|
611
|
+
var t1 = (-qb + sg * sq2) / (2 * qa);
|
|
612
|
+
if (t1 > 0 && t1 < 1) cand.push(t1);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
// the crossing must lie ON the seam segment (s in 0..1 measured from W),
|
|
617
|
+
// and the exit nearest the corner wins — the other root sits on the line's
|
|
618
|
+
// extension, outside the silhouette
|
|
619
|
+
var best = null;
|
|
620
|
+
for (var ci = 0; ci < cand.length; ci++) {
|
|
621
|
+
var t2 = cand[ci];
|
|
622
|
+
var bx = c1 + t2 * c2 + t2 * t2 * c3 - W[0], by = c4 + t2 * c5 + t2 * t2 * c6 - W[1];
|
|
623
|
+
var ss = (bx * Dx + by * Dy) / d2n;
|
|
624
|
+
if (ss > 0 && ss < 1 && (best == null || ss > best)) best = ss;
|
|
625
|
+
}
|
|
626
|
+
// pull the end one stroke-width back inside the silhouette: a hairline gap
|
|
627
|
+
// reads as "the edge meets the boundary" and never merges into the outline
|
|
628
|
+
// — which matters most in glass, where bold seams cross overlapping dice
|
|
629
|
+
if (best != null) {
|
|
630
|
+
var gs = (glass ? 2.2 : 1.4) * c.w / Math.sqrt(d2n);
|
|
631
|
+
V = [W[0] + Dx * (best - gs), W[1] + Dy * (best - gs)];
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
seams += el('path', ['clip-path', glass ? null : 'url(#' + cid + ')',
|
|
636
|
+
'd', 'M' + n(W[0], 1) + ' ' + n(W[1], 1) + 'L' + n(V[0], 1) + ' ' + n(V[1], 1), 'fill', 'none',
|
|
637
|
+
'stroke', glass ? c.col('outline') : c.col('ink'),
|
|
638
|
+
'stroke-opacity', glass ? null : '.15',
|
|
639
|
+
'stroke-width', n((glass ? 2.2 : 1.4) * c.w, 1), 'stroke-linecap', 'round']);
|
|
640
|
+
}
|
|
641
|
+
// the nose corners: bare stops read as a ragged triple point, each face's corner still
|
|
642
|
+
// skewed — so every pair of stops joins through a small Q-arc, control at the nose
|
|
643
|
+
// (rquad2's own corner construction: tangent to both seams at any stop distance)
|
|
644
|
+
if (nose && stops.length === 3) for (i = 0; i < 3; i++) for (var j3 = i + 1; j3 < 3; j3++) {
|
|
645
|
+
seams += el('path', ['clip-path', glass ? null : 'url(#' + cid + ')',
|
|
646
|
+
'd', 'M' + n(stops[i][0], 1) + ' ' + n(stops[i][1], 1) + 'Q' + n(nose[0], 1) + ' ' + n(nose[1], 1) +
|
|
647
|
+
' ' + n(stops[j3][0], 1) + ' ' + n(stops[j3][1], 1), 'fill', 'none',
|
|
648
|
+
'stroke', glass ? c.col('outline') : c.col('ink'),
|
|
649
|
+
'stroke-opacity', glass ? null : '.15',
|
|
650
|
+
'stroke-width', n((glass ? 2.2 : 1.4) * c.w, 1), 'stroke-linecap', 'round']);
|
|
651
|
+
}
|
|
652
|
+
// the silhouette lives in defs with NO fill of its own: a fill set on the source would
|
|
653
|
+
// beat the uses' paints (and would render if the path sat in the body) — so the uses
|
|
654
|
+
// own every paint, and its d is computed from the yaw, keeping zero-d honest
|
|
655
|
+
c.defs += el('path', ['id', sid, 'd', sil]) + el('clipPath', ['id', cid], el('use', ['href', '#' + sid]));
|
|
656
|
+
return {
|
|
657
|
+
shadow: el('ellipse', ['cx', 0, 'cy', 118, 'rx', 132, 'ry', 24, 'fill', c.col('ink'), 'fill-opacity', '.16', 'filter', 'url(#' + d.b14 + ')']) +
|
|
658
|
+
el('ellipse', ['cx', 0, 'cy', 112, 'rx', 62, 'ry', 11, 'fill', c.col('ink'), 'fill-opacity', '.18', 'filter', 'url(#' + d.b5 + ')']),
|
|
659
|
+
content: glass
|
|
660
|
+
? el('use', ['href', '#' + sid, 'fill', esc(c.dieGlass), 'fill-opacity', '.55', 'stroke', c.col('outline'), 'stroke-width', n(2.6 * c.w, 1)]) +
|
|
661
|
+
el('g', ['clip-path', 'url(#' + cid + ')'], faces + seams)
|
|
662
|
+
: el('use', ['href', '#' + sid, 'fill', esc(c.dieUnder)]) +
|
|
663
|
+
el('g', ['clip-path', 'url(#' + cid + ')', 'filter', 'url(#' + d.sf + ')'], faces) +
|
|
664
|
+
el('use', ['href', '#' + sid, 'fill', 'url(#' + d.a + ')']) +
|
|
665
|
+
el('use', ['href', '#' + sid, 'fill', 'none', 'stroke', '#00000010', 'stroke-width', 2]) +
|
|
666
|
+
seams
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// ── the throw ───────────────────────────────────────────────────────────
|
|
671
|
+
|
|
672
|
+
// The canonical slots: one nested list — count k takes the first k, so `dice` appends
|
|
673
|
+
// without moving a die already on the table (values, yaws and placements all hold). The
|
|
674
|
+
// pair is the constant core; the triangle, the diamond and the quincunx grow from it.
|
|
675
|
+
var SLOTS = [[-1, 0], [1, 0], [0, -1], [0, 1], [0, 0]];
|
|
676
|
+
var RESULTS = ['lucky7', 'snake', 'boxcars', 'matched', 'mixed'];
|
|
677
|
+
var BELT7 = [[1, 6], [2, 5], [3, 4]];
|
|
678
|
+
|
|
679
|
+
// Results are data (ADR 003): a name places values, nothing is scored. Each result reads
|
|
680
|
+
// its own streams; dice it does not name keep the seed's values.
|
|
681
|
+
function resultValue(r, i, S, used) {
|
|
682
|
+
if (r === 'snake') return i < 2 ? 1 : 0;
|
|
683
|
+
if (r === 'boxcars') return i < 2 ? 6 : 0;
|
|
684
|
+
if (r === 'lucky7' && i < 2) {
|
|
685
|
+
// one pair draw for the throw, one swap draw — both dice name it
|
|
686
|
+
if (!used.l7) {
|
|
687
|
+
var p = BELT7[Math.floor(S('result:lucky7')() * 3)];
|
|
688
|
+
used.l7 = S('result:lucky7:swap')() < 0.5 ? p : [p[1], p[0]];
|
|
689
|
+
}
|
|
690
|
+
return used.l7[i];
|
|
691
|
+
}
|
|
692
|
+
if (r === 'matched') return 1 + Math.floor(S('result:matched')() * 6);
|
|
693
|
+
if (r === 'mixed') {
|
|
694
|
+
var avail = [];
|
|
695
|
+
for (var v = 1; v <= 6; v++) if (!used[v]) avail.push(v);
|
|
696
|
+
var val = avail[Math.floor(S('result:mixed:' + i)() * avail.length)];
|
|
697
|
+
used[val] = 1;
|
|
698
|
+
return val;
|
|
699
|
+
}
|
|
700
|
+
return 0; // the seed decides
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function toss(opts) {
|
|
704
|
+
var o = opts || {}, k = Math.max(2, Math.min(5, Math.round(o.dice) || 3));
|
|
705
|
+
var c = context(o, 'table' + k), p = c.p, S = c.S, i;
|
|
706
|
+
dieDefs(c);
|
|
707
|
+
// the felt is sized to the projected dice at the canonical slots (spec: Machine space)
|
|
708
|
+
var ax = 0, ay = 0;
|
|
709
|
+
for (i = 0; i < k; i++) {
|
|
710
|
+
ax = Math.max(ax, Math.abs(SLOTS[i][0]));
|
|
711
|
+
ay = Math.max(ay, Math.abs(SLOTS[i][1]));
|
|
712
|
+
}
|
|
713
|
+
var fx = ax * SLOT_A + HX, fy = ay * SLOT_A + HY;
|
|
714
|
+
// the rail: a padded ring around the felt; width and corner from table:* draws
|
|
715
|
+
var rw = RAIL[0] + (RAIL[1] - RAIL[0]) * S('table:rail')();
|
|
716
|
+
var rc = 28 + 40 * S('table:corner')();
|
|
717
|
+
var cab = c.cab, line = c.col('outline'), trim = c.col('trim'), W4 = n(4 * c.w, 2);
|
|
718
|
+
var rule = function (col) { return cab ? ['fill', col] : ['fill', 'none', 'stroke', line, 'stroke-width', W4]; };
|
|
719
|
+
var rect = function (x, y, w, h, r, paint) {
|
|
720
|
+
return el('rect', ['x', n(x, p), 'y', n(y, p), 'width', n(w, p), 'height', n(h, p), 'rx', r ? n(r, p) : null].concat(paint));
|
|
721
|
+
};
|
|
722
|
+
// values: values pin > result names > the seed decides (spec: Results). ALL valid pins
|
|
723
|
+
// mark `used` before any result draw — mixed must never repeat one, early or late
|
|
724
|
+
var pins = o.values || [], res = RESULTS.indexOf(o.result) >= 0 ? o.result : '', used = {};
|
|
725
|
+
var seeded = function (i) { return 1 + Math.floor(S('die:' + i + ':value')() * 6); };
|
|
726
|
+
var values = [];
|
|
727
|
+
for (i = 0; i < k; i++) {
|
|
728
|
+
var pin = Math.round(pins[i]);
|
|
729
|
+
if (pin >= 1 && pin <= 6) { values[i] = pin; used[pin] = 1; }
|
|
730
|
+
}
|
|
731
|
+
for (i = 0; i < k; i++) {
|
|
732
|
+
if (values[i] != null) continue;
|
|
733
|
+
if (res) { var rv = resultValue(res, i, S, used); values[i] = rv || seeded(i); }
|
|
734
|
+
else values[i] = seeded(i);
|
|
735
|
+
}
|
|
736
|
+
// the roll (ADR 005 + addendum): a settle, off by default. Names from a stream of their
|
|
737
|
+
// own salted with speed; the animated group never carries a transform attribute
|
|
738
|
+
var speed = o.speed == null ? 1 : o.speed, mo = !!o.motion && speed > 0, css = '', tk = null;
|
|
739
|
+
if (mo) tk = tokens(S, 'motion' + (o.salt || '') + c.key + speed);
|
|
740
|
+
// the dice: value, yaw and placement each from their own stream; depth from the slot,
|
|
741
|
+
// so appending never re-rolls a die. Light overlap is allowed — dice touch like on a
|
|
742
|
+
// real table; shadows sit in a layer below, dice paint far-to-near by ground position
|
|
743
|
+
var dice = [];
|
|
744
|
+
for (i = 0; i < k; i++) {
|
|
745
|
+
var yaw = (S('die:' + i + ':orient')() < 0.5 ? -1 : 1) * (YAW[0] + YAW[1] * S('die:' + i + ':orient:yaw')());
|
|
746
|
+
var px = SLOTS[i][0] * SLOT_A + (S('die:' + i + ':place')() - 0.5) * 2 * JIT * SLOT_A;
|
|
747
|
+
var py = SLOTS[i][1] * SLOT_A + (S('die:' + i + ':place:v')() - 0.5) * 2 * JIT * SLOT_A;
|
|
748
|
+
var scale = 0.9 + 0.1 * (SLOTS[i][1] + 1) / 2;
|
|
749
|
+
var g = 'translate(' + n(px, 1) + ' ' + n(py, 1) + ')' + (scale !== 1 ? ' scale(' + scale + ')' : '');
|
|
750
|
+
var dd = die(c, values[i], yaw);
|
|
751
|
+
var inner = dd.content, cls = null;
|
|
752
|
+
if (mo) {
|
|
753
|
+
var kf = tk(), drop = 30 + 40 * S('die:' + i + ':drop')();
|
|
754
|
+
cls = tk();
|
|
755
|
+
css += '@keyframes ' + kf + '{from{transform:translateY(' + n(-drop, 1) + 'px)}}.' + cls +
|
|
756
|
+
'{animation:' + kf + ' ' + n((0.8 + 0.3 * i) / speed, 2) + 's cubic-bezier(.2,.7,.3,1.08)}';
|
|
757
|
+
inner = el('g', ['class', cls], inner);
|
|
758
|
+
}
|
|
759
|
+
dice.push({ x: px, y: py, cls: cls, shadow: el('g', ['transform', g], dd.shadow), body: el('g', ['transform', g], inner) });
|
|
760
|
+
}
|
|
761
|
+
dice.sort(function (a, b) { return a.y - b.y; });
|
|
762
|
+
var shadows = '', bodies = '';
|
|
763
|
+
for (i = 0; i < k; i++) { shadows += dice[i].shadow; bodies += dice[i].body; }
|
|
764
|
+
// seam AO where dice touch: a small dark pool under the contact point, in the shadow layer
|
|
765
|
+
for (i = 0; i < k; i++) {
|
|
766
|
+
for (var j = i + 1; j < k; j++) {
|
|
767
|
+
var dx = dice[i].x - dice[j].x, dy = dice[i].y - dice[j].y;
|
|
768
|
+
if (Math.sqrt(dx * dx + dy * dy) < 240) shadows += el('ellipse',
|
|
769
|
+
['cx', n((dice[i].x + dice[j].x) / 2, 1), 'cy', n((dice[i].y + dice[j].y) / 2 + 54, 1),
|
|
770
|
+
'rx', 36, 'ry', 9, 'fill', c.col('ink'), 'fill-opacity', '.14', 'filter', 'url(#' + c.die.b5 + ')']);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
// the rail, the felt, the lattice; then the shadows and the dice. The roll's <style>
|
|
774
|
+
// leads the picture (document-global CSS); reduced motion restores the static bytes
|
|
775
|
+
var out = '';
|
|
776
|
+
if (mo) {
|
|
777
|
+
var clss = [];
|
|
778
|
+
for (i = 0; i < k; i++) if (dice[i].cls) clss.push('.' + dice[i].cls);
|
|
779
|
+
out = el('style', [], css + '@media(prefers-reduced-motion:reduce){' + clss.join(',') + '{animation:none}}');
|
|
780
|
+
}
|
|
781
|
+
out += rect(-fx - rw, -fy - rw, 2 * (fx + rw), 2 * (fy + rw), rc, rule(c.col('rail')));
|
|
782
|
+
out += rect(-fx, -fy, 2 * fx, 2 * fy, Math.max(0, rc - rw), rule(c.col('felt')));
|
|
783
|
+
out += rect(-fx, -fy, 2 * fx, 2 * fy, Math.max(0, rc - rw), tile(c, trim, '.25'));
|
|
784
|
+
out += shadows + bodies;
|
|
785
|
+
var box = [-fx - MARGIN[0], -fy - MARGIN[1], 2 * (fx + MARGIN[0]), 2 * (fy + MARGIN[1])];
|
|
786
|
+
return wrap(o, c, box, out);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// One die on its em — an icon. The value is the caller's, or the seed's.
|
|
790
|
+
function one(opts) {
|
|
791
|
+
var o = opts || {};
|
|
792
|
+
var c = context(o, 'die'), S = c.S;
|
|
793
|
+
dieDefs(c);
|
|
794
|
+
var value = Math.round(o.value) >= 1 && Math.round(o.value) <= 6 ? Math.round(o.value) : 1 + Math.floor(S('die:0:value')() * 6);
|
|
795
|
+
var yaw = (S('die:0:orient')() < 0.5 ? -1 : 1) * (YAW[0] + YAW[1] * S('die:0:orient:yaw')());
|
|
796
|
+
var d = die(c, value, yaw);
|
|
797
|
+
return wrap(o, c, [-HX, -HY, 2 * HX, 2 * HY + 42], el('g', ['transform', 'translate(0 0)'], d.shadow + d.content));
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// Browser convenience: draw a throw into an element and keep it drawn. Options merge
|
|
801
|
+
// across set(), so pins stay pinned; a settle plays once per draw (cards-lite's init).
|
|
802
|
+
function init(target, opts) {
|
|
803
|
+
var host = typeof target === 'string' ? document.querySelector(target) : target, cur = {};
|
|
804
|
+
if (!host) return null;
|
|
805
|
+
var set = function (o) {
|
|
806
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) cur[k] = o[k];
|
|
807
|
+
host.innerHTML = toss(cur);
|
|
808
|
+
};
|
|
809
|
+
set(opts || {});
|
|
810
|
+
return {
|
|
811
|
+
el: host,
|
|
812
|
+
get: function () { var g = {}; for (var k in cur) g[k] = cur[k]; return g; },
|
|
813
|
+
set: set,
|
|
814
|
+
destroy: function () { host.innerHTML = ''; }
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
return { toss: toss, die: one, palette: palette, init: init };
|
|
819
|
+
});
|
package/dice.min.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* dice-lite — a procedural throw of dice as an SVG string.
|
|
3
|
+
* MIT © 301ST (https://301.st) · for spintax.net
|
|
4
|
+
*
|
|
5
|
+
* One file, ES5, zero dependencies. Works as a <script> (defines `Dice`) and as a CommonJS
|
|
6
|
+
* module (Node, bundlers) — ADR 001. The spec is docs/README.md: change the doc before the code.
|
|
7
|
+
* The colour engine and the seed machinery are the family's, ported unchanged from cards-lite
|
|
8
|
+
* (by way of slots-lite); a fixture test pins that they agree.
|
|
9
|
+
*/
|
|
10
|
+
!function(t){var r=function(){"use strict";var t="http://www.w3.org/2000/svg",r=["#00abf3","#d6af3c","#a91455"],n=100,e=.91,a=44,o=9,i=19,l=n+2,u=[.36,.08],f=Math.sqrt(3),s=(2-Math.SQRT2)/2,h=[90,64],c=[22,34],v=126,d=.06,p=145,M=176;function g(t,r){var n=Math.pow(10,r||0),e=Math.round(t*n)/n;return String(0===e?0:e)}function m(t){return String(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}function y(t,r,n){for(var e="<"+t,a=0;a<r.length;a+=2)null!=r[a+1]&&(e+=" "+r[a]+'="'+r[a+1]+'"');return null==n?e+"/>":e+">"+n+"</"+t+">"}function b(t){for(var r=2166136261,n=0;n<t.length;n++)r=Math.imul(r^t.charCodeAt(n),16777619);return r>>>0}function k(t){return t=Math.imul(t^t>>>16,2246822507),((t=Math.imul(t^t>>>13,3266489909))^t>>>16)>>>0}function x(t){return function(){t=t+1831565813>>>0;var r=Math.imul(t^t>>>15,1|t);return(((r=r+Math.imul(r^r>>>7,61|r)^r)^r>>>14)>>>0)/4294967296}}function w(t){var r="number"==typeof t?t>>>0:b(String(t));return function(t){return x(k(r^Math.imul(b(t),2654435769)))}}function q(t){if("#"===(t=String(t).trim()).charAt(0)){var r=t.slice(1);3===r.length&&(r=r[0]+r[0]+r[1]+r[1]+r[2]+r[2]);var n=parseInt(r,16);return[n>>16&255,n>>8&255,255&n]}var e=t.match(/rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/);return e?[+e[1],+e[2],+e[3]]:[255,255,255]}function S(t){return"#"+((1<<24)+(t[0]<<16)+(t[1]<<8)+(0|t[2])).toString(16).slice(1)}function P(t){return(t/=255)<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4)}function I(t){return 255*(t<=.0031308?12.92*t:1.055*Math.pow(t,1/2.4)-.055)}function z(t){return t>216/24389?Math.pow(t,1/3):(24389/27*t+16)/116}function G(t){var r=t*t*t;return r>216/24389?r:27*(116*t-16)/24389}function j(t){var r=P(t[0]),n=P(t[1]),e=P(t[2]),a=.21267*r+.71515*n+.07218*e,o=(.01933*r+.11919*n+.9503*e)/1.08883,i=z((.41246*r+.35758*n+.18044*e)/.95047),l=z(a),u=500*(i-l),f=200*(l-z(o));return[116*l-16,Math.sqrt(u*u+f*f),(180*Math.atan2(f,u)/Math.PI+360)%360]}function L(t,r,n){var e=r*Math.cos(n*Math.PI/180),a=(t+16)/116,o=a-r*Math.sin(n*Math.PI/180)/200,i=.95047*G(a+e/500),l=G(a),u=1.08883*G(o);return[3.24045*i-1.53714*l-.49853*u,-.96927*i+1.87601*l+.04156*u,.05564*i-.20403*l+1.05723*u]}function O(t,r,n){var e,a=L(t,r,n),o=0,i=r;if(!A(a)){for(e=0;e<20;e++){var l=(o+i)/2;A(a=L(t,l,n))?o=l:i=l}a=L(t,o,n)}return[B(I(a[0])),B(I(a[1])),B(I(a[2]))]}function A(t){for(var r=0;r<3;r++)if(t[r]<-5e-4||t[r]>1.0005)return!1;return!0}function B(t){return Math.max(0,Math.min(255,Math.round(t)))}function D(t){return.2126*P(t[0])+.7152*P(t[1])+.0722*P(t[2])}function T(t,r){var n=D(t),e=D(r);return(Math.max(n,e)+.05)/(Math.min(n,e)+.05)}function U(t,r,n,e){if(T(t,r)>=n)return t;for(var a=j(t),o=a[0];o>=2&&o<=98;o+=2*e){var i=O(o,a[1],a[2]);if(T(i,r)>=n)return i}return t}function Q(t,r){for(var n=("string"==typeof t?[t]:t).map(function(t){return j(q(t))}),e=[],a=0;a<n.length;a++)n[a][1]>=12&&e.push(n[a]);var o=0===e.length,i=o?[50,0,0]:e[0],l=e.length>1?e[1]:[0,i[1],(i[2]+40)%360],u=(e.length>2?e[2]:i)[2],f=i[1],s=i[2],h="light"===r,c=h?O(97,o?0:3,s):O(6,Math.min(.2*f,10),s),v=h?O(92,Math.min(.2*f,8),s):O(12,Math.min(.3*f,15),s),d=h?[72,52,32]:[32,58,82],p=h?-1:1;return{colors:[U(O(d[0],.9*f,s),c,1.5,p),U(O(d[1],f,s),c,2.5,p),U(O(d[2],.55*f,s),c,5,p)],accent:U(O(h?45:70,l[1],l[2]),c,3,p),hot:U(O(h?28:92,o?0:h?24:12,u),c,7,p),background:c,halo:v}}function H(t,n,e){var a,o=null==t?r:"string"==typeof t?[t]:t,i=[],l=null;for(a=0;a<o.length;a++){var u=j(q(o[a]));u[1]>=12?i.push({lch:u,hex:o[a]}):l||(l=o[a])}var f="light"===n?"light":"dark",s=i.length?i[0].hex:l||"#00abf3",h=j(q(s)),c=h[1],v=h[2],d=Q(s,f),p=-1,M=1/0;for(a=1;a<i.length;a++){var g=Math.abs(i[a].lch[2]-85)%360;(g=Math.min(g,360-g))<M&&(M=g,p=a)}M>30&&(p=-1);var m=p>=0?i[p].lch:null,y=O(66,l||!i.length?0:m?.5*m[1]:25,m?m[2]:85),b=O(96.5,Math.min(.08*c,4),v),k=U(O(14,Math.min(.15*c,6),v),b,7,-1),x=U(O(34,Math.min(.25*c,10),v),b,3,-1),w=null;for(a=1;a<i.length;a++)if(a!==p){w=i[a].hex;break}var S="brand"===e?w?q(w):d.colors["light"===f?2:0]:"felt"===e?q(s):k,P=Q(o,f);return{felt:q(s),rail:S,trim:y,die:b,pip:k,ink:x,outline:d.colors[1],stroke:P.colors,background:P.background,halo:P.halo}}function C(t,r){var n=r&&r.rail,e=H(t,r&&r.theme,n),a={};for(var o in e)a[o]="stroke"===o?e[o].map(S):S(e[o]);return null!=n&&"auto"!==n&&"brand"!==n&&"felt"!==n&&(a.rail=n),a}function R(t,r){var n=t("ids"+(r||"")),e={},a="abcdefghijklmnopqrstuvwxyz",o=a+"0123456789";return function(){var t;do{t=a.charAt(Math.floor(26*n()));for(var r=0;r<5;r++)t+=o.charAt(Math.floor(36*n()))}while(e[t]);return e[t]=1,t}}function Y(t,r){var n=w(null==t.seed?1:t.seed),e=H(t.brand,t.theme,t.rail),a={},o=r,i={o:t,S:n,defs:"",p:null==t.precision?0:t.precision,w:null==t.weight?1:t.weight,cab:t.style?"line"!==t.style:"light"!==t.theme,col:function(r){var n=t[r];return null!=n&&"auto"!==n&&("rail"!==r||"brand"!==n&&"felt"!==n)?m(n):S(e[r])}};for(var l in e)o+="stroke"===l?"":i.col(l);return o+=i.cab+i.w+i.p+(t.lattice||"auto")+("glass"===t.body?"g":""),i.key=o,i.tk=R(n,(t.salt||"")+o),i.add=function(t,r){if(!a[t]){var n,e=i.tk();a[t]=e,n=r(),i.defs+=n.replace('"%"','"'+e+'"')}return a[t]},i}function Z(r,n,e,a){var o=r.title?["role","img","aria-label",m(r.title)]:["aria-hidden","true"],i=e.map(function(t){return g(t,n.p)}).join(" ");return y("svg",["xmlns",t,"viewBox",i,"width",null==r.size?null:g(r.size),"height",null==r.size?null:g(r.size*e[3]/e[2])].concat(o),(n.defs?y("defs",[],n.defs):"")+a)}function $(t,r,n){var e=[],a=function(t){return Math.round(1e3*t)/1e3},o=function(t,r,o,i){e.push(n?"M"+a(r)+" "+a(t)+"L"+a(i)+" "+a(o):"M"+a(t)+" "+a(r)+"L"+a(o)+" "+a(i))};if("trigon"===t)o(0,0,r,0),o(0,r,r,r),o(0,0,0,r),o(r,0,r,r),o(r,0,0,r);else if("octagon"===t){var i=s*r;o(i,0,r-i,0),o(i,r,r-i,r),o(0,i,0,r-i),o(r,i,r,r-i),o(r-i,0,r,i),o(r,r-i,r-i,r),o(i,r,0,r-i),o(0,i,i,0)}else{var l=r/f;(function(t,r,n){for(var e=f/2*n,a=[[t,r-n],[t+e,r-n/2],[t+e,r+n/2],[t,r+n],[t-e,r+n/2],[t-e,r-n/2]],i=0;i<6;i++)o(a[i][0],a[i][1],a[(i+1)%6][0],a[(i+1)%6][1])})(r/2,l,l),o(r/2,2*l,r/2,3*l)}return e.join("")}var E=["trigon","octagon","hex"];function F(t,r,n){var e=t.S,a=t.o;if("none"===a.lattice)return["fill","none"];var o=E.indexOf(a.lattice)>=0?a.lattice:E[Math.floor(3*e("table:lattice")())],i=22+22*e("table:pitch")(),l="hex"===o&&e("table:orient")()<.5,u="hex"===o?i/f*3:i;return["fill","url(#"+t.add("tile"+r+n,function(){return y("pattern",["id","%","patternUnits","userSpaceOnUse","width",g(l?u:i,2),"height",g(l?i:u,2),"patternTransform","translate("+g(i*e("table:phase")(),2)+" "+g(i*e("table:phase2")(),2)+") rotate("+g(90*e("table:turn")(),2)+")"],y("path",["d",$(o,i,l),"fill","none","stroke",r,"stroke-width",g((1+1.4*e("table:weight")())*t.w,2),"stroke-opacity",n]))})+")"]}var J=function(t,r){var n=Math.cos(r),e=Math.sin(r);return[t[0]*n+t[2]*e,t[1],-t[0]*e+t[2]*n]},K=function(t,r){var n=Math.cos(r),e=Math.sin(r);return[t[0],t[1]*n-t[2]*e,t[1]*e+t[2]*n]},N=function(t,r,n){return[t[0]+r[0]*(null==n?1:n),t[1]+r[1]*(null==n?1:n),t[2]+r[2]*(null==n?1:n)]},V=function(t,r){return[t[1]*r[2]-t[2]*r[1],t[2]*r[0]-t[0]*r[2],t[0]*r[1]-t[1]*r[0]]},W=function(t){var r=Math.sqrt(t[0]*t[0]+t[1]*t[1]+t[2]*t[2])||1;return[t[0]/r,t[1]/r,t[2]/r]},X=[{n:[0,1,0],c:[0,n,0],u:[1,0,0],v:[0,0,1],val:1},{n:[0,-1,0],c:[0,-n,0],u:[1,0,0],v:[0,0,-1],val:6},{n:[0,0,1],c:[0,0,n],u:[1,0,0],v:[0,1,0],val:2},{n:[0,0,-1],c:[0,0,-n],u:[-1,0,0],v:[0,1,0],val:5},{n:[1,0,0],c:[n,0,0],u:[0,0,-1],v:[0,1,0],val:3},{n:[-1,0,0],c:[-n,0,0],u:[0,0,1],v:[0,1,0],val:4}],_=function(t,r){var n=Math.cos(r),e=Math.sin(r);return[t[0]*n-t[1]*e,t[0]*e+t[1]*n,t[2]]},tt={1:function(t){return t},6:function(t){return K(t,Math.PI)},2:function(t){return K(t,-Math.PI/2)},5:function(t){return K(t,Math.PI/2)},3:function(t){return _(t,Math.PI/2)},4:function(t){return _(t,-Math.PI/2)}},rt={1:[[0,0]],2:[[-1,-1],[1,1]],3:[[-1,-1],[0,0],[1,1]],4:[[-1,-1],[1,-1],[-1,1],[1,1]],5:[[-1,-1],[1,-1],[0,0],[-1,1],[1,1]],6:[[-1,-1.25],[1,-1.25],[-1,0],[1,0],[-1,1.25],[1,1.25]]};function nt(t){var r,n=t.slice().sort(function(t,r){return t[0]-r[0]||t[1]-r[1]}),e=[],a=[],o=function(t,r,n){return(r[0]-t[0])*(n[1]-t[1])-(r[1]-t[1])*(n[0]-t[0])};for(r=0;r<n.length;r++){for(;e.length>=2&&o(e[e.length-2],e[e.length-1],n[r])<=1e-8;)e.pop();e.push(n[r])}for(r=n.length-1;r>=0;r--){for(;a.length>=2&&o(a[a.length-2],a[a.length-1],n[r])<=1e-8;)a.pop();a.push(n[r])}return e.slice(0,-1).concat(a.slice(0,-1))}function et(t,r){var n,e="";for(n=0;n<t.length;n++){var a=t[(n-1+t.length)%t.length],o=t[n],i=t[(n+1)%t.length],l=Math.sqrt((o[0]-a[0])*(o[0]-a[0])+(o[1]-a[1])*(o[1]-a[1])),u=Math.sqrt((i[0]-o[0])*(i[0]-o[0])+(i[1]-o[1])*(i[1]-o[1]));if(!(l<1e-6||u<1e-6)){var f=Math.min(r,l/3,u/3),s=[o[0]-(o[0]-a[0])/l*f,o[1]-(o[1]-a[1])/l*f],h=[o[0]+(i[0]-o[0])/u*f,o[1]+(i[1]-o[1])/u*f];e+=(n?"L"+g(s[0],1)+" "+g(s[1],1):"M"+g(s[0],1)+" "+g(s[1],1))+"Q"+g(o[0],1)+" "+g(o[1],1)+" "+g(h[0],1)+" "+g(h[1],1)}}return e+"Z"}function at(t){var r=function(t,r){var n=j(q(t));return S(O(Math.max(2,Math.min(99,n[0]+r)),n[1],n[2]))},n=function(t){return/^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i.test(t)||/^rgba?\(\s*[\d.]+[\s,]+[\d.]+[\s,]+[\d.]+/i.test(t)},e=t.o.die&&"auto"!==t.o.die?t.o.die:t.col("die"),a=t.o.pip&&"auto"!==t.o.pip?t.o.pip:t.col("pip"),o=!n(e),i=!n(a),l=o?[e,e,e]:[r(e,14),r(e,5),e],u=o?[e,e,e]:[r(e,4),e,r(e,-11)],f=o?[e,e,e]:[e,r(e,-13),r(e,-21)],s=i?[a,a,a,a]:[r(a,24),r(a,9),a,r(a,-9)],h=function(t,r,n,e){var a,o="";for(a=0;a<r.length;a++)o+=y("stop",["offset",0===a?0:g(a/(r.length-1),2),"stop-color",m(r[a])]);return y("linearGradient",["id",t,"x1",n[0],"y1",n[1],"x2",e[0],"y2",e[1]],o)},c="glass"===t.o.body;return t.add("dice:defs",function(){var n=t.tk(),a=t.tk(),i=t.tk(),v=t.tk(),d=t.tk(),p=t.tk(),M=t.tk(),g=t.tk();t.die={t:n,l:a,r:i,p:v,a:d,b14:p,b5:M,sf:g},t.dieUnder=o?e:r(e,-20);var b=y("filter",["id",p,"x","-60%","y","-60%","width","220%","height","220%"],y("feGaussianBlur",["stdDeviation",14]))+y("filter",["id",M,"x","-60%","y","-60%","width","220%","height","220%"],y("feGaussianBlur",["stdDeviation",5]));return c?(t.dieGlass=o?e:r(e,6),b):h(n,l,[".25",0],[".6",1])+h(a,u,[".2",0],[".7",1])+h(i,f,[".2",0],[".7",1])+y("radialGradient",["id",v,"cx",".31","cy",".27","r",".78"],y("stop",["offset",0,"stop-color",m(s[0])])+y("stop",["offset",".1","stop-color",m(s[1])])+y("stop",["offset",".35","stop-color",m(s[2])])+y("stop",["offset",1,"stop-color",m(s[3])]))+y("radialGradient",["id",d,"cx",".34","cy",".24","r",".88"],y("stop",["offset",0,"stop-color","#fff","stop-opacity",".22"])+y("stop",["offset",".48","stop-color","#fff","stop-opacity",".04"])+y("stop",["offset",".72","stop-color","#000","stop-opacity",0])+y("stop",["offset",1,"stop-color","#000","stop-opacity",".09"]))+b+y("filter",["id",g,"x","-20%","y","-20%","width","140%","height","140%"],y("feGaussianBlur",["stdDeviation",1.4]))})}function ot(t,r,u){var f,s=tt[r],h=[],c="glass"===t.o.body;for(f=0;f<6;f++){var v=X[f];h.push({c:s(v.c),u:s(v.u),v:s(v.v),n:s(v.n),val:v.val})}var d=function(t){var r=K(J(t,u),e);return[r[0],-r[1]]},p=function(t){var r=K(t,e);return[r[0],-r[1]]},M=[];for(f=0;f<8;f++)M.push(d(s([1&f?n:-n,2&f?n:-n,4&f?n:-n])));var b=et(nt(M),a),k=t.die,x=[];for(f=0;f<6;f++){var w=K(J(h[f].n,u),e);w[2]>.02&&x.push({f:h[f],rn:w,z:w[2]})}x.sort(function(t,r){return t.z-r.z});var q=[0,Math.sin(e),Math.cos(e)],S="",P="",I="",z=[];for(f=0;f<x.length;f++){var G,j=x[f].f,L=[],O=[];for(G=0;G<4;G++){var A=1===G||2===G?1:-1,B=G>=2?1:-1;L.push(d(N(N(j.c,j.u,A*l),j.v,B*l))),O.push(d(N(N(j.c,j.u,A*n),j.v,B*n)))}z.push(O),P=c?"":y("path",["d",et(L,o),"fill","url(#"+(x[f].rn[1]>.4?k.t:x[f].rn[0]>0?k.r:k.l)+")"]);var D=J(j.n,u),T=W(V(D,q)),U=W(V(D,T)),Q=rt[j.val];for(G=0;G<Q.length;G++){var H=J(N(N(j.c,j.u,Q[G][0]*n/3),j.v,Q[G][1]*n/3),u),C=p(H),R=p(N(H,T,i)),Y=p(N(H,U,i)),Z=[R[0]-C[0],R[1]-C[1]];P+=y("ellipse",["cx",g(C[0],1),"cy",g(C[1],1),"rx",g(Math.sqrt(Z[0]*Z[0]+Z[1]*Z[1]),1),"ry",g(Math.sqrt((Y[0]-C[0])*(Y[0]-C[0])+(Y[1]-C[1])*(Y[1]-C[1])),1),"transform","rotate("+g(180*Math.atan2(Z[1],Z[0])/Math.PI,1)+" "+g(C[0],1)+" "+g(C[1],1)+")","fill",c?t.col("pip"):"url(#"+k.p+")"])}S+=P}var $=t.tk(),E=t.tk(),F=[];for(f=0;f<z.length;f++)for(var _=f+1;_<z.length;_++){for(var at=z[f],ot=z[_],it=[],lt=0;lt<4;lt++)for(var ut=0;ut<4;ut++){var ft=at[lt][0]-ot[ut][0],st=at[lt][1]-ot[ut][1];if(ft*ft+st*st<.01){it.push(at[lt]);break}}2===it.length&&F.push(it)}var ht=null;if(3===F.length)for(var ct=0;ct<2&&!ht;ct++){for(var vt=0,dt=0;dt<3;dt++)for(var pt=0;pt<2;pt++){var Mt=F[dt][pt][0]-F[0][ct][0],gt=F[dt][pt][1]-F[0][ct][1];Mt*Mt+gt*gt<.01&&vt++}3===vt&&(ht=F[0][ct])}var mt=nt(M),yt=[];for(f=0;f<F.length;f++){var bt=F[f][0],kt=F[f][1];if(ht){if(Math.abs(bt[0]-ht[0])+Math.abs(bt[1]-ht[1])>.1){var xt=bt;bt=kt,kt=xt}var wt=kt[0]-bt[0],qt=kt[1]-bt[1],St=Math.sqrt(wt*wt+qt*qt);if(St>1e-6){for(var Pt=Math.min(St,a),It=0;It<3;It++)if(It!==f){var zt=Math.abs(F[It][0][0]-bt[0])+Math.abs(F[It][0][1]-bt[1])<.1?F[It][1]:F[It][0],Gt=zt[0]-bt[0],jt=zt[1]-bt[1],Lt=Math.min(St,Math.sqrt(Gt*Gt+jt*jt))/3;Lt<Pt&&(Pt=Lt)}bt=[bt[0]+wt/St*Pt,bt[1]+qt/St*Pt]}yt.push(bt)}for(var Ot=-1,At=0;At<mt.length;At++){var Bt=mt[At][0]-kt[0],Dt=mt[At][1]-kt[1];if(Bt*Bt+Dt*Dt<.01){Ot=At;break}}if(Ot>=0){var Tt=mt[(Ot-1+mt.length)%mt.length],Ut=mt[(Ot+1)%mt.length],Qt=kt[0]-Tt[0],Ht=kt[1]-Tt[1],Ct=Ut[0]-kt[0],Rt=Ut[1]-kt[1],Yt=Math.sqrt(Qt*Qt+Ht*Ht),Zt=Math.sqrt(Ct*Ct+Rt*Rt),$t=kt[0]-bt[0],Et=kt[1]-bt[1],Ft=$t*$t+Et*Et;if(Yt>1e-6&&Zt>1e-6&&Ft>1e-6){var Jt=Math.min(a,Yt/3,Zt/3),Kt=[kt[0]-Qt/Yt*Jt,kt[1]-Ht/Yt*Jt],Nt=[kt[0]+Ct/Zt*Jt,kt[1]+Rt/Zt*Jt],Vt=Kt[0],Wt=2*(kt[0]-Kt[0]),Xt=Kt[0]-2*kt[0]+Nt[0],_t=Kt[1],tr=2*(kt[1]-Kt[1]),rr=Kt[1]-2*kt[1]+Nt[1],nr=Xt*Et-rr*$t,er=Wt*Et-tr*$t,ar=(Vt-bt[0])*Et-(_t-bt[1])*$t,or=[];if(Math.abs(nr)<1e-9){if(Math.abs(er)>1e-9){var ir=-ar/er;ir>0&&ir<1&&or.push(ir)}}else{var lr=er*er-4*nr*ar;if(lr>=0)for(var ur=Math.sqrt(lr),fr=-1;fr<=1;fr+=2){var sr=(fr*ur-er)/(2*nr);sr>0&&sr<1&&or.push(sr)}}for(var hr=null,cr=0;cr<or.length;cr++){var vr=or[cr],dr=((Vt+vr*Wt+vr*vr*Xt-bt[0])*$t+(_t+vr*tr+vr*vr*rr-bt[1])*Et)/Ft;dr>0&&dr<1&&(null==hr||dr>hr)&&(hr=dr)}if(null!=hr){var pr=(c?2.2:1.4)*t.w/Math.sqrt(Ft);kt=[bt[0]+$t*(hr-pr),bt[1]+Et*(hr-pr)]}}}I+=y("path",["clip-path",c?null:"url(#"+E+")","d","M"+g(bt[0],1)+" "+g(bt[1],1)+"L"+g(kt[0],1)+" "+g(kt[1],1),"fill","none","stroke",c?t.col("outline"):t.col("ink"),"stroke-opacity",c?null:".15","stroke-width",g((c?2.2:1.4)*t.w,1),"stroke-linecap","round"])}if(ht&&3===yt.length)for(f=0;f<3;f++)for(var Mr=f+1;Mr<3;Mr++)I+=y("path",["clip-path",c?null:"url(#"+E+")","d","M"+g(yt[f][0],1)+" "+g(yt[f][1],1)+"Q"+g(ht[0],1)+" "+g(ht[1],1)+" "+g(yt[Mr][0],1)+" "+g(yt[Mr][1],1),"fill","none","stroke",c?t.col("outline"):t.col("ink"),"stroke-opacity",c?null:".15","stroke-width",g((c?2.2:1.4)*t.w,1),"stroke-linecap","round"]);return t.defs+=y("path",["id",$,"d",b])+y("clipPath",["id",E],y("use",["href","#"+$])),{shadow:y("ellipse",["cx",0,"cy",118,"rx",132,"ry",24,"fill",t.col("ink"),"fill-opacity",".16","filter","url(#"+k.b14+")"])+y("ellipse",["cx",0,"cy",112,"rx",62,"ry",11,"fill",t.col("ink"),"fill-opacity",".18","filter","url(#"+k.b5+")"]),content:c?y("use",["href","#"+$,"fill",m(t.dieGlass),"fill-opacity",".55","stroke",t.col("outline"),"stroke-width",g(2.6*t.w,1)])+y("g",["clip-path","url(#"+E+")"],S+I):y("use",["href","#"+$,"fill",m(t.dieUnder)])+y("g",["clip-path","url(#"+E+")","filter","url(#"+k.sf+")"],S)+y("use",["href","#"+$,"fill","url(#"+k.a+")"])+y("use",["href","#"+$,"fill","none","stroke","#00000010","stroke-width",2])+I}}var it=[[-1,0],[1,0],[0,-1],[0,1],[0,0]],lt=["lucky7","snake","boxcars","matched","mixed"],ut=[[1,6],[2,5],[3,4]];function ft(t,r,n,e){if("snake"===t)return r<2?1:0;if("boxcars"===t)return r<2?6:0;if("lucky7"===t&&r<2){if(!e.l7){var a=ut[Math.floor(3*n("result:lucky7")())];e.l7=n("result:lucky7:swap")()<.5?a:[a[1],a[0]]}return e.l7[r]}if("matched"===t)return 1+Math.floor(6*n("result:matched")());if("mixed"===t){for(var o=[],i=1;i<=6;i++)e[i]||o.push(i);var l=o[Math.floor(n("result:mixed:"+r)()*o.length)];return e[l]=1,l}return 0}function st(t){var r,n=t||{},e=Math.max(2,Math.min(5,Math.round(n.dice)||3)),a=Y(n,"table"+e),o=a.p,i=a.S;at(a);var l=0,f=0;for(r=0;r<e;r++)l=Math.max(l,Math.abs(it[r][0])),f=Math.max(f,Math.abs(it[r][1]));var s=l*v+p,m=f*v+M,b=c[0]+(c[1]-c[0])*i("table:rail")(),k=28+40*i("table:corner")(),x=a.cab,w=a.col("outline"),q=a.col("trim"),S=g(4*a.w,2),P=function(t){return x?["fill",t]:["fill","none","stroke",w,"stroke-width",S]},I=function(t,r,n,e,a,i){return y("rect",["x",g(t,o),"y",g(r,o),"width",g(n,o),"height",g(e,o),"rx",a?g(a,o):null].concat(i))},z=n.values||[],G=lt.indexOf(n.result)>=0?n.result:"",j={},L=function(t){return 1+Math.floor(6*i("die:"+t+":value")())},O=[];for(r=0;r<e;r++){var A=Math.round(z[r]);A>=1&&A<=6&&(O[r]=A,j[A]=1)}for(r=0;r<e;r++)if(null==O[r])if(G){var B=ft(G,r,i,j);O[r]=B||L(r)}else O[r]=L(r);var D=null==n.speed?1:n.speed,T=!!n.motion&&D>0,U="",Q=null;T&&(Q=R(i,"motion"+(n.salt||"")+a.key+D));var H=[];for(r=0;r<e;r++){var C=(i("die:"+r+":orient")()<.5?-1:1)*(u[0]+u[1]*i("die:"+r+":orient:yaw")()),$=it[r][0]*v+2*(i("die:"+r+":place")()-.5)*d*v,E=it[r][1]*v+2*(i("die:"+r+":place:v")()-.5)*d*v,J=.9+.1*(it[r][1]+1)/2,K="translate("+g($,1)+" "+g(E,1)+")"+(1!==J?" scale("+J+")":""),N=ot(a,O[r],C),V=N.content,W=null;if(T){var X=Q(),_=30+40*i("die:"+r+":drop")();W=Q(),U+="@keyframes "+X+"{from{transform:translateY("+g(-_,1)+"px)}}."+W+"{animation:"+X+" "+g((.8+.3*r)/D,2)+"s cubic-bezier(.2,.7,.3,1.08)}",V=y("g",["class",W],V)}H.push({x:$,y:E,cls:W,shadow:y("g",["transform",K],N.shadow),body:y("g",["transform",K],V)})}H.sort(function(t,r){return t.y-r.y});var tt="",rt="";for(r=0;r<e;r++)tt+=H[r].shadow,rt+=H[r].body;for(r=0;r<e;r++)for(var nt=r+1;nt<e;nt++){var et=H[r].x-H[nt].x,ut=H[r].y-H[nt].y;Math.sqrt(et*et+ut*ut)<240&&(tt+=y("ellipse",["cx",g((H[r].x+H[nt].x)/2,1),"cy",g((H[r].y+H[nt].y)/2+54,1),"rx",36,"ry",9,"fill",a.col("ink"),"fill-opacity",".14","filter","url(#"+a.die.b5+")"]))}var st="";if(T){var ht=[];for(r=0;r<e;r++)H[r].cls&&ht.push("."+H[r].cls);st=y("style",[],U+"@media(prefers-reduced-motion:reduce){"+ht.join(",")+"{animation:none}}")}return st+=I(-s-b,-m-b,2*(s+b),2*(m+b),k,P(a.col("rail"))),st+=I(-s,-m,2*s,2*m,Math.max(0,k-b),P(a.col("felt"))),st+=I(-s,-m,2*s,2*m,Math.max(0,k-b),F(a,q,".25")),st+=tt+rt,Z(n,a,[-s-h[0],-m-h[1],2*(s+h[0]),2*(m+h[1])],st)}function ht(t){var r=t||{},n=Y(r,"die"),e=n.S;at(n);var a=ot(n,Math.round(r.value)>=1&&Math.round(r.value)<=6?Math.round(r.value):1+Math.floor(6*e("die:0:value")()),(e("die:0:orient")()<.5?-1:1)*(u[0]+u[1]*e("die:0:orient:yaw")()));return Z(r,n,[-p,-M,2*p,2*M+42],y("g",["transform","translate(0 0)"],a.shadow+a.content))}function ct(t,r){var n="string"==typeof t?document.querySelector(t):t,e={};if(!n)return null;var a=function(t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);n.innerHTML=st(e)};return a(r||{}),{el:n,get:function(){var t={};for(var r in e)t[r]=e[r];return t},set:a,destroy:function(){n.innerHTML=""}}}return{toss:st,die:ht,palette:C,init:ct}}();"object"==typeof module&&module.exports?module.exports=r:t.Dice=r}("undefined"!=typeof self?self:this);
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spintax/dice-lite",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A procedural throw of dice as an SVG string. Seeded placement and values, brand-aware table palette, paper or frosted-glass bodies, an optional CSS roll. Zero dependencies; runs in Node at build time and in the browser.",
|
|
5
|
+
"main": "dice.js",
|
|
6
|
+
"types": "dice.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dice.js",
|
|
9
|
+
"dice.min.js",
|
|
10
|
+
"dice.d.ts",
|
|
11
|
+
"CHANGELOG.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "terser dice.js --compress --mangle -o dice.min.js",
|
|
15
|
+
"size": "node scripts/size.mjs",
|
|
16
|
+
"lint": "eslint .",
|
|
17
|
+
"test": "node --test \"test/*.test.mjs\"",
|
|
18
|
+
"prepack": "npm run build",
|
|
19
|
+
"gate:browser": "node scripts/browser-gate.mjs"
|
|
20
|
+
},
|
|
21
|
+
"config": {
|
|
22
|
+
"sizeBudget": 8704
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"dice",
|
|
26
|
+
"die",
|
|
27
|
+
"craps",
|
|
28
|
+
"casino",
|
|
29
|
+
"gambling",
|
|
30
|
+
"svg",
|
|
31
|
+
"illustration",
|
|
32
|
+
"generative",
|
|
33
|
+
"generative-art",
|
|
34
|
+
"procedural",
|
|
35
|
+
"hero-background",
|
|
36
|
+
"line-art",
|
|
37
|
+
"palette",
|
|
38
|
+
"ssr",
|
|
39
|
+
"static-site",
|
|
40
|
+
"zero-dependencies",
|
|
41
|
+
"vanilla-js"
|
|
42
|
+
],
|
|
43
|
+
"author": "301st (https://301.st)",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/investblog/dice-lite#readme",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/investblog/dice-lite.git"
|
|
52
|
+
},
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/investblog/dice-lite/issues"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"eslint": "^10.11.0",
|
|
58
|
+
"terser": "^5.51.2"
|
|
59
|
+
}
|
|
60
|
+
}
|