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,315 @@
|
|
|
1
|
+
// m-keyboard:音楽の授業のピアノの鍵盤。段階ごとに鍵盤を光らせ(音階・和音・半音と全音)、その音を再生器に鳴らしてもらう
|
|
2
|
+
// <m-keyboard style="height:380px"><script type="application/json">
|
|
3
|
+
// {"range": "C4-C6", "names": "solfa", "steps": [{"keys": ["C4", "E4", "G4"], "label": "主和音"}]}
|
|
4
|
+
// (JSON の終わり)</m-keyboard>
|
|
5
|
+
// 段階 0 は鍵盤だけ。段階 k は steps[k-1] の keys を --emph(または step の tone)で光らせる。keys ごとの t(秒)でずらせば音階・分散和音、
|
|
6
|
+
// 同じ t なら和音。cumulative: true なら前の段階の鍵盤も薄く光ったまま
|
|
7
|
+
// 音(仕様 §8.4「Component sounds」):events(k) は鍵盤 1 つに 1 つの note(pitch は鍵盤の名前、vel 0.7、dur は dur の欄か、
|
|
8
|
+
// 次の t まで(なければ 0.8 秒))。duration(k) は最後の音の終わりまで。音だけに頼らない:光った鍵盤と印がいつも同じことを示す
|
|
9
|
+
// 鍵盤の形(customElements.get("m-keyboard").kb、tools/test-keyboard.mjs):白鍵の幅を 1 として、ド〜ミの 3 つを 5 等分、ファ〜シの 4 つを
|
|
10
|
+
// 7 等分した 2・4(・6)番目に黒鍵を置く(本物の鍵盤と同じく、ド♯とレ♯は外へ、ファ♯とラ♯も外へ寄り、ソ♯は真ん中)
|
|
11
|
+
// 授業の表示(ctx.preview):先の段階の鍵盤をテーマの --preview-opacity(なければ 0.45)で薄く、印なしで見せる
|
|
12
|
+
// 見せ方(docs/conventions.md §1):light(既定。光がフェード)・press(鍵盤が少し沈んで戻る)・rise(光が鍵盤の下から上へ満ちる)
|
|
13
|
+
// 2.0(Egatari spec §6.1 の部品):base の MotionElement・el・prog・variant・previewOpacity を使い、プレイヤーのグローバルは使わない。
|
|
14
|
+
// 段階の数は ctx にも言語にもよらない(spec §8.4):既定の鍵盤の名前(日本語ならドレミ、ほかは CDE)は plan() ではなく mount で選ぶ。
|
|
15
|
+
// レッスンのデータや要素の大きさの問題は ctx.report で知らせる(1.x の console.warn("m-keyboard: …") の代わり)
|
|
16
|
+
egatariParts.define({name: "keyboard", version: "2.0.4", requires: {base: "1.2.0"}, element: "m-keyboard"}, function(deps){
|
|
17
|
+
"use strict";
|
|
18
|
+
var base = deps.base;
|
|
19
|
+
// ---- 鍵盤の名前と形(DOM を使わない。tools/test-keyboard.mjs が試す) ----
|
|
20
|
+
var KB = (function(){
|
|
21
|
+
var LETTERS = "CDEFGAB", PC = {C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11};
|
|
22
|
+
var SOLFA = {C: "ド", D: "レ", E: "ミ", F: "ファ", G: "ソ", A: "ラ", B: "シ"};
|
|
23
|
+
var IROHA = {C: "ハ", D: "ニ", E: "ホ", F: "ヘ", G: "ト", A: "イ", B: "ロ"};
|
|
24
|
+
var BW = 0.58, BH = 0.62; // 黒鍵の幅(白鍵の幅に対して)と長さ(白鍵の長さに対して)
|
|
25
|
+
// "C4" "C#4" "Db4"(♯ ♭ も)→ {letter, acc: "" | "#" | "b", oct, midi, white, name}。読めなければ null
|
|
26
|
+
function parseKey(s){
|
|
27
|
+
var m = /^([A-G])([#b♯♭]?)(-?\d)$/.exec(String(s).trim());
|
|
28
|
+
if (!m) return null;
|
|
29
|
+
var acc = m[2] === "♯" ? "#" : m[2] === "♭" ? "b" : m[2], oct = +m[3];
|
|
30
|
+
// E♯・B♯・F♭・C♭ は白鍵の別の名前:鍵盤の名前としては使わない
|
|
31
|
+
if ((acc === "#" && (m[1] === "E" || m[1] === "B")) || (acc === "b" && (m[1] === "F" || m[1] === "C"))) return null;
|
|
32
|
+
var midi = 12 * (oct + 1) + PC[m[1]] + (acc === "#" ? 1 : acc === "b" ? -1 : 0);
|
|
33
|
+
return {letter: m[1], acc: acc, oct: oct, midi: midi, white: !acc, name: m[1] + acc + oct};
|
|
34
|
+
}
|
|
35
|
+
// "C4-C6" → {lo, hi}(鍵盤 2 つ)。両端は白鍵、1〜3 オクターブ(半音 12〜36 個)。違えば Error(何が・どう書くか)
|
|
36
|
+
function parseRange(s){
|
|
37
|
+
var m = /^\s*([A-G][#b♯♭]?-?\d)\s*[-–〜~]\s*([A-G][#b♯♭]?-?\d)\s*$/.exec(String(s || ""));
|
|
38
|
+
if (!m) throw new Error('m-keyboard: range "' + s + '" is not two key names such as "C4-C6"');
|
|
39
|
+
var lo = parseKey(m[1]), hi = parseKey(m[2]);
|
|
40
|
+
if (!lo || !hi) throw new Error('m-keyboard: range "' + s + '": ' + (!lo ? m[1] : m[2]) + " is not a key name (C4, F#3, Bb4, …)");
|
|
41
|
+
if (!lo.white || !hi.white) throw new Error('m-keyboard: range "' + s + '" must start and end on white keys (C D E F G A B)');
|
|
42
|
+
var span = hi.midi - lo.midi;
|
|
43
|
+
if (span < 12 || span > 36) throw new Error('m-keyboard: range "' + s + '" covers ' + span + " semitones; it must cover 1–3 octaves (12–36, such as C4-C5 or C3-C6)");
|
|
44
|
+
return {lo: lo, hi: hi};
|
|
45
|
+
}
|
|
46
|
+
// 鍵 lo〜hi(midi)を覆う範囲の書き方:"C4-D5"(両端は白鍵、半音 12〜36 個。12 に足りなければ上に広げる)。36 を超えれば null
|
|
47
|
+
function cover(lo, hi){
|
|
48
|
+
var name = function(mi){ return "C D EF G A B".charAt(((mi % 12) + 12) % 12) + (Math.floor(mi / 12) - 1); };
|
|
49
|
+
var white = function(mi){ return [0, 2, 4, 5, 7, 9, 11].indexOf(((mi % 12) + 12) % 12) >= 0; };
|
|
50
|
+
if (!white(lo)) lo--;
|
|
51
|
+
if (!white(hi)) hi++;
|
|
52
|
+
if (hi - lo < 12){ hi = lo + 12; if (!white(hi)) hi++; }
|
|
53
|
+
return hi - lo > 36 ? null : name(lo) + "-" + name(hi);
|
|
54
|
+
}
|
|
55
|
+
// 範囲の鍵盤:白鍵 {midi, white: true, i, x(左の端), w: 1}、黒鍵 {midi, white: false, x(左の端), cx(真ん中), w: BW}。単位は白鍵の幅
|
|
56
|
+
function keys(r){
|
|
57
|
+
var out = [], whites = [], wi = -1;
|
|
58
|
+
for (var mi = r.lo.midi; mi <= r.hi.midi; mi++){
|
|
59
|
+
var pc = ((mi % 12) + 12) % 12, white = [0, 2, 4, 5, 7, 9, 11].indexOf(pc) >= 0;
|
|
60
|
+
if (white){ wi++; var k = {midi: mi, pc: pc, white: true, i: wi, x: wi, w: 1}; whites.push(k); out.push(k); continue; }
|
|
61
|
+
// 黒鍵:下の白鍵(i = wi)の組(ド〜ミ か ファ〜シ)の始まりから数える
|
|
62
|
+
var g = pc < 5 ? {start: wi - (pc - 1) / 2, n: 3, slot: (pc === 1 ? 1 : 3)} : {start: wi - (pc - 6) / 2, n: 4, slot: (pc === 6 ? 1 : pc === 8 ? 3 : 5)};
|
|
63
|
+
var cx = g.start + (g.slot + 0.5) * g.n / (2 * g.n - 1);
|
|
64
|
+
out.push({midi: mi, pc: pc, white: false, below: wi, cx: cx, x: cx - BW / 2, w: BW});
|
|
65
|
+
}
|
|
66
|
+
return {all: out, whites: whites, n: whites.length, BW: BW, BH: BH};
|
|
67
|
+
}
|
|
68
|
+
// 鍵盤の名前:names = "solfa"(ドレミ)・"abc"(CDE)・"iroha"(ハニホ)・"none"(null)。黒鍵は ♯・♭ をつける(ド♯ / C♯、レ♭ / D♭)。
|
|
69
|
+
// iroha は前に 嬰・変 をつける(嬰ハ、変ニ)
|
|
70
|
+
function label(k, names){
|
|
71
|
+
if (names === "none") return null;
|
|
72
|
+
if (names === "iroha") return (k.acc === "#" ? "嬰" : k.acc === "b" ? "変" : "") + IROHA[k.letter];
|
|
73
|
+
var s = names === "abc" ? k.letter : SOLFA[k.letter];
|
|
74
|
+
return s + (k.acc === "#" ? "♯" : k.acc === "b" ? "♭" : "");
|
|
75
|
+
}
|
|
76
|
+
// 白鍵の名前(範囲の中の midi から。白鍵は ♯ も ♭ もない)
|
|
77
|
+
function whiteLetter(midi){ return "C D EF G A B".charAt(((midi % 12) + 12) % 12); }
|
|
78
|
+
// 段階 1 つの音:鍵盤 1 つに 1 つ。t は keys ごと(なければ 0)。dur は step.dur → 全体の dur → 次の(より遅い)t まで → 0.8 秒
|
|
79
|
+
function notes(step, dur){
|
|
80
|
+
var ts = (step.keys || []).map(function(_, i){ return step.t && typeof step.t[i] === "number" ? step.t[i] : 0; });
|
|
81
|
+
var d0 = typeof step.dur === "number" ? step.dur : typeof dur === "number" ? dur : null;
|
|
82
|
+
return step.keys.map(function(key, i){
|
|
83
|
+
var k = parseKey(key), t = ts[i], later = ts.filter(function(u){ return u > t + 1e-9; }), next = later.length ? Math.min.apply(null, later) : null;
|
|
84
|
+
return {t: t, sound: "note", pitch: k ? k.name : String(key), dur: d0 !== null ? d0 : next !== null ? +(next - t).toFixed(6) : 0.8, vel: 0.7};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
// 段階の長さ:最後の音の終わり
|
|
88
|
+
function length(step, dur){ return notes(step, dur).reduce(function(a, n){ return Math.max(a, n.t + n.dur); }, 0); }
|
|
89
|
+
// 段階 k・時刻 t の鍵盤ごとの状態(render が描く。DOM を使わない)。steps は plan の段階(keys は parseKey の結果、ev は notes)。
|
|
90
|
+
// o = {cumulative, preview, variant, prog(t, at, len)}。{midi: {e: 今の段階の光の進み 0〜1, p: 動きの進み, tone, ids: {綴り: e},
|
|
91
|
+
// faded: 前の段階で光った(cumulative), fadeTone, fids, pre: 先の段階で光る(授業の表示で薄く。印なし), preTone}}
|
|
92
|
+
function state(steps, k, t, o){
|
|
93
|
+
var st = {}, cur = steps[k - 1];
|
|
94
|
+
var get = function(m){ return st[m] || (st[m] = {e: 0, p: 0, faded: false, pre: false, tone: null, fadeTone: null, preTone: null, ids: {}, fids: {}}); };
|
|
95
|
+
if (o.cumulative) for (var j = 0; j < k - 1 && j < steps.length; j++){
|
|
96
|
+
var sj = steps[j];
|
|
97
|
+
sj.keys.forEach(function(kk){ var s = get(kk.midi); s.faded = true; s.fadeTone = sj.tone; s.fids[kk.midi + "|" + kk.acc] = true; });
|
|
98
|
+
}
|
|
99
|
+
if (cur) cur.keys.forEach(function(kk, i){
|
|
100
|
+
var n = cur.ev[i], len = o.variant === "light" ? Math.min(0.2, n.dur) : Math.min(0.3, n.dur);
|
|
101
|
+
var e = t === Infinity ? 1 : o.prog(t, n.t, len), s = get(kk.midi), id = kk.midi + "|" + kk.acc;
|
|
102
|
+
if (e >= s.e){ s.e = e; s.p = t === Infinity ? 1 : Math.max(0, Math.min(1, (t - n.t) / len)); }
|
|
103
|
+
s.tone = cur.tone; if (e > 0) s.ids[id] = Math.max(s.ids[id] || 0, e);
|
|
104
|
+
});
|
|
105
|
+
// 授業の表示:先の段階の鍵盤を薄く(いちばん近い段階の色。今・前の段階で光っている鍵盤はそのまま)
|
|
106
|
+
if (o.preview) for (var q = steps.length - 1; q >= Math.max(0, k); q--){
|
|
107
|
+
var sq = steps[q];
|
|
108
|
+
sq.keys.forEach(function(kk){ var s = get(kk.midi); if (s.e > 0 || s.faded) return; s.pre = true; s.preTone = sq.tone; });
|
|
109
|
+
}
|
|
110
|
+
return st;
|
|
111
|
+
}
|
|
112
|
+
return {state: state, parseKey: parseKey, parseRange: parseRange, cover: cover, keys: keys, label: label, whiteLetter: whiteLetter, notes: notes, length: length, LETTERS: LETTERS, BW: BW, BH: BH};
|
|
113
|
+
})();
|
|
114
|
+
|
|
115
|
+
var TONES = /^(term|emph|muted|accent2|fg|series-[1-4])$/;
|
|
116
|
+
// 字の幅(base の textWidth:font の要素の字で測った幅)
|
|
117
|
+
var est = function(s, size, font){ return base.textWidth(s, size, {font: font}); };
|
|
118
|
+
return class extends base.MotionElement {
|
|
119
|
+
static partName = "keyboard"; // the registry name (define meta name), for report prefixes
|
|
120
|
+
static variants = ["light", "press", "rise"];
|
|
121
|
+
static kb = KB;
|
|
122
|
+
static shape = {
|
|
123
|
+
schema: {type: "object", additionalProperties: false, required: ["range", "steps"], properties: {
|
|
124
|
+
range: {type: "string"}, names: {type: "string", enum: ["solfa", "abc", "iroha", "none"]}, cumulative: {type: "boolean"}, dur: {type: "number"},
|
|
125
|
+
steps: {type: "array", minItems: 1, items: {type: "object", additionalProperties: false, required: ["keys"], properties: {
|
|
126
|
+
keys: {type: "array", minItems: 1, items: {type: "string"}}, label: {type: "string"}, t: {type: "array", items: {type: "number"}},
|
|
127
|
+
tone: {type: "string"}, dur: {type: "number"}}}}}},
|
|
128
|
+
example: {range: "C4-C6", names: "solfa", steps: [{keys: ["C4", "E4", "G4"], label: "主和音(ドミソ)"}]},
|
|
129
|
+
note: {en: "range \"C4-C6\" (white keys at both ends, 1–3 octaves); steps [{keys: [\"C4\", \"E4\"], label, t: [seconds per key], tone, dur}]",
|
|
130
|
+
ja: "range \"C4-C6\"(両端は白鍵、1〜3 オクターブ)、steps [{keys: [\"C4\", \"E4\"], label, t: [鍵盤ごとの秒], tone, dur}]"},
|
|
131
|
+
};
|
|
132
|
+
static props = {};
|
|
133
|
+
get steps(){
|
|
134
|
+
if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
|
|
135
|
+
return this.plan().steps.length;
|
|
136
|
+
}
|
|
137
|
+
// 範囲と段階を読む(mount の前にも steps が要る)。範囲・鍵盤の名前・t の数が違えば Error(札が出る)
|
|
138
|
+
plan(){
|
|
139
|
+
if (this._plan) return this._plan;
|
|
140
|
+
var d = this.data || {}, notes = [];
|
|
141
|
+
var r = KB.parseRange(d.range), kb = KB.keys(r), outside = [];
|
|
142
|
+
// 音の長さ dur(全体と段階ごと)は 0 より長い秒数:0 以下は知らせて、書いていないものとする(次の t まで、なければ 0.8 秒)
|
|
143
|
+
var gdur = d.dur;
|
|
144
|
+
if (typeof gdur === "number" && !(gdur > 0)){ notes.push("[keyboard] dur " + gdur + " is not more than 0 seconds (ignored: each note lasts until the next t, or 0.8 s)"); gdur = undefined; }
|
|
145
|
+
var steps = d.steps.map(function(s0, j){
|
|
146
|
+
var s = s0;
|
|
147
|
+
if (typeof s0.dur === "number" && !(s0.dur > 0)){ notes.push("[keyboard] steps[" + j + "] dur " + s0.dur + " is not more than 0 seconds (ignored)"); s = Object.assign({}, s0); delete s.dur; }
|
|
148
|
+
var ks = s.keys.map(function(name){
|
|
149
|
+
var k = KB.parseKey(name);
|
|
150
|
+
if (!k) throw new Error("m-keyboard: steps[" + j + "] key \"" + name + "\" is not a key name (C4, F#3, Bb4, …)");
|
|
151
|
+
if (k.midi < r.lo.midi || k.midi > r.hi.midi) outside.push("steps[" + j + "] key " + name);
|
|
152
|
+
return k;
|
|
153
|
+
});
|
|
154
|
+
if (s.t && s.t.length !== s.keys.length) throw new Error("m-keyboard: steps[" + j + "] has " + s.keys.length + " keys but " + s.t.length + " times in t (one per key)");
|
|
155
|
+
if (s.t && s.t.some(function(v){ return !(v >= 0); })) throw new Error("m-keyboard: steps[" + j + "] t must be seconds ≥ 0");
|
|
156
|
+
if (s.tone !== undefined && !TONES.test(s.tone)) notes.push('[keyboard] steps[' + j + '] tone "' + s.tone + '" is not a theme colour (emph is used)');
|
|
157
|
+
var ev = KB.notes(s, gdur);
|
|
158
|
+
return {o: s, keys: ks, ev: ev, len: KB.length(s, gdur), tone: TONES.test(s.tone || "") ? s.tone : "emph"};
|
|
159
|
+
});
|
|
160
|
+
if (outside.length){
|
|
161
|
+
// 範囲の外の鍵:すべての鍵を覆う範囲を示す(範囲そのものではなく、段階の鍵だけから)
|
|
162
|
+
var lo = Infinity, hi = -Infinity;
|
|
163
|
+
steps.forEach(function(s){ s.keys.forEach(function(k){ lo = Math.min(lo, k.midi); hi = Math.max(hi, k.midi); }); });
|
|
164
|
+
var c = KB.cover(lo, hi);
|
|
165
|
+
throw new Error("m-keyboard: " + outside.join(", ") + (outside.length > 1 ? " are" : " is") + " outside range " + d.range + "; " +
|
|
166
|
+
(c ? "use " + c + " (covers every key in steps)" : "the keys in steps span " + (hi - lo) + " semitones, more than 3 octaves: split them over two keyboards"));
|
|
167
|
+
}
|
|
168
|
+
return (this._plan = {r: r, kb: kb, steps: steps, cumulative: d.cumulative === true, notes: notes});
|
|
169
|
+
}
|
|
170
|
+
duration(k){ var s = k >= 1 && this.plan().steps[k - 1]; return s ? s.len : 0; }
|
|
171
|
+
events(k){ var s = k >= 1 && this.plan().steps[k - 1]; return s ? s.ev.map(function(e){ return Object.assign({}, e); }) : []; }
|
|
172
|
+
mount(ctx){
|
|
173
|
+
var self = this, D = base, pl = this.plan(), kb = pl.kb, d = this.data || {};
|
|
174
|
+
this.variant = base.variant(this);
|
|
175
|
+
// 鍵盤の名前:書いていなければ日本語の授業はドレミ、ほかは CDE(段階の数は変わらないので plan() ではなくここで選ぶ)
|
|
176
|
+
var names = d.names === undefined ? (/^ja\b/i.test(ctx.lang) ? "solfa" : "abc") : d.names;
|
|
177
|
+
pl.notes.forEach(function(n){ ctx.report("data", n); });
|
|
178
|
+
this.style.display = "block";
|
|
179
|
+
var W = this.clientWidth, H = this.clientHeight;
|
|
180
|
+
var svg = D.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true", style: "display:block;overflow:visible"}, this);
|
|
181
|
+
// 読み上げ:最後の形で光っている鍵盤(cumulative なら全部の段階の)を 1 文で。作者の aria-label があればそれを使う(上書きしない)
|
|
182
|
+
var ja = /^ja\b/i.test(ctx.lang || "ja");
|
|
183
|
+
if (this._aria === undefined) this._aria = this.getAttribute("aria-label");
|
|
184
|
+
this.setAttribute("role", "img");
|
|
185
|
+
if (!this._aria) this.setAttribute("aria-label", this.summary(ja, names === "none" ? (ja ? "solfa" : "abc") : names));
|
|
186
|
+
var hasLabel = pl.steps.some(function(s){ return s.o.label; });
|
|
187
|
+
// 光る黒鍵の名前の行:となりの名前に重なるなら 2 行目へ(どの段階でも同じ置き場所)
|
|
188
|
+
var LFS = 30, NFS = 26, BFS = 24;
|
|
189
|
+
var unitW = Math.min((W - 6) / kb.n, 96);
|
|
190
|
+
var x0 = (W - unitW * kb.n) / 2;
|
|
191
|
+
var bl = {};
|
|
192
|
+
pl.steps.forEach(function(s){ s.keys.forEach(function(k){ if (!k.white && names !== "none") bl[k.midi + "|" + k.acc] = k; }); });
|
|
193
|
+
var blist = Object.keys(bl).map(function(id){
|
|
194
|
+
var k = bl[id], key = kb.all.filter(function(q){ return q.midi === k.midi; })[0], s = KB.label(k, names);
|
|
195
|
+
return {id: id, k: k, s: s, cx: x0 + key.cx * unitW, w: est(s, BFS, self)};
|
|
196
|
+
}).sort(function(a, b){ return a.cx - b.cx || a.id.localeCompare(b.id); });
|
|
197
|
+
var rowsEnd = [-1e9, -1e9], nRows = 0;
|
|
198
|
+
blist.forEach(function(b){
|
|
199
|
+
var row = b.cx - b.w / 2 > rowsEnd[0] + 10 ? 0 : 1;
|
|
200
|
+
if (row === 1 && b.cx - b.w / 2 <= rowsEnd[1] + 10) ctx.report("size", "[keyboard] the names of black keys near " + b.s + " overlap (use a wider element or a narrower range)");
|
|
201
|
+
rowsEnd[row] = b.cx + b.w / 2; b.row = row; nRows = Math.max(nRows, row + 1);
|
|
202
|
+
});
|
|
203
|
+
// cumulative:段階の名前を、その段階の鍵盤の真ん中の上に残す(前の段階の名前も消さない。色だけで見分けないように)。
|
|
204
|
+
// 名前が重なれば次の行へ(行は 40 px)
|
|
205
|
+
var cumLabs = pl.cumulative && hasLabel ? [] : null, labRows = 1;
|
|
206
|
+
if (cumLabs) pl.steps.forEach(function(s, j){
|
|
207
|
+
if (!s.o.label) return;
|
|
208
|
+
var xs = s.keys.map(function(k){ var key = kb.all.filter(function(q){ return q.midi === k.midi; })[0]; return x0 + (key.white ? key.x + 0.5 : key.cx) * unitW; });
|
|
209
|
+
var w = est(s.o.label, LFS, self), cx = (Math.min.apply(null, xs) + Math.max.apply(null, xs)) / 2;
|
|
210
|
+
cx = Math.max(w / 2 + 2, Math.min(W - w / 2 - 2, cx));
|
|
211
|
+
var row = 0;
|
|
212
|
+
while (cumLabs.some(function(o){ return o.row === row && Math.abs(o.cx - cx) < (o.w + w) / 2 + 16; })) row++;
|
|
213
|
+
cumLabs.push({j: j, s: s.o.label, w: w, cx: cx, row: row, tone: s.tone}); labRows = Math.max(labRows, row + 1);
|
|
214
|
+
});
|
|
215
|
+
var labH = hasLabel ? 46 + (labRows - 1) * 40 : 0, bnH = nRows * 32, namesH = names === "none" ? 0 : 40, gapTop = nRows ? 8 : (hasLabel ? 10 : 0);
|
|
216
|
+
var avail = H - labH - bnH - gapTop - namesH - 8;
|
|
217
|
+
var keyH = Math.min(avail, unitW * 4.4);
|
|
218
|
+
if (avail < 110) ctx.report("size", "[keyboard] the element (" + W + "×" + H + " px) leaves only " + Math.round(avail) + " px for the keys; make it taller (about " + Math.ceil(H + 110 - avail) + " px)");
|
|
219
|
+
if (unitW < 40) ctx.report("size", "[keyboard] white keys are " + unitW.toFixed(0) + " px wide; the names under them may not fit (use a narrower range or a wider element)");
|
|
220
|
+
var used = labH + bnH + gapTop + keyH + namesH, top0 = Math.max(0, (H - used) / 2);
|
|
221
|
+
var labY = top0 + 34, bnTop = top0 + labH, keyTop = bnTop + bnH + gapTop, keyBot = keyTop + keyH, bH = keyH * KB.BH;
|
|
222
|
+
this.geo = {x0: x0, unitW: unitW, keyTop: keyTop, keyH: keyH};
|
|
223
|
+
// 鍵盤:白鍵 → 白鍵の光 → 黒鍵 → 黒鍵の光(黒鍵は短く、上に)
|
|
224
|
+
var gW = D.el("g", {}, svg), gB = D.el("g", {}, svg), gT = D.el("g", {}, svg);
|
|
225
|
+
var K = this.K = {};
|
|
226
|
+
var rise = this.variant === "rise";
|
|
227
|
+
kb.all.forEach(function(key){
|
|
228
|
+
var white = key.white, x = x0 + key.x * unitW, w = key.w * unitW, h = white ? keyH : bH;
|
|
229
|
+
var par = white ? gW : gB, g = D.el("g", {}, par);
|
|
230
|
+
D.el("rect", {x: x.toFixed(1), y: keyTop.toFixed(1), width: w.toFixed(1), height: h.toFixed(1), rx: white ? 4 : 3,
|
|
231
|
+
fill: white ? "var(--bg)" : "var(--fg)", stroke: "var(--fg)", "stroke-width": white ? 2 : 1.5}, g);
|
|
232
|
+
var ins = white ? 3 : 4;
|
|
233
|
+
var lit = D.el("rect", {x: (x + ins).toFixed(1), y: (keyTop + (white ? 2 : 0)).toFixed(1), width: (w - 2 * ins).toFixed(1), height: (h - ins - (white ? 2 : 0)).toFixed(1), rx: 2, opacity: 0}, g);
|
|
234
|
+
var cx = x + w / 2, r = white ? Math.min(9, w * 0.16) : Math.min(7, w * 0.2), cy = keyTop + h - (white ? 14 + r : 10 + r);
|
|
235
|
+
var dot = D.el("circle", {cx: cx.toFixed(1), cy: cy.toFixed(1), r: r.toFixed(1), opacity: 0}, g);
|
|
236
|
+
K[key.midi] = {key: key, g: g, lit: lit, dot: dot, y: keyTop + (white ? 2 : 0), h: h - ins - (white ? 2 : 0), white: white, nm: null, bn: {}};
|
|
237
|
+
});
|
|
238
|
+
// 白鍵の名前(鍵盤の下)
|
|
239
|
+
if (names !== "none") kb.whites.forEach(function(key){
|
|
240
|
+
var s = KB.label({letter: KB.whiteLetter(key.midi), acc: ""}, names);
|
|
241
|
+
var t = D.el("text", {x: (x0 + (key.x + 0.5) * unitW).toFixed(1), y: (keyBot + 32).toFixed(1), "font-size": NFS, "text-anchor": "middle", fill: "var(--fg)"}, gT);
|
|
242
|
+
t.textContent = s;
|
|
243
|
+
K[key.midi].nm = t;
|
|
244
|
+
});
|
|
245
|
+
// 光る黒鍵の名前(鍵盤の上。光っているときだけ)
|
|
246
|
+
blist.forEach(function(b){
|
|
247
|
+
var t = D.el("text", {x: b.cx.toFixed(1), y: (bnTop + 25 + (nRows - 1 - b.row) * 32).toFixed(1), "font-size": BFS, "font-weight": 700, "text-anchor": "middle", fill: "var(--fg)", opacity: 0}, gT);
|
|
248
|
+
t.textContent = b.s;
|
|
249
|
+
K[b.k.midi].bn[b.id] = t;
|
|
250
|
+
});
|
|
251
|
+
// 段階の名前(鍵盤の上、24 px 以上)
|
|
252
|
+
this.lab = D.el("text", {x: (W / 2).toFixed(1), y: labY.toFixed(1), "font-size": LFS, "font-weight": 700, "text-anchor": "middle", fill: "var(--fg)", opacity: 0}, gT);
|
|
253
|
+
// cumulative の段階の名前:字と、その段階の色の下線(鍵盤の光と同じ色)
|
|
254
|
+
this.cumLabs = cumLabs && cumLabs.map(function(o){
|
|
255
|
+
var g = D.el("g", {opacity: 0}, gT), y = labY + o.row * 40;
|
|
256
|
+
var tx = D.el("text", {x: o.cx.toFixed(1), y: y.toFixed(1), "font-size": LFS, "font-weight": 700, "text-anchor": "middle", fill: "var(--fg)"}, g);
|
|
257
|
+
tx.textContent = o.s;
|
|
258
|
+
D.el("rect", {x: (o.cx - o.w / 2).toFixed(1), y: (y + 6).toFixed(1), width: o.w.toFixed(1), height: 4, rx: 2, fill: "var(--" + o.tone + ")"}, g);
|
|
259
|
+
return {j: o.j, g: g, tx: tx};
|
|
260
|
+
});
|
|
261
|
+
pl.steps.forEach(function(s, j){ if (s.o.label && est(s.o.label, LFS, self) > W) ctx.report("size", "[keyboard] steps[" + j + "] label is wider than the element; shorten it"); });
|
|
262
|
+
if (rise) Object.keys(K).forEach(function(m){ K[m].lit.setAttribute("data-y", K[m].y); });
|
|
263
|
+
}
|
|
264
|
+
// 最後の形を 1 文で(読み上げの名前):「鍵盤:ド・ミ・ソ(Ⅰ)」。cumulative なら全部の段階を順に、そうでなければ最後の段階
|
|
265
|
+
summary(ja, names){
|
|
266
|
+
var pl = this.plan(), st = pl.cumulative ? pl.steps : pl.steps.slice(-1);
|
|
267
|
+
return (ja ? "鍵盤:" : "Keyboard: ") + st.map(function(s){
|
|
268
|
+
return s.keys.map(function(k){ return KB.label(k, names); }).join(ja ? "・" : ", ") + (s.o.label ? (ja ? "(" + s.o.label + ")" : " (" + s.o.label + ")") : "");
|
|
269
|
+
}).join(ja ? "、" : "; ");
|
|
270
|
+
}
|
|
271
|
+
render(k, t, ctx){
|
|
272
|
+
var pl = this.plan(), K = this.K, v = this.variant, cur = pl.steps[k - 1];
|
|
273
|
+
// 薄さはテーマの --preview-opacity(仕様 §8.4「Answers under preview」)。書いていないテーマだけ 0.45
|
|
274
|
+
var preview = !!ctx.preview, FAINT = preview ? base.previewOpacity(this) : 0.45;
|
|
275
|
+
var st = KB.state(pl.steps, k, t, {cumulative: pl.cumulative, preview: preview, variant: v, prog: base.prog});
|
|
276
|
+
Object.keys(K).forEach(function(m){
|
|
277
|
+
var o = K[m], s = st[m] || {e: 0, faded: false, pre: false, ids: {}, fids: {}}, on = s.e > 0;
|
|
278
|
+
var tone = on ? s.tone : s.faded ? s.fadeTone : s.preTone;
|
|
279
|
+
o.lit.setAttribute("fill", "var(--" + (tone || "emph") + ")");
|
|
280
|
+
var op = on ? (s.faded ? Math.max(0.4, v === "light" ? s.e : 1) : v === "light" ? s.e : 1) : s.faded ? 0.4 : s.pre ? FAINT : 0;
|
|
281
|
+
// rise:光が鍵盤の下から上へ満ちる
|
|
282
|
+
if (v === "rise"){
|
|
283
|
+
var f = on ? (s.faded ? 1 : s.e) : 1, h = o.h * f;
|
|
284
|
+
o.lit.setAttribute("y", (o.y + o.h - h).toFixed(1)); o.lit.setAttribute("height", h.toFixed(1));
|
|
285
|
+
}
|
|
286
|
+
o.lit.setAttribute("opacity", op.toFixed(3));
|
|
287
|
+
// 印:今の段階は塗った丸、前の段階は輪(色だけに頼らない)
|
|
288
|
+
o.dot.setAttribute("fill", on ? "var(--bg)" : "none");
|
|
289
|
+
o.dot.setAttribute("stroke", on ? "none" : o.white ? "var(--fg)" : "var(--bg)");
|
|
290
|
+
o.dot.setAttribute("stroke-width", on ? 0 : 2.5);
|
|
291
|
+
o.dot.setAttribute("opacity", (on ? (v === "press" ? 1 : s.e) : s.faded ? 0.9 : 0).toFixed(3));
|
|
292
|
+
// press:少し沈んで戻る(4 px、0.3 秒まで)
|
|
293
|
+
var dy = v === "press" && on && s.p < 1 ? 4 * Math.sin(Math.PI * s.p) : 0;
|
|
294
|
+
o.g.setAttribute("transform", dy ? "translate(0," + dy.toFixed(2) + ")" : "");
|
|
295
|
+
for (var id in o.bn){
|
|
296
|
+
var a = s.ids[id] ? (v === "light" ? s.ids[id] : 1) : 0;
|
|
297
|
+
if (!a && s.fids[id]) a = 0.75;
|
|
298
|
+
o.bn[id].setAttribute("opacity", a.toFixed(3));
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
var lab = cur && cur.o.label, la = lab ? (t === Infinity ? 1 : base.prog(t, 0, Math.min(0.3, cur.len || 0.3))) : 0;
|
|
302
|
+
if (this.cumLabs){
|
|
303
|
+
// cumulative:前の段階の名前はそのまま、今の段階の名前は太字でフェードして現れる
|
|
304
|
+
this.lab.textContent = ""; this.lab.setAttribute("opacity", 0);
|
|
305
|
+
this.cumLabs.forEach(function(o){
|
|
306
|
+
o.g.setAttribute("opacity", (o.j < k - 1 ? 1 : o.j === k - 1 ? la : 0).toFixed(3));
|
|
307
|
+
o.tx.setAttribute("font-weight", o.j === k - 1 ? 700 : 400);
|
|
308
|
+
});
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
this.lab.textContent = lab || "";
|
|
312
|
+
this.lab.setAttribute("opacity", lab ? la.toFixed(3) : 0);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Sample; stack statistics
|
|
3
|
+
steps: Population → sample → dot → repeat (4)
|
|
4
|
+
props:
|
|
5
|
+
dur: Seconds
|
|
6
|
+
stagger: Delay
|
|
7
|
+
fast: Repeat seconds
|
|
8
|
+
size: Text px
|
|
9
|
+
example: {"values":[1,2,3,4,5,6],"n":2,"replace":true,"repeat":40,"seed":3}
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Child JSON:
|
|
13
|
+
- `balls`: colour counts, total 2–30; proportion of `of`.
|
|
14
|
+
- `values`: 2–30 numbers; mean. One form.
|
|
15
|
+
- `n`: 1–population size; 1–30 with replacement. Clamp/report.
|
|
16
|
+
- `replace`: false; `repeat`: 1–200 (40); `seed`: integer (1).
|
|
17
|
+
- `stat`: proportion/mean; `digits`: 0–10 (2).
|
|
18
|
+
|
|
19
|
+
<!-- llm: omit -->
|
|
20
|
+
Copies with replacement; gaps without. Restore after recording. Seeded simulation, no answer preview. Final marker: population value. Caption: n and repetitions. Image label includes mean of sample statistics; author's label retained.
|
|
21
|
+
|
|
22
|
+
1184×440px suggested. One sample row; large n needs width. Text ≥24px; line spacing ≥1.45em. Dots shrink to radius 5px, then report size. Integer ticks for small integer ranges, otherwise 1–2–5 steps; proportions use quarters. Composition: red 3, blue 2; omit for cards. Incompatible stat: report, use form default. Missing of: first colour; absent: report, zero. Invalid population: report, no draw. Fictional simulations.
|
|
23
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// m-sampling: population → sample → simulated sampling distribution.
|
|
2
|
+
egatariParts.define({name: "sampling", version: "2.0.1", requires: {base: "1.2.0", plot: "1.1.2"}, element: "m-sampling"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base;
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {population: "母集団", sample: "標本", distribution: "標本分布", with: "戻す(復元抽出)", without: "戻さない(非復元抽出)", mean: "平均", proportion: "の割合", meanAxis: "標本平均", proportionAxis: "標本比率", statsMean: "標本統計量の平均", caption: function(n, r){ return "n = " + n + "、" + r + " 回"; }},
|
|
7
|
+
en: {population: "population", sample: "Sample", distribution: "Sampling distribution", with: "with replacement", without: "without replacement", mean: "Mean", proportion: " proportion", meanAxis: "Sample mean", proportionAxis: "Sample proportion", statsMean: "mean of sample statistics", caption: function(n, r){ return "n = " + n + ", " + r + " samples"; }}
|
|
8
|
+
};
|
|
9
|
+
var OBJ = {white: "#ffffff", black: "#222222", red: "#d62728", green: "#2ca02c", yellow: "#f2c12e", blue: "#1f77b4", brown: "#8c564b", purple: "#9467bd", pink: "#e377c2", orange: "#ff7f0e", gray: "#7f7f7f"};
|
|
10
|
+
var OBJ_NAME = {"白": "white", "黒": "black", "赤": "red", "緑": "green", "黄": "yellow", "青": "blue", "茶": "brown", "紫": "purple", "桃": "pink", "橙": "orange", "灰": "gray",
|
|
11
|
+
"白色": "white", "黒色": "black", "赤色": "red", "緑色": "green", "黄色": "yellow", "青色": "blue", "茶色": "brown", "紫色": "purple", "桃色": "pink", "橙色": "orange", "灰色": "gray", "ピンク": "pink", "オレンジ": "orange", "グレー": "gray", "grey": "gray"};
|
|
12
|
+
function objColor(name){
|
|
13
|
+
var k = String(name).trim(), lower = k.toLowerCase(), own = Object.prototype.hasOwnProperty;
|
|
14
|
+
var en = own.call(OBJ, lower) ? lower : own.call(OBJ_NAME, k) ? OBJ_NAME[k] : own.call(OBJ_NAME, lower) ? OBJ_NAME[lower] : null;
|
|
15
|
+
return en ? "var(--obj-" + en + ", " + OBJ[en] + ")" : null;
|
|
16
|
+
}
|
|
17
|
+
// Mulberry32; the only mutable random state lives in mount's local generator.
|
|
18
|
+
function random(seed){
|
|
19
|
+
return function(){
|
|
20
|
+
seed = (seed + 0x6D2B79F5) | 0;
|
|
21
|
+
var z = Math.imul(seed ^ seed >>> 15, 1 | seed);
|
|
22
|
+
z ^= z + Math.imul(z ^ z >>> 7, 61 | z);
|
|
23
|
+
return ((z ^ z >>> 14) >>> 0) / 4294967296;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function set(e, a, v){ e.setAttribute(a, String(v)); }
|
|
27
|
+
function mean(values){
|
|
28
|
+
var scale = Math.max.apply(null, values.map(function(v){ return Math.abs(v); }).concat([1]));
|
|
29
|
+
var average = values.reduce(function(s, v){ return s + v / scale; }, 0) / values.length;
|
|
30
|
+
return Math.max(-1, Math.min(1, average)) * scale;
|
|
31
|
+
}
|
|
32
|
+
function meanTicks(lo, hi, values){
|
|
33
|
+
if (lo === hi) return [lo];
|
|
34
|
+
if (hi - lo <= 10 && values.every(Number.isInteger)) return Array.from({length: hi - lo + 1}, function(_, i){ return lo + i; });
|
|
35
|
+
var scale = Math.max(1, Math.abs(lo), Math.abs(hi));
|
|
36
|
+
var rough = Math.max(Number.MIN_VALUE, (hi / scale - lo / scale) / 4 * scale);
|
|
37
|
+
var power = Math.pow(10, Math.floor(Math.log10(rough))) || Number.MIN_VALUE, fraction = rough / power;
|
|
38
|
+
var step = (fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10) * power;
|
|
39
|
+
var start = Math.ceil(lo / step), ticks = [];
|
|
40
|
+
for (var i = 0; i < 10; i++){
|
|
41
|
+
var v = (start + i) * step;
|
|
42
|
+
if (!Number.isFinite(v) || v > hi + step * 1e-10) break;
|
|
43
|
+
ticks.push(v);
|
|
44
|
+
}
|
|
45
|
+
return ticks;
|
|
46
|
+
}
|
|
47
|
+
return class extends base.MotionElement {
|
|
48
|
+
static partName = "sampling";
|
|
49
|
+
static preview = "none";
|
|
50
|
+
static steps = 4;
|
|
51
|
+
static shape = {
|
|
52
|
+
schema: {type: "object", additionalProperties: false, required: ["n"], anyOf: [{required: ["balls"]}, {required: ["values"]}], properties: {
|
|
53
|
+
balls: {type: "object"}, values: {type: "array", minItems: 2, maxItems: 30, items: {type: "number"}},
|
|
54
|
+
of: {type: "string"}, n: {type: "number"}, replace: {type: "boolean"}, repeat: {type: "number"}, seed: {type: "number"},
|
|
55
|
+
stat: {enum: ["mean", "proportion"]}, digits: {type: "number"}
|
|
56
|
+
}},
|
|
57
|
+
example: {values: [1, 2, 3, 4, 5, 6], n: 2, replace: true, repeat: 40, seed: 3}
|
|
58
|
+
};
|
|
59
|
+
static props = {
|
|
60
|
+
dur: {type: "number", default: 1, doc: "ステップの秒数"},
|
|
61
|
+
stagger: {type: "number", default: 0.25, doc: "抽出のずれ(秒)"},
|
|
62
|
+
fast: {type: "number", default: 2.5, doc: "反復の秒数"},
|
|
63
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は 26)"}
|
|
64
|
+
};
|
|
65
|
+
timing(name, fallback, zero){ var v = this.props[name]; return Number.isFinite(v) && (zero ? v >= 0 : v > 0) ? v : fallback; }
|
|
66
|
+
duration(k){
|
|
67
|
+
if (k < 1 || k > 4) return 0;
|
|
68
|
+
if (k === 4) return this.timing("fast", 2.5);
|
|
69
|
+
var n = this.model ? this.model.n : Math.max(1, Math.min(30, Math.round(this.data.n)));
|
|
70
|
+
return this.timing("dur", 1) + (k === 2 ? (n - 1) * this.timing("stagger", 0.25, true) : 0);
|
|
71
|
+
}
|
|
72
|
+
fmt(v){ return v.toFixed(this.model.digits); }
|
|
73
|
+
label(g, x, y, s, anchor){
|
|
74
|
+
var t = base.el("text", {x: x, y: y, "font-size": this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, g);
|
|
75
|
+
t.textContent = s; return t;
|
|
76
|
+
}
|
|
77
|
+
mount(ctx){
|
|
78
|
+
var self = this, d = this.data, P = this.props;
|
|
79
|
+
this.T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
80
|
+
var T = this.T;
|
|
81
|
+
[["dur", 1, false], ["stagger", 0.25, true], ["fast", 2.5, false]].forEach(function(a){
|
|
82
|
+
if (self.timing(a[0], -1, a[2]) === -1){ ctx.report("attr", "[sampling] invalid " + a[0] + "; using " + a[1]); P[a[0]] = a[1]; }
|
|
83
|
+
});
|
|
84
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[sampling] size must be 0 or at least 24; using 26"); P.size = 0; }
|
|
85
|
+
this.fs = P.size || 26;
|
|
86
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
|
|
87
|
+
this.setAttribute("role", "img");
|
|
88
|
+
var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight);
|
|
89
|
+
var items = [], composition = [], balls = d.balls !== undefined;
|
|
90
|
+
var invalid = balls && d.values !== undefined;
|
|
91
|
+
if (balls){
|
|
92
|
+
Object.keys(d.balls).forEach(function(name){
|
|
93
|
+
var count = d.balls[name], col = objColor(name);
|
|
94
|
+
if (!col) ctx.report("data", "[sampling] unknown colour " + name + "; drawn as gray with its name");
|
|
95
|
+
if (!Number.isInteger(count) || count < 0 || count > 30){ invalid = true; return; }
|
|
96
|
+
composition.push(name + " " + count);
|
|
97
|
+
for (var i = 0; i < count; i++) items.push({label: name, color: col || objColor("gray")});
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
items = d.values.map(function(v){ return {label: String(v), value: v}; });
|
|
101
|
+
composition = items.map(function(o){ return o.label; });
|
|
102
|
+
}
|
|
103
|
+
if (invalid || items.length < 2 || items.length > 30){
|
|
104
|
+
ctx.report("data", "[sampling] supply exactly one population: 2–30 values or balls with non-negative integer counts totalling 2–30");
|
|
105
|
+
this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true"}, this);
|
|
106
|
+
this.label(this.svg, 8, this.fs + 4, T.population + ": 2–30");
|
|
107
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", T.population + ": 2–30");
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
function integer(key, fallback, lo, hi){
|
|
111
|
+
var v = d[key] === undefined ? fallback : d[key];
|
|
112
|
+
if (!Number.isInteger(v) || v < lo || v > hi){
|
|
113
|
+
v = Math.max(lo, Math.min(hi, Math.round(v)));
|
|
114
|
+
ctx.report("data", "[sampling] " + key + " must be an integer " + lo + "–" + hi + "; clamped to " + v);
|
|
115
|
+
}
|
|
116
|
+
return v;
|
|
117
|
+
}
|
|
118
|
+
var stat = balls ? "proportion" : "mean", of = d.of === undefined ? (balls ? Object.keys(d.balls)[0] : "") : d.of;
|
|
119
|
+
if (d.stat !== undefined && d.stat !== stat) ctx.report("data", "[sampling] stat=" + d.stat + " is incompatible with this population; using " + stat);
|
|
120
|
+
if (balls && !Object.prototype.hasOwnProperty.call(d.balls, of)) ctx.report("data", "[sampling] of=" + of + " is absent from the population; proportion is zero");
|
|
121
|
+
var n = integer("n", 1, 1, d.replace ? 30 : items.length), repeat = integer("repeat", 40, 1, 200), digits = integer("digits", 2, 0, 10);
|
|
122
|
+
var seed = d.seed === undefined ? 1 : d.seed;
|
|
123
|
+
if (!Number.isSafeInteger(seed)){ ctx.report("data", "[sampling] seed must be a safe integer; using 1"); seed = 1; }
|
|
124
|
+
function statistic(sample){
|
|
125
|
+
return balls ? sample.filter(function(id){ return items[id].label === of; }).length / sample.length : mean(sample.map(function(id){ return items[id].value; }));
|
|
126
|
+
}
|
|
127
|
+
var rng = random(seed), samples = [];
|
|
128
|
+
for (var r = 0; r < repeat; r++){
|
|
129
|
+
var pool = items.map(function(_, i){ return i; }), ids = [];
|
|
130
|
+
for (var j = 0; j < n; j++){
|
|
131
|
+
var at = Math.floor(rng() * pool.length); ids.push(pool[at]);
|
|
132
|
+
if (!d.replace) pool.splice(at, 1);
|
|
133
|
+
}
|
|
134
|
+
samples.push({ids: ids, value: statistic(ids)});
|
|
135
|
+
}
|
|
136
|
+
this.model = {items: items, samples: samples, n: n, repeat: repeat, digits: digits, replace: !!d.replace, stat: stat, of: of,
|
|
137
|
+
population: statistic(items.map(function(_, i){ return i; })), average: mean(samples.map(function(o){ return o.value; }))};
|
|
138
|
+
var m = this.model, rule = m.replace ? T.with : T.without;
|
|
139
|
+
this.composition = composition.join(T === TEXT.ja ? "・" : ", ");
|
|
140
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", T.population + ": " + this.composition + "; " + rule + "; " + T.caption(n, repeat) + "; " + T.population + " " + this.fmt(m.population) + "; " + T.statsMean + " " + this.fmt(m.average));
|
|
141
|
+
// Source Sans 3's SVG box is 1.43em tall, including its descender.
|
|
142
|
+
var fs = this.fs, line = fs * 1.45 + 4, width = function(s){ return base.textWidth(s, fs, {font: self}) + 8; };
|
|
143
|
+
var left = W * 0.24, middle = W * 0.38, sx = left + 22, px = left + middle + 48, pw = Math.max(1, W - px - 22);
|
|
144
|
+
// Plot supplies the numerical mapping and axis; normalized values avoid overflow.
|
|
145
|
+
var vals = items.map(function(o){ return o.value; }), lo = balls ? 0 : Math.min.apply(null, vals), hi = balls ? 1 : Math.max.apply(null, vals);
|
|
146
|
+
var scale = Math.max(1, Math.abs(lo), Math.abs(hi)), a = lo / scale, b = hi / scale;
|
|
147
|
+
var captionY = H - fs * 0.4 - 4, axisY = captionY - 3 * line;
|
|
148
|
+
var plotTop = fs + 4 + fs * 0.4 + 8, plotH = axisY - plotTop;
|
|
149
|
+
var plot = deps.plot.Plot(this, {xr: a === b ? [a - 1, b + 1] : [a, b], yr: [0, 1], xt: [], yt: [], noY: true,
|
|
150
|
+
m: {l: px + 12, r: W - px - 12 - Math.max(1, pw - 24), t: plotTop, b: H - axisY}});
|
|
151
|
+
this.svg = plot.svg;
|
|
152
|
+
set(this.svg, "width", W); set(this.svg, "height", H); set(this.svg, "aria-hidden", "true");
|
|
153
|
+
var axes = [].filter.call(this.svg.children, function(e){ return e.getAttribute("class") === "axes"; })[0];
|
|
154
|
+
this.svg.removeChild(axes);
|
|
155
|
+
var X = function(v){ return plot.x(v / scale); };
|
|
156
|
+
this.populationLayer = base.el("g", {opacity: 0}, this.svg);
|
|
157
|
+
this.sampleLayer = base.el("g", {opacity: 0}, this.svg);
|
|
158
|
+
this.plotLayer = base.el("g", {opacity: 0}, this.svg);
|
|
159
|
+
this.plotLayer.appendChild(axes);
|
|
160
|
+
this.finalLayer = base.el("g", {opacity: 0}, this.plotLayer);
|
|
161
|
+
this.label(this.populationLayer, 8, fs + 4, T.population);
|
|
162
|
+
this.label(this.sampleLayer, sx, fs + 4, T.sample);
|
|
163
|
+
this.label(this.plotLayer, px, fs + 4, T.distribution);
|
|
164
|
+
var small = false, itemW = Math.max(52, ...items.map(function(o){ return width(o.label) + (balls ? 36 : 16); }));
|
|
165
|
+
var cols = Math.max(1, Math.min(items.length, Math.floor((left - 24) / itemW))), rows = Math.ceil(items.length / cols), rowH = Math.max(48, line);
|
|
166
|
+
var bagTop = line + 10, bagBottom = bagTop + rows * rowH + 12;
|
|
167
|
+
base.el("rect", {x: 4, y: bagTop, width: Math.max(1, left - 8), height: bagBottom - bagTop, rx: 18, fill: "none", stroke: "var(--fg)", "stroke-width": 2}, this.populationLayer);
|
|
168
|
+
function item(g, o){
|
|
169
|
+
var shape = base.el(balls ? "circle" : "rect", balls ? {cx: 14, cy: 0, r: 12, fill: o.color, stroke: "var(--fg)", "stroke-width": 2} : {x: 0, y: -rowH / 2 + 3, width: itemW - 8, height: rowH - 6, rx: 6, fill: "var(--bg)", stroke: "var(--fg)", "stroke-width": 2}, g);
|
|
170
|
+
var text = self.label(g, balls ? 34 : (itemW - 8) / 2, fs * 0.312, o.label, balls ? "start" : "middle");
|
|
171
|
+
return {g: g, shape: shape, text: text};
|
|
172
|
+
}
|
|
173
|
+
this.originals = items.map(function(o, i){
|
|
174
|
+
var x = 14 + (i % cols) * itemW, y = bagTop + 8 + rowH / 2 + Math.floor(i / cols) * rowH;
|
|
175
|
+
var q = item(base.el("g", {transform: "translate(" + x + " " + y + ")"}, self.populationLayer), o); q.x = x; q.y = y; return q;
|
|
176
|
+
});
|
|
177
|
+
// Wrap word groups below the bag; reserve a full font box between baselines.
|
|
178
|
+
var y = bagBottom;
|
|
179
|
+
if (balls){
|
|
180
|
+
// Keep separators inside the string: width padding must not add spaces around ・.
|
|
181
|
+
var words = "";
|
|
182
|
+
composition.forEach(function(s){
|
|
183
|
+
var joined = words ? words + (T === TEXT.ja ? "・" : ", ") + s : s;
|
|
184
|
+
if (words && width(joined) > left - 16){ y += line; self.label(self.populationLayer, 8, y, words); words = s; }
|
|
185
|
+
else words = joined;
|
|
186
|
+
if (width(s) > left - 16) small = true;
|
|
187
|
+
});
|
|
188
|
+
y += line; self.label(self.populationLayer, 8, y, words);
|
|
189
|
+
}
|
|
190
|
+
var rules = width(rule) <= left - 16 ? [rule] : (T === TEXT.ja ? [m.replace ? "戻す" : "戻さない", m.replace ? "(復元抽出)" : "(非復元抽出)"] : rule.split(" "));
|
|
191
|
+
rules.forEach(function(s){ y += line; self.label(self.populationLayer, 8, y, s); if (width(s) > left - 16) small = true; });
|
|
192
|
+
if (y + fs * 0.4 + 4 > H || itemW > left - 24) small = true;
|
|
193
|
+
var sampleY = bagTop + rowH / 2 + 8, sampleW = n * itemW;
|
|
194
|
+
if (sampleW > middle - 10) small = true;
|
|
195
|
+
this.drawn = Array.from({length: n}, function(_, j){ var q = item(base.el("g", {}, self.sampleLayer), items[samples[0].ids[j]]); q.x = sx + j * itemW; q.y = sampleY; return q; });
|
|
196
|
+
this.statLayer = base.el("g", {opacity: 0}, this.sampleLayer);
|
|
197
|
+
var statY = sampleY + rowH / 2 + line;
|
|
198
|
+
this.statName = this.label(this.statLayer, sx, statY, balls ? of + T.proportion : T.mean);
|
|
199
|
+
this.statValue = this.label(this.statLayer, sx, statY + line, "");
|
|
200
|
+
var statLabel = balls ? of + T.proportion : T.mean;
|
|
201
|
+
if (width(statLabel) > middle - 10 || statY + line + fs * 0.4 > H) small = true;
|
|
202
|
+
var ticks = balls ? [0, 0.25, 0.5, 0.75, 1] : meanTicks(lo, hi, vals), lastRight = -Infinity;
|
|
203
|
+
ticks.forEach(function(v){
|
|
204
|
+
var xx = X(v), rounded = +v.toPrecision(4), s = Number.isFinite(rounded) ? String(rounded) : String(v), w = width(s);
|
|
205
|
+
base.el("line", {x1: xx, x2: xx, y1: axisY, y2: axisY + 7, stroke: "var(--fg)", "stroke-width": 2}, self.plotLayer);
|
|
206
|
+
var anchor = v === lo ? "start" : v === hi ? "end" : "middle";
|
|
207
|
+
var tx = anchor === "start" ? xx : anchor === "end" ? xx - w : xx - w / 2;
|
|
208
|
+
if (tx < lastRight + 4) small = true; lastRight = tx + w;
|
|
209
|
+
self.label(self.plotLayer, xx, axisY + line, s, anchor);
|
|
210
|
+
});
|
|
211
|
+
this.label(this.plotLayer, px, axisY + 2 * line, balls ? T.proportionAxis : T.meanAxis);
|
|
212
|
+
this.label(this.finalLayer, px, captionY, T.caption(n, repeat));
|
|
213
|
+
var markerLabel = T.population + " " + this.fmt(m.population);
|
|
214
|
+
var markerY = fs + 4 + line, markerRight = px + width(markerLabel);
|
|
215
|
+
this.label(this.finalLayer, px, markerY, markerLabel);
|
|
216
|
+
var markerBottom = markerY + fs * 0.4 + 8;
|
|
217
|
+
base.el("line", {x1: X(m.population), x2: X(m.population), y1: markerBottom, y2: axisY, stroke: "var(--emph)", "stroke-width": 2, "stroke-dasharray": "6 5"}, this.finalLayer);
|
|
218
|
+
// Stack horizontally intersecting dots at separate levels, preserving exact numerical x.
|
|
219
|
+
function stack(radius){
|
|
220
|
+
var levels = [];
|
|
221
|
+
return samples.map(function(o){
|
|
222
|
+
var xx = X(o.value), lv = 0;
|
|
223
|
+
while (levels[lv] && levels[lv].some(function(v){ return Math.abs(v - xx) < 2 * radius + 2; })) lv++;
|
|
224
|
+
if (!levels[lv]) levels[lv] = []; levels[lv].push(xx);
|
|
225
|
+
return {x: xx, level: lv};
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
// The marker label occupies only the plot's upper-left corner, not a whole row.
|
|
229
|
+
// A 1px gap between circle fills leaves more room for repeated values.
|
|
230
|
+
function fits(positions, radius){
|
|
231
|
+
return positions.every(function(o){
|
|
232
|
+
var top = axisY - 2 * radius - 3 - o.level * (2 * radius + 1);
|
|
233
|
+
return top >= (o.x - radius < markerRight && o.x + radius > px ? markerBottom : plotTop);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
var radius = 8, positions;
|
|
237
|
+
while (true){
|
|
238
|
+
positions = stack(radius);
|
|
239
|
+
if (fits(positions, radius) || radius === 5) break;
|
|
240
|
+
radius--;
|
|
241
|
+
}
|
|
242
|
+
if (!fits(positions, radius)) ctx.report("size", "[sampling] dot stack exceeds plot at minimum 5px radius; increase height or reduce repeat");
|
|
243
|
+
this.dots = positions.map(function(o){
|
|
244
|
+
var yy = axisY - radius - 3 - o.level * (2 * radius + 1);
|
|
245
|
+
return {x: o.x, y: yy, el: base.el("circle", {cx: o.x, cy: yy, r: radius, fill: "var(--term)", stroke: "var(--fg)", "stroke-width": 1, opacity: 0}, self.plotLayer)};
|
|
246
|
+
});
|
|
247
|
+
this.plotTop = plotTop;
|
|
248
|
+
if ([T.distribution, markerLabel, T.caption(n, repeat), balls ? T.proportionAxis : T.meanAxis].some(function(s){ return width(s) > pw; }) || plotH < 40) small = true;
|
|
249
|
+
if (small) ctx.report("size", "[sampling] population, sample row or plot labels do not fit; enlarge the element or shorten names");
|
|
250
|
+
// Check all statistic strings, since a later sample can be wider than the first.
|
|
251
|
+
if (samples.some(function(o){ return width(self.statText(o)) > middle - 10; })) ctx.report("size", "[sampling] statistic does not fit; enlarge width or reduce digits");
|
|
252
|
+
}
|
|
253
|
+
statText(sample){
|
|
254
|
+
var m = this.model;
|
|
255
|
+
return (m.stat === "proportion" ? sample.ids.filter(function(i){ return m.items[i].label === m.of; }).length + "/" + m.n + " = " : "") + this.fmt(sample.value);
|
|
256
|
+
}
|
|
257
|
+
render(k, t){
|
|
258
|
+
if (!this.model) return;
|
|
259
|
+
var self = this, m = this.model, P = this.props;
|
|
260
|
+
var phase = function(step){ return k > step ? 1 : k < step ? 0 : base.prog(t, 0, P.dur); };
|
|
261
|
+
set(this.populationLayer, "opacity", phase(1));
|
|
262
|
+
set(this.sampleLayer, "opacity", k >= 2 ? 1 : 0);
|
|
263
|
+
set(this.statLayer, "opacity", phase(3));
|
|
264
|
+
set(this.plotLayer, "opacity", phase(3));
|
|
265
|
+
var progress = k > 4 ? 1 : k < 4 ? 0 : Math.max(0, Math.min(1, t / P.fast));
|
|
266
|
+
var completed = k < 3 ? 0 : k === 3 ? (t >= P.dur ? 1 : 0) : 1 + Math.floor(progress * (m.repeat - 1));
|
|
267
|
+
var rep = k >= 4 ? Math.min(m.repeat - 1, completed - 1) : 0, sample = m.samples[rep];
|
|
268
|
+
var full = k >= 4 && progress === 1 || k === 3 && t >= P.dur;
|
|
269
|
+
this.originals.forEach(function(o, i){
|
|
270
|
+
var j = sample.ids.indexOf(i), gone = !m.replace && j >= 0 && !full && (k >= 3 || k === 2 && t > j * P.stagger);
|
|
271
|
+
set(o.g, "opacity", gone ? 0 : 1);
|
|
272
|
+
});
|
|
273
|
+
this.drawn.forEach(function(o, j){
|
|
274
|
+
var id = sample.ids[j], original = self.originals[id], e = k > 2 ? 1 : k < 2 ? 0 : base.prog(t, j * P.stagger, P.dur);
|
|
275
|
+
set(o.g, "opacity", k >= 2 && (k > 2 || t > j * P.stagger) ? 1 : 0);
|
|
276
|
+
set(o.g, "transform", "translate(" + base.lerp(original.x, o.x, e) + " " + base.lerp(original.y, o.y, e) + ")");
|
|
277
|
+
o.text.textContent = m.items[id].label;
|
|
278
|
+
if (m.stat === "proportion") set(o.shape, "style", "fill:" + m.items[id].color + ";stroke:var(--fg)");
|
|
279
|
+
});
|
|
280
|
+
this.statValue.textContent = this.statText(sample);
|
|
281
|
+
this.dots.forEach(function(o, i){
|
|
282
|
+
var drop = i === 0 && k === 3 ? base.prog(t, 0, P.dur) : 1;
|
|
283
|
+
set(o.el, "opacity", i < completed || i === 0 && k === 3 && t > 0 ? 1 : 0);
|
|
284
|
+
set(o.el, "cy", base.lerp(self.plotTop, o.y, drop));
|
|
285
|
+
});
|
|
286
|
+
set(this.finalLayer, "opacity", k >= 4 && progress === 1 ? 1 : 0);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
});
|