@tscircuit/schematic-viewer 1.2.5 → 1.2.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscircuit/schematic-viewer",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "repository": "tscircuit/schematic-viewer",
@@ -32,8 +32,8 @@
32
32
  "@storybook/nextjs": "^7.4.0",
33
33
  "@storybook/react": "^7.4.0",
34
34
  "@storybook/testing-library": "^0.0.14-next.2",
35
- "@tscircuit/builder": "^1.5.88",
36
- "@tscircuit/react-fiber": "^1.1.1",
35
+ "@tscircuit/builder": "^1.5.89",
36
+ "@tscircuit/react-fiber": "^1.1.6",
37
37
  "@tscircuit/routing": "^1.3.0",
38
38
  "@tscircuit/table-viewer": "^0.0.6",
39
39
  "@types/node": "^18.6.0",
@@ -58,6 +58,8 @@
58
58
  "zustand": "^4.0.0"
59
59
  },
60
60
  "dependencies": {
61
+ "@tscircuit/soup": "^0.0.7",
62
+ "convert-units": "^2.3.4",
61
63
  "react-error-boundary": "^4.0.4",
62
64
  "react-supergrid": "^1.0.10",
63
65
  "use-mouse-matrix-transform": "^1.1.12"
@@ -4,32 +4,35 @@ import { Schematic } from "Schematic"
4
4
  export const ExampleCircuit = () => {
5
5
  return (
6
6
  <Schematic style={{ height: 500 }}>
7
- <resistor name="R1" resistance="10 ohm" center={[2, 1]} />
7
+ <resistor name="R1" resistance="10 ohm" schX={2} schY={1} />
8
8
  <capacitor
9
9
  name="C1"
10
10
  capacitance="10 uF"
11
- center={[4, 2]}
12
- rotation="90deg"
11
+ schX={4}
12
+ schY={2}
13
+ schRotation="90deg"
13
14
  />
14
15
  <resistor
15
16
  name="R2"
16
17
  resistance="10 ohm"
17
- center={[6, 1]}
18
- rotation="90deg"
18
+ schX={6}
19
+ schY={1}
20
+ schRotation="90deg"
19
21
  />
20
22
  <trace
21
23
  path={[".R1 > port.right", ".C1 > port.left", ".R2 > port.left"]}
22
24
  />
23
- <powersource voltage="5V" center={[1, 2]} name="main_power" />
25
+ <powersource voltage="5V" schX={1} schY={2} name="main_power" />
24
26
  <trace path={[".main_power > port.positive", ".R1 > port.left"]} />
25
27
  <trace
26
28
  path={["power > port.negative", ".C1 > port.right", ".R2 > port.right"]}
27
29
  />
28
30
  <bug
29
31
  name="B1"
30
- port_arrangement={{ left_size: 3, right_size: 3 }}
31
- center={[8, 3]}
32
- port_labels={{
32
+ schPortArrangement={{ leftSize: 3, rightSize: 3 }}
33
+ schX={8}
34
+ schY={3}
35
+ pinLabels={{
33
36
  1: "PWR",
34
37
  2: "NC",
35
38
  3: "RG",
@@ -39,9 +42,9 @@ export const ExampleCircuit = () => {
39
42
  }}
40
43
  />
41
44
  <trace path={[".B1 > port.PWR", ".R2 > port.left"]} />
42
- <ground name="GND" center={[11, 3]} />
45
+ {/* <ground name="GND" schX={11} schY={3} /> */}
43
46
  <trace from=".B1 > port.GND" to=".GND" />
44
- <diode name="D1" center={[6, 3.5]} rotation="180deg" />
47
+ <diode name="D1" schX={6} schY={3.5} schRotation="180deg" />
45
48
  <trace from=".D1 > .left" to=".B1 > .RG" />
46
49
  <trace from=".D1 > .right" to=".C1> .right" />
47
50
  </Schematic>
@@ -23,23 +23,27 @@ export const ProjectComponent = ({ project: $project }: Props) => {
23
23
  {project.schematic_components.map((schematic_component) => (
24
24
  <Components.SchematicComponent
25
25
  key={schematic_component.schematic_component_id}
26
- component={{
27
- source: projectClass.getSourceComponent(
28
- schematic_component.source_component_id
29
- ),
30
- schematic: schematic_component,
31
- }}
26
+ component={
27
+ {
28
+ source: projectClass.getSourceComponent(
29
+ schematic_component.source_component_id
30
+ ),
31
+ schematic: schematic_component,
32
+ } as any
33
+ }
32
34
  />
33
35
  ))}
34
36
  {project.schematic_ports.map((schematic_port) => (
35
37
  <Components.SchematicPort
36
38
  key={schematic_port.schematic_port_id}
37
- port={{
38
- source: projectClass.getSourcePort(
39
- schematic_port.schematic_port_id
40
- ),
41
- schematic: schematic_port,
42
- }}
39
+ port={
40
+ {
41
+ source: projectClass.getSourcePort(
42
+ schematic_port.schematic_port_id
43
+ ),
44
+ schematic: schematic_port,
45
+ } as any
46
+ }
43
47
  />
44
48
  ))}
45
49
  {project.schematic_traces.map((schematic_trace) => (
@@ -2,7 +2,14 @@ import { useCameraTransform } from "lib/render-context"
2
2
  import getSVGPathBounds from "lib/utils/get-svg-path-bounds"
3
3
  import { useCallback, useState } from "react"
4
4
 
5
- import { applyToPoint } from "transformation-matrix"
5
+ import {
6
+ applyToPoint,
7
+ toSVG,
8
+ inverse,
9
+ compose,
10
+ translate,
11
+ scale,
12
+ } from "transformation-matrix"
6
13
 
7
14
  interface Props {
8
15
  rotation: number
@@ -41,8 +48,8 @@ export const SVGPathComponent = ({
41
48
  // `Ratio doesn't match for component. ${pathBounds.width}:${pathBounds.height} is not close to ${size.width}:${size.height}`
42
49
  // )
43
50
  }
44
- pathBounds.height = Math.max(pathBounds.height, 0.01)
45
- pathBounds.width = Math.max(pathBounds.width, 0.01)
51
+ // pathBounds.height = Math.max(pathBounds.height, 0.01)
52
+ // pathBounds.width = Math.max(pathBounds.width, 0.01)
46
53
  const absoluteCenter = applyToPoint(ct, center)
47
54
  const actualAbsWidth = size.width * ct.a
48
55
  const actualAbsHeight = size.height * Math.abs(ct.d)
@@ -50,7 +57,6 @@ export const SVGPathComponent = ({
50
57
  width: Math.max(1, actualAbsWidth),
51
58
  height: Math.max(1, actualAbsHeight),
52
59
  }
53
- console.log(absoluteSize, pathBounds)
54
60
 
55
61
  const [hovering, setHovering] = useState(false)
56
62
 
@@ -58,6 +64,18 @@ export const SVGPathComponent = ({
58
64
  const svgTop = absoluteCenter.y - absoluteSize.height / 2
59
65
 
60
66
  const viewBox = `${pathBounds.minX} ${pathBounds.minY} ${pathBounds.width} ${pathBounds.height}`
67
+ // const viewBox2 = `${svgLeft} ${svgTctualAbsWidth} ${actualAbsHeight}`
68
+
69
+ const svgToScreen = compose(
70
+ // translate(0, 0)
71
+ scale(
72
+ actualAbsWidth / pathBounds.width,
73
+ actualAbsHeight / pathBounds.height
74
+ ),
75
+ translate(-pathBounds.minX, -pathBounds.minY)
76
+ // translate(center.x, center.y)
77
+ )
78
+ // translate(..., ...),
61
79
 
62
80
  return (
63
81
  <>
@@ -66,11 +84,11 @@ export const SVGPathComponent = ({
66
84
  <div
67
85
  style={{
68
86
  position: "absolute",
69
- left: svgLeft - 4,
70
- top: svgTop - 4,
87
+ left: svgLeft - 6,
88
+ top: svgTop - 6,
71
89
  pointerEvents: "none",
72
- width: actualAbsWidth + 8,
73
- height: actualAbsHeight + 8,
90
+ width: actualAbsWidth + 12,
91
+ height: actualAbsHeight + 12,
74
92
  border: "1px red solid",
75
93
  mixBlendMode: "difference",
76
94
  zIndex: 1000,
@@ -108,19 +126,24 @@ export const SVGPathComponent = ({
108
126
  ].join(" "),
109
127
  left: svgLeft,
110
128
  top: svgTop,
111
- // backgroundColor: badRatio ? "rgba(255, 0, 0, 0.5)" : "transparent",
129
+ // overflow: "hidden",
130
+ // backgroundColor: badRatio ? "rgba(255, 0, 0, 0.1)" : "transparent",
131
+ // backgroundColor: "rgba(255, 0, 0, 0.1)",
112
132
  }}
113
133
  overflow="visible"
114
134
  width={absoluteSize.width}
115
135
  height={absoluteSize.height}
116
- viewBox={viewBox}
117
136
  >
118
137
  {paths.map((p, i) => (
119
138
  <path
120
139
  key={i}
140
+ // transform={toSVG(inverse(ct))}
141
+ // transform={`scale(${ct.a}, ${ct.a})`}
142
+ transform={toSVG(svgToScreen)}
121
143
  fill={p.fill ?? "none"}
122
144
  strokeLinecap="round"
123
- strokeWidth={2 * (p.strokeWidth || 1)}
145
+ // strokeWidth={2 * (p.strokeWidth || 1)}
146
+ strokeWidth={1.5 * (p.strokeWidth || 1)}
124
147
  stroke={p.stroke || "red"}
125
148
  d={p.d}
126
149
  />
@@ -19,6 +19,7 @@ export const SchematicPort = ({
19
19
  source_port?.name ?? source_port?.pin_number
20
20
  }`
21
21
  : `.${source_port?.name ?? source_port?.pin_number}`
22
+ const vec = directionToVec(schematic.facing_direction)
22
23
  return (
23
24
  <Component.SVGPathComponent
24
25
  rotation={0}
@@ -36,7 +37,10 @@ export const SchematicPort = ({
36
37
  </div>
37
38
  }
38
39
  center={schematic.center}
39
- size={{ width: 0.2, height: 0.2 }}
40
+ size={{
41
+ width: 0.2 + Math.abs(vec.x) * 0.04,
42
+ height: 0.2 + Math.abs(vec.y) * 0.04,
43
+ }}
40
44
  zIndex={10}
41
45
  paths={[
42
46
  {
@@ -48,9 +52,7 @@ export const SchematicPort = ({
48
52
  ? {
49
53
  stroke: "blue",
50
54
  strokeWidth: 0.5,
51
- d: `M 5 5 l ${directionToVec(schematic.facing_direction).x * 7} ${
52
- directionToVec(schematic.facing_direction).y * 7
53
- }`,
55
+ d: `M 5 5 l ${vec.x * 7} ${vec.y * 7}`,
54
56
  }
55
57
  : null,
56
58
  ].filter(Boolean)}
@@ -18,8 +18,8 @@ export const SimpleCapacitor = ({
18
18
  size={schematic.size}
19
19
  paths={[
20
20
  { stroke: "red", strokeWidth: 1, d: "M 0 15 l 12 0" },
21
- { stroke: "red", strokeWidth: 2, d: "M 12 0 l 0 30" },
22
- { stroke: "red", strokeWidth: 2, d: "M 18 0 l 0 30" },
21
+ { stroke: "red", strokeWidth: 1, d: "M 12 0 l 0 30" },
22
+ { stroke: "red", strokeWidth: 1, d: "M 18 0 l 0 30" },
23
23
  { stroke: "red", strokeWidth: 1, d: "M 18 15 l 12 0" },
24
24
  ]}
25
25
  />
@@ -0,0 +1,18 @@
1
+ import { useResistor } from "@tscircuit/react-fiber"
2
+ import { Schematic } from "../../Schematic"
3
+
4
+ export const Bug3ScalingTrace = () => {
5
+ const R1 = useResistor("R1", { resistance: "10" })
6
+ const R2 = useResistor("R2", { resistance: "1k" })
7
+ return (
8
+ <Schematic style={{ height: 500 }}>
9
+ <R1 schX={2} schY={1} />
10
+ <R2 schRotation="90deg" schX={0} schY={3} left={R1.left} />
11
+ </Schematic>
12
+ )
13
+ }
14
+
15
+ export default {
16
+ title: "Bugs/Bug3ScalingTrace",
17
+ component: Bug3ScalingTrace,
18
+ }
@@ -0,0 +1,14 @@
1
+ import { Schematic } from "../Schematic"
2
+
3
+ export const Resistor = () => {
4
+ return (
5
+ <Schematic style={{ height: 500 }}>
6
+ <resistor name="R1" resistance="10 ohm" schX={2} schY={1} />
7
+ </Schematic>
8
+ )
9
+ }
10
+
11
+ export default {
12
+ title: "Resistor",
13
+ component: Resistor,
14
+ }
@@ -0,0 +1,21 @@
1
+ import { Schematic } from "../Schematic"
2
+
3
+ export const RotatedResistor = () => {
4
+ return (
5
+ <Schematic style={{ height: 500 }}>
6
+ <resistor
7
+ name="R1"
8
+ resistance="10 ohm"
9
+ schX={2}
10
+ schY={1}
11
+ schRotation="90deg"
12
+ rotation="90deg"
13
+ />
14
+ </Schematic>
15
+ )
16
+ }
17
+
18
+ export default {
19
+ title: "RotatedResistor",
20
+ component: RotatedResistor,
21
+ }