paper-svg 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 lokkit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,188 @@
1
- # Temporary Holding Version
1
+ # paper-svg
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Build articulated 2D characters and puppets from a tree of joints and sprites, then render them to SVG with React.
4
+
5
+ You describe a skeleton with chainable builders. `paper-svg` resolves the transforms, layers the sprites by z-index, and generates the masks, clip paths and outlines.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install paper-svg
11
+ ```
12
+
13
+ `react` 18 or later is a peer dependency.
14
+
15
+ ## Quick start
16
+
17
+ ```tsx
18
+ import { NodeBuilder, Rig, SpriteBuilder } from "paper-svg";
19
+
20
+ const skin = new SpriteBuilder().setFill("#e8b796").setStroke("#222").setStrokeWidth(2);
21
+
22
+ const forearm = new NodeBuilder("forearm")
23
+ .setJointPosition(0, -60)
24
+ .setRotation(25)
25
+ .setSprite(skin.setShape("rect").setScale(0.9, 6).setOffset(0, -30).build());
26
+
27
+ const arm = new NodeBuilder("arm")
28
+ .setJointPosition(30, 40)
29
+ .setRotation(20)
30
+ .setSprite(skin.setShape("rect").setScale(1, 6).setOffset(0, -30).build());
31
+
32
+ const body = new NodeBuilder("torso")
33
+ .setSprite(skin.setShape("ellipse").setScale(6, 10).build())
34
+ .addJoints(
35
+ arm.addJoints(forearm.buildRight()).buildRight(),
36
+ arm.addJoints(forearm.buildLeft()).buildLeft(),
37
+ )
38
+ .build();
39
+
40
+ export const Character = () => (
41
+ <svg viewBox="-150 -150 300 300">
42
+ <Rig body={body} />
43
+ </svg>
44
+ );
45
+ ```
46
+
47
+ `Rig` renders SVG elements, so place it inside an `<svg>`. The root node's joint is at the origin of that SVG, so use a `viewBox` that includes `0, 0`.
48
+
49
+ ## Concepts
50
+
51
+ ### Coordinates
52
+
53
+ - Positive y points **up**, for both joint positions and sprite offsets.
54
+ - Positive rotation is **clockwise**, in degrees.
55
+ - A joint is a node's pivot. Its sprite is drawn relative to the joint, so offset a limb by half its length to make it hang from the joint.
56
+ - Built-in shapes are 10 units across at scale 1.
57
+
58
+ ### Joints and attachments
59
+
60
+ A node has two kinds of children:
61
+
62
+ | | `addJoints` | `addAttachments` |
63
+ | ------------------- | ------------------------------ | ----------------------------- |
64
+ | Use for | Limbs and other moving parts | Details that ride on a parent |
65
+ | Parent's scale | Moves the child, does not resize it | Moves and resizes the child |
66
+ | Rotates when pressed | Yes | No, the press goes to the nearest articulated ancestor |
67
+
68
+ Use `setArticulated(true)` or `setArticulated(false)` to override the default for a node.
69
+
70
+ ### Builders
71
+
72
+ `NodeBuilder` and `SpriteBuilder` are immutable. Every call returns a new builder, so you can share a base and branch from it, as `skin` and `arm` do above.
73
+
74
+ A few methods combine with earlier calls instead of replacing them:
75
+
76
+ | Method | Effect |
77
+ | ---------------------------------------------------- | ---------------------- |
78
+ | `NodeBuilder.setJointPosition`, `translatesWithX/Y` | Adds to the position |
79
+ | `NodeBuilder.setScale`, `scalesWith`, `scalesWithX/Y` | Multiplies the scale |
80
+ | `rotatesWith` | Adds to the rotation |
81
+ | `NodeBuilder.setRotation`, all `SpriteBuilder` `set*` | Replaces the value |
82
+
83
+ ### Left and right sides
84
+
85
+ Build one side, then mirror it:
86
+
87
+ - `build()` keeps the node's name as its id.
88
+ - `buildRight()` adds the suffix `_right`.
89
+ - `buildLeft()` adds the suffix `_left` and mirrors x positions, sprites and rotation.
90
+
91
+ Children are already built when you add them, so build them for the matching side, as in the quick start.
92
+
93
+ ### Layering
94
+
95
+ `setZIndex(z)` is relative to the parent by default. `setZIndex(z, "absolute")` ignores the parent.
96
+
97
+ `setOutlineZIndex(z)` draws the node's stroke as a separate layer. Put it below the fills of neighboring parts to make connected parts look like one outlined shape.
98
+
99
+ ### Masks and clips
100
+
101
+ Nodes can cut each other out, across different branches of the tree. Both systems work by group name:
102
+
103
+ | Purpose | Soft mask (`<mask>`) | Hard clip (`<clipPath>`) |
104
+ | ----------------------- | -------------------- | ------------------------ |
105
+ | Add a shape to a group | `addMaskGroup(name)` | `addClippingGroup(name)` |
106
+ | Restrict a node to it | `addMaskedBy(name)` | `addClippedBy(name)` |
107
+
108
+ Masks support soft edges and negative groups (`addNegativeMaskGroup`). Clips are cheaper and have hard edges.
109
+
110
+ `inheritMaskingForChildren()` passes a node's restrictions to its descendants. `inheritGroupsForChildren()` adds its descendants to its groups.
111
+
112
+ > A node restricted to a group that no node defines is not drawn.
113
+
114
+ ### Constraints
115
+
116
+ Both constraints use a target's full id, including any `_left` or `_right` suffix. A node can target any node in the tree.
117
+
118
+ - `setAimAt(targetId, point)` keeps a node rotated toward a point on the target.
119
+ - `setPinTo(targetId, point)` keeps a node's joint on a point on the target.
120
+
121
+ ## Custom sprites
122
+
123
+ Turn SVG markup into a sprite. Each distinct fill becomes a color slot, numbered in the order it first appears.
124
+
125
+ ```ts
126
+ import { makeSprite, registerCustomSprite, SpriteBuilder } from "paper-svg";
127
+
128
+ registerCustomSprite(makeSprite("hand", svgText));
129
+
130
+ const hand = new SpriteBuilder()
131
+ .setCustomSprite("hand")
132
+ .setCustomColors("#e8b796", "#c9926f")
133
+ .setStroke("#222")
134
+ .setStrokeWidth(2)
135
+ .build();
136
+ ```
137
+
138
+ Register a sprite before you call `setCustomSprite` with its id, because the name is resolved at that call. You can also pass the `CustomSprite` object directly.
139
+
140
+ `makeSprite` measures paths with the DOM, so it runs only in a browser.
141
+
142
+ ## `Rig` props
143
+
144
+ | Prop | Description |
145
+ | ----------------------- | --------------------------------------------------------------------------- |
146
+ | `body` | Root `Node` to render. Required. |
147
+ | `idPrefix` | Prefix for generated SVG ids. Give each rig in a document a unique value. |
148
+ | `scale`, `mirror` | Scale or horizontally flip the whole rig. |
149
+ | `onRotate` | `(id, pivot) => (event) => void`. Called when an articulated node is pressed. |
150
+ | `colorTransform` | Changes each sprite color at paint time, for example for scene lighting. |
151
+ | `effects` | `outerOutline`, `glow`, `tint`, `blur`, `opacity`, `isHidden`. |
152
+ | `getRenderGroupOptions` | Mask, clip, opacity and blend mode for nodes grouped with `setRenderGroup`. |
153
+ | `showPivots` | Draws joint markers for debugging. |
154
+
155
+ ### Rotation
156
+
157
+ `Rig` does not store poses. `onRotate` gives you the id of the node to rotate and its joint's screen position. Track the pointer, store the angle in your state, and rebuild the tree with the new `setRotation` value.
158
+
159
+ ### Zoom
160
+
161
+ Strokes keep the same width on screen when the SVG is scaled. If you zoom by changing the `viewBox`, set `getZoomVars(zoom)` as the `<svg>` element's `style` so stroke widths follow the zoom.
162
+
163
+ ## Lower-level APIs
164
+
165
+ | API | Use |
166
+ | -------------------------------------------------- | ---------------------------------------------------- |
167
+ | `getRenderNodes`, `buildRenderTree` | Resolve a tree to world matrices without React. |
168
+ | `useRenderNodes`, `RigLayers` | Add your own selection, overlays or filters. |
169
+ | `resolveRotationPivot` | Find a joint's screen position in a custom handler. |
170
+ | `SvgSprite` | Draw one sprite, for example in a thumbnail. |
171
+ | `matrixToTransform`, `applyMatrixToPoint` and others | Matrix and drag math. |
172
+
173
+ ## Server rendering
174
+
175
+ `Rig` renders on the server. Gradients are the exception: their definitions are added on the client, so gradient fills appear after hydration.
176
+
177
+ ## Demo
178
+
179
+ The repository includes a posable puppet that shows joints, mirroring, clipping, constraints, custom sprites and effects:
180
+
181
+ ```sh
182
+ npm install
183
+ npm run demo
184
+ ```
185
+
186
+ ## License
187
+
188
+ [MIT](./LICENSE)