decantr 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +868 -0
- package/CHANGELOG.md +255 -0
- package/CLAUDE.md +178 -0
- package/LICENSE +21 -0
- package/README.md +229 -0
- package/cli/art.js +127 -0
- package/cli/commands/a11y.js +61 -0
- package/cli/commands/audit.js +225 -0
- package/cli/commands/build.js +38 -0
- package/cli/commands/dev.js +18 -0
- package/cli/commands/doctor.js +197 -0
- package/cli/commands/figma-sync.js +48 -0
- package/cli/commands/figma-tokens.js +55 -0
- package/cli/commands/generate.js +26 -0
- package/cli/commands/init.js +116 -0
- package/cli/commands/lint.js +209 -0
- package/cli/commands/mcp.js +530 -0
- package/cli/commands/migrate.js +175 -0
- package/cli/commands/test.js +38 -0
- package/cli/commands/validate.js +354 -0
- package/cli/index.js +113 -0
- package/package.json +95 -0
- package/reference/atoms.md +517 -0
- package/reference/behaviors.md +384 -0
- package/reference/build-tooling.md +275 -0
- package/reference/color-guidelines.md +965 -0
- package/reference/component-lifecycle.md +137 -0
- package/reference/compound-spacing.md +95 -0
- package/reference/decantation-process.md +499 -0
- package/reference/dev-server-routes.md +93 -0
- package/reference/form-system.md +253 -0
- package/reference/i18n.md +336 -0
- package/reference/icons.md +576 -0
- package/reference/llm-primer.md +953 -0
- package/reference/plugins.md +252 -0
- package/reference/registry-consumption.md +76 -0
- package/reference/router.md +217 -0
- package/reference/shells.md +116 -0
- package/reference/spatial-guidelines.md +541 -0
- package/reference/ssr.md +234 -0
- package/reference/state-data.md +215 -0
- package/reference/state-patterns.md +166 -0
- package/reference/state.md +194 -0
- package/reference/style-system.md +110 -0
- package/reference/tokens.md +460 -0
- package/src/app.js +19 -0
- package/src/chart/_animate.js +266 -0
- package/src/chart/_base.js +109 -0
- package/src/chart/_data.js +209 -0
- package/src/chart/_format.js +106 -0
- package/src/chart/_interact.js +364 -0
- package/src/chart/_palette.js +105 -0
- package/src/chart/_renderer.js +52 -0
- package/src/chart/_scene.js +262 -0
- package/src/chart/_shared.js +371 -0
- package/src/chart/index.js +637 -0
- package/src/chart/layouts/_layout-base.js +328 -0
- package/src/chart/layouts/cartesian.js +148 -0
- package/src/chart/layouts/hierarchy.js +562 -0
- package/src/chart/layouts/polar.js +101 -0
- package/src/chart/renderers/canvas.js +179 -0
- package/src/chart/renderers/svg.js +256 -0
- package/src/chart/renderers/webgpu.js +715 -0
- package/src/chart/types/_type-base.js +26 -0
- package/src/chart/types/area.js +134 -0
- package/src/chart/types/bar.js +173 -0
- package/src/chart/types/box-plot.js +125 -0
- package/src/chart/types/bubble.js +63 -0
- package/src/chart/types/candlestick.js +115 -0
- package/src/chart/types/chord.js +85 -0
- package/src/chart/types/combination.js +108 -0
- package/src/chart/types/funnel.js +68 -0
- package/src/chart/types/gauge.js +163 -0
- package/src/chart/types/heatmap.js +98 -0
- package/src/chart/types/histogram.js +71 -0
- package/src/chart/types/line.js +111 -0
- package/src/chart/types/org-chart.js +93 -0
- package/src/chart/types/pie.js +81 -0
- package/src/chart/types/radar.js +96 -0
- package/src/chart/types/radial.js +68 -0
- package/src/chart/types/range-area.js +55 -0
- package/src/chart/types/range-bar.js +61 -0
- package/src/chart/types/sankey.js +73 -0
- package/src/chart/types/scatter.js +66 -0
- package/src/chart/types/sparkline.js +81 -0
- package/src/chart/types/sunburst.js +69 -0
- package/src/chart/types/swimlane.js +88 -0
- package/src/chart/types/treemap.js +62 -0
- package/src/chart/types/waterfall.js +100 -0
- package/src/components/_base.js +1658 -0
- package/src/components/_behaviors.js +1140 -0
- package/src/components/_primitives.js +534 -0
- package/src/components/_qr-encoder.js +539 -0
- package/src/components/accordion.js +207 -0
- package/src/components/affix.js +62 -0
- package/src/components/alert-dialog.js +75 -0
- package/src/components/alert.js +47 -0
- package/src/components/aspect-ratio.js +24 -0
- package/src/components/avatar-group.js +55 -0
- package/src/components/avatar.js +38 -0
- package/src/components/back-top.js +75 -0
- package/src/components/badge.js +74 -0
- package/src/components/banner.js +68 -0
- package/src/components/breadcrumb.js +162 -0
- package/src/components/button.js +115 -0
- package/src/components/calendar.js +131 -0
- package/src/components/card.js +192 -0
- package/src/components/carousel.js +98 -0
- package/src/components/cascader.js +261 -0
- package/src/components/checkbox.js +80 -0
- package/src/components/chip.js +81 -0
- package/src/components/code-block.js +82 -0
- package/src/components/collapsible.js +50 -0
- package/src/components/color-palette.js +438 -0
- package/src/components/color-picker.js +314 -0
- package/src/components/combobox.js +181 -0
- package/src/components/command.js +174 -0
- package/src/components/comment.js +206 -0
- package/src/components/context-menu.js +76 -0
- package/src/components/data-table.js +724 -0
- package/src/components/date-picker.js +217 -0
- package/src/components/date-range-picker.js +244 -0
- package/src/components/datetime-picker.js +271 -0
- package/src/components/descriptions.js +68 -0
- package/src/components/drawer.js +179 -0
- package/src/components/dropdown.js +88 -0
- package/src/components/empty.js +41 -0
- package/src/components/float-button.js +90 -0
- package/src/components/form.js +106 -0
- package/src/components/hover-card.js +49 -0
- package/src/components/icon.js +87 -0
- package/src/components/image.js +97 -0
- package/src/components/index.js +117 -0
- package/src/components/input-group.js +75 -0
- package/src/components/input-number.js +155 -0
- package/src/components/input-otp.js +178 -0
- package/src/components/input.js +91 -0
- package/src/components/kbd.js +36 -0
- package/src/components/label.js +25 -0
- package/src/components/list.js +118 -0
- package/src/components/masked-input.js +236 -0
- package/src/components/mentions.js +165 -0
- package/src/components/menu.js +259 -0
- package/src/components/message.js +80 -0
- package/src/components/modal.js +147 -0
- package/src/components/navigation-menu.js +166 -0
- package/src/components/notification.js +84 -0
- package/src/components/pagination.js +104 -0
- package/src/components/placeholder.js +132 -0
- package/src/components/popconfirm.js +70 -0
- package/src/components/popover.js +58 -0
- package/src/components/progress.js +61 -0
- package/src/components/qrcode.js +251 -0
- package/src/components/radiogroup.js +120 -0
- package/src/components/range-slider.js +176 -0
- package/src/components/rate.js +186 -0
- package/src/components/resizable.js +83 -0
- package/src/components/result.js +57 -0
- package/src/components/scroll-area.js +43 -0
- package/src/components/segmented.js +97 -0
- package/src/components/select.js +165 -0
- package/src/components/separator.js +31 -0
- package/src/components/shell.js +407 -0
- package/src/components/skeleton.js +39 -0
- package/src/components/slider.js +141 -0
- package/src/components/sortable-list.js +176 -0
- package/src/components/space.js +42 -0
- package/src/components/spinner.js +112 -0
- package/src/components/splitter.js +147 -0
- package/src/components/statistic.js +136 -0
- package/src/components/steps.js +99 -0
- package/src/components/switch.js +95 -0
- package/src/components/table.js +44 -0
- package/src/components/tabs.js +216 -0
- package/src/components/tag.js +115 -0
- package/src/components/textarea.js +82 -0
- package/src/components/time-picker.js +153 -0
- package/src/components/time-range-picker.js +170 -0
- package/src/components/timeline.js +226 -0
- package/src/components/toast.js +71 -0
- package/src/components/toggle.js +213 -0
- package/src/components/tooltip.js +57 -0
- package/src/components/tour.js +159 -0
- package/src/components/transfer.js +163 -0
- package/src/components/tree-select.js +274 -0
- package/src/components/tree.js +141 -0
- package/src/components/typography.js +136 -0
- package/src/components/upload.js +118 -0
- package/src/components/visually-hidden.js +20 -0
- package/src/components/watermark.js +124 -0
- package/src/core/index.js +539 -0
- package/src/core/lifecycle.js +69 -0
- package/src/css/atoms.js +651 -0
- package/src/css/components.js +940 -0
- package/src/css/derive.js +1296 -0
- package/src/css/index.js +265 -0
- package/src/css/runtime.js +268 -0
- package/src/css/styles/addons/bioluminescent.js +93 -0
- package/src/css/styles/addons/clay.js +70 -0
- package/src/css/styles/addons/clean.js +57 -0
- package/src/css/styles/addons/command-center.js +143 -0
- package/src/css/styles/addons/dopamine.js +83 -0
- package/src/css/styles/addons/editorial.js +80 -0
- package/src/css/styles/addons/glassmorphism.js +99 -0
- package/src/css/styles/addons/liquid-glass.js +105 -0
- package/src/css/styles/addons/prismatic.js +100 -0
- package/src/css/styles/addons/retro.js +63 -0
- package/src/css/styles/auradecantism.js +96 -0
- package/src/css/theme-registry.js +444 -0
- package/src/data/entity.js +281 -0
- package/src/data/index.js +13 -0
- package/src/data/persist.js +225 -0
- package/src/data/query.js +839 -0
- package/src/data/realtime.js +299 -0
- package/src/data/url.js +177 -0
- package/src/data/worker.js +134 -0
- package/src/explorer/archetypes.js +243 -0
- package/src/explorer/atoms.js +228 -0
- package/src/explorer/charts.js +497 -0
- package/src/explorer/components.js +129 -0
- package/src/explorer/foundations.js +949 -0
- package/src/explorer/icons.js +178 -0
- package/src/explorer/patterns.js +247 -0
- package/src/explorer/recipes.js +194 -0
- package/src/explorer/shared/pattern-examples.js +1337 -0
- package/src/explorer/shared/showcase-renderer.js +958 -0
- package/src/explorer/shared/spec-table.js +41 -0
- package/src/explorer/shared/usage-links.js +87 -0
- package/src/explorer/shell-config.js +10 -0
- package/src/explorer/shells.js +551 -0
- package/src/explorer/styles.js +161 -0
- package/src/explorer/tokens.js +262 -0
- package/src/explorer/tools.js +525 -0
- package/src/form/index.js +804 -0
- package/src/i18n/index.js +251 -0
- package/src/icons/essential.js +479 -0
- package/src/icons/index.js +53 -0
- package/src/plugins/index.js +282 -0
- package/src/registry/archetypes/content-site.json +71 -0
- package/src/registry/archetypes/docs-explorer.json +23 -0
- package/src/registry/archetypes/ecommerce.json +104 -0
- package/src/registry/archetypes/financial-dashboard.json +77 -0
- package/src/registry/archetypes/index.json +41 -0
- package/src/registry/archetypes/portfolio.json +82 -0
- package/src/registry/archetypes/recipe-community.json +159 -0
- package/src/registry/archetypes/saas-dashboard.json +86 -0
- package/src/registry/architect/cross-cutting.json +45 -0
- package/src/registry/architect/domains/ecommerce.json +294 -0
- package/src/registry/architect/domains/financial-services.json +302 -0
- package/src/registry/architect/index.json +26 -0
- package/src/registry/architect/traits.json +379 -0
- package/src/registry/atoms.json +16 -0
- package/src/registry/chart-showcase.json +160 -0
- package/src/registry/chart.json +136 -0
- package/src/registry/components.json +8616 -0
- package/src/registry/core.json +216 -0
- package/src/registry/css.json +319 -0
- package/src/registry/data.json +135 -0
- package/src/registry/foundations.json +11 -0
- package/src/registry/icons.json +463 -0
- package/src/registry/index.json +101 -0
- package/src/registry/patterns/activity-feed.json +37 -0
- package/src/registry/patterns/article-content.json +27 -0
- package/src/registry/patterns/auth-form.json +37 -0
- package/src/registry/patterns/author-card.json +20 -0
- package/src/registry/patterns/card-grid.json +127 -0
- package/src/registry/patterns/category-nav.json +26 -0
- package/src/registry/patterns/chart-grid.json +36 -0
- package/src/registry/patterns/chat-interface.json +37 -0
- package/src/registry/patterns/checklist-card.json +55 -0
- package/src/registry/patterns/comparison-panel.json +27 -0
- package/src/registry/patterns/component-showcase.json +24 -0
- package/src/registry/patterns/contact-form.json +31 -0
- package/src/registry/patterns/cta-section.json +20 -0
- package/src/registry/patterns/data-table.json +37 -0
- package/src/registry/patterns/detail-header.json +83 -0
- package/src/registry/patterns/detail-panel.json +27 -0
- package/src/registry/patterns/explorer-shell.json +22 -0
- package/src/registry/patterns/filter-bar.json +33 -0
- package/src/registry/patterns/filter-sidebar.json +27 -0
- package/src/registry/patterns/form-sections.json +110 -0
- package/src/registry/patterns/goal-tracker.json +27 -0
- package/src/registry/patterns/hero.json +107 -0
- package/src/registry/patterns/index.json +47 -0
- package/src/registry/patterns/kpi-grid.json +36 -0
- package/src/registry/patterns/media-gallery.json +20 -0
- package/src/registry/patterns/order-history.json +20 -0
- package/src/registry/patterns/pagination.json +19 -0
- package/src/registry/patterns/photo-to-recipe.json +36 -0
- package/src/registry/patterns/pipeline-tracker.json +28 -0
- package/src/registry/patterns/post-list.json +27 -0
- package/src/registry/patterns/pricing-table.json +32 -0
- package/src/registry/patterns/scorecard.json +28 -0
- package/src/registry/patterns/search-bar.json +20 -0
- package/src/registry/patterns/specimen-grid.json +19 -0
- package/src/registry/patterns/stat-card.json +55 -0
- package/src/registry/patterns/stats-bar.json +55 -0
- package/src/registry/patterns/steps-card.json +55 -0
- package/src/registry/patterns/table-of-contents.json +19 -0
- package/src/registry/patterns/testimonials.json +21 -0
- package/src/registry/patterns/timeline.json +27 -0
- package/src/registry/patterns/token-inspector.json +21 -0
- package/src/registry/patterns/wizard.json +27 -0
- package/src/registry/recipe-auradecantism.json +69 -0
- package/src/registry/recipe-clean.json +65 -0
- package/src/registry/recipe-command-center.json +78 -0
- package/src/registry/router.json +73 -0
- package/src/registry/schema/README.md +197 -0
- package/src/registry/skeletons.json +259 -0
- package/src/registry/state.json +137 -0
- package/src/registry/tokens.json +40 -0
- package/src/router/hash.js +17 -0
- package/src/router/history.js +18 -0
- package/src/router/index.js +598 -0
- package/src/ssr/index.js +922 -0
- package/src/state/arrays.js +181 -0
- package/src/state/devtools.js +647 -0
- package/src/state/index.js +498 -0
- package/src/state/middleware.js +288 -0
- package/src/state/scheduler.js +206 -0
- package/src/state/store.js +300 -0
- package/src/tags/index.js +19 -0
- package/src/tannins/auth.js +396 -0
- package/src/test/dom.js +352 -0
- package/src/test/index.js +62 -0
- package/src/test/state.js +306 -0
- package/tools/a11y-audit.js +487 -0
- package/tools/analyzer.js +315 -0
- package/tools/audit.js +706 -0
- package/tools/builder.js +1422 -0
- package/tools/css-extract.js +188 -0
- package/tools/dev-server.js +316 -0
- package/tools/dts-gen.js +1260 -0
- package/tools/figma-components.js +329 -0
- package/tools/figma-patterns.js +516 -0
- package/tools/figma-plugin/code.js +453 -0
- package/tools/figma-plugin/manifest.json +14 -0
- package/tools/figma-plugin/ui.html +268 -0
- package/tools/figma-render.js +293 -0
- package/tools/figma-tokens.js +712 -0
- package/tools/figma-upload.js +318 -0
- package/tools/generate.js +738 -0
- package/tools/icons.js +133 -0
- package/tools/init-templates.js +265 -0
- package/tools/install-hooks.sh +5 -0
- package/tools/migrations/0.5.0.js +53 -0
- package/tools/migrations/0.6.0.js +95 -0
- package/tools/minify.js +170 -0
- package/tools/pre-commit +4 -0
- package/tools/registry.js +662 -0
- package/tools/reset-playground.js +61 -0
- package/tools/starter-templates/content-site/app.js +49 -0
- package/tools/starter-templates/content-site/essence.js +19 -0
- package/tools/starter-templates/content-site/pages.js +31 -0
- package/tools/starter-templates/ecommerce/app.js +50 -0
- package/tools/starter-templates/ecommerce/essence.js +19 -0
- package/tools/starter-templates/ecommerce/pages.js +31 -0
- package/tools/starter-templates/landing-page/app.js +38 -0
- package/tools/starter-templates/landing-page/essence.js +18 -0
- package/tools/starter-templates/landing-page/pages.js +21 -0
- package/tools/starter-templates/portfolio/app.js +45 -0
- package/tools/starter-templates/portfolio/essence.js +19 -0
- package/tools/starter-templates/portfolio/pages.js +33 -0
- package/tools/starter-templates/saas-dashboard/app.js +70 -0
- package/tools/starter-templates/saas-dashboard/essence.js +19 -0
- package/tools/starter-templates/saas-dashboard/pages.js +31 -0
- package/tools/verify-pack.js +203 -0
- package/types/chart.d.ts +77 -0
- package/types/components.d.ts +587 -0
- package/types/core.d.ts +89 -0
- package/types/css.d.ts +149 -0
- package/types/data.d.ts +238 -0
- package/types/form.d.ts +164 -0
- package/types/i18n.d.ts +51 -0
- package/types/icons.d.ts +27 -0
- package/types/index.d.ts +13 -0
- package/types/router.d.ts +116 -0
- package/types/ssr.d.ts +102 -0
- package/types/state.d.ts +83 -0
- package/types/tags.d.ts +62 -0
- package/types/tannins.d.ts +63 -0
- package/types/test.d.ts +48 -0
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebGPU Renderer — GPU-accelerated rendering for 50K+ data point charts.
|
|
3
|
+
* Instanced circles (scatter/bubble), batched rects (bar/heatmap),
|
|
4
|
+
* tessellated lines (line charts). Text + complex paths fall back to Canvas 2D overlay.
|
|
5
|
+
* @module renderers/webgpu
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { arcToPath } from '../_scene.js';
|
|
9
|
+
import { renderCanvas } from './canvas.js';
|
|
10
|
+
|
|
11
|
+
// --- WGSL Shaders ---
|
|
12
|
+
|
|
13
|
+
const CIRCLE_SHADER = /* wgsl */`
|
|
14
|
+
struct Uniforms { resolution: vec2f, dpr: f32 }
|
|
15
|
+
@group(0) @binding(0) var<uniform> u: Uniforms;
|
|
16
|
+
|
|
17
|
+
struct Instance {
|
|
18
|
+
@location(0) pos: vec2f,
|
|
19
|
+
@location(1) radius: f32,
|
|
20
|
+
@location(2) fillColor: vec4f,
|
|
21
|
+
@location(3) strokeColor: vec4f,
|
|
22
|
+
@location(4) strokeWidth: f32,
|
|
23
|
+
@location(5) opacity: f32,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
struct VSOut {
|
|
27
|
+
@builtin(position) pos: vec4f,
|
|
28
|
+
@location(0) uv: vec2f,
|
|
29
|
+
@location(1) fillColor: vec4f,
|
|
30
|
+
@location(2) strokeColor: vec4f,
|
|
31
|
+
@location(3) strokeWidth: f32,
|
|
32
|
+
@location(4) radius: f32,
|
|
33
|
+
@location(5) opacity: f32,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@vertex fn vs(@builtin(vertex_index) vi: u32, inst: Instance) -> VSOut {
|
|
37
|
+
let corners = array<vec2f, 6>(
|
|
38
|
+
vec2f(-1,-1), vec2f(1,-1), vec2f(-1,1),
|
|
39
|
+
vec2f(-1,1), vec2f(1,-1), vec2f(1,1)
|
|
40
|
+
);
|
|
41
|
+
let uv = corners[vi];
|
|
42
|
+
let extent = inst.radius + inst.strokeWidth + 1.0;
|
|
43
|
+
let pixel = inst.pos + uv * extent;
|
|
44
|
+
let ndc = vec2f(
|
|
45
|
+
pixel.x / u.resolution.x * 2.0 - 1.0,
|
|
46
|
+
1.0 - pixel.y / u.resolution.y * 2.0
|
|
47
|
+
);
|
|
48
|
+
var o: VSOut;
|
|
49
|
+
o.pos = vec4f(ndc, 0.0, 1.0);
|
|
50
|
+
o.uv = uv * extent;
|
|
51
|
+
o.fillColor = inst.fillColor;
|
|
52
|
+
o.strokeColor = inst.strokeColor;
|
|
53
|
+
o.strokeWidth = inst.strokeWidth;
|
|
54
|
+
o.radius = inst.radius;
|
|
55
|
+
o.opacity = inst.opacity;
|
|
56
|
+
return o;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@fragment fn fs(v: VSOut) -> @location(0) vec4f {
|
|
60
|
+
let dist = length(v.uv);
|
|
61
|
+
let aa = 1.0 / v.radius;
|
|
62
|
+
if dist > v.radius + v.strokeWidth + 1.0 { discard; }
|
|
63
|
+
let fillEdge = smoothstep(v.radius, v.radius - aa, dist);
|
|
64
|
+
let strokeOuter = smoothstep(v.radius + v.strokeWidth, v.radius + v.strokeWidth - aa, dist);
|
|
65
|
+
let strokeInner = 1.0 - fillEdge;
|
|
66
|
+
let strokeAlpha = strokeOuter * strokeInner;
|
|
67
|
+
let color = v.fillColor * fillEdge + v.strokeColor * strokeAlpha;
|
|
68
|
+
return vec4f(color.rgb, color.a * v.opacity);
|
|
69
|
+
}`;
|
|
70
|
+
|
|
71
|
+
const RECT_SHADER = /* wgsl */`
|
|
72
|
+
struct Uniforms { resolution: vec2f, dpr: f32 }
|
|
73
|
+
@group(0) @binding(0) var<uniform> u: Uniforms;
|
|
74
|
+
|
|
75
|
+
struct Instance {
|
|
76
|
+
@location(0) posSize: vec4f,
|
|
77
|
+
@location(1) fillColor: vec4f,
|
|
78
|
+
@location(2) strokeColor: vec4f,
|
|
79
|
+
@location(3) strokeWidth: f32,
|
|
80
|
+
@location(4) cornerRadius: f32,
|
|
81
|
+
@location(5) opacity: f32,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
struct VSOut {
|
|
85
|
+
@builtin(position) pos: vec4f,
|
|
86
|
+
@location(0) localPos: vec2f,
|
|
87
|
+
@location(1) size: vec2f,
|
|
88
|
+
@location(2) fillColor: vec4f,
|
|
89
|
+
@location(3) strokeColor: vec4f,
|
|
90
|
+
@location(4) strokeWidth: f32,
|
|
91
|
+
@location(5) cornerRadius: f32,
|
|
92
|
+
@location(6) opacity: f32,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@vertex fn vs(@builtin(vertex_index) vi: u32, inst: Instance) -> VSOut {
|
|
96
|
+
let corners = array<vec2f, 6>(
|
|
97
|
+
vec2f(0,0), vec2f(1,0), vec2f(0,1),
|
|
98
|
+
vec2f(0,1), vec2f(1,0), vec2f(1,1)
|
|
99
|
+
);
|
|
100
|
+
let c = corners[vi];
|
|
101
|
+
let pixel = inst.posSize.xy + c * inst.posSize.zw;
|
|
102
|
+
let ndc = vec2f(
|
|
103
|
+
pixel.x / u.resolution.x * 2.0 - 1.0,
|
|
104
|
+
1.0 - pixel.y / u.resolution.y * 2.0
|
|
105
|
+
);
|
|
106
|
+
var o: VSOut;
|
|
107
|
+
o.pos = vec4f(ndc, 0.0, 1.0);
|
|
108
|
+
o.localPos = c * inst.posSize.zw;
|
|
109
|
+
o.size = inst.posSize.zw;
|
|
110
|
+
o.fillColor = inst.fillColor;
|
|
111
|
+
o.strokeColor = inst.strokeColor;
|
|
112
|
+
o.strokeWidth = inst.strokeWidth;
|
|
113
|
+
o.cornerRadius = inst.cornerRadius;
|
|
114
|
+
o.opacity = inst.opacity;
|
|
115
|
+
return o;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
fn roundedRectSDF(p: vec2f, size: vec2f, r: f32) -> f32 {
|
|
119
|
+
let half = size * 0.5;
|
|
120
|
+
let q = abs(p - half) - half + vec2f(r);
|
|
121
|
+
return min(max(q.x, q.y), 0.0) + length(max(q, vec2f(0.0))) - r;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@fragment fn fs(v: VSOut) -> @location(0) vec4f {
|
|
125
|
+
let r = min(v.cornerRadius, min(v.size.x, v.size.y) * 0.5);
|
|
126
|
+
let dist = roundedRectSDF(v.localPos, v.size, r);
|
|
127
|
+
let aa = 0.7;
|
|
128
|
+
let fillAlpha = smoothstep(aa, -aa, dist);
|
|
129
|
+
let strokeAlpha = smoothstep(aa, -aa, dist) - smoothstep(-v.strokeWidth + aa, -v.strokeWidth - aa, dist);
|
|
130
|
+
let hasFill = step(0.001, v.fillColor.a);
|
|
131
|
+
let hasStroke = step(0.001, v.strokeColor.a) * step(0.001, v.strokeWidth);
|
|
132
|
+
let color = v.fillColor * fillAlpha * hasFill + v.strokeColor * strokeAlpha * hasStroke;
|
|
133
|
+
if color.a < 0.001 { discard; }
|
|
134
|
+
return vec4f(color.rgb, color.a * v.opacity);
|
|
135
|
+
}`;
|
|
136
|
+
|
|
137
|
+
const LINE_SHADER = /* wgsl */`
|
|
138
|
+
struct Uniforms { resolution: vec2f, dpr: f32 }
|
|
139
|
+
@group(0) @binding(0) var<uniform> u: Uniforms;
|
|
140
|
+
|
|
141
|
+
struct Vert {
|
|
142
|
+
@location(0) position: vec2f,
|
|
143
|
+
@location(1) color: vec4f,
|
|
144
|
+
@location(2) opacity: f32,
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
struct VSOut {
|
|
148
|
+
@builtin(position) pos: vec4f,
|
|
149
|
+
@location(0) color: vec4f,
|
|
150
|
+
@location(1) opacity: f32,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
@vertex fn vs(v: Vert) -> VSOut {
|
|
154
|
+
let ndc = vec2f(
|
|
155
|
+
v.position.x / u.resolution.x * 2.0 - 1.0,
|
|
156
|
+
1.0 - v.position.y / u.resolution.y * 2.0
|
|
157
|
+
);
|
|
158
|
+
var o: VSOut;
|
|
159
|
+
o.pos = vec4f(ndc, 0.0, 1.0);
|
|
160
|
+
o.color = v.color;
|
|
161
|
+
o.opacity = v.opacity;
|
|
162
|
+
return o;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@fragment fn fs(v: VSOut) -> @location(0) vec4f {
|
|
166
|
+
return vec4f(v.color.rgb, v.color.a * v.opacity);
|
|
167
|
+
}`;
|
|
168
|
+
|
|
169
|
+
// --- Color utilities ---
|
|
170
|
+
|
|
171
|
+
function resolveColor(color) {
|
|
172
|
+
if (!color || color === 'none') return null;
|
|
173
|
+
if (color.startsWith('var(')) {
|
|
174
|
+
const prop = color.match(/var\((--[^,)]+)/)?.[1];
|
|
175
|
+
if (prop && typeof getComputedStyle === 'function') {
|
|
176
|
+
const resolved = getComputedStyle(document.documentElement).getPropertyValue(prop).trim();
|
|
177
|
+
if (resolved) return resolved;
|
|
178
|
+
}
|
|
179
|
+
const fallback = color.match(/,\s*([^)]+)\)/)?.[1];
|
|
180
|
+
return fallback || '#666';
|
|
181
|
+
}
|
|
182
|
+
return color;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function parseColorToVec4(colorStr) {
|
|
186
|
+
const c = resolveColor(colorStr);
|
|
187
|
+
if (!c) return [0, 0, 0, 0];
|
|
188
|
+
if (c.startsWith('#')) {
|
|
189
|
+
const hex = c.length === 4
|
|
190
|
+
? c[1]+c[1]+c[2]+c[2]+c[3]+c[3]
|
|
191
|
+
: c.length === 9 ? c.slice(1, 7) : c.slice(1);
|
|
192
|
+
const n = parseInt(hex.slice(0, 6), 16);
|
|
193
|
+
const a = hex.length === 8 ? parseInt(hex.slice(6), 16) / 255 : 1;
|
|
194
|
+
return [(n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255, a];
|
|
195
|
+
}
|
|
196
|
+
const m = c.match(/rgba?\((\d+),?\s*(\d+),?\s*(\d+)(?:[,/]\s*([\d.]+))?\)/);
|
|
197
|
+
if (m) return [+m[1]/255, +m[2]/255, +m[3]/255, m[4] != null ? +m[4] : 1];
|
|
198
|
+
const named = { red:[1,0,0,1], blue:[0,0,1,1], green:[0,.5,0,1], white:[1,1,1,1],
|
|
199
|
+
black:[0,0,0,1], gray:[.5,.5,.5,1], grey:[.5,.5,.5,1], orange:[1,.65,0,1],
|
|
200
|
+
yellow:[1,1,0,1], purple:[.5,0,.5,1], cyan:[0,1,1,1], magenta:[1,0,1,1],
|
|
201
|
+
transparent:[0,0,0,0] };
|
|
202
|
+
return named[c.toLowerCase()] || [.4,.4,.4,1];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// --- Scene graph traversal & batching ---
|
|
206
|
+
|
|
207
|
+
function flattenScene(node, batches, tx, ty, opacity) {
|
|
208
|
+
if (!node) return;
|
|
209
|
+
const op = (node.opacity != null ? node.opacity : 1) * opacity;
|
|
210
|
+
|
|
211
|
+
switch (node.type) {
|
|
212
|
+
case 'group': {
|
|
213
|
+
let gx = tx, gy = ty;
|
|
214
|
+
if (node.transform) {
|
|
215
|
+
const m = node.transform.match(/translate\(([^,]+),([^)]+)\)/);
|
|
216
|
+
if (m) { gx += +m[1]; gy += +m[2]; }
|
|
217
|
+
}
|
|
218
|
+
if (node.children) {
|
|
219
|
+
for (const child of node.children) flattenScene(child, batches, gx, gy, op);
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
case 'circle':
|
|
224
|
+
batches.circles.push({ cx: node.cx + tx, cy: node.cy + ty, r: node.r,
|
|
225
|
+
fill: node.fill, stroke: node.stroke, strokeWidth: node.strokeWidth || 0, opacity: op });
|
|
226
|
+
break;
|
|
227
|
+
case 'rect':
|
|
228
|
+
batches.rects.push({ x: node.x + tx, y: node.y + ty, w: node.w, h: node.h,
|
|
229
|
+
rx: node.rx || 0, fill: node.fill, stroke: node.stroke,
|
|
230
|
+
strokeWidth: node.strokeWidth || 0, opacity: op });
|
|
231
|
+
break;
|
|
232
|
+
case 'line':
|
|
233
|
+
batches.lines.push({ x1: node.x1 + tx, y1: node.y1 + ty,
|
|
234
|
+
x2: node.x2 + tx, y2: node.y2 + ty, stroke: node.stroke,
|
|
235
|
+
strokeWidth: node.strokeWidth || 1, strokeDash: node.strokeDash, opacity: op });
|
|
236
|
+
break;
|
|
237
|
+
case 'text':
|
|
238
|
+
batches.overlay.push({ ...node, x: node.x + tx, y: node.y + ty, opacity: op });
|
|
239
|
+
break;
|
|
240
|
+
case 'path':
|
|
241
|
+
batches.overlay.push({ ...node, _tx: tx, _ty: ty, opacity: op });
|
|
242
|
+
break;
|
|
243
|
+
case 'arc':
|
|
244
|
+
batches.overlay.push({ ...node, cx: node.cx + tx, cy: node.cy + ty, opacity: op });
|
|
245
|
+
break;
|
|
246
|
+
case 'polygon':
|
|
247
|
+
batches.overlay.push({ ...node,
|
|
248
|
+
points: node.points?.map(p => ({ x: p.x + tx, y: p.y + ty })), opacity: op });
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// --- Tessellate lines into triangle strips ---
|
|
254
|
+
|
|
255
|
+
function tessellateLines(lines, dpr) {
|
|
256
|
+
const verts = [];
|
|
257
|
+
for (const ln of lines) {
|
|
258
|
+
const dx = ln.x2 - ln.x1, dy = ln.y2 - ln.y1;
|
|
259
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
260
|
+
if (len < 0.001) continue;
|
|
261
|
+
const hw = (ln.strokeWidth || 1) * 0.5 * dpr;
|
|
262
|
+
const nx = -dy / len * hw, ny = dx / len * hw;
|
|
263
|
+
const col = parseColorToVec4(ln.stroke || 'var(--d-border)');
|
|
264
|
+
const op = ln.opacity;
|
|
265
|
+
const x1 = ln.x1 * dpr, y1 = ln.y1 * dpr, x2 = ln.x2 * dpr, y2 = ln.y2 * dpr;
|
|
266
|
+
// Two triangles forming the line quad
|
|
267
|
+
verts.push(
|
|
268
|
+
x1 + nx, y1 + ny, ...col, op,
|
|
269
|
+
x1 - nx, y1 - ny, ...col, op,
|
|
270
|
+
x2 + nx, y2 + ny, ...col, op,
|
|
271
|
+
x2 + nx, y2 + ny, ...col, op,
|
|
272
|
+
x1 - nx, y1 - ny, ...col, op,
|
|
273
|
+
x2 - nx, y2 - ny, ...col, op
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
return new Float32Array(verts);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// --- Canvas 2D overlay for text, arcs, paths, polygons, dashed lines ---
|
|
280
|
+
|
|
281
|
+
function renderOverlay(ctx, nodes, dashedLines) {
|
|
282
|
+
for (const node of dashedLines) {
|
|
283
|
+
ctx.save();
|
|
284
|
+
if (node.opacity != null) ctx.globalAlpha = node.opacity;
|
|
285
|
+
const stroke = resolveColor(node.stroke) || resolveColor('var(--d-border)') || '#ccc';
|
|
286
|
+
ctx.beginPath();
|
|
287
|
+
ctx.moveTo(node.x1, node.y1);
|
|
288
|
+
ctx.lineTo(node.x2, node.y2);
|
|
289
|
+
ctx.strokeStyle = stroke;
|
|
290
|
+
ctx.lineWidth = node.strokeWidth || 1;
|
|
291
|
+
if (node.strokeDash) ctx.setLineDash(node.strokeDash.split(',').map(Number));
|
|
292
|
+
ctx.stroke();
|
|
293
|
+
ctx.setLineDash([]);
|
|
294
|
+
ctx.restore();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
for (const node of nodes) {
|
|
298
|
+
ctx.save();
|
|
299
|
+
if (node.opacity != null) ctx.globalAlpha = node.opacity;
|
|
300
|
+
|
|
301
|
+
switch (node.type) {
|
|
302
|
+
case 'text': {
|
|
303
|
+
const fill = resolveColor(node.fill) || resolveColor('var(--d-muted)') || '#666';
|
|
304
|
+
ctx.fillStyle = fill;
|
|
305
|
+
const fontFamily = typeof getComputedStyle === 'function'
|
|
306
|
+
? getComputedStyle(document.documentElement).getPropertyValue('--d-font').trim() || 'sans-serif'
|
|
307
|
+
: 'sans-serif';
|
|
308
|
+
ctx.font = `${node.fontWeight || ''} ${node.fontSize || '10px'} ${fontFamily}`.trim();
|
|
309
|
+
ctx.textAlign = node.anchor === 'middle' ? 'center' : (node.anchor === 'end' ? 'right' : 'left');
|
|
310
|
+
ctx.textBaseline = node.baseline === 'middle' ? 'middle' : 'alphabetic';
|
|
311
|
+
if (node.rotate) {
|
|
312
|
+
ctx.translate(node.x, node.y);
|
|
313
|
+
ctx.rotate(node.rotate * Math.PI / 180);
|
|
314
|
+
ctx.fillText(node.content || '', 0, 0);
|
|
315
|
+
} else {
|
|
316
|
+
ctx.fillText(node.content || '', node.x, node.y);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case 'path': {
|
|
321
|
+
if (!node.d) break;
|
|
322
|
+
ctx.save();
|
|
323
|
+
if (node._tx || node._ty) ctx.translate(node._tx || 0, node._ty || 0);
|
|
324
|
+
const p = new Path2D(node.d);
|
|
325
|
+
const fill = resolveColor(node.fill);
|
|
326
|
+
const stroke = resolveColor(node.stroke);
|
|
327
|
+
if (fill) { ctx.fillStyle = fill; ctx.fill(p); }
|
|
328
|
+
if (stroke) {
|
|
329
|
+
ctx.strokeStyle = stroke;
|
|
330
|
+
ctx.lineWidth = node.strokeWidth || 1;
|
|
331
|
+
if (node.strokeLinecap) ctx.lineCap = node.strokeLinecap;
|
|
332
|
+
if (node.strokeLinejoin) ctx.lineJoin = node.strokeLinejoin;
|
|
333
|
+
if (node.strokeDash) ctx.setLineDash(node.strokeDash.split(',').map(Number));
|
|
334
|
+
ctx.stroke(p);
|
|
335
|
+
ctx.setLineDash([]);
|
|
336
|
+
}
|
|
337
|
+
ctx.restore();
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
case 'arc': {
|
|
341
|
+
const d = arcToPath(node.cx, node.cy, node.outerR, node.innerR || 0, node.startAngle, node.endAngle);
|
|
342
|
+
const p = new Path2D(d);
|
|
343
|
+
const fill = resolveColor(node.fill);
|
|
344
|
+
if (fill) { ctx.fillStyle = fill; ctx.fill(p); }
|
|
345
|
+
if (node.stroke) {
|
|
346
|
+
ctx.strokeStyle = resolveColor(node.stroke);
|
|
347
|
+
ctx.lineWidth = node.strokeWidth || 1;
|
|
348
|
+
ctx.stroke(p);
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
case 'polygon': {
|
|
353
|
+
if (!node.points?.length) break;
|
|
354
|
+
ctx.beginPath();
|
|
355
|
+
ctx.moveTo(node.points[0].x, node.points[0].y);
|
|
356
|
+
for (let i = 1; i < node.points.length; i++) ctx.lineTo(node.points[i].x, node.points[i].y);
|
|
357
|
+
ctx.closePath();
|
|
358
|
+
const fill = resolveColor(node.fill);
|
|
359
|
+
if (fill) { ctx.fillStyle = fill; ctx.fill(); }
|
|
360
|
+
if (node.stroke) {
|
|
361
|
+
ctx.strokeStyle = resolveColor(node.stroke);
|
|
362
|
+
ctx.lineWidth = node.strokeWidth || 1;
|
|
363
|
+
ctx.stroke();
|
|
364
|
+
}
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
ctx.restore();
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// --- GPU initialization ---
|
|
373
|
+
|
|
374
|
+
async function initGPU(canvas, width, height, dpr) {
|
|
375
|
+
const adapter = await navigator.gpu.requestAdapter();
|
|
376
|
+
if (!adapter) return null;
|
|
377
|
+
const device = await adapter.requestDevice();
|
|
378
|
+
const ctx = canvas.getContext('webgpu');
|
|
379
|
+
const format = navigator.gpu.getPreferredCanvasFormat();
|
|
380
|
+
ctx.configure({ device, format, alphaMode: 'premultiplied' });
|
|
381
|
+
|
|
382
|
+
const uniformBuf = device.createBuffer({
|
|
383
|
+
size: 16,
|
|
384
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
385
|
+
});
|
|
386
|
+
device.queue.writeBuffer(uniformBuf, 0, new Float32Array([width * dpr, height * dpr, dpr, 0]));
|
|
387
|
+
|
|
388
|
+
const uniformLayout = device.createBindGroupLayout({
|
|
389
|
+
entries: [{ binding: 0, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
|
|
390
|
+
buffer: { type: 'uniform' } }]
|
|
391
|
+
});
|
|
392
|
+
const uniformGroup = device.createBindGroup({
|
|
393
|
+
layout: uniformLayout,
|
|
394
|
+
entries: [{ binding: 0, resource: { buffer: uniformBuf } }]
|
|
395
|
+
});
|
|
396
|
+
const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [uniformLayout] });
|
|
397
|
+
|
|
398
|
+
const blendState = {
|
|
399
|
+
color: { srcFactor: 'src-alpha', dstFactor: 'one-minus-src-alpha', operation: 'add' },
|
|
400
|
+
alpha: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' }
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
return { device, ctx, format, uniformGroup, pipelineLayout, blendState };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// --- Pipeline creators ---
|
|
407
|
+
|
|
408
|
+
function createCirclePipeline(gpu) {
|
|
409
|
+
const { device, format, pipelineLayout, blendState } = gpu;
|
|
410
|
+
const module = device.createShaderModule({ code: CIRCLE_SHADER });
|
|
411
|
+
return device.createRenderPipeline({
|
|
412
|
+
layout: pipelineLayout,
|
|
413
|
+
vertex: {
|
|
414
|
+
module, entryPoint: 'vs',
|
|
415
|
+
buffers: [{
|
|
416
|
+
arrayStride: 52, // 13 floats: pos(2) + r(1) + fill(4) + stroke(4) + sw(1) + op(1)
|
|
417
|
+
stepMode: 'instance',
|
|
418
|
+
attributes: [
|
|
419
|
+
{ shaderLocation: 0, offset: 0, format: 'float32x2' }, // pos
|
|
420
|
+
{ shaderLocation: 1, offset: 8, format: 'float32' }, // radius
|
|
421
|
+
{ shaderLocation: 2, offset: 12, format: 'float32x4' }, // fillColor
|
|
422
|
+
{ shaderLocation: 3, offset: 28, format: 'float32x4' }, // strokeColor
|
|
423
|
+
{ shaderLocation: 4, offset: 44, format: 'float32' }, // strokeWidth
|
|
424
|
+
{ shaderLocation: 5, offset: 48, format: 'float32' }, // opacity
|
|
425
|
+
]
|
|
426
|
+
}]
|
|
427
|
+
},
|
|
428
|
+
fragment: { module, entryPoint: 'fs', targets: [{ format, blend: blendState }] },
|
|
429
|
+
primitive: { topology: 'triangle-list' }
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function createRectPipeline(gpu) {
|
|
434
|
+
const { device, format, pipelineLayout, blendState } = gpu;
|
|
435
|
+
const module = device.createShaderModule({ code: RECT_SHADER });
|
|
436
|
+
return device.createRenderPipeline({
|
|
437
|
+
layout: pipelineLayout,
|
|
438
|
+
vertex: {
|
|
439
|
+
module, entryPoint: 'vs',
|
|
440
|
+
buffers: [{
|
|
441
|
+
arrayStride: 60, // 15 floats: xywh(4) + fill(4) + stroke(4) + sw(1) + cr(1) + op(1)
|
|
442
|
+
stepMode: 'instance',
|
|
443
|
+
attributes: [
|
|
444
|
+
{ shaderLocation: 0, offset: 0, format: 'float32x4' }, // posSize
|
|
445
|
+
{ shaderLocation: 1, offset: 16, format: 'float32x4' }, // fillColor
|
|
446
|
+
{ shaderLocation: 2, offset: 32, format: 'float32x4' }, // strokeColor
|
|
447
|
+
{ shaderLocation: 3, offset: 48, format: 'float32' }, // strokeWidth
|
|
448
|
+
{ shaderLocation: 4, offset: 52, format: 'float32' }, // cornerRadius
|
|
449
|
+
{ shaderLocation: 5, offset: 56, format: 'float32' }, // opacity
|
|
450
|
+
]
|
|
451
|
+
}]
|
|
452
|
+
},
|
|
453
|
+
fragment: { module, entryPoint: 'fs', targets: [{ format, blend: blendState }] },
|
|
454
|
+
primitive: { topology: 'triangle-list' }
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function createLinePipeline(gpu) {
|
|
459
|
+
const { device, format, pipelineLayout, blendState } = gpu;
|
|
460
|
+
const module = device.createShaderModule({ code: LINE_SHADER });
|
|
461
|
+
return device.createRenderPipeline({
|
|
462
|
+
layout: pipelineLayout,
|
|
463
|
+
vertex: {
|
|
464
|
+
module, entryPoint: 'vs',
|
|
465
|
+
buffers: [{
|
|
466
|
+
arrayStride: 28, // 7 floats: pos(2) + color(4) + opacity(1)
|
|
467
|
+
stepMode: 'vertex',
|
|
468
|
+
attributes: [
|
|
469
|
+
{ shaderLocation: 0, offset: 0, format: 'float32x2' }, // position
|
|
470
|
+
{ shaderLocation: 1, offset: 8, format: 'float32x4' }, // color
|
|
471
|
+
{ shaderLocation: 2, offset: 24, format: 'float32' }, // opacity
|
|
472
|
+
]
|
|
473
|
+
}]
|
|
474
|
+
},
|
|
475
|
+
fragment: { module, entryPoint: 'fs', targets: [{ format, blend: blendState }] },
|
|
476
|
+
primitive: { topology: 'triangle-list' }
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// --- Instance data packing ---
|
|
481
|
+
|
|
482
|
+
function packCircles(circles, dpr) {
|
|
483
|
+
const data = new Float32Array(circles.length * 13);
|
|
484
|
+
for (let i = 0; i < circles.length; i++) {
|
|
485
|
+
const c = circles[i], o = i * 13;
|
|
486
|
+
const fill = parseColorToVec4(c.fill);
|
|
487
|
+
const stroke = parseColorToVec4(c.stroke);
|
|
488
|
+
data[o] = c.cx * dpr;
|
|
489
|
+
data[o + 1] = c.cy * dpr;
|
|
490
|
+
data[o + 2] = c.r * dpr;
|
|
491
|
+
data[o + 3] = fill[0]; data[o + 4] = fill[1]; data[o + 5] = fill[2]; data[o + 6] = fill[3];
|
|
492
|
+
data[o + 7] = stroke[0]; data[o + 8] = stroke[1]; data[o + 9] = stroke[2]; data[o + 10] = stroke[3];
|
|
493
|
+
data[o + 11] = (c.strokeWidth || 0) * dpr;
|
|
494
|
+
data[o + 12] = c.opacity;
|
|
495
|
+
}
|
|
496
|
+
return data;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function packRects(rects, dpr) {
|
|
500
|
+
const data = new Float32Array(rects.length * 15);
|
|
501
|
+
for (let i = 0; i < rects.length; i++) {
|
|
502
|
+
const r = rects[i], o = i * 15;
|
|
503
|
+
const fill = parseColorToVec4(r.fill);
|
|
504
|
+
const stroke = parseColorToVec4(r.stroke);
|
|
505
|
+
data[o] = r.x * dpr;
|
|
506
|
+
data[o + 1] = r.y * dpr;
|
|
507
|
+
data[o + 2] = Math.max(0, r.w) * dpr;
|
|
508
|
+
data[o + 3] = Math.max(0, r.h) * dpr;
|
|
509
|
+
data[o + 4] = fill[0]; data[o + 5] = fill[1]; data[o + 6] = fill[2]; data[o + 7] = fill[3];
|
|
510
|
+
data[o + 8] = stroke[0]; data[o + 9] = stroke[1]; data[o + 10] = stroke[2]; data[o + 11] = stroke[3];
|
|
511
|
+
data[o + 12] = (r.strokeWidth || 0) * dpr;
|
|
512
|
+
data[o + 13] = (r.rx || 0) * dpr;
|
|
513
|
+
data[o + 14] = r.opacity;
|
|
514
|
+
}
|
|
515
|
+
return data;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// --- GPU render pass ---
|
|
519
|
+
|
|
520
|
+
async function renderGPU(canvas, width, height, dpr, circles, rects, lines) {
|
|
521
|
+
const gpu = await initGPU(canvas, width, height, dpr);
|
|
522
|
+
if (!gpu) throw new Error('WebGPU init failed');
|
|
523
|
+
|
|
524
|
+
const { device, ctx, uniformGroup } = gpu;
|
|
525
|
+
const encoder = device.createCommandEncoder();
|
|
526
|
+
const textureView = ctx.getCurrentTexture().createView();
|
|
527
|
+
|
|
528
|
+
const renderPass = encoder.beginRenderPass({
|
|
529
|
+
colorAttachments: [{
|
|
530
|
+
view: textureView,
|
|
531
|
+
clearValue: { r: 0, g: 0, b: 0, a: 0 },
|
|
532
|
+
loadOp: 'clear',
|
|
533
|
+
storeOp: 'store'
|
|
534
|
+
}]
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
// Lines — tessellated into triangle strips
|
|
538
|
+
if (lines.length > 0) {
|
|
539
|
+
const lineData = tessellateLines(lines, dpr);
|
|
540
|
+
if (lineData.length > 0) {
|
|
541
|
+
const pipeline = createLinePipeline(gpu);
|
|
542
|
+
const buf = device.createBuffer({
|
|
543
|
+
size: lineData.byteLength,
|
|
544
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
545
|
+
});
|
|
546
|
+
device.queue.writeBuffer(buf, 0, lineData);
|
|
547
|
+
renderPass.setPipeline(pipeline);
|
|
548
|
+
renderPass.setBindGroup(0, uniformGroup);
|
|
549
|
+
renderPass.setVertexBuffer(0, buf);
|
|
550
|
+
renderPass.draw(lineData.length / 7);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Rects — GPU instanced
|
|
555
|
+
if (rects.length > 0) {
|
|
556
|
+
const rectData = packRects(rects, dpr);
|
|
557
|
+
const pipeline = createRectPipeline(gpu);
|
|
558
|
+
const buf = device.createBuffer({
|
|
559
|
+
size: rectData.byteLength,
|
|
560
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
561
|
+
});
|
|
562
|
+
device.queue.writeBuffer(buf, 0, rectData);
|
|
563
|
+
renderPass.setPipeline(pipeline);
|
|
564
|
+
renderPass.setBindGroup(0, uniformGroup);
|
|
565
|
+
renderPass.setVertexBuffer(0, buf);
|
|
566
|
+
renderPass.draw(6, rects.length);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// Circles — GPU instanced
|
|
570
|
+
if (circles.length > 0) {
|
|
571
|
+
const circleData = packCircles(circles, dpr);
|
|
572
|
+
const pipeline = createCirclePipeline(gpu);
|
|
573
|
+
const buf = device.createBuffer({
|
|
574
|
+
size: circleData.byteLength,
|
|
575
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
576
|
+
});
|
|
577
|
+
device.queue.writeBuffer(buf, 0, circleData);
|
|
578
|
+
renderPass.setPipeline(pipeline);
|
|
579
|
+
renderPass.setBindGroup(0, uniformGroup);
|
|
580
|
+
renderPass.setVertexBuffer(0, buf);
|
|
581
|
+
renderPass.draw(6, circles.length);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
renderPass.end();
|
|
585
|
+
device.queue.submit([encoder.finish()]);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// --- Canvas 2D fallback for GPU failure mid-render ---
|
|
589
|
+
|
|
590
|
+
function renderFallbackShapes(ctx, circles, rects, lines) {
|
|
591
|
+
for (const ln of lines) {
|
|
592
|
+
ctx.save();
|
|
593
|
+
ctx.globalAlpha = ln.opacity;
|
|
594
|
+
ctx.beginPath();
|
|
595
|
+
ctx.moveTo(ln.x1, ln.y1);
|
|
596
|
+
ctx.lineTo(ln.x2, ln.y2);
|
|
597
|
+
ctx.strokeStyle = resolveColor(ln.stroke) || '#ccc';
|
|
598
|
+
ctx.lineWidth = ln.strokeWidth || 1;
|
|
599
|
+
ctx.stroke();
|
|
600
|
+
ctx.restore();
|
|
601
|
+
}
|
|
602
|
+
for (const r of rects) {
|
|
603
|
+
ctx.save();
|
|
604
|
+
ctx.globalAlpha = r.opacity;
|
|
605
|
+
const fill = resolveColor(r.fill);
|
|
606
|
+
const stroke = resolveColor(r.stroke);
|
|
607
|
+
if (r.rx > 0) {
|
|
608
|
+
const rad = Math.min(r.rx, r.w / 2, r.h / 2);
|
|
609
|
+
ctx.beginPath();
|
|
610
|
+
ctx.moveTo(r.x + rad, r.y);
|
|
611
|
+
ctx.lineTo(r.x + r.w - rad, r.y);
|
|
612
|
+
ctx.arcTo(r.x + r.w, r.y, r.x + r.w, r.y + rad, rad);
|
|
613
|
+
ctx.lineTo(r.x + r.w, r.y + r.h - rad);
|
|
614
|
+
ctx.arcTo(r.x + r.w, r.y + r.h, r.x + r.w - rad, r.y + r.h, rad);
|
|
615
|
+
ctx.lineTo(r.x + rad, r.y + r.h);
|
|
616
|
+
ctx.arcTo(r.x, r.y + r.h, r.x, r.y + r.h - rad, rad);
|
|
617
|
+
ctx.lineTo(r.x, r.y + rad);
|
|
618
|
+
ctx.arcTo(r.x, r.y, r.x + rad, r.y, rad);
|
|
619
|
+
ctx.closePath();
|
|
620
|
+
if (fill) { ctx.fillStyle = fill; ctx.fill(); }
|
|
621
|
+
if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = r.strokeWidth || 1; ctx.stroke(); }
|
|
622
|
+
} else {
|
|
623
|
+
if (fill) { ctx.fillStyle = fill; ctx.fillRect(r.x, r.y, Math.max(0, r.w), Math.max(0, r.h)); }
|
|
624
|
+
if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = r.strokeWidth || 1; ctx.strokeRect(r.x, r.y, Math.max(0, r.w), Math.max(0, r.h)); }
|
|
625
|
+
}
|
|
626
|
+
ctx.restore();
|
|
627
|
+
}
|
|
628
|
+
for (const c of circles) {
|
|
629
|
+
ctx.save();
|
|
630
|
+
ctx.globalAlpha = c.opacity;
|
|
631
|
+
ctx.beginPath();
|
|
632
|
+
ctx.arc(c.cx, c.cy, c.r, 0, Math.PI * 2);
|
|
633
|
+
const fill = resolveColor(c.fill);
|
|
634
|
+
const stroke = resolveColor(c.stroke);
|
|
635
|
+
if (fill) { ctx.fillStyle = fill; ctx.fill(); }
|
|
636
|
+
if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = c.strokeWidth || 1; ctx.stroke(); }
|
|
637
|
+
ctx.restore();
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// --- Main export ---
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Render scene graph via WebGPU with Canvas 2D overlay for text/complex shapes.
|
|
645
|
+
* Returns HTMLElement synchronously; GPU rendering starts asynchronously.
|
|
646
|
+
* Falls back to Canvas renderer if WebGPU is unavailable.
|
|
647
|
+
* @param {Object} sceneNode — scene graph root
|
|
648
|
+
* @returns {HTMLElement} container div with canvas layers
|
|
649
|
+
*/
|
|
650
|
+
export function renderWebGPU(sceneNode) {
|
|
651
|
+
// Feature detection — synchronous fallback to Canvas renderer
|
|
652
|
+
if (typeof navigator === 'undefined' || !navigator.gpu) {
|
|
653
|
+
return renderCanvas(sceneNode);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
if (!sceneNode || sceneNode.type !== 'scene') {
|
|
657
|
+
const c = document.createElement('div');
|
|
658
|
+
c.style.width = '100px';
|
|
659
|
+
c.style.height = '100px';
|
|
660
|
+
return c;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const { width, height, children } = sceneNode;
|
|
664
|
+
const dpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1;
|
|
665
|
+
|
|
666
|
+
// Container
|
|
667
|
+
const container = document.createElement('div');
|
|
668
|
+
container.className = 'd-chart-svg';
|
|
669
|
+
container.style.position = 'relative';
|
|
670
|
+
container.style.width = width + 'px';
|
|
671
|
+
container.style.height = height + 'px';
|
|
672
|
+
|
|
673
|
+
// GPU canvas (bottom layer)
|
|
674
|
+
const gpuCanvas = document.createElement('canvas');
|
|
675
|
+
gpuCanvas.width = width * dpr;
|
|
676
|
+
gpuCanvas.height = height * dpr;
|
|
677
|
+
gpuCanvas.style.width = width + 'px';
|
|
678
|
+
gpuCanvas.style.height = height + 'px';
|
|
679
|
+
gpuCanvas.style.position = 'absolute';
|
|
680
|
+
gpuCanvas.style.top = '0';
|
|
681
|
+
gpuCanvas.style.left = '0';
|
|
682
|
+
container.appendChild(gpuCanvas);
|
|
683
|
+
|
|
684
|
+
// Overlay canvas (top layer — text, arcs, paths, polygons, dashed lines)
|
|
685
|
+
const overlayCanvas = document.createElement('canvas');
|
|
686
|
+
overlayCanvas.width = width * dpr;
|
|
687
|
+
overlayCanvas.height = height * dpr;
|
|
688
|
+
overlayCanvas.style.width = width + 'px';
|
|
689
|
+
overlayCanvas.style.height = height + 'px';
|
|
690
|
+
overlayCanvas.style.position = 'absolute';
|
|
691
|
+
overlayCanvas.style.top = '0';
|
|
692
|
+
overlayCanvas.style.left = '0';
|
|
693
|
+
overlayCanvas.style.pointerEvents = 'none';
|
|
694
|
+
container.appendChild(overlayCanvas);
|
|
695
|
+
|
|
696
|
+
// Batch scene nodes by type
|
|
697
|
+
const batches = { circles: [], rects: [], lines: [], overlay: [] };
|
|
698
|
+
for (const child of children) flattenScene(child, batches, 0, 0, 1);
|
|
699
|
+
|
|
700
|
+
// Dashed lines cannot be GPU-rendered — route to overlay
|
|
701
|
+
const solidLines = batches.lines.filter(l => !l.strokeDash);
|
|
702
|
+
const dashedLines = batches.lines.filter(l => l.strokeDash);
|
|
703
|
+
|
|
704
|
+
// Render overlay synchronously (Canvas 2D)
|
|
705
|
+
const octx = overlayCanvas.getContext('2d');
|
|
706
|
+
octx.scale(dpr, dpr);
|
|
707
|
+
renderOverlay(octx, batches.overlay, dashedLines);
|
|
708
|
+
|
|
709
|
+
// Kick off async GPU rendering; fall back to Canvas 2D on failure
|
|
710
|
+
renderGPU(gpuCanvas, width, height, dpr, batches.circles, batches.rects, solidLines).catch(() => {
|
|
711
|
+
renderFallbackShapes(octx, batches.circles, batches.rects, solidLines);
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
return container;
|
|
715
|
+
}
|