@heyramzi/ui 2.9.0 → 2.10.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.
Files changed (313) hide show
  1. package/dist/agents/helpers.js +52 -0
  2. package/dist/agents/index.js +3 -0
  3. package/dist/agents/page-state.js +65 -0
  4. package/dist/agents/types.js +1 -0
  5. package/dist/agents/vendor/color.js +149 -0
  6. package/dist/agents/vendor/draw.js +548 -0
  7. package/dist/agents/vendor/engine.js +632 -0
  8. package/dist/agents/vendor/mesh.js +727 -0
  9. package/dist/agents/vendor/plastic.js +3273 -0
  10. package/dist/agents/vendor/presets.js +171 -0
  11. package/dist/agents/vendor/shapes.js +24 -0
  12. package/dist/agents/vendor/ticker.js +56 -0
  13. package/dist/agents/vendor/types.js +0 -0
  14. package/dist/agents/vendor/wear.js +1525 -0
  15. package/dist/ai/ai-button/index.js +2 -0
  16. package/dist/ai/confirmation/confirmation-context.svelte.js +12 -0
  17. package/dist/ai/confirmation/index.js +9 -0
  18. package/dist/ai/generation-progress/index.js +2 -0
  19. package/dist/ai/index.js +11 -0
  20. package/dist/ai/loading-state/index.js +3 -0
  21. package/dist/ai/prompt-input/index.js +6 -0
  22. package/dist/ai/reasoning/index.js +4 -0
  23. package/dist/ai/reasoning/reasoning-context.svelte.js +12 -0
  24. package/dist/ai/response/index.js +3 -0
  25. package/dist/ai/suggestion/index.js +3 -0
  26. package/dist/ai/thinking-dots/index.js +3 -0
  27. package/dist/ai/typing-dots/index.js +2 -0
  28. package/dist/ai/voice-waveform/index.js +3 -0
  29. package/dist/animations/text-swap.js +30 -0
  30. package/dist/brand/index.js +65 -0
  31. package/dist/canvas/card/resize.js +10 -0
  32. package/dist/canvas/edges/edge-path.js +284 -0
  33. package/dist/canvas/editor/context-menu.js +42 -0
  34. package/dist/canvas/editor/dispatch.svelte.js +157 -0
  35. package/dist/canvas/editor/editor-context.js +8 -0
  36. package/dist/canvas/editor/editor-mode.js +0 -0
  37. package/dist/canvas/editor/frame.js +87 -0
  38. package/dist/canvas/editor/hand-context.js +8 -0
  39. package/dist/canvas/editor/history-core.js +221 -0
  40. package/dist/canvas/editor/keymap.js +193 -0
  41. package/dist/canvas/editor/layout-actions.js +170 -0
  42. package/dist/canvas/editor/lock.js +65 -0
  43. package/dist/canvas/editor/rebase.js +279 -0
  44. package/dist/canvas/editor/style.js +174 -0
  45. package/dist/canvas/editor/text-field-context.js +8 -0
  46. package/dist/canvas/editor/z-order.js +82 -0
  47. package/dist/canvas/headless/alignment.js +20 -0
  48. package/dist/canvas/headless/break-apart-find.js +324 -0
  49. package/dist/canvas/headless/break-apart-pieces.js +74 -0
  50. package/dist/canvas/headless/break-apart-plan.js +378 -0
  51. package/dist/canvas/headless/break-apart-style.js +93 -0
  52. package/dist/canvas/headless/break-apart.js +206 -0
  53. package/dist/canvas/headless/canvas-cleanup.js +70 -0
  54. package/dist/canvas/headless/edge-anchors.js +134 -0
  55. package/dist/canvas/headless/edge-memo.js +28 -0
  56. package/dist/canvas/headless/edge-serialization.js +31 -0
  57. package/dist/canvas/headless/figure-geometry.js +331 -0
  58. package/dist/canvas/headless/fit-viewport.js +82 -0
  59. package/dist/canvas/headless/grid.js +96 -0
  60. package/dist/canvas/headless/icon-node.js +45 -0
  61. package/dist/canvas/headless/index.js +1 -0
  62. package/dist/canvas/headless/linkify.js +25 -0
  63. package/dist/canvas/headless/pen-marks.js +115 -0
  64. package/dist/canvas/headless/sketch.js +250 -0
  65. package/dist/canvas/headless/snap-guides.js +100 -0
  66. package/dist/canvas/headless/svg-figure.js +137 -0
  67. package/dist/canvas/headless/table-layout.js +161 -0
  68. package/dist/canvas/index.js +25 -0
  69. package/dist/canvas/lod/lod-mode.svelte.js +22 -0
  70. package/dist/canvas/lod/render-all.svelte.js +45 -0
  71. package/dist/canvas/ops/arrange.js +83 -0
  72. package/dist/canvas/ops/ops.js +318 -0
  73. package/dist/canvas/pen/colors.js +41 -0
  74. package/dist/canvas/pen/eraser.js +35 -0
  75. package/dist/canvas/pen/geometry.js +143 -0
  76. package/dist/canvas/pen/index.js +9 -0
  77. package/dist/canvas/pen/ops.js +15 -0
  78. package/dist/canvas/pen/pen-session.js +171 -0
  79. package/dist/canvas/pen/plugin.js +129 -0
  80. package/dist/canvas/pen/shapes.js +80 -0
  81. package/dist/canvas/pen/stroke-path.js +37 -0
  82. package/dist/canvas/pen/types.js +30 -0
  83. package/dist/canvas/pen/ui.js +20 -0
  84. package/dist/canvas/plugin.js +51 -0
  85. package/dist/canvas/screen/glyphs.js +24 -0
  86. package/dist/canvas/screen/labels.js +24 -0
  87. package/dist/canvas/screen/parts.js +140 -0
  88. package/dist/canvas/screen/plugin.js +71 -0
  89. package/dist/canvas/screen/types.js +44 -0
  90. package/dist/canvas/screen/ui.js +13 -0
  91. package/dist/canvas/test-utils.js +18 -0
  92. package/dist/canvas/types.js +0 -0
  93. package/dist/canvas/ui/forward-wheel.js +27 -0
  94. package/dist/canvas/ui/icons.js +133 -0
  95. package/dist/canvas/ui/place-panel.js +57 -0
  96. package/dist/canvas/ui/shortcut-tip.js +18 -0
  97. package/dist/canvas/ui/tools.js +411 -0
  98. package/dist/charts/dashboard/chart.js +1 -0
  99. package/dist/charts/dashboard/data-table.js +7 -0
  100. package/dist/charts/dashboard/index.js +13 -0
  101. package/dist/charts/dashboard/windows.js +5 -0
  102. package/dist/charts/edge-fade.js +7 -0
  103. package/dist/charts/hover-band.js +13 -0
  104. package/dist/charts/index.js +19 -0
  105. package/dist/charts/motion.js +15 -0
  106. package/dist/charts/primitives/chart-utils.js +22 -0
  107. package/dist/charts/primitives/index.js +4 -0
  108. package/dist/components/copy/clipboard.js +38 -0
  109. package/dist/components/copy/copied.svelte.js +26 -0
  110. package/dist/components/copy/index.js +3 -0
  111. package/dist/components/data-table/data-table.js +55 -0
  112. package/dist/components/data-table/index.js +7 -0
  113. package/dist/components/docs/index.js +3 -0
  114. package/dist/components/docs/scroll-spy.svelte.js +71 -0
  115. package/dist/components/field-row/index.js +1 -0
  116. package/dist/components/hold-to-confirm/index.js +1 -0
  117. package/dist/components/icon/index.js +1 -0
  118. package/dist/components/language-flag/index.js +1 -0
  119. package/dist/components/list-page/filter-section.js +4 -0
  120. package/dist/components/list-page/index.js +25 -0
  121. package/dist/components/list-page/list-table.js +5 -0
  122. package/dist/components/list-page/queue-rail.js +3 -0
  123. package/dist/components/list-page/section-host.js +8 -0
  124. package/dist/components/list-page/step.js +5 -0
  125. package/dist/components/list-page/types.js +0 -0
  126. package/dist/components/locale-switch/index.js +1 -0
  127. package/dist/components/mark/index.js +1 -0
  128. package/dist/components/open-in/index.js +1 -0
  129. package/dist/components/picker/index.js +3 -0
  130. package/dist/components/picker/trigger.js +33 -0
  131. package/dist/components/picker/types.js +0 -0
  132. package/dist/components/section-card/index.js +1 -0
  133. package/dist/components/section-header/index.js +2 -0
  134. package/dist/components/segmented/collapse.js +32 -0
  135. package/dist/components/segmented/index.js +8 -0
  136. package/dist/components/segmented/segmented.js +50 -0
  137. package/dist/components/star-rating-input/index.js +2 -0
  138. package/dist/components/svg/index.js +1 -0
  139. package/dist/components/table-view/index.js +1 -0
  140. package/dist/components/table-view/types.js +0 -0
  141. package/dist/components/tags-input/index.js +2 -0
  142. package/dist/components/tags-input/tags.js +90 -0
  143. package/dist/components/theme/index.js +3 -0
  144. package/dist/components/toolbar/index.js +3 -0
  145. package/dist/components/toolbar/toolbar-trigger.js +4 -0
  146. package/dist/editor/anchors.js +17 -0
  147. package/dist/editor/block-id.js +62 -0
  148. package/dist/editor/block-ops.js +105 -0
  149. package/dist/editor/doc-blocks.js +181 -0
  150. package/dist/editor/extensions.js +200 -0
  151. package/dist/editor/index.js +4 -0
  152. package/dist/editor/markdown-test-helpers.js +6 -0
  153. package/dist/editor/markdown.js +486 -0
  154. package/dist/editor/serializer.js +268 -0
  155. package/dist/editor/slash-items.js +104 -0
  156. package/dist/editor/youtube-url.js +27 -0
  157. package/dist/effects/animated-beam/index.js +1 -0
  158. package/dist/effects/animated-beam/path-calculator.svelte.js +42 -0
  159. package/dist/effects/animated-beam/types.js +0 -0
  160. package/dist/effects/border-beam/css.js +420 -0
  161. package/dist/effects/border-beam/presets.js +1792 -0
  162. package/dist/effects/border-beam/register-properties.js +39 -0
  163. package/dist/effects/border-beam/types.js +0 -0
  164. package/dist/effects/image-comparison/compare.js +4 -0
  165. package/dist/effects/image-reveal/index.js +1 -0
  166. package/dist/effects/image-reveal/types.js +0 -0
  167. package/dist/effects/magnetic/magnetic.js +54 -0
  168. package/dist/effects/reveal/index.js +4 -0
  169. package/dist/effects/reveal/observe.js +43 -0
  170. package/dist/effects/reveal/types.js +0 -0
  171. package/dist/effects/shader-gradient/color.js +34 -0
  172. package/dist/effects/shader-gradient/index.js +1 -0
  173. package/dist/effects/shader-gradient/renderer.js +320 -0
  174. package/dist/effects/shader-gradient/shaders.js +280 -0
  175. package/dist/effects/shader-gradient/types.js +0 -0
  176. package/dist/icons/apple-symbol-exports.js +126 -0
  177. package/dist/icons/sf-symbols.js +465 -0
  178. package/dist/icons/trades.js +34 -0
  179. package/dist/inbox/index.js +4 -0
  180. package/dist/inbox/logic.js +156 -0
  181. package/dist/inbox/types.js +46 -0
  182. package/dist/index.js +18 -0
  183. package/dist/lib/arrive.js +13 -0
  184. package/dist/lib/icon-types.js +0 -0
  185. package/dist/lib/initials.js +17 -0
  186. package/dist/lib/layout.js +1 -0
  187. package/dist/lib/panel.js +3 -0
  188. package/dist/lib/result.js +26 -0
  189. package/dist/lib/sliding-pill.js +45 -0
  190. package/dist/lib/theme-transition.js +53 -0
  191. package/dist/lib/tone.js +32 -0
  192. package/dist/lib/utils.js +5 -0
  193. package/dist/onboarding/define-tours.js +28 -0
  194. package/dist/onboarding/index.js +4 -0
  195. package/dist/onboarding/onboarding.svelte.js +15 -0
  196. package/dist/onboarding/pick.js +16 -0
  197. package/dist/onboarding/stores.js +50 -0
  198. package/dist/onboarding/types.js +1 -0
  199. package/dist/onboarding/wait.js +25 -0
  200. package/dist/panel/index.js +1 -0
  201. package/dist/panel/side-panel-types.js +0 -0
  202. package/dist/record/demo-data.js +512 -0
  203. package/dist/record/fields.js +148 -0
  204. package/dist/record/format.js +97 -0
  205. package/dist/record/gutter.js +1 -0
  206. package/dist/record/index.js +15 -0
  207. package/dist/record/kinds.js +110 -0
  208. package/dist/record/logic.js +255 -0
  209. package/dist/record/types.js +52 -0
  210. package/dist/shadcn/accordion/index.js +5 -0
  211. package/dist/shadcn/alert/index.js +5 -0
  212. package/dist/shadcn/alert-dialog/index.js +12 -0
  213. package/dist/shadcn/attachment/index.js +10 -0
  214. package/dist/shadcn/avatar/index.js +7 -0
  215. package/dist/shadcn/badge/index.js +2 -0
  216. package/dist/shadcn/breadcrumb/index.js +8 -0
  217. package/dist/shadcn/bubble/index.js +5 -0
  218. package/dist/shadcn/button/index.js +2 -0
  219. package/dist/shadcn/calendar/index.js +19 -0
  220. package/dist/shadcn/card/index.js +8 -0
  221. package/dist/shadcn/checkbox/index.js +2 -0
  222. package/dist/shadcn/code-block/index.js +9 -0
  223. package/dist/shadcn/collapsible/index.js +4 -0
  224. package/dist/shadcn/command/index.js +13 -0
  225. package/dist/shadcn/context-menu/index.js +17 -0
  226. package/dist/shadcn/dialog/index.js +11 -0
  227. package/dist/shadcn/dropdown-menu/index.js +19 -0
  228. package/dist/shadcn/dropdown-menu/menu-item.js +23 -0
  229. package/dist/shadcn/empty/index.js +7 -0
  230. package/dist/shadcn/field/index.js +12 -0
  231. package/dist/shadcn/hooks/highlighter.js +92 -0
  232. package/dist/shadcn/hooks/is-mobile.svelte.js +7 -0
  233. package/dist/shadcn/hooks/use-highlighted-code.svelte.js +65 -0
  234. package/dist/shadcn/hover-card/index.js +5 -0
  235. package/dist/shadcn/input/index.js +3 -0
  236. package/dist/shadcn/input-otp/index.js +5 -0
  237. package/dist/shadcn/kbd/index.js +3 -0
  238. package/dist/shadcn/label/index.js +2 -0
  239. package/dist/shadcn/marker/index.js +4 -0
  240. package/dist/shadcn/message/index.js +7 -0
  241. package/dist/shadcn/message-scroller/index.js +8 -0
  242. package/dist/shadcn/popover/index.js +9 -0
  243. package/dist/shadcn/progress/index.js +2 -0
  244. package/dist/shadcn/progress/tone.js +11 -0
  245. package/dist/shadcn/radio-group/index.js +3 -0
  246. package/dist/shadcn/scroll-area/index.js +3 -0
  247. package/dist/shadcn/select/index.js +12 -0
  248. package/dist/shadcn/separator/index.js +2 -0
  249. package/dist/shadcn/sheet/index.js +11 -0
  250. package/dist/shadcn/sidebar/constants.js +6 -0
  251. package/dist/shadcn/sidebar/context.svelte.js +38 -0
  252. package/dist/shadcn/sidebar/index.js +25 -0
  253. package/dist/shadcn/skeleton/index.js +2 -0
  254. package/dist/shadcn/slider/index.js +2 -0
  255. package/dist/shadcn/sonner/index.js +1 -0
  256. package/dist/shadcn/spinner/index.js +1 -0
  257. package/dist/shadcn/switch/index.js +2 -0
  258. package/dist/shadcn/table/index.js +9 -0
  259. package/dist/shadcn/tabs/index.js +5 -0
  260. package/dist/shadcn/textarea/index.js +2 -0
  261. package/dist/shadcn/toggle/index.js +3 -0
  262. package/dist/shadcn/toggle-group/index.js +3 -0
  263. package/dist/shadcn/tooltip/context.js +8 -0
  264. package/dist/shadcn/tooltip/index.js +6 -0
  265. package/dist/shadcn/trace/index.js +9 -0
  266. package/dist/slides/bridges.js +66 -0
  267. package/dist/slides/deck-controller.svelte.js +221 -0
  268. package/dist/slides/export/capture.js +81 -0
  269. package/dist/slides/export/ffmpeg.js +85 -0
  270. package/dist/slides/export/index.js +4 -0
  271. package/dist/slides/export/launch.js +8 -0
  272. package/dist/slides/export/pdf.js +78 -0
  273. package/dist/slides/export/types.js +0 -0
  274. package/dist/slides/index.js +7 -0
  275. package/dist/slides/labels.js +26 -0
  276. package/dist/slides/layout.js +47 -0
  277. package/dist/slides/model.js +9 -0
  278. package/dist/slides/navigation.js +89 -0
  279. package/dist/slides/types.js +6 -0
  280. package/dist/status/define-statuses.js +16 -0
  281. package/dist/status/index.js +4 -0
  282. package/dist/status/status-types.js +0 -0
  283. package/dist/test-setup.js +26 -0
  284. package/dist/thread/index.js +3 -0
  285. package/dist/thread/logic.js +94 -0
  286. package/dist/thread/types.js +23 -0
  287. package/dist/work/board-order.js +109 -0
  288. package/dist/work/dates.js +19 -0
  289. package/dist/work/detail/detail-types.js +20 -0
  290. package/dist/work/docs/edited.js +39 -0
  291. package/dist/work/docs/index.js +7 -0
  292. package/dist/work/docs/labels.js +38 -0
  293. package/dist/work/docs/menu.js +121 -0
  294. package/dist/work/docs/outline.js +39 -0
  295. package/dist/work/gantt/logic.js +260 -0
  296. package/dist/work/inbox/inbox-logic.js +94 -0
  297. package/dist/work/inbox/inbox-statuses.js +86 -0
  298. package/dist/work/inbox/inbox-types.js +0 -0
  299. package/dist/work/inbox/index.js +1 -0
  300. package/dist/work/index.js +21 -0
  301. package/dist/work/kanban-drag.js +25 -0
  302. package/dist/work/kanban-types.js +0 -0
  303. package/dist/work/presets/sales.js +35 -0
  304. package/dist/work/reorder.js +10 -0
  305. package/dist/work/task-types.js +0 -0
  306. package/dist/work/time/duration.js +60 -0
  307. package/dist/work/time/time-types.js +25 -0
  308. package/dist/work/use-kanban-dnd.svelte.js +128 -0
  309. package/package.json +45 -3
  310. package/src/components/toolbar/toolbar-trigger.ts +8 -2
  311. package/src/record/demo-data.ts +4 -4
  312. package/src/record/record-demo.svelte +2 -2
  313. package/src/status/status-dot.svelte +0 -2
