@adecore/drawing 0.17.0-beta.1 → 0.17.0-beta.2
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/LICENSE +1 -1
- package/README.md +38 -31
- package/dist/svg.js +3 -3
- package/package.json +1 -1
- package/src/svg.ts +3 -3
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,42 +1,49 @@
|
|
|
1
1
|
# @adecore/drawing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@adecore/drawing)
|
|
4
|
+
[](https://adecore.dev/drawing/)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
Hand-drawn shapes, arrows, freehand strokes, text and sticky notes as plain JSON, with hit tests and resizing for an editor, paths for a painter, an SVG export and a reading of a drawing as lines of text. It needs no DOM, so a backend exports a stored drawing the same way a page draws it.
|
|
7
|
+
|
|
8
|
+
**[Documentation with live demos](https://adecore.dev/drawing/)**
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
bun add @adecore/drawing
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Use
|
|
6
17
|
|
|
7
18
|
```ts
|
|
8
19
|
import { DEFAULT_PALETTE, readingOrder, toSvg } from '@adecore/drawing';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
elements: [
|
|
15
|
-
{
|
|
16
|
-
kind: 'note',
|
|
17
|
-
id: 'note',
|
|
18
|
-
x: 0,
|
|
19
|
-
y: 0,
|
|
20
|
-
w: 180,
|
|
21
|
-
h: 100,
|
|
22
|
-
stroke: 'ink',
|
|
23
|
-
strokeWidth: 1,
|
|
24
|
-
seed: 7,
|
|
25
|
-
fillColor: 'yellow',
|
|
26
|
-
text: 'Review the result',
|
|
27
|
-
size: 20
|
|
28
|
-
}
|
|
29
|
-
]
|
|
30
|
-
});
|
|
31
|
-
if (duplicateElementIdIn(document.elements) !== null) {
|
|
32
|
-
throw new Error('Element ids must be unique');
|
|
20
|
+
import { duplicateElementIdIn, migrateDrawing } from '@adecore/drawing/protocol';
|
|
21
|
+
|
|
22
|
+
const drawing = migrateDrawing(JSON.parse(file));
|
|
23
|
+
if (drawing === null || duplicateElementIdIn(drawing.elements) !== null) {
|
|
24
|
+
throw new Error('Not a drawing');
|
|
33
25
|
}
|
|
34
|
-
|
|
35
|
-
const
|
|
26
|
+
|
|
27
|
+
const svg = toSvg(drawing.elements, { palette: DEFAULT_PALETTE });
|
|
28
|
+
const lines = readingOrder(drawing.elements);
|
|
36
29
|
```
|
|
37
30
|
|
|
38
|
-
|
|
31
|
+
## Entry points
|
|
32
|
+
|
|
33
|
+
| Import | What it holds |
|
|
34
|
+
|---|---|
|
|
35
|
+
| `@adecore/drawing` | Geometry, paths, SVG export, text and reading order |
|
|
36
|
+
| `@adecore/drawing/protocol` | The Zod schemas and types of a drawing; loads nothing else |
|
|
37
|
+
|
|
38
|
+
## Documentation
|
|
39
|
+
|
|
40
|
+
| Page | What it covers |
|
|
41
|
+
|---|---|
|
|
42
|
+
| [Documents](https://adecore.dev/drawing/documents) | The document, the seven element kinds and reading a file |
|
|
43
|
+
| [Geometry](https://adecore.dev/drawing/geometry) | Bounds, hit tests, selection and resizing |
|
|
44
|
+
| [Paths and SVG](https://adecore.dev/drawing/rendering) | `toSvg` and the paths of one element for a painter of your own |
|
|
45
|
+
| [Text and reading order](https://adecore.dev/drawing/text) | Wrapping, measuring and `readingOrder` |
|
|
39
46
|
|
|
40
|
-
|
|
47
|
+
## License
|
|
41
48
|
|
|
42
|
-
|
|
49
|
+
FSL-1.1-MIT, see [LICENSE](./LICENSE). Rough.js and perfect-freehand keep their own licenses.
|
package/dist/svg.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { boundsOfElements
|
|
1
|
+
import { boundsOfElements } from './geometry.js';
|
|
2
2
|
import { pathsOfElement } from './paths.js';
|
|
3
3
|
import { DEFAULT_FONT_STACKS, LINE_HEIGHT, approximateMeasure, fontOf, linesOf, writingFrameOf } from './text.js';
|
|
4
4
|
export const DEFAULT_SVG_MARGIN = 32;
|
|
@@ -51,8 +51,8 @@ function round(value) {
|
|
|
51
51
|
return Math.round(value * 100) / 100;
|
|
52
52
|
}
|
|
53
53
|
function transformOf(element) {
|
|
54
|
-
|
|
55
|
-
const turn = element.angle ? ` rotate(${round((element.angle * 180) / Math.PI)} ${round(
|
|
54
|
+
// After the translate the paths are in the element's own frame, so its center is half its size.
|
|
55
|
+
const turn = element.angle ? ` rotate(${round((element.angle * 180) / Math.PI)} ${round(element.w / 2)} ${round(element.h / 2)})` : '';
|
|
56
56
|
return `translate(${round(element.x)} ${round(element.y)})${turn}`;
|
|
57
57
|
}
|
|
58
58
|
function textSvg(element, options) {
|
package/package.json
CHANGED
package/src/svg.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DrawingColor, DrawingElement, DrawingFont } from './protocol.ts';
|
|
2
|
-
import { boundsOfElements,
|
|
2
|
+
import { boundsOfElements, type Rect } from './geometry.ts';
|
|
3
3
|
import { pathsOfElement } from './paths.ts';
|
|
4
4
|
import { DEFAULT_FONT_STACKS, LINE_HEIGHT, approximateMeasure, fontOf, linesOf, writingFrameOf, type MeasureLine, type WrittenElement } from './text.ts';
|
|
5
5
|
|
|
@@ -76,8 +76,8 @@ function round(value: number): number {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
function transformOf(element: DrawingElement): string {
|
|
79
|
-
|
|
80
|
-
const turn = element.angle ? ` rotate(${round((element.angle * 180) / Math.PI)} ${round(
|
|
79
|
+
// After the translate the paths are in the element's own frame, so its center is half its size.
|
|
80
|
+
const turn = element.angle ? ` rotate(${round((element.angle * 180) / Math.PI)} ${round(element.w / 2)} ${round(element.h / 2)})` : '';
|
|
81
81
|
return `translate(${round(element.x)} ${round(element.y)})${turn}`;
|
|
82
82
|
}
|
|
83
83
|
|