Skip to content

Commit 6271f22

Browse files
authored
Add InteractiveGraphicsCanvas (#46)
* Add InteractiveGraphicsCanvas * export interactive graphics canvas * fix types
1 parent cd2c5d1 commit 6271f22

File tree

6 files changed

+416
-8
lines changed

6 files changed

+416
-8
lines changed

examples/canvas-renderer.fixture.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default function CanvasRenderer() {
201201
<h2>Canvas Renderer with Mouse Panning/Zooming</h2>
202202
<p>Drag to pan, scroll to zoom</p>
203203
<div
204-
ref={node => {
204+
ref={(node) => {
205205
// Using a callback ref approach
206206
containerRef.current = node
207207
// Apply the mouse transform ref if available
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import React from "react"
2+
import { GraphicsObject } from "../lib"
3+
import InteractiveGraphicsCanvas from "../site/components/InteractiveGraphicsCanvas"
4+
5+
// Example graphics object with different elements and steps
6+
const steppedGraphics: GraphicsObject = {
7+
title: "Interactive Canvas Demo with Steps",
8+
points: [
9+
{ x: 0, y: 0, label: "Origin", color: "blue", step: 0 },
10+
{ x: 50, y: 50, label: "Point A", color: "red", step: 1 },
11+
{ x: -50, y: 20, label: "Point B", color: "green", step: 2 },
12+
{ x: -20, y: -30, label: "Point C", color: "purple", step: 3 },
13+
],
14+
lines: [
15+
{
16+
points: [
17+
{ x: 0, y: 0 },
18+
{ x: 50, y: 50 },
19+
],
20+
strokeColor: "gray",
21+
strokeWidth: 2,
22+
step: 1,
23+
},
24+
{
25+
points: [
26+
{ x: 0, y: 0 },
27+
{ x: -50, y: 20 },
28+
],
29+
strokeColor: "green",
30+
strokeWidth: 2,
31+
step: 2,
32+
},
33+
{
34+
points: [
35+
{ x: 0, y: 0 },
36+
{ x: -20, y: -30 },
37+
],
38+
strokeColor: "purple",
39+
strokeWidth: 2,
40+
step: 3,
41+
},
42+
],
43+
rects: [
44+
{
45+
center: { x: 0, y: 30 },
46+
width: 40,
47+
height: 20,
48+
fill: "rgba(255, 0, 0, 0.2)",
49+
stroke: "red",
50+
step: 1,
51+
},
52+
{
53+
center: { x: -30, y: 0 },
54+
width: 30,
55+
height: 30,
56+
fill: "rgba(0, 0, 255, 0.2)",
57+
stroke: "blue",
58+
step: 2,
59+
},
60+
],
61+
circles: [
62+
{
63+
center: { x: 25, y: 25 },
64+
radius: 15,
65+
fill: "rgba(0, 255, 0, 0.2)",
66+
stroke: "green",
67+
step: 1,
68+
},
69+
{
70+
center: { x: -20, y: -30 },
71+
radius: 10,
72+
fill: "rgba(255, 0, 255, 0.2)",
73+
stroke: "purple",
74+
step: 3,
75+
},
76+
],
77+
coordinateSystem: "cartesian",
78+
}
79+
80+
export default function InteractiveGraphicsCanvasFixture() {
81+
return (
82+
<div style={{ display: "flex", flexDirection: "column", gap: "10px" }}>
83+
<h2>Interactive Graphics Canvas</h2>
84+
<p>Drag to pan, scroll to zoom</p>
85+
<p>Use the step controls to visualize the graphics step by step</p>
86+
<p>
87+
When dimension tool is enabled, press 'd' key to measure distances and
88+
coordinates
89+
</p>
90+
91+
<InteractiveGraphicsCanvas
92+
graphics={steppedGraphics}
93+
height={500}
94+
showGrid={true}
95+
/>
96+
</div>
97+
)
98+
}

lib/drawGraphicsToCanvas.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,15 @@ export function drawGraphicsToCanvas(
232232
ctx.lineWidth = line.strokeWidth || 1
233233

234234
if (line.strokeDash) {
235-
if (typeof line.strokeDash === 'string') {
236-
ctx.setLineDash(line.strokeDash.split(",").map(Number).map(n => n * Math.abs(matrix.a)))
235+
if (typeof line.strokeDash === "string") {
236+
ctx.setLineDash(
237+
line.strokeDash
238+
.split(",")
239+
.map(Number)
240+
.map((n) => n * Math.abs(matrix.a)),
241+
)
237242
} else {
238-
ctx.setLineDash(line.strokeDash.map(n => n * Math.abs(matrix.a)))
243+
ctx.setLineDash(line.strokeDash.map((n) => n * Math.abs(matrix.a)))
239244
}
240245
} else {
241246
ctx.setLineDash([])

lib/react.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "../site/components/InteractiveGraphics/InteractiveGraphics"
2+
export * from "../site/components/InteractiveGraphicsCanvas"
23
export * from "../site/components/CanvasGraphics/CanvasGraphics"

site/assets/exampleGraphics.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
],
1717
"lines": [
1818
{
19-
"points": [
20-
{ "x": 0, "y": 0 },
21-
{ "x": 100, "y": 100 }
22-
],
19+
"points": [{ "x": 0, "y": 0 }, { "x": 100, "y": 100 }],
2320
"strokeWidth": 1,
2421
"strokeDash": [10, 5],
2522
"label": "hello"

0 commit comments

Comments
 (0)