@@ -0,0 +1,1525 @@
1
+ import { rasterize, PAD, SPAN } from "./plastic.js";
2
+ import { shade, parseColor } from "./color.js";
3
+ import { makeMesh, makeTube, makeView, projectMesh, section, paintLayers, boundsOf, hull, norm, cross, sub, segs, felt, velvet, paper, plastic, satin, gold, silver, metal, sphereSprite, cutGem, scale } from "./mesh.js";
4
+ function seat(m, frac) {
5
+ const W = m.right - m.left;
6
+ for(let i = 0; i < m.widths.length; i++)if (m.widths[i] >= frac * W) return i * m.step - PAD;
7
+ return m.top;
8
+ }
9
+ const marks = new WeakMap();
10
+ function marksFor(path) {
11
+ const hit = marks.get(path);
12
+ if (hit) return hit;
13
+ const N = 128;
14
+ const cov = rasterize(path, N);
15
+ if (!cov) return null;
16
+ const u = SPAN / N;
17
+ const toX = (i)=>(i + 0.5) * u - PAD;
18
+ const inside = (x, y)=>cov[y * N + x] >= 128;
19
+ const c0 = Math.floor((42 + PAD) / u), c1 = Math.ceil((58 + PAD) / u);
20
+ let top = N, bottom = -1;
21
+ for(let x = c0; x <= c1; x++){
22
+ for(let y = 0; y < N; y++)if (inside(x, y)) {
23
+ if (y < top) top = y;
24
+ break;
25
+ }
26
+ for(let y = N - 1; y >= 0; y--)if (inside(x, y)) {
27
+ if (y > bottom) bottom = y;
28
+ break;
29
+ }
30
+ }
31
+ const row = (y)=>{
32
+ let l = -1, r = -1;
33
+ for(let x = 0; x < N; x++)if (inside(x, y)) {
34
+ if (l < 0) l = x;
35
+ r = x;
36
+ }
37
+ return [
38
+ l,
39
+ r
40
+ ];
41
+ };
42
+ const yT = Math.min(N - 1, top + Math.round(8 / u));
43
+ const [tl, tr] = row(yT);
44
+ let best = -1, bl = 0, br = 0, by = 0;
45
+ for(let y = Math.round((33 + PAD) / u); y <= Math.round((67 + PAD) / u); y++){
46
+ const [l, r] = row(y);
47
+ if (l >= 0 && r - l > best) {
48
+ best = r - l;
49
+ bl = l;
50
+ br = r;
51
+ by = y;
52
+ }
53
+ }
54
+ const widths = new Float32Array(N);
55
+ const edge = [];
56
+ for(let y = 0; y < N; y++){
57
+ const [l, r] = row(y);
58
+ widths[y] = l < 0 ? 0 : (r - l + 1) * u;
59
+ if (l >= 0) edge.push([
60
+ toX(l) - 0.5 * u,
61
+ toX(y)
62
+ ], [
63
+ toX(r) + 0.5 * u,
64
+ toX(y)
65
+ ]);
66
+ }
67
+ const m = {
68
+ top: toX(top) - 0.5 * u,
69
+ topL: toX(tl),
70
+ topR: toX(tr),
71
+ mid: toX(by),
72
+ left: toX(bl) - 0.5 * u,
73
+ right: toX(br) + 0.5 * u,
74
+ bottom: toX(bottom) + 0.5 * u,
75
+ widths,
76
+ step: u,
77
+ hull: hull(edge)
78
+ };
79
+ marks.set(path, m);
80
+ return m;
81
+ }
82
+ function project(r, p) {
83
+ const X = p[0] - 50, Y = p[1] - 50;
84
+ return [
85
+ r.cy * X + r.sy * p[2],
86
+ r.sy * r.sp * X + r.cp * Y - r.cy * r.sp * p[2]
87
+ ];
88
+ }
89
+ function depthOf(r, p) {
90
+ return -r.sy * r.cp * (p[0] - 50) + r.sp * (p[1] - 50) + r.cy * r.cp * p[2];
91
+ }
92
+ function tilt(v, roll, lean) {
93
+ const cr = Math.cos(roll), sr = Math.sin(roll);
94
+ const x = v[0] * cr - v[1] * sr, y = v[0] * sr + v[1] * cr;
95
+ const cl = Math.cos(lean), sl = Math.sin(lean);
96
+ return [
97
+ x,
98
+ y * cl - v[2] * sl,
99
+ y * sl + v[2] * cl
100
+ ];
101
+ }
102
+ const add = (a, b)=>[
103
+ a[0] + b[0],
104
+ a[1] + b[1],
105
+ a[2] + b[2]
106
+ ];
107
+ const along = (a, d, k)=>[
108
+ a[0] + d[0] * k,
109
+ a[1] + d[1] * k,
110
+ a[2] + d[2] * k
111
+ ];
112
+ function polyPath(pts, close = true) {
113
+ const p = new Path2D();
114
+ pts.forEach((q, i)=>i ? p.lineTo(q[0], q[1]) : p.moveTo(q[0], q[1]));
115
+ if (close) p.closePath();
116
+ return p;
117
+ }
118
+ function bounds(pts) {
119
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
120
+ for (const [x, y] of pts){
121
+ if (x < x0) x0 = x;
122
+ if (y < y0) y0 = y;
123
+ if (x > x1) x1 = x;
124
+ if (y > y1) y1 = y;
125
+ }
126
+ return {
127
+ x0,
128
+ y0,
129
+ x1,
130
+ y1,
131
+ cx: (x0 + x1) / 2,
132
+ cy: (y0 + y1) / 2,
133
+ w: x1 - x0,
134
+ h: y1 - y0
135
+ };
136
+ }
137
+ const rnd = (i, k = 0)=>{
138
+ let h = i * 2654435761 + k * 40503 | 0;
139
+ h = Math.imul(h ^ h >>> 15, 2246822507);
140
+ h = Math.imul(h ^ h >>> 13, 3266489909);
141
+ return ((h ^ h >>> 16) >>> 0) / 4294967296;
142
+ };
143
+ let grain = null;
144
+ function grainTile() {
145
+ if (grain) return grain;
146
+ const n = 48;
147
+ const c = typeof OffscreenCanvas === "function" ? new OffscreenCanvas(n, n) : typeof document !== "undefined" ? Object.assign(document.createElement("canvas"), {
148
+ width: n,
149
+ height: n
150
+ }) : null;
151
+ const g = c && c.getContext("2d");
152
+ if (!c || !g) return null;
153
+ const img = g.createImageData(n, n);
154
+ for(let i = 0; i < n * n; i++){
155
+ const x = i % n, y = i / n | 0;
156
+ let v = 0;
157
+ for(let k = -2; k <= 2; k++)v += rnd((x + k + n) % n + (y + k + n) % n * n, 5);
158
+ v /= 5;
159
+ const m = Math.round(255 * (0.82 + 0.36 * v));
160
+ img.data[i * 4] = img.data[i * 4 + 1] = img.data[i * 4 + 2] = Math.min(255, m);
161
+ img.data[i * 4 + 3] = 255;
162
+ }
163
+ g.putImageData(img, 0, 0);
164
+ grain = c;
165
+ return c;
166
+ }
167
+ const patterns = new WeakMap();
168
+ function grainOver(ctx, outline, alpha, scaleK = 0.18) {
169
+ const tile = grainTile();
170
+ if (!tile || outline.length < 3) return;
171
+ let byScale = patterns.get(ctx);
172
+ if (!byScale) patterns.set(ctx, byScale = new Map());
173
+ let pat = byScale.get(scaleK);
174
+ if (pat === undefined) {
175
+ pat = ctx.createPattern(tile, "repeat");
176
+ pat?.setTransform?.(new DOMMatrix().scale(scaleK));
177
+ byScale.set(scaleK, pat);
178
+ }
179
+ if (!pat) return;
180
+ const b = bounds(outline);
181
+ ctx.save();
182
+ ctx.clip(polyPath(outline));
183
+ ctx.globalCompositeOperation = "multiply";
184
+ ctx.globalAlpha = alpha;
185
+ ctx.fillStyle = pat;
186
+ ctx.fillRect(b.x0 - 1, b.y0 - 1, b.w + 2, b.h + 2);
187
+ ctx.restore();
188
+ }
189
+ function fuzz(ctx, outline, base, seed, len, px = 2.4) {
190
+ const n = outline.length;
191
+ if (n < 3) return;
192
+ let total = 0;
193
+ const seg = [];
194
+ for(let i = 0; i < n; i++){
195
+ const a = outline[i], c = outline[(i + 1) % n];
196
+ const d = Math.hypot(c[0] - a[0], c[1] - a[1]);
197
+ seg.push(d);
198
+ total += d;
199
+ }
200
+ const count = Math.min(360, Math.max(24, Math.round(total * Math.min(2.4, px * 0.5))));
201
+ const b = bounds(outline);
202
+ const lightP = new Path2D(), darkP = new Path2D();
203
+ let si = 0, acc = 0;
204
+ for(let k = 0; k < count; k++){
205
+ const want = (k + rnd(k, seed) * 0.8) * (total / count);
206
+ while(si < n - 1 && acc + seg[si] < want)acc += seg[si++];
207
+ const a = outline[si], c = outline[(si + 1) % n];
208
+ const t = seg[si] ? (want - acc) / seg[si] : 0;
209
+ const x = a[0] + (c[0] - a[0]) * t, y = a[1] + (c[1] - a[1]) * t;
210
+ let ox = x - b.cx, oy = y - b.cy;
211
+ const ol = Math.hypot(ox, oy) || 1;
212
+ ox /= ol;
213
+ oy /= ol;
214
+ const ang = (rnd(k, seed + 1) - 0.5) * 0.9;
215
+ const ca = Math.cos(ang), sa = Math.sin(ang);
216
+ const hx = ox * ca - oy * sa, hy = ox * sa + oy * ca;
217
+ const l = len * (0.35 + rnd(k, seed + 2));
218
+ const p = rnd(k, seed + 3) > 0.5 ? lightP : darkP;
219
+ p.moveTo(x - hx * 0.4, y - hy * 0.4);
220
+ p.lineTo(x + hx * l, y + hy * l);
221
+ }
222
+ ctx.save();
223
+ ctx.lineCap = "round";
224
+ ctx.lineWidth = 0.4;
225
+ ctx.globalAlpha = 0.55;
226
+ ctx.strokeStyle = shade(base, 0.14);
227
+ ctx.stroke(lightP);
228
+ ctx.strokeStyle = shade(base, -0.06);
229
+ ctx.stroke(darkP);
230
+ ctx.restore();
231
+ }
232
+ function contact(ctx, clip, cx, cy, rx, ry, a) {
233
+ if (!(rx > 0) || !(ry > 0)) return;
234
+ ctx.save();
235
+ if (clip) ctx.clip(clip);
236
+ ctx.translate(cx, cy);
237
+ ctx.scale(1, ry / rx);
238
+ const g = ctx.createRadialGradient(0, 0, 0, 0, 0, rx);
239
+ g.addColorStop(0, `rgba(0,0,0,${a})`);
240
+ g.addColorStop(0.55, `rgba(0,0,0,${a * 0.45})`);
241
+ g.addColorStop(1, "rgba(0,0,0,0)");
242
+ ctx.fillStyle = g;
243
+ ctx.beginPath();
244
+ ctx.arc(0, 0, rx, 0, Math.PI * 2);
245
+ ctx.fill();
246
+ ctx.restore();
247
+ }
248
+ const EL = 48 * Math.PI / 180;
249
+ function lightOf(lx, ly) {
250
+ const le = Math.cos(EL), lz = Math.sin(EL);
251
+ const L = norm([
252
+ lx * le,
253
+ ly * le,
254
+ lz
255
+ ]);
256
+ return {
257
+ L,
258
+ H: norm([
259
+ L[0],
260
+ L[1],
261
+ L[2] + 1
262
+ ])
263
+ };
264
+ }
265
+ function pxOf(ctx) {
266
+ const t = ctx.getTransform?.();
267
+ return t ? Math.sqrt(Math.abs(t.a * t.d - t.b * t.c)) || 1 : 2;
268
+ }
269
+ function viewOf(r, px) {
270
+ const { L, H } = lightOf(r.lx, r.ly);
271
+ return makeView([
272
+ r.cy,
273
+ 0,
274
+ r.sy,
275
+ r.sy * r.sp,
276
+ r.cp,
277
+ -r.cy * r.sp,
278
+ -r.sy * r.cp,
279
+ r.sp,
280
+ r.cy * r.cp
281
+ ], [
282
+ 50,
283
+ 50,
284
+ 0
285
+ ], L, H, px);
286
+ }
287
+ const kept = new WeakMap();
288
+ let owner = kept;
289
+ function keep(key, make) {
290
+ let c = kept.get(owner);
291
+ if (!c) kept.set(owner, c = new Map());
292
+ let g = c.get(key);
293
+ if (!g) {
294
+ if (c.size > 240) c.clear();
295
+ g = make();
296
+ c.set(key, g);
297
+ }
298
+ return g;
299
+ }
300
+ function surface(view, key, surf, nu, nv, inside, smooth = true) {
301
+ return projectMesh(view, keep(`${key}|${nu}x${nv}`, ()=>makeMesh(surf, nu, nv, inside, smooth)));
302
+ }
303
+ function tube(view, key, path, up, prof, nu, nv, closed = false) {
304
+ return projectMesh(view, keep(`${key}|${nu}x${nv}`, ()=>makeTube(path, up, prof, nu, nv, closed)));
305
+ }
306
+ const rgbOf = (c, fallback = [
307
+ 40,
308
+ 40,
309
+ 44
310
+ ])=>parseColor(c) ?? fallback;
311
+ const TAU = Math.PI * 2;
312
+ const smooth01 = (t)=>t <= 0 ? 0 : t >= 1 ? 1 : t * t * (3 - 2 * t);
313
+ const spow = (c, e)=>Math.sign(c) * Math.pow(Math.abs(c), e);
314
+ function sphereMesh(view, key, c, rad, nu, nv) {
315
+ return surface(view, key, (u, v)=>{
316
+ const th = u * TAU, ph = v * Math.PI;
317
+ return [
318
+ c[0] + rad * Math.sin(ph) * Math.cos(th),
319
+ c[1] - rad * Math.cos(ph),
320
+ c[2] + rad * Math.sin(ph) * Math.sin(th)
321
+ ];
322
+ }, nu, nv, c);
323
+ }
324
+ function ringMesh(view, key, place, R, y, t, zScale, nu, nv) {
325
+ return surface(view, key, (u, v)=>{
326
+ const th = u * TAU, ph = v * TAU;
327
+ const rr = R + t * Math.cos(ph);
328
+ return place([
329
+ rr * Math.cos(th),
330
+ y - t * Math.sin(ph),
331
+ rr * zScale * Math.sin(th)
332
+ ]);
333
+ }, nu, nv, (u)=>place([
334
+ R * Math.cos(u * TAU),
335
+ y,
336
+ R * zScale * Math.sin(u * TAU)
337
+ ]));
338
+ }
339
+ function offsetPath(path, up, off) {
340
+ return (u)=>{
341
+ const e = 1e-3;
342
+ const T = norm(sub(path(Math.min(1, u + e)), path(Math.max(0, u - e))));
343
+ const B = cross(T, up);
344
+ return along(path(u), B, off(u));
345
+ };
346
+ }
347
+ function yarnBall(ctx, view, c, rad, base, seed) {
348
+ const rgb = rgbOf(base);
349
+ const n = segs(view, TAU * rad, 12, 20);
350
+ const vis = paintLayers(ctx, [
351
+ {
352
+ parts: [
353
+ sphereMesh(view, `ball${seed}`, c, rad * 0.86, n, Math.max(6, n >> 1))
354
+ ],
355
+ shade: felt(view, rgb, 0.92)
356
+ }
357
+ ], view.px);
358
+ const centre = view.toScreen(c);
359
+ const b = bounds(vis);
360
+ const R = Math.max(b.w, b.h) / 2;
361
+ const count = Math.round(Math.min(340, 50 + R * view.px * 5));
362
+ const light = new Path2D(), dark = new Path2D();
363
+ for(let k = 0; k < count; k++){
364
+ const a = rnd(k, seed) * TAU;
365
+ const r0 = R * (0.25 + 0.7 * Math.sqrt(rnd(k, seed + 1)));
366
+ const len = R * (0.25 + 0.35 * rnd(k, seed + 2));
367
+ const x0 = centre[0] + Math.cos(a) * r0, y0 = centre[1] + Math.sin(a) * r0;
368
+ const bend = (rnd(k, seed + 3) - 0.5) * 0.6;
369
+ const ex = Math.cos(a + bend), ey = Math.sin(a + bend);
370
+ const p = rnd(k, seed + 4) > 0.45 ? light : dark;
371
+ p.moveTo(x0, y0);
372
+ p.quadraticCurveTo(x0 + Math.cos(a) * len * 0.5, y0 + Math.sin(a) * len * 0.5, x0 + ex * len, y0 + ey * len);
373
+ }
374
+ ctx.save();
375
+ ctx.lineCap = "round";
376
+ ctx.lineWidth = Math.max(0.3, R * 0.06);
377
+ const L = view.L;
378
+ const g = ctx.createLinearGradient(centre[0] + L[0] * R, centre[1] + L[1] * R, centre[0] - L[0] * R, centre[1] - L[1] * R);
379
+ g.addColorStop(0, shade(base, 0.2, -0.03));
380
+ g.addColorStop(1, shade(base, -0.12, 0.02));
381
+ ctx.strokeStyle = g;
382
+ ctx.globalAlpha = 0.85;
383
+ ctx.stroke(light);
384
+ ctx.strokeStyle = shade(base, -0.18, 0.03);
385
+ ctx.globalAlpha = 0.6;
386
+ ctx.stroke(dark);
387
+ ctx.restore();
388
+ }
389
+ function drawBeret(ctx, view, m, base, bodyClip) {
390
+ const W = m.right - m.left;
391
+ const R = 0.46 * W, Hh = 0.21 * W, Rb = 0.33 * W, lift = 0.05 * W;
392
+ const C = [
393
+ 50 - 0.07 * W,
394
+ Math.max(m.top + 0.22 * W, seat(m, 0.5) + 0.1 * W),
395
+ 0
396
+ ];
397
+ const roll = -0.24, lean = -0.2;
398
+ const place = (p)=>add(C, tilt(p, roll, lean));
399
+ const nu = segs(view, TAU * R, 32, 56), nv = segs(view, 1.2 * R, 10, 32, 6);
400
+ const surf = (u, v)=>{
401
+ const th = u * TAU;
402
+ let rr, hh;
403
+ const E = 0.74;
404
+ if (v <= E) {
405
+ const phi = v / E * (Math.PI / 2);
406
+ rr = R * Math.sin(phi);
407
+ hh = -(lift + Hh * Math.pow(Math.cos(phi), 0.8));
408
+ } else {
409
+ const f = smooth01((v - E) / (1 - E));
410
+ rr = R + (Rb - R) * f;
411
+ hh = -lift * (1 - f);
412
+ }
413
+ const band = Math.sin(Math.min(1, v / E) * Math.PI * 0.9);
414
+ rr *= 1 + 0.035 * Math.sin(th * 7 + 0.4) * band;
415
+ return place([
416
+ rr * Math.cos(th),
417
+ hh,
418
+ rr * 0.93 * Math.sin(th)
419
+ ]);
420
+ };
421
+ const dome = surface(view, "beret:dome", surf, nu, nv, place([
422
+ 0,
423
+ -lift - Hh * 0.4,
424
+ 0
425
+ ]));
426
+ const under = surface(view, "beret:under", (u, v)=>{
427
+ const th = u * TAU, rr = Rb * (1 - v);
428
+ return place([
429
+ rr * Math.cos(th),
430
+ -0.004 * W * v,
431
+ rr * 0.93 * Math.sin(th)
432
+ ]);
433
+ }, 24, 2, place([
434
+ 0,
435
+ -lift - Hh * 0.4,
436
+ 0
437
+ ]));
438
+ const rgb = rgbOf(base);
439
+ const bandMid = view.toScreen(place([
440
+ 0,
441
+ 0.02 * W,
442
+ 0
443
+ ]));
444
+ contact(ctx, bodyClip, bandMid[0] + 0.02 * W, bandMid[1] + 0.045 * W, Rb * 1.25, 0.08 * W, 0.4);
445
+ const binding = ringMesh(view, "beret:binding", place, Rb, -0.005 * W, 0.022 * W, 0.93, segs(view, TAU * Rb, 24, 48), 5);
446
+ const leather = rgbOf(shade(base, -0.06));
447
+ const vis = paintLayers(ctx, [
448
+ {
449
+ parts: [
450
+ under
451
+ ],
452
+ shade: felt(view, rgb, 0.7)
453
+ },
454
+ {
455
+ parts: [
456
+ binding
457
+ ],
458
+ shade: plastic(view, leather, 0.45)
459
+ },
460
+ {
461
+ parts: [
462
+ dome
463
+ ],
464
+ shade: felt(view, rgb)
465
+ }
466
+ ], view.px);
467
+ const outline = hull(vis);
468
+ grainOver(ctx, outline, 0.5, 0.14);
469
+ fuzz(ctx, outline, base, 11, 0.4, view.px);
470
+ const x0 = 0.02 * W, y0 = -lift - Hh + 0.006 * W;
471
+ const stalk = tube(view, "beret:stalk", (u)=>place([
472
+ x0 + 0.014 * W * u * u,
473
+ y0 - 0.045 * W * u,
474
+ 0
475
+ ]), [
476
+ 0,
477
+ 0,
478
+ 1
479
+ ], (u, ph)=>[
480
+ 0.016 * W * (1 - 0.25 * u) * Math.cos(ph),
481
+ 0.016 * W * (1 - 0.25 * u) * Math.sin(ph)
482
+ ], 8, 3);
483
+ paintLayers(ctx, [
484
+ {
485
+ parts: [
486
+ stalk
487
+ ],
488
+ shade: felt(view, rgb, 0.95)
489
+ }
490
+ ], view.px);
491
+ sphereSprite(ctx, view, place([
492
+ x0 + 0.014 * W,
493
+ y0 - 0.047 * W,
494
+ 0
495
+ ]), 0.019 * W, scale(rgb, 1.15));
496
+ }
497
+ function drawBeanie(ctx, view, m, base, bodyClip) {
498
+ const W = m.right - m.left;
499
+ const headW = Math.max(0.5 * W, m.topR - m.topL);
500
+ const Rd = 0.53 * headW + 0.08 * W, Hd = 0.32 * W, cuffH = 0.1 * W, Rc = Rd * 1.05;
501
+ const C = [
502
+ 50,
503
+ seat(m, 0.72) + 0.06 * W,
504
+ 0
505
+ ];
506
+ const roll = 0, lean = -0.14;
507
+ const place = (p)=>add(C, tilt(p, roll, lean));
508
+ const domeSurf = (u, v)=>{
509
+ const th = u * TAU, phi = v * (Math.PI / 2);
510
+ const rr = Rd * Math.sin(phi) * (1 - 0.03 * (1 - v));
511
+ const hh = -(cuffH + Hd * Math.cos(phi));
512
+ return place([
513
+ rr * Math.cos(th),
514
+ hh,
515
+ rr * 0.92 * Math.sin(th)
516
+ ]);
517
+ };
518
+ const inDome = place([
519
+ 0,
520
+ -cuffH - Hd * 0.4,
521
+ 0
522
+ ]);
523
+ const dome = surface(view, "beanie:dome", domeSurf, segs(view, TAU * Rd, 28, 48), segs(view, 1.3 * Hd, 9, 24, 6), inDome);
524
+ const cuffSurf = (u, v)=>{
525
+ const th = u * TAU;
526
+ const e = 2 * v - 1;
527
+ const rr = Rc * (1 - 0.07 * e * e * e * e) * 1.01;
528
+ return place([
529
+ rr * Math.cos(th),
530
+ -cuffH * (1 - v),
531
+ rr * 0.92 * Math.sin(th)
532
+ ]);
533
+ };
534
+ const cuff = surface(view, "beanie:cuff", cuffSurf, segs(view, TAU * Rc, 28, 56), 4, place([
535
+ 0,
536
+ -cuffH / 2,
537
+ 0
538
+ ]));
539
+ const rgb = rgbOf(base), cuffRgb = rgbOf(shade(base, 0.03));
540
+ const bb = boundsOf([
541
+ cuff
542
+ ]);
543
+ contact(ctx, bodyClip, bb.cx, bb.y1 - 0.01 * W, bb.w * 0.5, 0.06 * W, 0.34);
544
+ const visDome = paintLayers(ctx, [
545
+ {
546
+ parts: [
547
+ dome
548
+ ],
549
+ shade: felt(view, rgb, 0.96)
550
+ }
551
+ ], view.px);
552
+ const NU = 40, NV = 11;
553
+ const cell = Rd * TAU / NU * view.px;
554
+ if (cell >= 2.2) {
555
+ const grid = surface(view, "beanie:stitch", domeSurf, NU, NV, inDome, false);
556
+ const dark = new Path2D(), light = new Path2D();
557
+ const GW = NU + 1;
558
+ for(let c = 0; c < NU * NV; c++){
559
+ if (grid.f[c * 3 + 2] < 0.12) continue;
560
+ const k = Math.floor(c / NU) * GW + c % NU;
561
+ const A = [
562
+ grid.x[k],
563
+ grid.y[k]
564
+ ], B = [
565
+ grid.x[k + 1],
566
+ grid.y[k + 1]
567
+ ];
568
+ const Cq = [
569
+ grid.x[k + GW + 1],
570
+ grid.y[k + GW + 1]
571
+ ], D = [
572
+ grid.x[k + GW],
573
+ grid.y[k + GW]
574
+ ];
575
+ const at = (s, t)=>{
576
+ const top = [
577
+ A[0] + (B[0] - A[0]) * s,
578
+ A[1] + (B[1] - A[1]) * s
579
+ ];
580
+ const bot = [
581
+ D[0] + (Cq[0] - D[0]) * s,
582
+ D[1] + (Cq[1] - D[1]) * s
583
+ ];
584
+ return [
585
+ top[0] + (bot[0] - top[0]) * t,
586
+ top[1] + (bot[1] - top[1]) * t
587
+ ];
588
+ };
589
+ for (const [p, dx] of [
590
+ [
591
+ dark,
592
+ 0
593
+ ],
594
+ [
595
+ light,
596
+ -0.06
597
+ ]
598
+ ]){
599
+ const a = at(0.14 + dx, 0.12), b = at(0.5 + dx, 0.88), c = at(0.86 + dx, 0.12);
600
+ p.moveTo(a[0], a[1]);
601
+ p.lineTo(b[0], b[1]);
602
+ p.lineTo(c[0], c[1]);
603
+ }
604
+ }
605
+ const cw = Rd * TAU / NU;
606
+ ctx.save();
607
+ ctx.clip(polyPath(hull(visDome)));
608
+ ctx.lineCap = "round";
609
+ ctx.lineJoin = "round";
610
+ ctx.lineWidth = Math.max(0.35, cw * 0.2);
611
+ ctx.strokeStyle = shade(base, -0.22, 0.03);
612
+ ctx.globalAlpha = 0.5;
613
+ ctx.stroke(dark);
614
+ ctx.lineWidth = Math.max(0.3, cw * 0.12);
615
+ ctx.strokeStyle = shade(base, 0.2, -0.03);
616
+ ctx.globalAlpha = 0.35;
617
+ ctx.stroke(light);
618
+ ctx.restore();
619
+ }
620
+ const visCuff = paintLayers(ctx, [
621
+ {
622
+ parts: [
623
+ cuff
624
+ ],
625
+ shade: felt(view, cuffRgb, 0.98)
626
+ }
627
+ ], view.px);
628
+ const ribGap = TAU * Rc / 30;
629
+ if (ribGap * view.px >= 3) {
630
+ const groove = new Path2D(), ridge = new Path2D();
631
+ for(let k = 0; k < 60; k++){
632
+ const u = k / 60;
633
+ const th = u * TAU;
634
+ if (view.dir(tilt([
635
+ Math.cos(th),
636
+ 0,
637
+ 0.92 * Math.sin(th)
638
+ ], roll, lean))[2] < 0.08) continue;
639
+ const a = view.toScreen(cuffSurf(u, 0.1)), b = view.toScreen(cuffSurf(u, 0.9));
640
+ const p = k % 2 ? groove : ridge;
641
+ p.moveTo(a[0], a[1]);
642
+ p.lineTo(b[0], b[1]);
643
+ }
644
+ ctx.save();
645
+ ctx.lineCap = "round";
646
+ ctx.lineWidth = ribGap * 0.34;
647
+ ctx.strokeStyle = shade(base, -0.2, 0.03);
648
+ ctx.globalAlpha = 0.45;
649
+ ctx.stroke(groove);
650
+ ctx.lineWidth = ribGap * 0.22;
651
+ ctx.strokeStyle = shade(base, 0.16, -0.02);
652
+ ctx.globalAlpha = 0.4;
653
+ ctx.stroke(ridge);
654
+ ctx.restore();
655
+ }
656
+ const outline = hull(visDome.concat(visCuff));
657
+ fuzz(ctx, outline, base, 23, 0.35, view.px);
658
+ yarnBall(ctx, view, place([
659
+ 0,
660
+ -cuffH - Hd - 0.055 * W,
661
+ 0
662
+ ]), 0.085 * W, shade(base, 0.06), 31);
663
+ }
664
+ function drawParty(ctx, view, m, base, bodyClip) {
665
+ const W = m.right - m.left;
666
+ const C = [
667
+ 50 + 0.06 * W,
668
+ seat(m, 0.3) + 0.04 * W,
669
+ 0
670
+ ];
671
+ const R = 0.18 * W, H = Math.max(0.3 * W, Math.min(0.46 * W, C[1] + 31 - 0.1 * W));
672
+ const roll = 0.18, lean = -0.12;
673
+ const place = (p)=>add(C, tilt(p, roll, lean));
674
+ const coneAt = (u, v, grow = 1)=>{
675
+ const th = u * TAU;
676
+ const rr = R * (1 - v) * (1 + 0.04 * Math.sin(v * Math.PI)) * grow;
677
+ return place([
678
+ rr * Math.cos(th),
679
+ -H * v,
680
+ rr * Math.sin(th)
681
+ ]);
682
+ };
683
+ const inCone = (_u, v)=>place([
684
+ 0,
685
+ -H * Math.min(0.97, v),
686
+ 0
687
+ ]);
688
+ const nAround = segs(view, TAU * R, 20, 40), nUp = segs(view, H, 10, 24, 6);
689
+ const cone = surface(view, "party:cone", (u, v)=>coneAt(u, v), nAround, nUp, inCone);
690
+ const a = rgbOf(base), b = [
691
+ 252,
692
+ 246,
693
+ 234
694
+ ];
695
+ const bb = boundsOf([
696
+ cone
697
+ ]);
698
+ contact(ctx, bodyClip, bb.cx, bb.y1, R * 1.15, 0.04 * W, 0.3);
699
+ const trim = ringMesh(view, "party:trim", place, R * 1.02, 0.004 * W, 0.016 * W, 1, segs(view, TAU * R, 20, 40), 5);
700
+ const stripes = [];
701
+ const K = 6, twist = 3.2;
702
+ for(let k = 0; k < K; k += 1){
703
+ stripes.push(surface(view, `party:stripe${k}`, (s, t)=>coneAt((k + 0.5 * t - twist * s * 0.97) / K, s * 0.97, 1.004), nUp + 3, 1, inCone));
704
+ }
705
+ const vis = paintLayers(ctx, [
706
+ {
707
+ parts: [
708
+ cone
709
+ ],
710
+ shade: paper(view, b)
711
+ },
712
+ {
713
+ parts: [
714
+ trim
715
+ ],
716
+ shade: gold(view)
717
+ }
718
+ ], view.px);
719
+ paintLayers(ctx, [
720
+ {
721
+ parts: stripes,
722
+ shade: paper(view, a)
723
+ }
724
+ ], view.px);
725
+ grainOver(ctx, hull(vis), 0.18, 0.1);
726
+ yarnBall(ctx, view, place([
727
+ 0,
728
+ -H - 0.035 * W,
729
+ 0
730
+ ]), 0.06 * W, "#f4efe6", 37);
731
+ }
732
+ function drawCrown(ctx, view, m, bodyClip) {
733
+ const W = m.right - m.left;
734
+ const headW = Math.max(0.45 * W, m.topR - m.topL);
735
+ const R = 0.44 * headW + 0.07 * W, bandH = 0.11 * W, pointH = 0.15 * W, ez = 0.9;
736
+ const C = [
737
+ 50,
738
+ seat(m, 0.42) + 0.05 * W,
739
+ 0
740
+ ];
741
+ const roll = 0.05, lean = -0.22;
742
+ const place = (p)=>add(C, tilt(p, roll, lean));
743
+ const dirOf = (v)=>norm(tilt(v, roll, lean));
744
+ const on = (th, rr, y)=>place([
745
+ rr * Math.cos(th),
746
+ y,
747
+ rr * ez * Math.sin(th)
748
+ ]);
749
+ const axis = (y)=>place([
750
+ 0,
751
+ y,
752
+ 0
753
+ ]);
754
+ const nu = segs(view, TAU * R, 32, 64);
755
+ const band = surface(view, "crown:band", (u, v)=>on(u * TAU, R * (1 - 0.018 * Math.sin(v * Math.PI)), -bandH * (1 - v)), nu, 2, (_u, v)=>axis(-bandH * (1 - v)));
756
+ const rimT = 0.017 * W;
757
+ const rimTop = ringMesh(view, "crown:rimTop", place, R + 0.004 * W, -bandH, rimT, ez, nu, 5);
758
+ const rimBot = ringMesh(view, "crown:rimBot", place, R + 0.004 * W, 0, rimT * 1.15, ez, nu, 5);
759
+ const capBase = -bandH * 0.55;
760
+ const capH = Math.max(0.12 * W, C[1] - m.top + 0.04 * W + capBase);
761
+ const P0 = Math.PI / 2;
762
+ const cap = surface(view, "crown:cap", (u, v)=>{
763
+ const th = u * TAU, phi = v * (Math.PI / 2);
764
+ const puff = 0.08 * Math.sin(2 * phi) * (0.55 + 0.45 * Math.cos(5 * (th - P0 - Math.PI / 5)));
765
+ return on(th, R * (0.9 * Math.sin(phi) + puff), capBase - capH * Math.cos(phi));
766
+ }, segs(view, TAU * R, 24, 48), segs(view, 1.4 * capH, 8, 20, 6), axis(capBase - capH * 0.4));
767
+ const points = [], buds = [];
768
+ const pearls = [], budTops = [];
769
+ const plate = (key, a0, w, h, ridge, flare, out)=>{
770
+ out.push(surface(view, key, (u, v)=>{
771
+ const s = u * 2 - 1;
772
+ const th = a0 + s * w * Math.pow(1 - v, 1.3);
773
+ const rr = R + 0.004 * W + ridge * (1 - Math.abs(s)) * (1 - 0.5 * v) + flare * v * v;
774
+ return on(th, rr, -bandH - h * v);
775
+ }, 2, segs(view, h, 3, 5), (_u, v)=>on(a0, R * 0.5, -bandH - h * v), false));
776
+ };
777
+ for(let i = 0; i < 5; i++){
778
+ const a0 = P0 + i / 5 * TAU;
779
+ plate(`crown:point${i}`, a0, TAU / 5 * 0.34, pointH, 0.05 * W, 0.03 * W, points);
780
+ pearls.push(on(a0, R + 0.004 * W + 0.03 * W + 0.02 * W, -bandH - pointH - 0.018 * W));
781
+ const b0 = a0 + Math.PI / 5;
782
+ plate(`crown:bud${i}`, b0, TAU / 5 * 0.12, pointH * 0.36, 0.02 * W, 0.01 * W, buds);
783
+ budTops.push(on(b0, R + 0.004 * W + 0.012 * W, -bandH - pointH * 0.36 - 0.012 * W));
784
+ }
785
+ const bb = boundsOf([
786
+ band
787
+ ]);
788
+ contact(ctx, bodyClip, bb.cx, bb.y1, bb.w * 0.5, 0.045 * W, 0.34);
789
+ const mid = view.depth(axis(capBase));
790
+ const g = gold(view);
791
+ const facet = metal(view, [
792
+ 255,
793
+ 196,
794
+ 80
795
+ ], [
796
+ 92,
797
+ 54,
798
+ 10
799
+ ], [
800
+ 255,
801
+ 222,
802
+ 150
803
+ ]);
804
+ const goldBoth = (n)=>facet(n[2] < 0 ? [
805
+ -n[0],
806
+ -n[1],
807
+ -n[2]
808
+ ] : n);
809
+ const pearlR = 0.024 * W, budR = 0.019 * W;
810
+ for (const p of pearls)if (view.depth(p) < mid) sphereSprite(ctx, view, p, pearlR, "pearl");
811
+ for (const p of budTops)if (view.depth(p) < mid) sphereSprite(ctx, view, p, budR, "gold");
812
+ paintLayers(ctx, [
813
+ {
814
+ parts: [
815
+ cap
816
+ ],
817
+ shade: velvet(view, [
818
+ 150,
819
+ 22,
820
+ 44
821
+ ])
822
+ },
823
+ {
824
+ parts: [
825
+ band
826
+ ],
827
+ shade: g,
828
+ bias: 0.01 * W
829
+ },
830
+ {
831
+ parts: [
832
+ rimTop
833
+ ],
834
+ shade: g,
835
+ bias: 0.012 * W
836
+ },
837
+ {
838
+ parts: [
839
+ rimBot
840
+ ],
841
+ shade: g,
842
+ bias: 0.012 * W
843
+ },
844
+ {
845
+ parts: points,
846
+ shade: goldBoth,
847
+ cull: false,
848
+ flat: true
849
+ },
850
+ {
851
+ parts: buds,
852
+ shade: goldBoth,
853
+ cull: false,
854
+ flat: true
855
+ }
856
+ ], view.px);
857
+ const stones = [
858
+ [
859
+ 208,
860
+ 20,
861
+ 60
862
+ ],
863
+ [
864
+ 30,
865
+ 150,
866
+ 92
867
+ ],
868
+ [
869
+ 36,
870
+ 90,
871
+ 220
872
+ ],
873
+ [
874
+ 36,
875
+ 90,
876
+ 220
877
+ ],
878
+ [
879
+ 30,
880
+ 150,
881
+ 92
882
+ ]
883
+ ];
884
+ for(let i = 0; i < 5; i++){
885
+ for (const [th, kind] of [
886
+ [
887
+ P0 + i / 5 * TAU,
888
+ "stone"
889
+ ],
890
+ [
891
+ P0 + i / 5 * TAU + Math.PI / 5,
892
+ "stud"
893
+ ]
894
+ ]){
895
+ const N = dirOf([
896
+ Math.cos(th),
897
+ 0,
898
+ ez * Math.sin(th)
899
+ ]);
900
+ if (view.dir(N)[2] < 0.12) continue;
901
+ const c = on(th, R + 0.01 * W, -bandH * 0.5);
902
+ if (kind === "stud") {
903
+ sphereSprite(ctx, view, c, 0.013 * W, "gold");
904
+ continue;
905
+ }
906
+ const size = (i === 0 ? 0.034 : 0.027) * W;
907
+ sphereSprite(ctx, view, c, size * 1.3, "gold");
908
+ const T = dirOf([
909
+ -Math.sin(th),
910
+ 0,
911
+ ez * Math.cos(th)
912
+ ]);
913
+ cutGem(ctx, view, along(c, N, 0.006 * W), N, T, size, stones[i]);
914
+ }
915
+ }
916
+ for (const p of pearls)if (view.depth(p) >= mid) sphereSprite(ctx, view, p, pearlR, "pearl");
917
+ for (const p of budTops)if (view.depth(p) >= mid) sphereSprite(ctx, view, p, budR, "gold");
918
+ const orbR = 0.034 * W;
919
+ const orb = axis(capBase - capH - orbR * 0.75);
920
+ const up = dirOf([
921
+ 0,
922
+ -1,
923
+ 0
924
+ ]);
925
+ const bar = (key, a, b)=>tube(view, key, (u)=>[
926
+ a[0] + (b[0] - a[0]) * u,
927
+ a[1] + (b[1] - a[1]) * u,
928
+ a[2] + (b[2] - a[2]) * u
929
+ ], dirOf([
930
+ 0,
931
+ 0,
932
+ 1
933
+ ]), section(0.009 * W, 0.009 * W, 0.5), 2, 6);
934
+ const c0 = along(orb, up, orbR * 0.8), c1 = along(orb, up, orbR * 0.8 + 0.075 * W);
935
+ const cm = along(orb, up, orbR * 0.8 + 0.05 * W), side = dirOf([
936
+ 1,
937
+ 0,
938
+ 0
939
+ ]);
940
+ sphereSprite(ctx, view, orb, orbR, "gold");
941
+ paintLayers(ctx, [
942
+ {
943
+ parts: [
944
+ bar("crown:barV", c0, c1)
945
+ ],
946
+ shade: g
947
+ },
948
+ {
949
+ parts: [
950
+ bar("crown:barH", along(cm, side, -0.026 * W), along(cm, side, 0.026 * W))
951
+ ],
952
+ shade: g
953
+ }
954
+ ], view.px);
955
+ }
956
+ function phonesOf(view, m, base) {
957
+ const W = m.right - m.left;
958
+ const rc = 0.15 * W, oval = 1.1, pad = 0.05 * W, tc = 0.068 * W;
959
+ const yc = m.mid - 0.07 * W;
960
+ const tilt = 0.5;
961
+ const shellRgb = rgbOf(base), padRgb = rgbOf(shade(base, -0.12)), plateRgb = rgbOf(shade(base, 0.08));
962
+ const nu = segs(view, TAU * rc, 20, 40);
963
+ const layers = [];
964
+ const pivots = [];
965
+ const tops = [];
966
+ const seats = [];
967
+ const g = silver(view);
968
+ for (const s of [
969
+ -1,
970
+ 1
971
+ ]){
972
+ const A = [
973
+ s * Math.cos(tilt),
974
+ 0,
975
+ Math.sin(tilt)
976
+ ];
977
+ const E1 = [
978
+ 0,
979
+ 1,
980
+ 0
981
+ ];
982
+ const E2 = [
983
+ -Math.sin(tilt),
984
+ 0,
985
+ s * Math.cos(tilt)
986
+ ];
987
+ const O = [
988
+ (s < 0 ? m.left : m.right) - s * 0.02 * W,
989
+ yc,
990
+ 0.01 * W
991
+ ];
992
+ const at = (t, rho, th)=>{
993
+ const c = Math.cos(th) * oval * rho, sn = Math.sin(th) * rho;
994
+ return [
995
+ O[0] + A[0] * t + E1[0] * c + E2[0] * sn,
996
+ O[1] + A[1] * t + E1[1] * c + E2[1] * sn,
997
+ O[2] + A[2] * t + E1[2] * c + E2[2] * sn
998
+ ];
999
+ };
1000
+ const axis = (t)=>along(O, A, t);
1001
+ seats.push({
1002
+ c: O,
1003
+ ry: rc * oval
1004
+ });
1005
+ const rho0 = rc * 0.9 - pad * 0.5;
1006
+ const creases = nu >= 48 ? 1 : 0;
1007
+ const cushion = surface(view, `phones:cushion${s}`, (u, v)=>{
1008
+ const th = u * TAU, a = v * Math.PI;
1009
+ const crease = 1 + 0.006 * creases * Math.sin(th * 24) * Math.sin(a);
1010
+ return at(pad * 0.5 * (1 - Math.cos(a)), (rho0 + pad * 0.62 * Math.sin(a)) * crease, th);
1011
+ }, nu, 5, (_u, v)=>axis(pad * 0.5 * (1 - Math.cos(v * Math.PI))));
1012
+ const shell = surface(view, `phones:shell${s}`, (u, v)=>{
1013
+ const a = v * (Math.PI / 2);
1014
+ return at(pad + tc * Math.pow(Math.sin(a), 0.55), rc * (0.7 + 0.3 * Math.pow(Math.cos(a), 0.55)), u * TAU);
1015
+ }, nu, 5, axis(pad + tc * 0.5));
1016
+ const ringR = 0.66 * rc, ringT = 0.017 * W;
1017
+ const ring = surface(view, `phones:ring${s}`, (u, v)=>at(pad + tc + ringT * 0.45 * Math.sin(v * TAU), ringR + ringT * Math.cos(v * TAU), u * TAU), nu, 5, (u)=>at(pad + tc, ringR, u * TAU));
1018
+ const plateR = ringR - ringT * 0.5;
1019
+ const plate = surface(view, `phones:plate${s}`, (u, v)=>at(pad + tc + 0.013 * W * (1 - v * v), plateR * v, u * TAU), nu, 3, axis(pad + tc - 0.02 * W));
1020
+ const ty = pad + tc * 0.45, ry = rc * 1.04 + 0.022 * W;
1021
+ const yokeAt = (u)=>{
1022
+ const a = u * Math.PI;
1023
+ const c = -Math.sin(a) * oval * ry, sn = Math.cos(a) * ry;
1024
+ return [
1025
+ O[0] + A[0] * ty + E1[0] * c + E2[0] * sn,
1026
+ O[1] + A[1] * ty + E1[1] * c + E2[1] * sn,
1027
+ O[2] + A[2] * ty + E1[2] * c + E2[2] * sn
1028
+ ];
1029
+ };
1030
+ const yoke = tube(view, `phones:yoke${s}`, yokeAt, A, section(0.013 * W, 0.008 * W, 0.6), segs(view, Math.PI * ry, 12, 24), 6);
1031
+ pivots.push(yokeAt(0), yokeAt(1));
1032
+ tops.push(yokeAt(0.5));
1033
+ layers.push({
1034
+ parts: [
1035
+ cushion
1036
+ ],
1037
+ shade: plastic(view, padRgb, 0.35)
1038
+ }, {
1039
+ parts: [
1040
+ shell
1041
+ ],
1042
+ shade: plastic(view, shellRgb, 0.8)
1043
+ }, {
1044
+ parts: [
1045
+ ring
1046
+ ],
1047
+ shade: g,
1048
+ bias: 0.02 * W
1049
+ }, {
1050
+ parts: [
1051
+ plate
1052
+ ],
1053
+ shade: plastic(view, plateRgb, 1.3),
1054
+ bias: 0.01 * W
1055
+ }, {
1056
+ parts: [
1057
+ yoke
1058
+ ],
1059
+ shade: g
1060
+ });
1061
+ }
1062
+ const [PL, PR] = tops;
1063
+ const zb = (PL[2] + PR[2]) / 2, cx = (PL[0] + PR[0]) / 2, ax = (PR[0] - PL[0]) / 2, yEnd = (PL[1] + PR[1]) / 2;
1064
+ const padT = (u)=>0.005 * W + 0.022 * W * Math.pow(Math.sin(Math.PI * u), 0.35);
1065
+ const e = 2 / 2.2;
1066
+ const topAt = (x)=>{
1067
+ let y = Infinity;
1068
+ const h = m.hull;
1069
+ for(let i = 0; i < h.length; i++){
1070
+ const p = h[i], q = h[(i + 1) % h.length];
1071
+ if (p[0] === q[0] || x < Math.min(p[0], q[0]) || x > Math.max(p[0], q[0])) continue;
1072
+ y = Math.min(y, p[1] + (q[1] - p[1]) * (x - p[0]) / (q[0] - p[0]));
1073
+ }
1074
+ return y;
1075
+ };
1076
+ let lift = yEnd - m.top;
1077
+ for(let i = 1; i < 64; i++){
1078
+ const t = Math.PI * (1 - i / 64);
1079
+ const s = Math.pow(Math.abs(Math.sin(t)), e);
1080
+ const yt = topAt(cx + ax * spow(Math.cos(t), e));
1081
+ if (s > 0.05 && Number.isFinite(yt)) lift = Math.max(lift, (yEnd - yt + 2 * padT(i / 64) + 0.006 * W) / s);
1082
+ }
1083
+ const arch = (u)=>{
1084
+ const t = Math.PI * (1 - u);
1085
+ return [
1086
+ cx + ax * spow(Math.cos(t), e),
1087
+ yEnd - lift * Math.pow(Math.abs(Math.sin(t)), e),
1088
+ zb
1089
+ ];
1090
+ };
1091
+ const part = (u0, u1)=>(u)=>arch(u0 + (u1 - u0) * u);
1092
+ const Z = [
1093
+ 0,
1094
+ 0,
1095
+ 1
1096
+ ];
1097
+ const span = ax * 2 + lift * 2;
1098
+ const nb = segs(view, span, 24, 48);
1099
+ const us = 0.09;
1100
+ const shellOff = 0.016 * W;
1101
+ const shellPath = offsetPath(part(us, 1 - us), Z, ()=>shellOff);
1102
+ const taper = (u)=>smooth01(Math.min(u, 1 - u) / 0.07);
1103
+ const bandShell = tube(view, "phones:bandShell", shellPath, Z, (u, ph)=>{
1104
+ const k = taper(u);
1105
+ return [
1106
+ (0.024 + 0.016 * k) * W * spow(Math.cos(ph), 0.4),
1107
+ (0.008 + 0.008 * k) * W * spow(Math.sin(ph), 0.4)
1108
+ ];
1109
+ }, nb, 6);
1110
+ const cushionPath = offsetPath(part(us + 0.02, 1 - us - 0.02), Z, (u)=>-padT(us + 0.02 + (1 - 2 * us - 0.04) * u));
1111
+ const bandPad = tube(view, "phones:bandPad", cushionPath, Z, (u, ph)=>[
1112
+ 0.034 * W * spow(Math.cos(ph), 0.5),
1113
+ padT(us + 0.02 + (1 - 2 * us - 0.04) * u) * spow(Math.sin(ph), 0.7)
1114
+ ], nb, 6);
1115
+ const sliders = [
1116
+ part(0, us + 0.03),
1117
+ part(1 - us - 0.03, 1)
1118
+ ].map((p, i)=>tube(view, `phones:slider${i}`, p, Z, section(0.022 * W, 0.006 * W, 0.4), 4, 6));
1119
+ layers.push({
1120
+ parts: [
1121
+ bandShell
1122
+ ],
1123
+ shade: plastic(view, shellRgb, 0.9)
1124
+ }, {
1125
+ parts: [
1126
+ bandPad
1127
+ ],
1128
+ shade: plastic(view, padRgb, 0.3)
1129
+ }, {
1130
+ parts: sliders,
1131
+ shade: g
1132
+ });
1133
+ const stitch = [];
1134
+ for(let i = 0; i <= 48; i++)stitch.push(along(cushionPath(0.04 + 0.92 * i / 48), Z, 0.034 * W + 0.001 * W));
1135
+ return {
1136
+ layers,
1137
+ pivots,
1138
+ pivotR: 0.017 * W,
1139
+ stitch,
1140
+ seats
1141
+ };
1142
+ }
1143
+ let lastPhones = null;
1144
+ function drawPhones(ctx, view, m, base, pass, bodyClip) {
1145
+ const key = `${view.m.join()}|${view.px}|${view.L.join()}|${base}`;
1146
+ if (!lastPhones || lastPhones.m !== m || lastPhones.key !== key) lastPhones = {
1147
+ m,
1148
+ key,
1149
+ ph: phonesOf(view, m, base)
1150
+ };
1151
+ const ph = lastPhones.ph;
1152
+ const W = m.right - m.left;
1153
+ const near = (d)=>pass === "front" ? d >= 0 : d < 0;
1154
+ if (pass === "front") {
1155
+ for (const s of ph.seats){
1156
+ if (view.depth(s.c) < 0) continue;
1157
+ const c = view.toScreen(s.c);
1158
+ contact(ctx, bodyClip, c[0], c[1] + 0.01 * W, 0.06 * W, s.ry * 1.05, 0.26);
1159
+ }
1160
+ }
1161
+ paintLayers(ctx, ph.layers.map((l)=>({
1162
+ ...l,
1163
+ keep: near
1164
+ })), view.px);
1165
+ for (const p of ph.pivots)if (near(view.depth(p))) sphereSprite(ctx, view, p, ph.pivotR, "silver");
1166
+ if (pass === "front" && W * view.px > 120 && view.dir([
1167
+ 0,
1168
+ 0,
1169
+ 1
1170
+ ])[2] > 0.3) {
1171
+ const pts = ph.stitch.map((p)=>view.toScreen(p));
1172
+ ctx.save();
1173
+ ctx.setLineDash([
1174
+ 0.022 * W,
1175
+ 0.014 * W
1176
+ ]);
1177
+ ctx.lineCap = "round";
1178
+ ctx.lineWidth = 0.0045 * W;
1179
+ ctx.strokeStyle = shade(base, 0.12);
1180
+ ctx.globalAlpha = 0.55;
1181
+ ctx.stroke(polyPath(pts, false));
1182
+ ctx.restore();
1183
+ }
1184
+ }
1185
+ function drawBowTie(ctx, view, r, m, base, bodyZ, bodyClip, face, fabricBow = false) {
1186
+ if (r.facing < 0.05) return;
1187
+ const W = m.right - m.left;
1188
+ const clear = face.y + 22 * face.scale;
1189
+ const cyB = Math.max(clear, m.bottom - 0.15 * W);
1190
+ const room = Math.max(0, m.bottom - 3 - cyB);
1191
+ const k = Math.min(W, 100) * Math.max(0.5, Math.min(1, room / 15));
1192
+ const zFront = bodyZ(50, cyB);
1193
+ const Q = (x, y, z)=>[
1194
+ 50 + x * k,
1195
+ cyB + y * k,
1196
+ zFront + z * k
1197
+ ];
1198
+ const rgb = rgbOf(base);
1199
+ const corners = [
1200
+ Q(-0.36, -0.18, 0),
1201
+ Q(0.36, 0.18, 0),
1202
+ Q(-0.36, 0.18, 0),
1203
+ Q(0.36, -0.18, 0)
1204
+ ].map((p)=>view.toScreen(p));
1205
+ const bb = bounds(corners);
1206
+ const sh = view.toScreen(Q(0.01, 0.08, 0));
1207
+ contact(ctx, bodyClip, sh[0], sh[1], bb.w * 0.52, bb.h * 0.5, 0.3);
1208
+ const nu = segs(view, 0.32 * k, 8, 16), nv = segs(view, 0.3 * k, 8, 16, 6);
1209
+ const at = `${zFront.toFixed(2)},${cyB.toFixed(2)},${k.toFixed(2)}`;
1210
+ const soft = !!fabricBow;
1211
+ const wingShade = soft ? felt(view, rgb) : satin(view, rgb);
1212
+ const layers = [];
1213
+ for (const dir of [
1214
+ -1,
1215
+ 1
1216
+ ]){
1217
+ const wing = (u, v)=>{
1218
+ const x = 0.058 + 0.3 * u - 0.035 * Math.pow(u, 4) * Math.sin(Math.PI * v);
1219
+ const h = 0.05 + 0.12 * Math.pow(u, 0.8);
1220
+ const y = -h * Math.cos(Math.PI * v) + 0.012 * u * u;
1221
+ const bulge = 0.018 + 0.058 * Math.sin(Math.PI * (0.12 + 0.88 * u));
1222
+ const fold = 0.016 * Math.sin(3 * Math.PI * v) * Math.pow(1 - u, 1.3);
1223
+ return Q(dir * x, y, (bulge + fold) * Math.sin(Math.PI * v) + 0.004);
1224
+ };
1225
+ layers.push({
1226
+ parts: [
1227
+ surface(view, `bow:wing${dir}@${at}`, wing, nu, nv, (u)=>Q(dir * (0.058 + 0.3 * u), 0, -0.03))
1228
+ ],
1229
+ shade: wingShade
1230
+ });
1231
+ layers.push({
1232
+ parts: [
1233
+ surface(view, `bow:end${dir}@${at}`, (s, t)=>{
1234
+ const p = wing(1, t);
1235
+ return [
1236
+ p[0],
1237
+ p[1],
1238
+ p[2] - (p[2] - zFront) * s
1239
+ ];
1240
+ }, 1, nv, Q(dir * 0.2, 0, -0.03))
1241
+ ],
1242
+ shade: wingShade
1243
+ });
1244
+ }
1245
+ const knot = surface(view, `bow:knot@${at}`, (u, v)=>{
1246
+ const x = 0.068 * (2 * u - 1) * (1 - 0.12 * Math.sin(Math.PI * v));
1247
+ const y = 0.075 * (2 * v - 1);
1248
+ const zx = Math.pow(Math.max(0, 1 - Math.pow(Math.abs(2 * u - 1), 3)), 0.5);
1249
+ const zy = Math.pow(Math.max(0, 1 - Math.pow(Math.abs(2 * v - 1), 5)), 0.4);
1250
+ return Q(x, y, 0.012 + 0.075 * zx * zy);
1251
+ }, 6, 6, Q(0, 0, -0.02));
1252
+ layers.push({
1253
+ parts: [
1254
+ knot
1255
+ ],
1256
+ shade: soft ? felt(view, rgb, 0.9) : satin(view, scale(rgb, 0.9))
1257
+ });
1258
+ if (!soft) {
1259
+ paintLayers(ctx, layers, view.px);
1260
+ return;
1261
+ }
1262
+ for(let i = 0; i < layers.length; i += 2){
1263
+ const part = layers.slice(i, i + 2);
1264
+ const outline = hull(paintLayers(ctx, part, view.px));
1265
+ grainOver(ctx, outline, 0.5, 0.1);
1266
+ fuzz(ctx, outline, base, 41 + i, 0.3, view.px);
1267
+ }
1268
+ }
1269
+ const FRAMES = {
1270
+ round: {
1271
+ rx: 10.8,
1272
+ ry: 10.8,
1273
+ n: 2,
1274
+ tr: 1.15,
1275
+ td: 1.2
1276
+ },
1277
+ square: {
1278
+ rx: 12,
1279
+ ry: 9.6,
1280
+ n: 4.2,
1281
+ tr: 1.45,
1282
+ td: 1.4
1283
+ },
1284
+ shades: {
1285
+ rx: 12.6,
1286
+ ry: 10,
1287
+ n: 3.4,
1288
+ tr: 1.6,
1289
+ td: 1.5
1290
+ }
1291
+ };
1292
+ export function drawGlasses(ctx, kind, eyes, light, pass, turn = {
1293
+ yaw: 0,
1294
+ pitch: 0
1295
+ }) {
1296
+ if (kind === "none") return;
1297
+ owner = FRAMES;
1298
+ const F = FRAMES[kind];
1299
+ const cy = Math.cos(turn.yaw), sy = Math.sin(turn.yaw), cp = Math.cos(turn.pitch), sp = Math.sin(turn.pitch);
1300
+ const { L: lightL, H: lightH } = lightOf(light.lx, light.ly);
1301
+ const view = makeView([
1302
+ cy,
1303
+ 0,
1304
+ sy,
1305
+ sp * sy,
1306
+ cp,
1307
+ -sp * cy,
1308
+ -cp * sy,
1309
+ sp,
1310
+ cp * cy
1311
+ ], [
1312
+ 0,
1313
+ 0,
1314
+ 0
1315
+ ], lightL, lightH, pxOf(ctx));
1316
+ const zPlane = Math.max(eyes[0].fz, eyes[1].fz) + 3.2;
1317
+ const zAt = (x)=>zPlane - 0.0045 * x * x;
1318
+ const e2 = 2 / F.n;
1319
+ const rimAt = (e, grow)=>(u)=>{
1320
+ const t = u * TAU;
1321
+ const x = e.ox + (F.rx + grow) * spow(Math.cos(t), e2), y = e.oy + (F.ry + grow) * spow(Math.sin(t), e2);
1322
+ return [
1323
+ x,
1324
+ y,
1325
+ zAt(x)
1326
+ ];
1327
+ };
1328
+ const ring = (e, grow, n = 48)=>{
1329
+ const f = rimAt(e, grow);
1330
+ const pts = [];
1331
+ for(let i = 0; i < n; i++)pts.push(view.toScreen(f(i / n)));
1332
+ return pts;
1333
+ };
1334
+ const vis = eyes.map((e)=>Math.max(0, Math.min(1, (e.z - 0.18) * 3.2)));
1335
+ if (pass === "shadow") {
1336
+ for(let i = 0; i < 2; i++){
1337
+ if (vis[i] <= 0) continue;
1338
+ ctx.save();
1339
+ ctx.globalAlpha = 0.22 * vis[i];
1340
+ ctx.translate(-light.lx * 1.6, -light.ly * 1.6 + 0.8);
1341
+ ctx.lineWidth = F.tr * 2 + 0.6;
1342
+ ctx.lineJoin = "round";
1343
+ ctx.strokeStyle = "#000";
1344
+ ctx.stroke(polyPath(ring(eyes[i], 0)));
1345
+ ctx.restore();
1346
+ }
1347
+ return;
1348
+ }
1349
+ const frame = [
1350
+ 22,
1351
+ 22,
1352
+ 26
1353
+ ];
1354
+ const rimShade = plastic(view, frame, 1.1);
1355
+ for(let i = 0; i < 2; i++){
1356
+ if (vis[i] <= 0) continue;
1357
+ const lens = polyPath(ring(eyes[i], -F.tr * 0.5));
1358
+ const b = bounds(ring(eyes[i], 0, 16));
1359
+ ctx.save();
1360
+ ctx.globalAlpha = vis[i];
1361
+ if (kind === "shades") {
1362
+ const g = ctx.createLinearGradient(b.cx, b.y0, b.cx, b.y1);
1363
+ g.addColorStop(0, "rgba(58,60,78,0.97)");
1364
+ g.addColorStop(0.55, "rgba(18,18,26,0.98)");
1365
+ g.addColorStop(1, "rgba(30,26,44,0.98)");
1366
+ ctx.fillStyle = g;
1367
+ } else {
1368
+ const g = ctx.createLinearGradient(b.cx, b.y0, b.cx, b.y1);
1369
+ g.addColorStop(0, "rgba(236,244,255,0.26)");
1370
+ g.addColorStop(0.45, "rgba(214,230,255,0.08)");
1371
+ g.addColorStop(1, "rgba(214,230,255,0.14)");
1372
+ ctx.fillStyle = g;
1373
+ }
1374
+ ctx.fill(lens);
1375
+ ctx.clip(lens);
1376
+ const w = b.w, h = b.h;
1377
+ const streak = (x0, width, a)=>{
1378
+ ctx.globalAlpha = vis[i] * a;
1379
+ ctx.fillStyle = "#fff";
1380
+ ctx.beginPath();
1381
+ ctx.moveTo(b.x0 + w * x0, b.y1 + h * 0.1);
1382
+ ctx.lineTo(b.x0 + w * (x0 + 0.55), b.y0 - h * 0.1);
1383
+ ctx.lineTo(b.x0 + w * (x0 + 0.55 + width), b.y0 - h * 0.1);
1384
+ ctx.lineTo(b.x0 + w * (x0 + width), b.y1 + h * 0.1);
1385
+ ctx.closePath();
1386
+ ctx.fill();
1387
+ };
1388
+ const k = kind === "shades" ? 1.4 : 1;
1389
+ streak(-0.28, 0.26, 0.16 * k);
1390
+ streak(0.06, 0.07, 0.3 * k);
1391
+ ctx.restore();
1392
+ }
1393
+ const nr = segs(view, TAU * F.rx * 1.1, 28, 56, 8);
1394
+ const rim = (e)=>tube(view, `rim:${kind}:${e.ox},${e.oy},${e.fz.toFixed(2)}`, rimAt(e, 0), [
1395
+ 0,
1396
+ 0,
1397
+ 1
1398
+ ], section(F.td, F.tr, 0.55), nr, 6, true);
1399
+ const [L, R] = eyes;
1400
+ const bridgeAt = (u)=>{
1401
+ const round = kind === "round";
1402
+ const a = [
1403
+ L.ox + F.rx * (round ? 0.9 : 0.98),
1404
+ L.oy - F.ry * (round ? 0.32 : 0.42),
1405
+ 0
1406
+ ];
1407
+ const b = [
1408
+ R.ox - F.rx * (round ? 0.9 : 0.98),
1409
+ R.oy - F.ry * (round ? 0.32 : 0.42),
1410
+ 0
1411
+ ];
1412
+ const c = [
1413
+ 0,
1414
+ (L.oy + R.oy) / 2 - F.ry * (round ? 0.8 : 0.6),
1415
+ 0
1416
+ ];
1417
+ const m1 = 1 - u;
1418
+ const x = m1 * m1 * a[0] + 2 * m1 * u * c[0] + u * u * b[0], y = m1 * m1 * a[1] + 2 * m1 * u * c[1] + u * u * b[1];
1419
+ return [
1420
+ x,
1421
+ y,
1422
+ zAt(x) + 0.3
1423
+ ];
1424
+ };
1425
+ const bridge = tube(view, `bridge:${kind}:${L.ox},${L.oy},${R.ox},${zPlane.toFixed(2)}`, bridgeAt, [
1426
+ 0,
1427
+ 0,
1428
+ 1
1429
+ ], section(F.td * 0.8, F.tr * 0.75, 0.7), 8, 6);
1430
+ const parts = [
1431
+ {
1432
+ part: rim(L),
1433
+ a: vis[0],
1434
+ d: view.depth([
1435
+ L.ox,
1436
+ L.oy,
1437
+ zPlane
1438
+ ])
1439
+ },
1440
+ {
1441
+ part: rim(R),
1442
+ a: vis[1],
1443
+ d: view.depth([
1444
+ R.ox,
1445
+ R.oy,
1446
+ zPlane
1447
+ ])
1448
+ },
1449
+ {
1450
+ part: bridge,
1451
+ a: Math.min(vis[0], vis[1]),
1452
+ d: view.depth([
1453
+ 0,
1454
+ L.oy,
1455
+ zPlane
1456
+ ])
1457
+ }
1458
+ ].sort((p, q)=>p.d - q.d);
1459
+ for (const p of parts){
1460
+ if (p.a <= 0) continue;
1461
+ ctx.save();
1462
+ ctx.globalAlpha = p.a;
1463
+ paintLayers(ctx, [
1464
+ {
1465
+ parts: [
1466
+ p.part
1467
+ ],
1468
+ shade: rimShade
1469
+ }
1470
+ ], view.px);
1471
+ ctx.restore();
1472
+ }
1473
+ for (const [e, s, a] of [
1474
+ [
1475
+ L,
1476
+ -1,
1477
+ vis[0]
1478
+ ],
1479
+ [
1480
+ R,
1481
+ 1,
1482
+ vis[1]
1483
+ ]
1484
+ ]){
1485
+ if (a <= 0) continue;
1486
+ ctx.save();
1487
+ ctx.globalAlpha = a;
1488
+ const x = e.ox + s * F.rx * 0.93;
1489
+ sphereSprite(ctx, view, [
1490
+ x,
1491
+ e.oy - F.ry * 0.5,
1492
+ zAt(x) + F.td
1493
+ ], 0.75, "silver");
1494
+ ctx.restore();
1495
+ }
1496
+ }
1497
+ export function drawWearBehind(ctx, path, r, wear) {
1498
+ if (!wear.headphones) return;
1499
+ const m = marksFor(path);
1500
+ if (!m) return;
1501
+ owner = m;
1502
+ drawPhones(ctx, viewOf(r, pxOf(ctx)), m, wear.color, "behind", null);
1503
+ }
1504
+ export function drawWearFront(ctx, path, r, wear, bodyClip, bodyZ, face) {
1505
+ const m = marksFor(path);
1506
+ if (!m) return;
1507
+ owner = m;
1508
+ const view = viewOf(r, pxOf(ctx));
1509
+ if (wear.bowTie) drawBowTie(ctx, view, r, m, wear.color, bodyZ, bodyClip, face, wear.fabric);
1510
+ if (wear.headphones) drawPhones(ctx, view, m, wear.color, "front", bodyClip);
1511
+ switch(wear.hat){
1512
+ case "beret":
1513
+ drawBeret(ctx, view, m, wear.color, bodyClip);
1514
+ break;
1515
+ case "beanie":
1516
+ drawBeanie(ctx, view, m, wear.color, bodyClip);
1517
+ break;
1518
+ case "party":
1519
+ drawParty(ctx, view, m, wear.color, bodyClip);
1520
+ break;
1521
+ case "crown":
1522
+ drawCrown(ctx, view, m, bodyClip);
1523
+ break;
1524
+ }
1525
+ }