egatari 0.6.10 → 0.6.11-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +22 -22
- package/README.md +1 -1
- package/bin/egatari.mjs +1 -1
- package/components.lock.json +288 -144
- package/components2.lock.json +657 -1
- package/kit/build.mjs +3 -2
- package/kit/check-messages.mjs +46 -12
- package/kit/check.mjs +54 -19
- package/kit/core/article-messages.mjs +5 -4
- package/kit/core/attachments.mjs +2 -1
- package/kit/core/build-core.mjs +2 -1
- package/kit/core/check-parity.mjs +3 -1
- package/kit/core/check-result.mjs +2 -1
- package/kit/core/check-rules.mjs +132 -0
- package/kit/core/colab-core.mjs +2 -2
- package/kit/core/geometry-check.mjs +302 -0
- package/kit/core/layout-scan.mjs +9 -2
- package/kit/core/live-results.mjs +1 -1
- package/kit/core/llm-fix.mjs +3 -2
- package/kit/core/llm-outline.mjs +3 -2
- package/kit/core/llm-pack-data.mjs +114 -114
- package/kit/core/narration-lint.mjs +37 -0
- package/kit/core/new-deck-pack.mjs +2 -1
- package/kit/core/new-deck.mjs +1 -1
- package/kit/core/plural.mjs +5 -0
- package/kit/core/portable.mjs +6 -5
- package/kit/core/sample-layout.mjs +14 -0
- package/kit/core/text-band.mjs +37 -0
- package/kit/core/tier-a.mjs +2 -1
- package/kit/instruction-version.json +1 -1
- package/kit/player.json +1 -1
- package/kit/player.lock.json +2 -2
- package/kit/render-compare.mjs +5 -4
- package/kit/tools.js +2 -2
- package/package.json +3 -2
- package/server/editor-ui.js +2 -2
- package/vendor/components/CATALOG.md +12 -12
- package/vendor/components/sha256/00d9072cb279a1a68166787f53395c747072388971d916c082a106844dbf888f/index.js +315 -0
- package/vendor/components/sha256/03e20fb6803de0d94e93de7c01d7d00cede57b0b2604b449daa4cb156367500d/README.en.md +23 -0
- package/vendor/components/sha256/03e20fb6803de0d94e93de7c01d7d00cede57b0b2604b449daa4cb156367500d/index.js +289 -0
- package/vendor/components/sha256/0e5458deb63e32aefdbf8722fa156fd44cd90d5d8f9d6a25a636007b4b599450/README.en.md +40 -0
- package/vendor/components/sha256/0e5458deb63e32aefdbf8722fa156fd44cd90d5d8f9d6a25a636007b4b599450/index.js +341 -0
- package/vendor/components/sha256/1073e03374f0fe65d2691f160bfd8c092986c168d0bd6a6094ebd444d4b89c27/index.js +592 -0
- package/vendor/components/sha256/12b1b32eb3a75e9f11eff407b7876473674ae996d7ce276e2ab6ffaf41ad4376/README.en.md +48 -0
- package/vendor/components/sha256/12b1b32eb3a75e9f11eff407b7876473674ae996d7ce276e2ab6ffaf41ad4376/index.js +384 -0
- package/vendor/components/sha256/147987bd84722249dab609293d4fafa93f0a2aac25b8517fda1710840b575d00/index.js +341 -0
- package/vendor/components/sha256/170e9072285213a2b4e79d7c2e4ddb882d1501e60b5da63c59b1679643163005/README.en.md +23 -0
- package/vendor/components/sha256/170e9072285213a2b4e79d7c2e4ddb882d1501e60b5da63c59b1679643163005/index.js +120 -0
- package/vendor/components/sha256/17da8ef5ce58354df54ebb52c8a9b0726e69d06ac28cc8e78f807ec338002782/README.en.md +32 -0
- package/vendor/components/sha256/17da8ef5ce58354df54ebb52c8a9b0726e69d06ac28cc8e78f807ec338002782/index.js +222 -0
- package/vendor/components/sha256/1b30dfd9b366ff07556024eb8ce75cca2eac8001fa392eed4e1232047001ec74/index.js +404 -0
- package/vendor/components/sha256/37846655693f8dd869d11d3d4cddb43be57f848037d0e49564b2250401448ab5/README.en.md +66 -0
- package/vendor/components/sha256/37846655693f8dd869d11d3d4cddb43be57f848037d0e49564b2250401448ab5/index.js +920 -0
- package/vendor/components/sha256/3fdd15756f779a16362dd854f143ca4343154059882d88d44adf64745c7876e3/README.en.md +50 -0
- package/vendor/components/sha256/3fdd15756f779a16362dd854f143ca4343154059882d88d44adf64745c7876e3/index.js +171 -0
- package/vendor/components/sha256/435c3f8e0c0abc123866e57ae2c7f3409e19d14a2eb67b1fde84c4da5d95f14c/README.en.md +31 -0
- package/vendor/components/sha256/435c3f8e0c0abc123866e57ae2c7f3409e19d14a2eb67b1fde84c4da5d95f14c/index.js +232 -0
- package/vendor/components/sha256/43740da51de85c1b7ec1eaf688f93fd9ea25b15977ab075e6abac196f08643a6/index.js +122 -0
- package/vendor/components/sha256/4817f77bd021ec5c77d5a15ee56e2e34d31d1d98630e86225eb20f2f9bf095e9/index.js +812 -0
- package/vendor/components/sha256/4e4bf3b2968c36a334dc997e32e36f22ecbc4f5dfe1c76bb7a5beef002fe8899/index.js +228 -0
- package/vendor/components/sha256/553e132d1f84caad648bb7175a206d4c5d18c005b5ca17989f5e8a5e3f5cdfa1/index.js +75 -0
- package/vendor/components/sha256/5b02d8a81a41f4d1658f1972f3fc91de3cc431b2c71bb6b92601d58f734de9ec/README.en.md +34 -0
- package/vendor/components/sha256/5b02d8a81a41f4d1658f1972f3fc91de3cc431b2c71bb6b92601d58f734de9ec/index.js +244 -0
- package/vendor/components/sha256/5c72414df5a948a7cbb882be1e5bdd4113d188e357d4a0f01fabda15781417eb/LICENSE-tabler.txt +21 -0
- package/vendor/components/sha256/5c72414df5a948a7cbb882be1e5bdd4113d188e357d4a0f01fabda15781417eb/index.js +4868 -0
- package/vendor/components/sha256/5c72414df5a948a7cbb882be1e5bdd4113d188e357d4a0f01fabda15781417eb/names.txt +4808 -0
- package/vendor/components/sha256/636f47e955f5550f046cfc7a3de8de70c4b97aa77771bde2ee0e31d706bb032e/index.js +278 -0
- package/vendor/components/sha256/65ddc34bd9c75e073f4c2cd283369417fe88769521809670c6b8730852d4f647/index.js +97 -0
- package/vendor/components/sha256/6b0c75b0f697fe451197301de82a113f1c2548b67f7a8d2b56576d2bae8e240c/index.js +607 -0
- package/vendor/components/sha256/6cdf432d3e61be3ff15e80910369324ac7ea0e8b9f62c32ef37bf09a5bfe93b9/README.en.md +33 -0
- package/vendor/components/sha256/6cdf432d3e61be3ff15e80910369324ac7ea0e8b9f62c32ef37bf09a5bfe93b9/index.js +390 -0
- package/vendor/components/sha256/6d648d0e5c96ec680c04a14b7c3d672010d3b9f101197689e9672ec00174383d/README.en.md +31 -0
- package/vendor/components/sha256/6d648d0e5c96ec680c04a14b7c3d672010d3b9f101197689e9672ec00174383d/index.js +267 -0
- package/vendor/components/sha256/71208afc5c8ea1ed6d2180f46de7385e43990cfb046ba350eefb917eca5d92fe/README.en.md +22 -0
- package/vendor/components/sha256/71208afc5c8ea1ed6d2180f46de7385e43990cfb046ba350eefb917eca5d92fe/index.js +145 -0
- package/vendor/components/sha256/7552a029f30f9c2f2ec8c5af4f144b63eeffc9172b53e35a925821f69a738b64/README.en.md +44 -0
- package/vendor/components/sha256/7552a029f30f9c2f2ec8c5af4f144b63eeffc9172b53e35a925821f69a738b64/index.js +203 -0
- package/vendor/components/sha256/781a45d97f1325cfb40aa98d00e1db9600949158fdb2749b0223b019c67a80f2/index.js +919 -0
- package/vendor/components/sha256/850944419734168be3dc202057de128131869c471cb8ab3baec2cfe789d56493/index.js +465 -0
- package/vendor/components/sha256/863d4d42075fbae1084089730f10da943c88d3e3e655235f28c24b9a3cf95c88/README.en.md +37 -0
- package/vendor/components/sha256/863d4d42075fbae1084089730f10da943c88d3e3e655235f28c24b9a3cf95c88/index.js +424 -0
- package/vendor/components/sha256/8a892d25d0bb6105ba31ea690b465876ce3af1fbaf18e2a9ce44ad2fee81260e/index.js +162 -0
- package/vendor/components/sha256/8acec8f299510287fbad6a69311ec58a39b0e99647dd03609c818e053b3b6087/index.js +484 -0
- package/vendor/components/sha256/8ef66bd1e800bcbedef436585469d308665aaa1ab6011aee699544302d0a8ad0/index.js +312 -0
- package/vendor/components/sha256/8fa5ed6ab12a27542e077ff965bea626ff9450b330609304dc1148fc8dbb68b0/README.en.md +40 -0
- package/vendor/components/sha256/8fa5ed6ab12a27542e077ff965bea626ff9450b330609304dc1148fc8dbb68b0/index.js +348 -0
- package/vendor/components/sha256/a0c71874d83045baaef70285bd00d87a7c6c43219fa5630dfff60d5cb08509be/index.js +250 -0
- package/vendor/components/sha256/a31f39625d7d1087d06a72b644cec417372f98add940288257ef07f812d8395a/index.js +747 -0
- package/vendor/components/sha256/ae04a57be4a26b7443796a93c58adcf1cbcf746120eba619bf9c2bcdce53c640/README.en.md +38 -0
- package/vendor/components/sha256/ae04a57be4a26b7443796a93c58adcf1cbcf746120eba619bf9c2bcdce53c640/index.js +289 -0
- package/vendor/components/sha256/ae06622a328da2a01260bc68b28bb632c391e8d62a4e8387638e5a944525a418/index.js +521 -0
- package/vendor/components/sha256/b490139e9afa84164054738532d5ec4985bf071e40791d903012fe3b850bae43/README.en.md +24 -0
- package/vendor/components/sha256/b490139e9afa84164054738532d5ec4985bf071e40791d903012fe3b850bae43/index.js +103 -0
- package/vendor/components/sha256/b756c66d5c6e6ace90912ab467dea3031f2c9b291d8b2a786ca660faa0e8ac84/index.js +121 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/ABCJS-ADAPTER.md +101 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/LICENSE-abcjs.txt +21 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/chords.json +89 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/doremi.json +261 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/extras.json +192 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/kaeru.json +521 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/rhythm.json +248 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/ties.json +360 -0
- package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/index.js +508 -0
- package/vendor/components/sha256/bea7cb344f55677d5756c9711b90cc971126830ec5f66a03f71f13dbfadc401e/index.js +351 -0
- package/vendor/components/sha256/c08b25a2f7be68c36634cdee180aa617fd018d80b7ddf2a484a2edb4acffdbe0/README.en.md +42 -0
- package/vendor/components/sha256/c08b25a2f7be68c36634cdee180aa617fd018d80b7ddf2a484a2edb4acffdbe0/index.js +268 -0
- package/vendor/components/sha256/cbe8bb4b2fd0845a9333bc2064d86758ea6465992ad86a99167ba17f57f3e382/README.en.md +51 -0
- package/vendor/components/sha256/cbe8bb4b2fd0845a9333bc2064d86758ea6465992ad86a99167ba17f57f3e382/index.js +228 -0
- package/vendor/components/sha256/d2df3d70fc7c6e07e3cad7332a7e5501e84abeb24653786e567f57046adebcba/index.js +268 -0
- package/vendor/components/sha256/d386f2b94cd9fbed765ad301aa7009228599edb2859efe7f37a98d7aaa44b9bb/README.en.md +35 -0
- package/vendor/components/sha256/d386f2b94cd9fbed765ad301aa7009228599edb2859efe7f37a98d7aaa44b9bb/index.js +179 -0
- package/vendor/components/sha256/e6d219f67d241fdf00a40d4cec261d9dd3c09ad3110d3418d4e843d0f2b8c196/index.js +429 -0
- package/vendor/components/sha256/ef60e6c089f5f6e909f439af8fc0c9c0a2d7a1aafad5e65be6328ba993b990a0/index.js +354 -0
- package/vendor/components/sha256/f6f47601c90f0195353e94b76d57499591d6330a7e35714958a2c41229f70da2/index.js +198 -0
- package/vendor/components/sha256/f9159c87fa998487787f738fdfab52aa2483f52f170fc258840b127f6ed06051/README.en.md +44 -0
- package/vendor/components/sha256/f9159c87fa998487787f738fdfab52aa2483f52f170fc258840b127f6ed06051/index.js +304 -0
- package/vendor/components/sha256/fb482cbe2ca1da07d08f41880e03fe75ff3f1d29842ff40946541e63030219b8/README.en.md +42 -0
- package/vendor/components/sha256/fb482cbe2ca1da07d08f41880e03fe75ff3f1d29842ff40946541e63030219b8/index.js +481 -0
- package/vendor/components/sha256/fdcea28e27638d8bfe234016672375e14de8bb97c32f7b6840d1b7b10ac4b1cd/index.js +233 -0
- package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/LICENSE-strokesvg.txt +115 -0
- package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/OFL.txt +93 -0
- package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/index.js +749 -0
- package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/strokes-CHANGES.md +20 -0
- package/vendor/components-2/manifest-2.7.0.json +2118 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// base 1.0.0: the shared base class and helpers of Egatari components 2.0 (a library part, Egatari spec §6.1).
|
|
2
|
+
// Element parts require it ({base: "1.2.0"}) and receive what this factory returns:
|
|
3
|
+
// {MotionElement, el, lerp, prog, clamp, mtext, variant, previewOpacity}
|
|
4
|
+
// It uses no import, no network and no global of the player (spec §6.1): only the DOM, getComputedStyle and console.
|
|
5
|
+
// It requires shape (the data-shape checker): MotionElement checks the part's data against its declared shape before the
|
|
6
|
+
// part counts its steps or mounts (spec §6.1 "Data shapes"; players no longer do). The dependency runs base → shape only:
|
|
7
|
+
// shape is pure and needs nothing from base, so there is no cycle.
|
|
8
|
+
//
|
|
9
|
+
// Provenance: MotionElement, el, mtext, lerp, prog and clamp (with the private ease and stepInt) are copied from
|
|
10
|
+
// Egatari Studio's kit/deck.js (https://github.com/egatari/egatari-studio, commit 03eaf0607598b993eb020f8e619caa53526bc771,
|
|
11
|
+
// where they are DECK.MotionElement, DECK.el, DECK.mtext, DECK.lerp, DECK.prog and DECK.clamp01), MIT License,
|
|
12
|
+
// Copyright (c) 2026 Gendo Kumoi, the same owner and licence as this repository. They are kept as close to that code as
|
|
13
|
+
// possible so that a part moved from DECK to base draws the same DOM (tools/test-base.mjs compares them with the pinned
|
|
14
|
+
// Studio's DECK). variant is tools/variant.js's VARIANT.of, taking the names instead of looking the tag up again;
|
|
15
|
+
// previewOpacity is the --preview-opacity read the parts used to write themselves. The data check (compText, compProblem,
|
|
16
|
+
// compSummary, compPlaceholder) is deck.js's, with DECK.shape → the shape part and DECK.ja → ctx.lang. Differences: base's
|
|
17
|
+
// own warnings start "[base] " (deck.js: "deck.js: ", "[deck] "), and a data problem is reported with a spec §8.4 code
|
|
18
|
+
// (reportCode) and the part's "[<name>] " prefix.
|
|
19
|
+
egatariParts.define({name: "base", version: "1.2.0", requires: {shape: "1.0.0"}}, function(deps){
|
|
20
|
+
"use strict";
|
|
21
|
+
var NS = "http://www.w3.org/2000/svg";
|
|
22
|
+
var SH = deps.shape;
|
|
23
|
+
|
|
24
|
+
// ---------------------------------------------------------------- 時間の部品 (kit/deck.js)
|
|
25
|
+
function clamp01(x){ return x < 0 ? 0 : x > 1 ? 1 : x; }
|
|
26
|
+
function ease(x){ x = clamp01(x); return x < .5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2; }
|
|
27
|
+
// t 秒のときの、start から dur 秒かける動きの進み具合(0〜1、イージング付き)
|
|
28
|
+
function prog(t, start, dur){ if (t === Infinity) return 1; if (dur <= 0) return t >= start ? 1 : 0; return ease((t - start) / dur); }
|
|
29
|
+
function lerp(a, b, e){ return a + (b - a) * e; }
|
|
30
|
+
// 段階の番号(steps / data-from):有限の整数で min 以上。そうでなければ警告して null
|
|
31
|
+
function stepInt(v, what, min){
|
|
32
|
+
if (v === null || v === undefined) return null;
|
|
33
|
+
var n = typeof v === "number" ? v : String(v).trim() === "" ? NaN : +v;
|
|
34
|
+
if (!isFinite(n) || n !== Math.floor(n) || n < (min || 0)){ console.warn("[base] " + what + "=" + JSON.stringify(v) + " は 0 以上の整数でないので無視 / ignored (not a non-negative integer)"); return null; }
|
|
35
|
+
return n;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------- SVG の部品 (kit/deck.js)
|
|
39
|
+
// 色は CSS 変数で書けるように、fill / stroke / color に var( があれば style に回す(属性では var() が効かない)
|
|
40
|
+
function el(tag, attrs, parent){
|
|
41
|
+
var e = document.createElementNS(NS, tag), st = "";
|
|
42
|
+
for (var k in attrs){
|
|
43
|
+
var v = attrs[k];
|
|
44
|
+
if ((k === "fill" || k === "stroke" || k === "color" || k === "stop-color") && String(v).indexOf("var(") >= 0) st += k + ":" + v + ";";
|
|
45
|
+
else e.setAttribute(k, v);
|
|
46
|
+
}
|
|
47
|
+
if (st) e.setAttribute("style", st + (attrs.style || ""));
|
|
48
|
+
if (parent) parent.appendChild(e);
|
|
49
|
+
return e;
|
|
50
|
+
}
|
|
51
|
+
// SVG の中の簡単な数式記法:*..* は斜体、_x は下付き、^x は上付き(本格的な式は組み立てのときに描く。spec §6.2)
|
|
52
|
+
function mtext(parent, x, y, str, attrs){
|
|
53
|
+
var t = el("text", Object.assign({x: x, y: y, "font-size": 24}, attrs || {}), parent);
|
|
54
|
+
// 下付き・上付きは本文の 3/4、ただし 24px より小さくしない(図の字は 24px 以上。1.0.0 は 17px だった)。ずらしは字の大きさの
|
|
55
|
+
// −0.25em・+0.45em(sub・super の語は字によってずれが違い、Source Sans 3 では下に 17px 近く落ちる)
|
|
56
|
+
var subSize = Math.max(24, Math.round((+t.getAttribute("font-size") || 24) * 0.75));
|
|
57
|
+
marks(str).forEach(function(m){
|
|
58
|
+
var s = el("tspan", m.shift ? {"baseline-shift": m.shift === "sub" ? "-0.25em" : "0.45em", "font-size": String(subSize)} : {}, t);
|
|
59
|
+
if (m.it){ s.setAttribute("font-style", "italic"); s.setAttribute("class", "mi"); }
|
|
60
|
+
s.textContent = m.text;
|
|
61
|
+
});
|
|
62
|
+
return t;
|
|
63
|
+
}
|
|
64
|
+
// mtext の記法を区切る(1.2.0)。* は対になるときだけ斜体:開く * の直後と閉じる * の直前が空白でなく、間に字があるとき
|
|
65
|
+
// (markdown と同じ)。「A* 探索」「5*3」「注*」の * はそのまま。\* \_ \^ はその字そのもの。_x・^x は次の 1 字を下付き・上付きに
|
|
66
|
+
function marks(str){
|
|
67
|
+
var out = [], it = false, buf = "", i = 0;
|
|
68
|
+
str = String(str == null ? "" : str);
|
|
69
|
+
function flush(shift){ if (buf){ out.push({text: buf, it: it, shift: shift || null}); buf = ""; } }
|
|
70
|
+
function closer(from){
|
|
71
|
+
for (var j = from; j < str.length; j++){
|
|
72
|
+
if (str[j] === "\\"){ j++; continue; }
|
|
73
|
+
if (str[j] === "*" && !/\s/.test(str[j - 1])) return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
while (i < str.length){
|
|
78
|
+
var c = str[i];
|
|
79
|
+
if (c === "\\" && i + 1 < str.length && "*_^\\".includes(str[i + 1])){ buf += str[i + 1]; i += 2; }
|
|
80
|
+
else if (c === "*" && (it ? !/\s/.test(str[i - 1]) : i + 1 < str.length && !/[\s*]/.test(str[i + 1]) && closer(i + 2))){ flush(); it = !it; i++; }
|
|
81
|
+
else if ((c === "_" || c === "^") && i + 1 < str.length){
|
|
82
|
+
flush(); buf = str[i + 1]; flush(c === "_" ? "sub" : "sup");
|
|
83
|
+
i += 2;
|
|
84
|
+
} else { buf += c; i++; }
|
|
85
|
+
}
|
|
86
|
+
flush();
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 字の幅の見積もり(px)。テーマの字で 1 字ずつ測った幅(em)を、字の種類ごとに表にした(2026-11 測定、普通と太字で同じ)。
|
|
91
|
+
// 字の種類:数字・英小文字・英大文字・空白・. , : ;・( ) [ ]・+ − × =・% # & / ?・ひらがな・カタカナ・、。()・「」・ほかのラテン文字・ほか(漢字ほか 1)
|
|
92
|
+
var TW_CLASS = [/[0-9]/, /[a-z]/, /[A-Z]/, / /, /[.,:;'"!|]/, /[()[\]{}]/, /[+\-\u2212\u00d7\u00f7=<>\u00b1~]/, /[%#&@$\/?]/,
|
|
93
|
+
/[\u3040-\u309f]/, /[\u30a0-\u30ff]/, /[\u3001\u3002\uff08\uff09\u30fb\u300c\u300d]/, /[\u0000-\u024f]/];
|
|
94
|
+
var TW_FONT = {
|
|
95
|
+
"biz udpgothic": [0.75, 0.62, 0.76, 0.3, 0.31, 0.5, 0.84, 0.75, 0.97, 0.93, 0.6, 0.62],
|
|
96
|
+
"biz udpmincho": [0.67, 0.6, 0.76, 0.3, 0.33, 0.49, 0.84, 0.75, 0.94, 0.9, 0.6, 0.6],
|
|
97
|
+
"source sans 3": [0.5, 0.48, 0.57, 0.22, 0.25, 0.31, 0.5, 0.6, 1, 1, 0.7, 0.5]
|
|
98
|
+
};
|
|
99
|
+
// 知らない字(テーマが別の字を名前で指すときなど):表の中でいちばん広い幅(はみ出さない側に見積もる)
|
|
100
|
+
var TW_WIDE = TW_CLASS.map(function(_, i){ return Math.max.apply(null, Object.keys(TW_FONT).map(function(f){ return TW_FONT[f][i]; })); });
|
|
101
|
+
function twTable(font){
|
|
102
|
+
if (font && font.nodeType === 1) font = getComputedStyle(font).fontFamily;
|
|
103
|
+
var first = String(font || "").split(",")[0].replace(/["']/g, "").trim().toLowerCase();
|
|
104
|
+
return TW_FONT[first] || TW_WIDE;
|
|
105
|
+
}
|
|
106
|
+
// textWidth(文字列, 大きさ, {font: 要素か font-family, mark: true で mtext の *_^ を数えない})
|
|
107
|
+
function textWidth(str, size, opts){
|
|
108
|
+
opts = opts || {};
|
|
109
|
+
var s = String(str == null ? "" : str), tb = twTable(opts.font), w = 0;
|
|
110
|
+
if (opts.mark) s = marks(s).map(function(m){ return m.text; }).join("");
|
|
111
|
+
for (var ch of s){ var e = 1; for (var i = 0; i < TW_CLASS.length; i++) if (TW_CLASS[i].test(ch)){ e = tb[i]; break; } w += e; }
|
|
112
|
+
return w * (size || 24);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------- 部品のデータの形 (kit/deck.js; spec §6.1 Data shapes)
|
|
116
|
+
// static shape(形は shape パーツが確かめる)と、static props の json の値の shape。違えば知らせの札を出し、描かない
|
|
117
|
+
function compText(c){
|
|
118
|
+
var t = "";
|
|
119
|
+
[].forEach.call(c.childNodes, function(n){ if (!(n.nodeType === 1 && n.localName === "script")) t += n.textContent; });
|
|
120
|
+
return t.trim();
|
|
121
|
+
}
|
|
122
|
+
function compProblem(c){
|
|
123
|
+
var C = c.constructor, decl = C.props || {}, sh = C.shape;
|
|
124
|
+
for (var name in decl){
|
|
125
|
+
var d = decl[name], a = c.getAttribute(name);
|
|
126
|
+
if (d.type !== "json" || a === null) continue;
|
|
127
|
+
var v;
|
|
128
|
+
try { v = JSON.parse(a); } catch (e) { return {kind: "attrJson", attr: name, msg: String(e && e.message || e), example: JSON.stringify(d.default)}; }
|
|
129
|
+
var ea = d.shape && SH ? SH.check(d.shape, v) : null;
|
|
130
|
+
if (ea) return Object.assign({kind: "attr", attr: name, example: JSON.stringify(d.example !== undefined ? d.example : d.default)}, ea);
|
|
131
|
+
}
|
|
132
|
+
var data;
|
|
133
|
+
try { data = c.data; } catch (e) { return {kind: "json", msg: String(e && e.message || e)}; }
|
|
134
|
+
if (!sh || !SH) return null;
|
|
135
|
+
if (sh.text){ if (data !== null) return {kind: "notText"}; if (!compText(c)) return {kind: "empty"}; return null; }
|
|
136
|
+
if (sh.links){ if (data !== null) return {kind: "notLinks"}; if (!c.querySelector("a[href]")) return {kind: "noLinks"}; return null; }
|
|
137
|
+
if (sh.schema){
|
|
138
|
+
if (data === null) return {kind: "missing"};
|
|
139
|
+
var e2 = SH.check(sh.schema, data);
|
|
140
|
+
if (e2 && e2.path === "" && e2.why === "type" && SH.typeOf(data) === "object" && [].concat(sh.schema.type)[0] === "array"){
|
|
141
|
+
// 配列のはずが object:よくある書き方({"series": [...], "xr": [...]})なら、どれが属性で、どの配列が中身かを言う
|
|
142
|
+
var ks = Object.keys(data);
|
|
143
|
+
e2.keys = ks;
|
|
144
|
+
e2.attrKeys = ks.filter(function(k){ return Object.prototype.hasOwnProperty.call(decl, k); });
|
|
145
|
+
// 中身らしい配列:object の並び({"series": [{…}]})を先に、なければ属性でない配列
|
|
146
|
+
var arr = ks.filter(function(k){ return Array.isArray(data[k]); });
|
|
147
|
+
e2.arrayKey = arr.filter(function(k){ return SH.typeOf(data[k][0]) === "object"; })[0] || arr.filter(function(k){ return e2.attrKeys.indexOf(k) < 0; })[0] || null;
|
|
148
|
+
var it = e2.arrayKey && data[e2.arrayKey][0];
|
|
149
|
+
if (it && SH.typeOf(it) === "object" && sh.schema.items){ var ei = SH.check(sh.schema.items, it, "[0]"); if (ei) e2.inner = ei; }
|
|
150
|
+
}
|
|
151
|
+
if (e2) return Object.assign({kind: "shape"}, e2);
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
function compSummary(r, ja){
|
|
156
|
+
var at = r.path ? r.path : (ja ? "データ" : "data");
|
|
157
|
+
switch (r.kind){
|
|
158
|
+
case "shape": return r.why === "required" ? (ja ? at + " がない(" + r.expected + ")" : at + " is missing (" + r.expected + ")")
|
|
159
|
+
: r.why === "unknown" ? (ja ? "知らない欄 " + r.key : "unknown field " + r.key)
|
|
160
|
+
: r.why === "anyOf" ? (ja ? (r.path || "データ") + " に " + r.expected + " がない" : (r.path || "data") + " needs " + r.expected)
|
|
161
|
+
: (ja ? at + ":" + r.expected + " のはずが " + r.got : at + ": expected " + r.expected + ", got " + r.got);
|
|
162
|
+
case "missing": return ja ? "データ(子の <script type=\"application/json\">)がない" : "no data (child <script type=\"application/json\">)";
|
|
163
|
+
case "json": return (ja ? "データの JSON が読めない:" : "the data is not valid JSON: ") + r.msg;
|
|
164
|
+
case "attrJson": return (ja ? "属性 " + r.attr + " の JSON が読めない:" : "attribute " + r.attr + " is not valid JSON: ") + r.msg;
|
|
165
|
+
case "attr": return (ja ? "属性 " + r.attr + (r.path ? " の " + r.path : "") + ":" + r.expected + " のはずが " + r.got : "attribute " + r.attr + (r.path ? " at " + r.path : "") + ": expected " + r.expected + ", got " + r.got);
|
|
166
|
+
case "notText": return ja ? "JSON ではなく要素の中の字で書く" : "write it as text inside the element, not JSON";
|
|
167
|
+
case "empty": return ja ? "中身の字がない" : "no text inside";
|
|
168
|
+
case "notLinks": return ja ? "JSON ではなく子の <a href> で書く" : "write it as child <a href> links, not JSON";
|
|
169
|
+
case "noLinks": return ja ? "子の <a href> がない" : "no child <a href> links";
|
|
170
|
+
default: return (ja ? "描けない:" : "cannot draw: ") + r.msg;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// The ctx.report code of a data problem (spec §8.4 vocabulary): an attribute value (attr, attrJson) → attr, anything else
|
|
174
|
+
// about the element's data (shape, missing, json, notText, empty, notLinks, noLinks) → data. The problem's own kind stays in
|
|
175
|
+
// the message: "[<name>] data (<kind>): <summary>"
|
|
176
|
+
function reportCode(kind){ return kind === "attr" || kind === "attrJson" ? "attr" : "data"; }
|
|
177
|
+
function compPlaceholder(c, r, ja){
|
|
178
|
+
[].slice.call(c.childNodes).forEach(function(n){ if (!(n.nodeType === 1 && n.localName === "script")) c.removeChild(n); });
|
|
179
|
+
var box = document.createElement("div");
|
|
180
|
+
box.className = "m-error"; box.setAttribute("role", "note");
|
|
181
|
+
box.textContent = "<" + r.tag + "> " + (ja ? "のデータの形が違う:" : "data problem: ") + compSummary(r, ja);
|
|
182
|
+
c.appendChild(box);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// The element's data problem (compProblem with the record fields deck.js's compFail added), checked once, or null
|
|
186
|
+
function dataProblem(e){
|
|
187
|
+
if (e._shapeDone) return e._shapeProblem;
|
|
188
|
+
e._shapeDone = true;
|
|
189
|
+
var p = compProblem(e);
|
|
190
|
+
if (p){
|
|
191
|
+
var sh = e.constructor.shape || {};
|
|
192
|
+
p = Object.assign({tag: e.tagName.toLowerCase(), label: e.getAttribute("aria-label") || "", phase: "data"}, p);
|
|
193
|
+
if (sh.schema && p.shape === undefined) p.shape = SH.brief(sh.schema);
|
|
194
|
+
if (sh.example !== undefined && p.example === undefined) p.example = typeof sh.example === "string" ? sh.example : JSON.stringify(sh.example);
|
|
195
|
+
}
|
|
196
|
+
return (e._shapeProblem = p || null);
|
|
197
|
+
}
|
|
198
|
+
function up(e, name){ return Reflect.get(Object.getPrototypeOf(e), name, e); }
|
|
199
|
+
|
|
200
|
+
// ---------------------------------------------------------------- containment (spec §6.1 Containment)
|
|
201
|
+
// An exception thrown by the part while the player calls it (steps, mount, render, layout; also duration and cues) does not
|
|
202
|
+
// escape: the first one is kept as the instance's fault, reported once through ctx.report("error", "[<name>] <phase>: <error>")
|
|
203
|
+
// as soon as a ctx is at hand (steps, duration and cues get none: it is reported at the next mount, render or layout), and
|
|
204
|
+
// the element then shows the .m-error placeholder with its text equivalent (aria-label, else its own text). From then on
|
|
205
|
+
// steps is the steps attribute (else 0), and the part is not called again. <name> is the registry name the class declares
|
|
206
|
+
// (static partName, = its define meta's name; lint checks it), else the tag without m-, else the whole tag.
|
|
207
|
+
function partName(e){
|
|
208
|
+
var n = e.constructor.partName, t = e.tagName.toLowerCase();
|
|
209
|
+
if (typeof n === "string" && n) return n;
|
|
210
|
+
return t.indexOf("m-") === 0 ? t.slice(2) : t;
|
|
211
|
+
}
|
|
212
|
+
function errText(x){ return x && x.message !== undefined ? (x.name && x.name !== "Error" ? x.name + ": " : "") + x.message : String(x); }
|
|
213
|
+
function fallbackSteps(e){ var n = stepInt(e.getAttribute("steps"), e.tagName.toLowerCase() + " steps"); return n !== null ? n : 0; }
|
|
214
|
+
function textEquivalent(e){
|
|
215
|
+
var label = e.getAttribute("aria-label");
|
|
216
|
+
if (label && label.trim()) return label.trim();
|
|
217
|
+
var t = compText(e).replace(/\s+/g, " ");
|
|
218
|
+
return t || "<" + e.tagName.toLowerCase() + ">";
|
|
219
|
+
}
|
|
220
|
+
function fault(e, phase, x, ctx){
|
|
221
|
+
if (!e._fault) e._fault = {phase: phase, msg: errText(x)};
|
|
222
|
+
if (ctx) showFault(e, ctx);
|
|
223
|
+
}
|
|
224
|
+
function showFault(e, ctx){
|
|
225
|
+
if (e._faultShown) return;
|
|
226
|
+
e._faultShown = true;
|
|
227
|
+
var text = textEquivalent(e);
|
|
228
|
+
try { if (ctx && typeof ctx.report === "function") ctx.report("error", "[" + partName(e) + "] " + e._fault.phase + ": " + e._fault.msg); } catch (x) { /* a player's report must not undo containment */ }
|
|
229
|
+
[].slice.call(e.childNodes).forEach(function(n){ if (!(n.nodeType === 1 && n.localName === "script")) e.removeChild(n); });
|
|
230
|
+
var box = document.createElement("div");
|
|
231
|
+
box.className = "m-error"; box.setAttribute("role", "note"); box.setAttribute("data-part-failure", "error");
|
|
232
|
+
box.textContent = text;
|
|
233
|
+
e.appendChild(box);
|
|
234
|
+
}
|
|
235
|
+
// Installed on every instance (own properties, so they come before the part's own steps, mount, render, duration, cues,
|
|
236
|
+
// layout): with data that fits, they pass to the part inside try/catch (containment, above); otherwise steps is the
|
|
237
|
+
// attribute steps (else 0), mount reports the problem once through ctx.report(reportCode(kind), "[<name>] data (<kind>): …") and shows the placeholder
|
|
238
|
+
// (like deck.js), and the rest draws nothing
|
|
239
|
+
function guard(e){
|
|
240
|
+
Object.defineProperty(e, "steps", {configurable: true, get: function(){
|
|
241
|
+
if (dataProblem(e) || e._fault) return fallbackSteps(e);
|
|
242
|
+
try { return up(e, "steps"); } catch (x) { fault(e, "steps", x, null); return fallbackSteps(e); }
|
|
243
|
+
}});
|
|
244
|
+
e.mount = function(ctx){
|
|
245
|
+
var p = dataProblem(e);
|
|
246
|
+
if (!p){
|
|
247
|
+
if (e._fault) return showFault(e, ctx);
|
|
248
|
+
try { return up(e, "mount").call(e, ctx); } catch (x) { fault(e, "mount", x, ctx); return; }
|
|
249
|
+
}
|
|
250
|
+
if (!e._shapeReported){ e._shapeReported = true; if (ctx && typeof ctx.report === "function") ctx.report(reportCode(p.kind), "[" + partName(e) + "] data (" + p.kind + "): " + compSummary(p, false)); }
|
|
251
|
+
compPlaceholder(e, p, /^ja\b/i.test((ctx && ctx.lang) || "ja"));
|
|
252
|
+
};
|
|
253
|
+
e.render = function(k, t, ctx){
|
|
254
|
+
if (dataProblem(e)) return;
|
|
255
|
+
if (e._fault) return showFault(e, ctx);
|
|
256
|
+
try { return up(e, "render").call(e, k, t, ctx); } catch (x) { fault(e, "render", x, ctx); }
|
|
257
|
+
};
|
|
258
|
+
e.duration = function(k){
|
|
259
|
+
if (dataProblem(e) || e._fault) return 0;
|
|
260
|
+
try { return up(e, "duration").call(e, k); } catch (x) { fault(e, "duration", x, null); return 0; }
|
|
261
|
+
};
|
|
262
|
+
e.cues = function(k){
|
|
263
|
+
if (dataProblem(e) || e._fault) return null;
|
|
264
|
+
try { return up(e, "cues").call(e, k); } catch (x) { fault(e, "cues", x, null); return null; }
|
|
265
|
+
};
|
|
266
|
+
// layout(ctx) is optional (spec §8.4 Layout): wrapped only when the part has one, so that a player sees none otherwise
|
|
267
|
+
if (typeof up(e, "layout") === "function") e.layout = function(ctx){
|
|
268
|
+
if (dataProblem(e)) return;
|
|
269
|
+
if (e._fault) return showFault(e, ctx);
|
|
270
|
+
try { return up(e, "layout").call(e, ctx); } catch (x) { fault(e, "layout", x, ctx); }
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ---------------------------------------------------------------- 部品の基底クラス (kit/deck.js)
|
|
275
|
+
class MotionElement extends HTMLElement {
|
|
276
|
+
constructor(){ super(); guard(this); }
|
|
277
|
+
get steps(){ var a = stepInt(this.getAttribute("steps"), this.tagName.toLowerCase() + " steps"); return a !== null ? a : (this.constructor.steps || 1); }
|
|
278
|
+
get from(){ var a = stepInt(this.getAttribute("data-from"), this.tagName.toLowerCase() + " data-from", 1); return a !== null ? a : 1; }
|
|
279
|
+
// static props の宣言に従って属性を読む
|
|
280
|
+
get props(){
|
|
281
|
+
if (this._props) return this._props;
|
|
282
|
+
var decl = this.constructor.props || {}, out = {};
|
|
283
|
+
for (var name in decl){
|
|
284
|
+
var d = decl[name], a = this.getAttribute(name);
|
|
285
|
+
if (a === null){ out[name] = d.default; continue; }
|
|
286
|
+
out[name] = d.type === "number" ? +a : d.type === "bool" ? a !== "false" : d.type === "json" ? JSON.parse(a) : a;
|
|
287
|
+
}
|
|
288
|
+
return (this._props = out);
|
|
289
|
+
}
|
|
290
|
+
// 子の <script type="application/json"> の中身。type の前後の空白を除いて大文字小文字を区別せずに application/json、id によらない。
|
|
291
|
+
// 子に 2 つ以上あれば、どれを使うか決められないので使わない(null)
|
|
292
|
+
get data(){
|
|
293
|
+
if (this._data !== undefined) return this._data;
|
|
294
|
+
var c = [].filter.call(this.children, function(x){ return x.localName === "script" && String(x.getAttribute("type") || "").trim().toLowerCase() === "application/json"; });
|
|
295
|
+
if (c.length > 1) console.warn("[base] " + this.tagName.toLowerCase() + ": more than one <script type=\"application/json\"> child (ignored)");
|
|
296
|
+
return (this._data = c.length === 1 ? JSON.parse(c[0].textContent) : null);
|
|
297
|
+
}
|
|
298
|
+
mount(ctx){}
|
|
299
|
+
render(k, t, ctx){}
|
|
300
|
+
duration(k){ return 0; }
|
|
301
|
+
cues(k){ return null; }
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ---------------------------------------------------------------- 見せ方 (tools/variant.js VARIANT.of; spec §8.4 Presentation variants)
|
|
305
|
+
// The variant is the element's `variant` attribute when it names one of `names`, otherwise the first name. names defaults
|
|
306
|
+
// to the class's `static variants`. No seed; an unknown name warns and gives the first.
|
|
307
|
+
function variant(e, names){
|
|
308
|
+
var tag = e.tagName.toLowerCase(), v = e.getAttribute("variant");
|
|
309
|
+
if (!names) names = e.constructor.variants || [];
|
|
310
|
+
if (v !== null && names.indexOf(v) >= 0) return v;
|
|
311
|
+
if (v !== null) console.warn(tag + ' variant="' + v + '" is not one of ' + names.join(", ") + " (the first, " + (names[0] || "none") + ", is used)");
|
|
312
|
+
return names[0] || null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ---------------------------------------------------------------- 先を薄く見せる濃さ (spec §8.4 Theme colours: --preview-opacity)
|
|
316
|
+
// The theme's --preview-opacity as seen from the element (a number in 0…1), or 0.45 when it is not set or not in 0…1
|
|
317
|
+
function previewOpacity(e){
|
|
318
|
+
var pv = parseFloat(getComputedStyle(e).getPropertyValue("--preview-opacity"));
|
|
319
|
+
return pv >= 0 && pv <= 1 ? pv : 0.45;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// ---------------------------------------------------------------- 色の段階 (spec §8.4 Theme colours: colour ramps --ramp-1 … --ramp-5)
|
|
323
|
+
// The colour at t (0…1, clamped) on the theme's ramp: at a stop var(--ramp-k, old), between two stops their color-mix in oklab.
|
|
324
|
+
// `old` is the colour the part drew at t before ramps (a CSS colour built from --bg and its role); both sides fall back to
|
|
325
|
+
// it, so on a theme without ramps the result is exactly `old` (spec: a component keeps the role it used).
|
|
326
|
+
function ramp(t, old){
|
|
327
|
+
t = +t; t = t > 0 ? (t < 1 ? t : 1) : 0;
|
|
328
|
+
var s = t * 4, k = Math.floor(s + 1e-9), f = s - k;
|
|
329
|
+
if (k >= 4) return "var(--ramp-5, " + old + ")";
|
|
330
|
+
if (f < 1e-9) return "var(--ramp-" + (k + 1) + ", " + old + ")";
|
|
331
|
+
return "color-mix(in oklab, var(--ramp-" + (k + 1) + ", " + old + ") " + (100 * (1 - f)).toFixed(1) + "%, var(--ramp-" + (k + 2) + ", " + old + "))";
|
|
332
|
+
}
|
|
333
|
+
// The theme's five ramp stops as seen from element e (computed strings), or null when any of them is not set
|
|
334
|
+
function rampStops(e){
|
|
335
|
+
var cs = getComputedStyle(e), out = [];
|
|
336
|
+
for (var i = 1; i <= 5; i++){ var v = cs.getPropertyValue("--ramp-" + i).trim(); if (!v) return null; out.push(v); }
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return {MotionElement: MotionElement, el: el, lerp: lerp, prog: prog, clamp: clamp01, mtext: mtext, textWidth: textWidth, variant: variant, previewOpacity: previewOpacity, ramp: ramp, rampStops: rampStops};
|
|
341
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Balance at expectation
|
|
3
|
+
steps: Weights → pivot → sum (3)
|
|
4
|
+
props:
|
|
5
|
+
dur: Seconds
|
|
6
|
+
digits: Decimal places
|
|
7
|
+
size: Text px
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
JSON:
|
|
11
|
+
- `values`: 2–12 distinct numbers.
|
|
12
|
+
- `probs`: sum to 1 (else reported).
|
|
13
|
+
- `name`: X.
|
|
14
|
+
|
|
15
|
+
<!-- llm: omit -->
|
|
16
|
+
Bars proportional to probabilities. Pivot: minimum → E[X]. Moment controls tilt, clamped ±8°, then levels. Beam, bars and ticks rotate together. Starts horizontal; tilts at step 2. Final E and mtext sum; first 6 terms then …; computation and accessible weights include all terms. Negative terms parenthesized.
|
|
17
|
+
|
|
18
|
+
Invalid totals stay unnormalized. Out-of-range probabilities, mismatched lengths, duplicates or unrepresentable calculations: report, no draw. dur 1.5s; digits 0–10 (2), trailing zeros removed; size 0=26px, ≥24. Bad props default/report.
|
|
19
|
+
|
|
20
|
+
1184×440px suggested. Scaled coordinates, nice ticks. Close bars, labels or long sums report size.
|
|
21
|
+
|
|
22
|
+
ctx.lang selects ja/en. Font boxes ≥1.43em plus gap. Image role, final summary; preserve author label, hide SVG. No answer preview. Fictional demo.
|
|
23
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// m-balance: expectation is the zero-moment position of probability weights.
|
|
2
|
+
egatariParts.define({name: "balance", version: "2.0.1", requires: {base: "1.2.0", plot: "1.1.2"}, element: "m-balance"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base;
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {title: "期待値はつり合いの位置", probability: "確率", sum: "確率の合計", expectation: "期待値"},
|
|
7
|
+
en: {title: "Expectation is the balance point", probability: "probability", sum: "probability total", expectation: "expectation"}
|
|
8
|
+
};
|
|
9
|
+
function set(e, a, v){ e.setAttribute(a, String(v)); }
|
|
10
|
+
function ticks(lo, hi){
|
|
11
|
+
var rough = (hi - lo) / 5, power = Math.pow(10, Math.floor(Math.log10(rough))), f = rough / power;
|
|
12
|
+
var step = (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * power, out = [];
|
|
13
|
+
for (var i = Math.ceil(lo / step); out.length < 10 && i * step <= hi + step * 1e-9; i++) out.push(+(i * step).toPrecision(12));
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function num(v){ return String(+v.toPrecision(6)); }
|
|
17
|
+
return class extends base.MotionElement {
|
|
18
|
+
static partName = "balance";
|
|
19
|
+
static preview = "none";
|
|
20
|
+
static steps = 3;
|
|
21
|
+
static shape = {schema: {type: "object", additionalProperties: false, required: ["values", "probs"], properties: {
|
|
22
|
+
values: {type: "array", minItems: 2, maxItems: 12, items: {type: "number"}},
|
|
23
|
+
probs: {type: "array", minItems: 2, maxItems: 12, items: {type: "number"}}, name: {type: "string"}
|
|
24
|
+
}}, example: {values: [0, 1, 2, 3], probs: [0.1, 0.3, 0.4, 0.2], name: "X"}};
|
|
25
|
+
static props = {
|
|
26
|
+
dur: {type: "number", default: 1.5, doc: "ステップの秒数"},
|
|
27
|
+
digits: {type: "number", default: 2, doc: "期待値の小数桁"},
|
|
28
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は 26)"}
|
|
29
|
+
};
|
|
30
|
+
duration(k){ return k >= 1 && k <= 3 ? this.props.dur : 0; }
|
|
31
|
+
label(g, x, y, s, anchor){
|
|
32
|
+
var e = base.el("text", {x: x, y: y, "font-size": this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, g);
|
|
33
|
+
e.textContent = s; return e;
|
|
34
|
+
}
|
|
35
|
+
mount(ctx){
|
|
36
|
+
var self = this, d = this.data, P = this.props;
|
|
37
|
+
this.T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
38
|
+
if (!(Number.isFinite(P.dur) && P.dur > 0)){ ctx.report("attr", "[balance] dur must be positive; using 1.5"); P.dur = 1.5; }
|
|
39
|
+
if (!Number.isInteger(P.digits) || P.digits < 0 || P.digits > 10){ ctx.report("attr", "[balance] digits must be an integer 0–10; using 2"); P.digits = 2; }
|
|
40
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[balance] size must be 0 or at least 24; using 26"); P.size = 0; }
|
|
41
|
+
this.fs = P.size || 26;
|
|
42
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb"; this.setAttribute("role", "img");
|
|
43
|
+
if (d.values.length !== d.probs.length || new Set(d.values).size !== d.values.length){ ctx.report("data", "[balance] values must be distinct and probs must have the same length"); return; }
|
|
44
|
+
var total = d.probs.reduce(function(s, p){ return s + p; }, 0);
|
|
45
|
+
if (Math.abs(total - 1) > 1e-9) ctx.report("data", "[balance] probs must sum to 1 (±1e-9); drawing supplied weights, total " + total);
|
|
46
|
+
if (d.probs.some(function(p){ return p < 0 || p > 1; })){ ctx.report("data", "[balance] probabilities must be between 0 and 1"); return; }
|
|
47
|
+
// Scale before sums and coordinate differences to keep finite extreme values usable.
|
|
48
|
+
var scale = Math.pow(10, Math.floor(Math.log10(Math.max(1, ...d.values.map(Math.abs))))), vals = d.values.map(function(v){ return v / scale; });
|
|
49
|
+
var expectedN = vals.reduce(function(s, v, i){ return s + v * d.probs[i]; }, 0), expected = expectedN * scale;
|
|
50
|
+
if (!Number.isFinite(expected)){ ctx.report("data", "[balance] expectation exceeds the numerical range; rescale values"); return; }
|
|
51
|
+
var lo = Math.min(...vals, expectedN), hi = Math.max(...vals, expectedN), span = hi - lo;
|
|
52
|
+
if (!(span > 0)){ ctx.report("data", "[balance] values are too close to represent; rescale values"); return; }
|
|
53
|
+
var name = d.name === undefined ? "X" : d.name, answer = String(+expected.toFixed(P.digits));
|
|
54
|
+
var expectation = "E[" + name + "] = " + answer;
|
|
55
|
+
var terms = d.values.map(function(v, i){ return (v < 0 ? "(" + num(v) + ")" : num(v)) + "×" + num(d.probs[i]); });
|
|
56
|
+
var formula = terms.slice(0, 6).join(" + ") + (terms.length > 6 ? " + …" : "") + " = " + answer;
|
|
57
|
+
this.model = {values: d.values.slice(), probs: d.probs.slice(), total: total, expected: expected, expectedN: expectedN, scale: scale, name: name, formula: formula, lo: lo, hi: hi};
|
|
58
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", this.T.title + "; " + d.values.map(function(v, i){ return num(v) + ": " + self.T.probability + " " + num(d.probs[i]); }).join("; ") + "; " + this.T.sum + " " + num(total) + "; " + this.T.expectation + " " + expectation + "; " + formula);
|
|
59
|
+
var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight), fs = this.fs, line = fs * 1.45 + 4;
|
|
60
|
+
var width = function(s){ return base.textWidth(s, fs, {font: self}); };
|
|
61
|
+
var pad = Math.max(56, ...d.values.map(function(v){ return width(num(v)) / 2 + 16; }));
|
|
62
|
+
var axisY = H / 2, plot = deps.plot.Plot(this, {xr: [lo, hi], yr: [0, 1], xt: [], yt: [], noY: true, m: {l: pad, r: pad, t: 0, b: H - axisY}});
|
|
63
|
+
this.svg = plot.svg; set(this.svg, "width", W); set(this.svg, "height", H); set(this.svg, "aria-hidden", "true");
|
|
64
|
+
var axes = Array.from(this.svg.children).filter(function(e){ return e.getAttribute("class") === "axes"; })[0]; this.svg.removeChild(axes);
|
|
65
|
+
this.weightLayer = base.el("g", {opacity: 0}, this.svg);
|
|
66
|
+
this.beam = base.el("g", {}, this.weightLayer);
|
|
67
|
+
base.el("line", {x1: pad, x2: W - pad, y1: axisY, y2: axisY, stroke: "var(--muted)", "stroke-width": 4}, this.beam);
|
|
68
|
+
var plotWidth = Math.max(1, W - 2 * pad), maxRise = plotWidth * Math.sin(8 * Math.PI / 180) / 4;
|
|
69
|
+
var barH = Math.max(20, axisY - 1.45 * fs - maxRise - 24), maxP = Math.max(1e-12, ...d.probs);
|
|
70
|
+
var positions = vals.map(plot.x), sorted = positions.slice().sort(function(a, b){ return a - b; });
|
|
71
|
+
var minGap = Math.min(...sorted.slice(1).map(function(x, i){ return x - sorted[i]; })), bw = Math.max(2, Math.min(44, minGap - 12));
|
|
72
|
+
var small = W < 2 * pad + 40 || axisY - barH - 1.1 * fs - maxRise < 0 || axisY + plotWidth * Math.sin(8 * Math.PI / 180) + line + fs * 0.4 > H;
|
|
73
|
+
this.bars = d.values.map(function(v, i){
|
|
74
|
+
var h = d.probs[i] / maxP * barH, x = positions[i], s = num(d.probs[i]);
|
|
75
|
+
var bar = base.el("rect", {x: x - bw / 2, y: axisY - h, width: bw, height: h, fill: "var(--term)"}, self.beam);
|
|
76
|
+
var text = self.label(self.beam, x, axisY - h - fs * 0.4 - 8, s, "middle");
|
|
77
|
+
return {bar: bar, text: text, x: x, h: h, w: width(s)};
|
|
78
|
+
});
|
|
79
|
+
this.bars.forEach(function(a, i){ self.bars.slice(i + 1).forEach(function(b){
|
|
80
|
+
if (Math.abs(a.x - b.x) < (a.w + b.w) / 2 + 6 && Math.abs(a.h - b.h) < 1.45 * fs + 4) small = true;
|
|
81
|
+
// A neighbouring tall bar must not run through a probability label.
|
|
82
|
+
if (Math.abs(a.x - b.x) < a.w / 2 + bw / 2 + 4 && b.h > a.h + 4 || Math.abs(a.x - b.x) < b.w / 2 + bw / 2 + 4 && a.h > b.h + 4) small = true;
|
|
83
|
+
}); });
|
|
84
|
+
// Nice ticks in normalized coordinates; labels retain the original units.
|
|
85
|
+
this.ticks = ticks(lo, hi);
|
|
86
|
+
var lastRight = -Infinity;
|
|
87
|
+
this.ticks.forEach(function(v){
|
|
88
|
+
var x = plot.x(v), s = num(v * scale), w = width(s);
|
|
89
|
+
if (x - w / 2 < lastRight + 4 || x - w / 2 < 0 || x + w / 2 > W) small = true;
|
|
90
|
+
lastRight = x + w / 2;
|
|
91
|
+
base.el("line", {x1: x, x2: x, y1: axisY, y2: axisY + 6, stroke: "var(--muted)", "stroke-width": 2}, self.beam);
|
|
92
|
+
self.label(self.beam, x, axisY + line, s, "middle");
|
|
93
|
+
});
|
|
94
|
+
this.fulcrum = base.el("g", {opacity: 0}, this.svg);
|
|
95
|
+
base.el("path", {d: "M0," + (axisY + 2) + "l-18,26h36Z", fill: "var(--emph-tint)", stroke: "var(--emph)", "stroke-width": 3}, this.fulcrum);
|
|
96
|
+
this.finalLayer = base.el("g", {opacity: 0}, this.svg);
|
|
97
|
+
this.start = plot.x(Math.min(...vals)); this.end = plot.x(expectedN); this.axisY = axisY;
|
|
98
|
+
var ew = width(expectation), labelX = Math.max(ew / 2 + 4, Math.min(W - ew / 2 - 4, this.end));
|
|
99
|
+
var labelY = axisY + 2 * line + 14, formulaY = H - fs * 0.4 - 4;
|
|
100
|
+
this.label(this.finalLayer, labelX, labelY, expectation, "middle");
|
|
101
|
+
// The sum has numeric terms only; variable names above are literal text.
|
|
102
|
+
this.formulaText = base.mtext(this.finalLayer, W / 2, formulaY, formula, {"font-size": fs, "text-anchor": "middle", fill: "var(--fg)"});
|
|
103
|
+
if (ew > W - 8 || width(formula) > W - 8 || formulaY - labelY < line || labelY + fs * 0.4 > H) small = true;
|
|
104
|
+
if (small) ctx.report("size", "[balance] weights, ticks, tilt or formula do not fit; enlarge the element or separate close values");
|
|
105
|
+
}
|
|
106
|
+
render(k, t){
|
|
107
|
+
if (!this.model) return;
|
|
108
|
+
var dur = this.props.dur, e = k > 2 ? 1 : k < 2 ? 0 : base.prog(t, 0, dur);
|
|
109
|
+
var pivot = base.lerp(this.start, this.end, e), m = this.model;
|
|
110
|
+
var pivotN = base.lerp(Math.min(...m.values.map(function(v){ return v / m.scale; })), m.expectedN, e);
|
|
111
|
+
// Positive moment means a heavier right side (clockwise in SVG). At E, the beam levels.
|
|
112
|
+
var moment = m.values.reduce(function(s, v, i){ return s + (v / m.scale - pivotN) * m.probs[i]; }, 0);
|
|
113
|
+
var angle = k >= 2 && e < 1 ? Math.max(-8, Math.min(8, 8 * moment / (m.hi - m.lo))) : 0;
|
|
114
|
+
set(this.weightLayer, "opacity", k > 1 ? 1 : k < 1 ? 0 : base.prog(t, 0, dur));
|
|
115
|
+
set(this.beam, "transform", "rotate(" + angle + " " + pivot + " " + this.axisY + ")");
|
|
116
|
+
set(this.fulcrum, "opacity", k >= 1 ? 1 : 0); set(this.fulcrum, "transform", "translate(" + pivot + " 0)");
|
|
117
|
+
set(this.finalLayer, "opacity", k > 3 ? 1 : k < 3 ? 0 : base.prog(t, 0, dur));
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Table comparing 2–3 things: one column, the other, then row differences
|
|
3
|
+
steps: columns + 1 (diff step; none if no diff/better)
|
|
4
|
+
props:
|
|
5
|
+
size: value text px (shrinks to 24)
|
|
6
|
+
dur: column appear (s)
|
|
7
|
+
diff-label: diff column heading
|
|
8
|
+
example: {"sides": [{"label": "Paper form", "tone": "muted"}, {"label": "Online form"}], "rows": [{"label": "Hours", "values": ["Weekdays 9–17", "24 h"], "better": 1, "diff": "Any time"}]}
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
2–3 things on the same points (old vs new, A vs B). Bars: `m-bars`; one number: `m-kpi`.
|
|
12
|
+
|
|
13
|
+
Child JSON: `{"sides": [{"label", "tone"}, …], "rows": [{"label", "values": [...], "diff", "better"}, …]}`.
|
|
14
|
+
|
|
15
|
+
- `sides`: 2 or 3; `label` = column heading; `tone` (`term`, `emph`, `muted`, `accent2`, `series-1..4`; default `term`, `accent2`, `series-4`); `muted` = old
|
|
16
|
+
- `rows`: `label` = point (optional); `values` in column order (text/number; `\n`), count = `sides` (else error card)
|
|
17
|
+
- `diff`: short difference, `--emph`, right column
|
|
18
|
+
- `better`: better column index (from 0): bold, `--emph` frame at diff step
|
|
19
|
+
- Steps: column 1 (+ point names) → 2 → (3) → diffs; column `step` overrides
|
|
20
|
+
- `variant`: `fade` (default), `slide`, `reveal` (row by row); same steps
|
|
21
|
+
|
|
22
|
+
The demo is `demo.html` (forms example fictional).
|
|
23
|
+
|
|
24
|
+
<!-- llm: omit -->
|
|
25
|
+
Notes
|
|
26
|
+
|
|
27
|
+
- Text width estimated from character count (CJK 1, Latin 0.6); may be off by a few characters
|
|
28
|
+
- Show later steps faintly (`ctx.preview`): later columns and diffs at `--preview-opacity` (default 0.45)
|
|
29
|
+
- Moves 16 px over `dur` (default 0.6 s); `reveal` rows dur/2 each, staggered within dur/2
|
|
30
|
+
- Table is top-aligned; spare height goes to rows
|
|
31
|
+
- Text shrinks from `size` to 24 px to fit; `slide` enters from the sides; all variants same end
|
|
32
|
+
<!-- llm: end -->
|