strokes-js 0.1.0 → 0.1.1
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/README.md +9 -0
- package/dist/renderer.js +18 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -197,6 +197,15 @@ export interface GlyphEntry {
|
|
|
197
197
|
|
|
198
198
|
Adding or editing a glyph means editing its own file under `glyphs/letters`, `glyphs/digits`, or `glyphs/punctuation` — no other file needs to change except `glyphs.ts`'s import/lookup if you're adding a brand-new character.
|
|
199
199
|
|
|
200
|
+
## Development
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
npm run build # tsc — compiles src/ to dist/ (ESM + .d.ts)
|
|
204
|
+
npm run typecheck # tsc --noEmit
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
There is no test suite or lint script yet.
|
|
208
|
+
|
|
200
209
|
## Demo
|
|
201
210
|
|
|
202
211
|
`demo/index.html` is a static page (loads `../dist/index.js`, so run `npm run build` first) with:
|
package/dist/renderer.js
CHANGED
|
@@ -16,9 +16,8 @@ function pickVariant(char, entry) {
|
|
|
16
16
|
lastVariant.set(char, index);
|
|
17
17
|
return index;
|
|
18
18
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
return text.split(/\s+/).filter((w) => w.length > 0);
|
|
19
|
+
function splitText(text) {
|
|
20
|
+
return text.split(/(\s+)/).filter((part) => part.length > 0);
|
|
22
21
|
}
|
|
23
22
|
const JITTER_ROTATE_DEG = 7;
|
|
24
23
|
const JITTER_BASELINE = 1;
|
|
@@ -72,15 +71,20 @@ function renderWord(word, delayOffset, animate) {
|
|
|
72
71
|
*/
|
|
73
72
|
export function renderText(text, options = {}) {
|
|
74
73
|
const { animate = true } = options;
|
|
75
|
-
const
|
|
74
|
+
const parts = splitText(text);
|
|
76
75
|
let delayOffset = 0;
|
|
77
|
-
const
|
|
78
|
-
for (const
|
|
76
|
+
const markup = [];
|
|
77
|
+
for (const part of parts) {
|
|
78
|
+
if (/^\s+$/.test(part)) {
|
|
79
|
+
markup.push(`<span class="hw-space" aria-hidden="true" style="white-space: pre-wrap">${escapeHtml(part)}</span>`);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const word = part;
|
|
79
83
|
const { svg, letterCount } = renderWord(word, delayOffset, animate);
|
|
80
|
-
|
|
84
|
+
markup.push(svg);
|
|
81
85
|
delayOffset += letterCount;
|
|
82
86
|
}
|
|
83
|
-
return `<span class="hw-sentence" aria-label="${escapeAttr(text)}">${
|
|
87
|
+
return `<span class="hw-sentence" aria-label="${escapeAttr(text)}">${markup.join("")}</span>`;
|
|
84
88
|
}
|
|
85
89
|
function escapeAttr(value) {
|
|
86
90
|
return value
|
|
@@ -89,6 +93,12 @@ function escapeAttr(value) {
|
|
|
89
93
|
.replace(/</g, "<")
|
|
90
94
|
.replace(/>/g, ">");
|
|
91
95
|
}
|
|
96
|
+
function escapeHtml(value) {
|
|
97
|
+
return value
|
|
98
|
+
.replace(/&/g, "&")
|
|
99
|
+
.replace(/</g, "<")
|
|
100
|
+
.replace(/>/g, ">");
|
|
101
|
+
}
|
|
92
102
|
/** Toggles the class that starts the draw-in transition for a rendered word's strokes. */
|
|
93
103
|
export function animateWriting(el) {
|
|
94
104
|
el.classList.add("hw-visible");
|