circuit-to-canvas 0.0.33 → 0.0.35
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/dist/index.js +94 -73
- package/lib/drawer/shapes/arrow.ts +3 -6
- package/lib/drawer/shapes/dimension-line.ts +107 -56
- package/package.json +1 -1
- package/tests/elements/__snapshots__/pcb-fabrication-note-dimension.snap.png +0 -0
- package/tests/elements/__snapshots__/pcb-note-dimension-angled-and-vertical.snap.png +0 -0
- package/tests/elements/__snapshots__/pcb-note-dimension-basic.snap.png +0 -0
- package/tests/elements/__snapshots__/pcb-note-dimension-vertical.snap.png +0 -0
- package/tests/elements/__snapshots__/pcb-note-dimension-with-offset.snap.png +0 -0
- package/tests/shapes/__snapshots__/dimension-line.snap.png +0 -0
package/dist/index.js
CHANGED
|
@@ -1620,27 +1620,6 @@ function drawPcbNoteText(params) {
|
|
|
1620
1620
|
|
|
1621
1621
|
// lib/drawer/shapes/dimension-line.ts
|
|
1622
1622
|
import { applyToPoint as applyToPoint13 } from "transformation-matrix";
|
|
1623
|
-
|
|
1624
|
-
// lib/drawer/shapes/arrow.ts
|
|
1625
|
-
function drawArrow(params) {
|
|
1626
|
-
const { ctx, x, y, angle, arrowSize, color, strokeWidth } = params;
|
|
1627
|
-
ctx.save();
|
|
1628
|
-
ctx.translate(x, y);
|
|
1629
|
-
ctx.rotate(angle);
|
|
1630
|
-
ctx.beginPath();
|
|
1631
|
-
ctx.moveTo(0, 0);
|
|
1632
|
-
ctx.lineTo(-arrowSize, -arrowSize / 2);
|
|
1633
|
-
ctx.moveTo(0, 0);
|
|
1634
|
-
ctx.lineTo(-arrowSize, arrowSize / 2);
|
|
1635
|
-
ctx.lineWidth = strokeWidth;
|
|
1636
|
-
ctx.strokeStyle = color;
|
|
1637
|
-
ctx.lineCap = "round";
|
|
1638
|
-
ctx.lineJoin = "round";
|
|
1639
|
-
ctx.stroke();
|
|
1640
|
-
ctx.restore();
|
|
1641
|
-
}
|
|
1642
|
-
|
|
1643
|
-
// lib/drawer/shapes/dimension-line.ts
|
|
1644
1623
|
var TEXT_OFFSET_MULTIPLIER = 1.5;
|
|
1645
1624
|
var CHARACTER_WIDTH_MULTIPLIER = 0.6;
|
|
1646
1625
|
var TEXT_INTERSECTION_PADDING_MULTIPLIER = 0.3;
|
|
@@ -1686,64 +1665,90 @@ function drawDimensionLine(params) {
|
|
|
1686
1665
|
const lineColor = color || "rgba(255,255,255,0.5)";
|
|
1687
1666
|
const extensionDirection = hasOffsetDirection && (Math.abs(normalizedOffsetDirection.x) > Number.EPSILON || Math.abs(normalizedOffsetDirection.y) > Number.EPSILON) ? normalizedOffsetDirection : perpendicular;
|
|
1688
1667
|
const extensionLength = offsetMagnitude + 0.5;
|
|
1689
|
-
const
|
|
1668
|
+
const allPoints = [];
|
|
1669
|
+
const getExtensionPoints = (anchor) => {
|
|
1690
1670
|
const endPoint = {
|
|
1691
1671
|
x: anchor.x + extensionDirection.x * extensionLength,
|
|
1692
1672
|
y: anchor.y + extensionDirection.y * extensionLength
|
|
1693
1673
|
};
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1674
|
+
const halfWidth2 = strokeWidth / 2;
|
|
1675
|
+
const extPerpendicular = {
|
|
1676
|
+
x: -extensionDirection.y,
|
|
1677
|
+
y: extensionDirection.x
|
|
1678
|
+
};
|
|
1679
|
+
return [
|
|
1680
|
+
{
|
|
1681
|
+
x: anchor.x + extPerpendicular.x * halfWidth2,
|
|
1682
|
+
y: anchor.y + extPerpendicular.y * halfWidth2
|
|
1683
|
+
},
|
|
1684
|
+
{
|
|
1685
|
+
x: anchor.x - extPerpendicular.x * halfWidth2,
|
|
1686
|
+
y: anchor.y - extPerpendicular.y * halfWidth2
|
|
1687
|
+
},
|
|
1688
|
+
{
|
|
1689
|
+
x: endPoint.x - extPerpendicular.x * halfWidth2,
|
|
1690
|
+
y: endPoint.y - extPerpendicular.y * halfWidth2
|
|
1691
|
+
},
|
|
1692
|
+
{
|
|
1693
|
+
x: endPoint.x + extPerpendicular.x * halfWidth2,
|
|
1694
|
+
y: endPoint.y + extPerpendicular.y * halfWidth2
|
|
1695
|
+
}
|
|
1696
|
+
];
|
|
1702
1697
|
};
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1698
|
+
const ext1 = getExtensionPoints(from);
|
|
1699
|
+
allPoints.push(...ext1, ext1[0]);
|
|
1700
|
+
const ext2 = getExtensionPoints(to);
|
|
1701
|
+
allPoints.push(...ext2, ext2[0]);
|
|
1702
|
+
const halfWidth = strokeWidth / 2;
|
|
1703
|
+
const mainLine = [
|
|
1704
|
+
{
|
|
1705
|
+
x: fromBase.x + perpendicular.x * halfWidth,
|
|
1706
|
+
y: fromBase.y + perpendicular.y * halfWidth
|
|
1707
|
+
},
|
|
1708
|
+
{
|
|
1709
|
+
x: fromBase.x - perpendicular.x * halfWidth,
|
|
1710
|
+
y: fromBase.y - perpendicular.y * halfWidth
|
|
1711
|
+
},
|
|
1712
|
+
{
|
|
1713
|
+
x: toBase.x - perpendicular.x * halfWidth,
|
|
1714
|
+
y: toBase.y - perpendicular.y * halfWidth
|
|
1715
|
+
},
|
|
1716
|
+
{
|
|
1717
|
+
x: toBase.x + perpendicular.x * halfWidth,
|
|
1718
|
+
y: toBase.y + perpendicular.y * halfWidth
|
|
1719
|
+
}
|
|
1720
|
+
];
|
|
1721
|
+
allPoints.push(...mainLine, mainLine[0]);
|
|
1722
|
+
const arrow1 = [
|
|
1723
|
+
fromOffset,
|
|
1724
|
+
{
|
|
1725
|
+
x: fromOffset.x + direction.x * arrowSize + perpendicular.x * (arrowSize / 2),
|
|
1726
|
+
y: fromOffset.y + direction.y * arrowSize + perpendicular.y * (arrowSize / 2)
|
|
1727
|
+
},
|
|
1728
|
+
{
|
|
1729
|
+
x: fromOffset.x + direction.x * arrowSize - perpendicular.x * (arrowSize / 2),
|
|
1730
|
+
y: fromOffset.y + direction.y * arrowSize - perpendicular.y * (arrowSize / 2)
|
|
1731
|
+
}
|
|
1732
|
+
];
|
|
1733
|
+
allPoints.push(...arrow1, arrow1[0]);
|
|
1734
|
+
const arrow2 = [
|
|
1735
|
+
toOffset,
|
|
1736
|
+
{
|
|
1737
|
+
x: toOffset.x - direction.x * arrowSize + perpendicular.x * (arrowSize / 2),
|
|
1738
|
+
y: toOffset.y - direction.y * arrowSize + perpendicular.y * (arrowSize / 2)
|
|
1739
|
+
},
|
|
1740
|
+
{
|
|
1741
|
+
x: toOffset.x - direction.x * arrowSize - perpendicular.x * (arrowSize / 2),
|
|
1742
|
+
y: toOffset.y - direction.y * arrowSize - perpendicular.y * (arrowSize / 2)
|
|
1743
|
+
}
|
|
1744
|
+
];
|
|
1745
|
+
allPoints.push(...arrow2, arrow2[0]);
|
|
1746
|
+
drawPolygon({
|
|
1706
1747
|
ctx,
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
strokeWidth,
|
|
1710
|
-
stroke: lineColor,
|
|
1748
|
+
points: allPoints,
|
|
1749
|
+
fill: lineColor,
|
|
1711
1750
|
realToCanvasMat
|
|
1712
1751
|
});
|
|
1713
|
-
const [canvasFromX, canvasFromY] = applyToPoint13(realToCanvasMat, [
|
|
1714
|
-
fromOffset.x,
|
|
1715
|
-
fromOffset.y
|
|
1716
|
-
]);
|
|
1717
|
-
const [canvasToX, canvasToY] = applyToPoint13(realToCanvasMat, [
|
|
1718
|
-
toOffset.x,
|
|
1719
|
-
toOffset.y
|
|
1720
|
-
]);
|
|
1721
|
-
const [canvasToDirX, canvasToDirY] = applyToPoint13(realToCanvasMat, [
|
|
1722
|
-
toOffset.x + direction.x,
|
|
1723
|
-
toOffset.y + direction.y
|
|
1724
|
-
]);
|
|
1725
|
-
const canvasLineAngle = Math.atan2(
|
|
1726
|
-
canvasToDirY - canvasToY,
|
|
1727
|
-
canvasToDirX - canvasToX
|
|
1728
|
-
);
|
|
1729
|
-
drawArrow({
|
|
1730
|
-
ctx,
|
|
1731
|
-
x: canvasFromX,
|
|
1732
|
-
y: canvasFromY,
|
|
1733
|
-
angle: canvasLineAngle + Math.PI,
|
|
1734
|
-
arrowSize: arrowSize * scaleValue,
|
|
1735
|
-
color: lineColor,
|
|
1736
|
-
strokeWidth: strokeWidth * scaleValue
|
|
1737
|
-
});
|
|
1738
|
-
drawArrow({
|
|
1739
|
-
ctx,
|
|
1740
|
-
x: canvasToX,
|
|
1741
|
-
y: canvasToY,
|
|
1742
|
-
angle: canvasLineAngle,
|
|
1743
|
-
arrowSize: arrowSize * scaleValue,
|
|
1744
|
-
color: lineColor,
|
|
1745
|
-
strokeWidth: strokeWidth * scaleValue
|
|
1746
|
-
});
|
|
1747
1752
|
if (text) {
|
|
1748
1753
|
const midPoint = {
|
|
1749
1754
|
x: (from.x + to.x) / 2 + offsetVector.x,
|
|
@@ -1773,9 +1778,9 @@ function drawDimensionLine(params) {
|
|
|
1773
1778
|
const rotationRad = textRotation * Math.PI / 180;
|
|
1774
1779
|
const sinRot = Math.abs(Math.sin(rotationRad));
|
|
1775
1780
|
const cosRot = Math.abs(Math.cos(rotationRad));
|
|
1776
|
-
const
|
|
1781
|
+
const halfWidth2 = textWidth / 2;
|
|
1777
1782
|
const halfHeight = textHeight / 2;
|
|
1778
|
-
const maxExtension =
|
|
1783
|
+
const maxExtension = halfWidth2 * sinRot + halfHeight * cosRot;
|
|
1779
1784
|
additionalOffset = maxExtension + fontSize * TEXT_INTERSECTION_PADDING_MULTIPLIER;
|
|
1780
1785
|
}
|
|
1781
1786
|
const textOffset = arrowSize * TEXT_OFFSET_MULTIPLIER + additionalOffset;
|
|
@@ -2211,6 +2216,22 @@ var CircuitToCanvasDrawer = class {
|
|
|
2211
2216
|
}
|
|
2212
2217
|
}
|
|
2213
2218
|
};
|
|
2219
|
+
|
|
2220
|
+
// lib/drawer/shapes/arrow.ts
|
|
2221
|
+
function drawArrow(params) {
|
|
2222
|
+
const { ctx, x, y, angle, arrowSize, color, strokeWidth } = params;
|
|
2223
|
+
ctx.save();
|
|
2224
|
+
ctx.translate(x, y);
|
|
2225
|
+
ctx.rotate(angle);
|
|
2226
|
+
ctx.beginPath();
|
|
2227
|
+
ctx.moveTo(0, 0);
|
|
2228
|
+
ctx.lineTo(-arrowSize, -arrowSize / 2);
|
|
2229
|
+
ctx.lineTo(-arrowSize, arrowSize / 2);
|
|
2230
|
+
ctx.closePath();
|
|
2231
|
+
ctx.fillStyle = color;
|
|
2232
|
+
ctx.fill();
|
|
2233
|
+
ctx.restore();
|
|
2234
|
+
}
|
|
2214
2235
|
export {
|
|
2215
2236
|
CircuitToCanvasDrawer,
|
|
2216
2237
|
DEFAULT_PCB_COLOR_MAP,
|
|
@@ -23,14 +23,11 @@ export function drawArrow(params: DrawArrowParams): void {
|
|
|
23
23
|
ctx.beginPath()
|
|
24
24
|
ctx.moveTo(0, 0)
|
|
25
25
|
ctx.lineTo(-arrowSize, -arrowSize / 2)
|
|
26
|
-
ctx.moveTo(0, 0)
|
|
27
26
|
ctx.lineTo(-arrowSize, arrowSize / 2)
|
|
27
|
+
ctx.closePath()
|
|
28
28
|
|
|
29
|
-
ctx.
|
|
30
|
-
ctx.
|
|
31
|
-
ctx.lineCap = "round"
|
|
32
|
-
ctx.lineJoin = "round"
|
|
33
|
-
ctx.stroke()
|
|
29
|
+
ctx.fillStyle = color
|
|
30
|
+
ctx.fill()
|
|
34
31
|
|
|
35
32
|
ctx.restore()
|
|
36
33
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { Matrix } from "transformation-matrix"
|
|
2
2
|
import { applyToPoint } from "transformation-matrix"
|
|
3
3
|
import type { CanvasContext } from "../types"
|
|
4
|
-
import {
|
|
4
|
+
import { drawPolygon } from "./polygon"
|
|
5
5
|
import { drawText } from "./text"
|
|
6
|
-
import { drawArrow } from "./arrow"
|
|
7
6
|
|
|
8
7
|
export interface DrawDimensionLineParams {
|
|
9
8
|
ctx: CanvasContext
|
|
@@ -90,71 +89,123 @@ export function drawDimensionLine(params: DrawDimensionLineParams): void {
|
|
|
90
89
|
|
|
91
90
|
const extensionLength = offsetMagnitude + 0.5
|
|
92
91
|
|
|
93
|
-
const
|
|
92
|
+
const allPoints: Array<{ x: number; y: number }> = []
|
|
93
|
+
|
|
94
|
+
const getExtensionPoints = (anchor: { x: number; y: number }) => {
|
|
94
95
|
const endPoint = {
|
|
95
96
|
x: anchor.x + extensionDirection.x * extensionLength,
|
|
96
97
|
y: anchor.y + extensionDirection.y * extensionLength,
|
|
97
98
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
99
|
+
const halfWidth = strokeWidth / 2
|
|
100
|
+
const extPerpendicular = {
|
|
101
|
+
x: -extensionDirection.y,
|
|
102
|
+
y: extensionDirection.x,
|
|
103
|
+
}
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
x: anchor.x + extPerpendicular.x * halfWidth,
|
|
107
|
+
y: anchor.y + extPerpendicular.y * halfWidth,
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
x: anchor.x - extPerpendicular.x * halfWidth,
|
|
111
|
+
y: anchor.y - extPerpendicular.y * halfWidth,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
x: endPoint.x - extPerpendicular.x * halfWidth,
|
|
115
|
+
y: endPoint.y - extPerpendicular.y * halfWidth,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
x: endPoint.x + extPerpendicular.x * halfWidth,
|
|
119
|
+
y: endPoint.y + extPerpendicular.y * halfWidth,
|
|
120
|
+
},
|
|
121
|
+
]
|
|
106
122
|
}
|
|
107
123
|
|
|
108
|
-
|
|
109
|
-
|
|
124
|
+
// Extension lines (ticks)
|
|
125
|
+
const ext1 = getExtensionPoints(from)
|
|
126
|
+
allPoints.push(...ext1, ext1[0]!)
|
|
127
|
+
|
|
128
|
+
const ext2 = getExtensionPoints(to)
|
|
129
|
+
allPoints.push(...ext2, ext2[0]!)
|
|
110
130
|
|
|
111
131
|
// Main dimension line
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
132
|
+
const halfWidth = strokeWidth / 2
|
|
133
|
+
const mainLine = [
|
|
134
|
+
{
|
|
135
|
+
x: fromBase.x + perpendicular.x * halfWidth,
|
|
136
|
+
y: fromBase.y + perpendicular.y * halfWidth,
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
x: fromBase.x - perpendicular.x * halfWidth,
|
|
140
|
+
y: fromBase.y - perpendicular.y * halfWidth,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
x: toBase.x - perpendicular.x * halfWidth,
|
|
144
|
+
y: toBase.y - perpendicular.y * halfWidth,
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
x: toBase.x + perpendicular.x * halfWidth,
|
|
148
|
+
y: toBase.y + perpendicular.y * halfWidth,
|
|
149
|
+
},
|
|
150
|
+
]
|
|
151
|
+
allPoints.push(...mainLine, mainLine[0]!)
|
|
120
152
|
|
|
121
|
-
// Arrows
|
|
122
|
-
const
|
|
123
|
-
fromOffset
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
153
|
+
// Arrows
|
|
154
|
+
const arrow1 = [
|
|
155
|
+
fromOffset,
|
|
156
|
+
{
|
|
157
|
+
x:
|
|
158
|
+
fromOffset.x +
|
|
159
|
+
direction.x * arrowSize +
|
|
160
|
+
perpendicular.x * (arrowSize / 2),
|
|
161
|
+
y:
|
|
162
|
+
fromOffset.y +
|
|
163
|
+
direction.y * arrowSize +
|
|
164
|
+
perpendicular.y * (arrowSize / 2),
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
x:
|
|
168
|
+
fromOffset.x +
|
|
169
|
+
direction.x * arrowSize -
|
|
170
|
+
perpendicular.x * (arrowSize / 2),
|
|
171
|
+
y:
|
|
172
|
+
fromOffset.y +
|
|
173
|
+
direction.y * arrowSize -
|
|
174
|
+
perpendicular.y * (arrowSize / 2),
|
|
175
|
+
},
|
|
176
|
+
]
|
|
177
|
+
allPoints.push(...arrow1, arrow1[0]!)
|
|
178
|
+
|
|
179
|
+
const arrow2 = [
|
|
180
|
+
toOffset,
|
|
181
|
+
{
|
|
182
|
+
x:
|
|
183
|
+
toOffset.x -
|
|
184
|
+
direction.x * arrowSize +
|
|
185
|
+
perpendicular.x * (arrowSize / 2),
|
|
186
|
+
y:
|
|
187
|
+
toOffset.y -
|
|
188
|
+
direction.y * arrowSize +
|
|
189
|
+
perpendicular.y * (arrowSize / 2),
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
x:
|
|
193
|
+
toOffset.x -
|
|
194
|
+
direction.x * arrowSize -
|
|
195
|
+
perpendicular.x * (arrowSize / 2),
|
|
196
|
+
y:
|
|
197
|
+
toOffset.y -
|
|
198
|
+
direction.y * arrowSize -
|
|
199
|
+
perpendicular.y * (arrowSize / 2),
|
|
200
|
+
},
|
|
201
|
+
]
|
|
202
|
+
allPoints.push(...arrow2, arrow2[0]!)
|
|
149
203
|
|
|
150
|
-
|
|
204
|
+
drawPolygon({
|
|
151
205
|
ctx,
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
arrowSize: arrowSize * scaleValue,
|
|
156
|
-
color: lineColor,
|
|
157
|
-
strokeWidth: strokeWidth * scaleValue,
|
|
206
|
+
points: allPoints,
|
|
207
|
+
fill: lineColor,
|
|
208
|
+
realToCanvasMat,
|
|
158
209
|
})
|
|
159
210
|
|
|
160
211
|
// Text
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|