@altpsyche/maths 0.6.0 → 0.7.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/README.md +32 -4
- package/dist/figure/animation.d.ts +19 -0
- package/dist/figure/animation.js +40 -0
- package/dist/figure/equation-match.d.ts +42 -0
- package/dist/figure/equation-match.js +57 -0
- package/dist/figure/equation.d.ts +5 -1
- package/dist/figure/equation.js +5 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,8 @@ svgMarkup(at(figure, 0.5), viewMatrix(figure.extent, 'contain', 640, 360), 640,
|
|
|
25
25
|
The picture arrives rather than appearing. The grid fades, the axes draw on, their labels come in one
|
|
26
26
|
after another, the curve draws, the dot grows out of the origin, and the dot is pointed at where the
|
|
27
27
|
slope is nothing. Then it walks the curve at one speed and flashes at the top. The number in the
|
|
28
|
-
corner is a value of the typeset rule under it
|
|
28
|
+
corner is a value of the typeset rule under it, and that rule reads no slope while the dot is held at
|
|
29
|
+
the stationary point and walks into the one that depends on x as the dot leaves.
|
|
29
30
|
|
|
30
31
|
```ts
|
|
31
32
|
import { axes, coordsOf, group, interval, numberPlane, plot, scaleOf, shape } from '@altpsyche/maths';
|
|
@@ -58,7 +59,7 @@ region is the limit of at the left edge, the right edge or the middle of each on
|
|
|
58
59
|
lays the tangent along the curve, cut where it leaves the graph. `slopeOf` reads the slope itself,
|
|
59
60
|
which is what the number in the corner is.
|
|
60
61
|
|
|
61
|
-
<img src="docs/tangent-strip.svg" width="960" alt="Four frames of the same figure side by side, the point walking up the curve
|
|
62
|
+
<img src="docs/tangent-strip.svg" width="960" alt="Four frames of the same figure side by side, the point walking up the curve, the shaded region growing behind it, and the typeset rule in the corner changing from a slope of nothing to one that depends on x.">
|
|
62
63
|
|
|
63
64
|
Four times of one figure, side by side: the picture arrived, the beat at the stationary point, half
|
|
64
65
|
way up, and the top. A moving picture in a README needs a GIF and this package has no encoder, so the
|
|
@@ -92,6 +93,33 @@ MathJax is the one runtime dependency and the typesetting call is what loads it.
|
|
|
92
93
|
reaches none of it, so a consumer who draws figures and typesets nothing pays nothing. What that
|
|
93
94
|
costs is that typesetting answers with a promise.
|
|
94
95
|
|
|
96
|
+
`matchGlyphs` says which glyph of one expression is which glyph of the other, and `morphEquation`
|
|
97
|
+
walks one into the next: the shared sub-expressions stay put and only the difference moves. Two marks
|
|
98
|
+
match on the part of the leaf name after the first dash, which the typesetter's own naming gives, so
|
|
99
|
+
a glyph is `3-1D465` and a fraction bar is `4-rule`. The pairing is the longest common subsequence of
|
|
100
|
+
the two token sequences, which pairs each occurrence of a repeated glyph once and refuses a pair that
|
|
101
|
+
would cross another pair on the way over.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { equationFromTex, equationNode, group, morphEquation, vec2 } from '@altpsyche/maths';
|
|
105
|
+
|
|
106
|
+
const box = { at: vec2(-4.86, 1.74), align: 'start', width: 1.2, height: 0.6, fill: { colour: '#1b1b1b' } } as const;
|
|
107
|
+
|
|
108
|
+
group('rule', [
|
|
109
|
+
equationNode('at-rest', await equationFromTex('\\frac{dy}{dx} = 0'), box),
|
|
110
|
+
equationNode('moving', await equationFromTex('\\frac{dy}{dx} = 2x'), box),
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
morphEquation('rule/at-rest', 'rule/moving');
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Both expressions are in the scene at every time and the animation moves one onto the other, because a
|
|
117
|
+
mark that arrived part way through a span would turn up in a comparison between two frames as
|
|
118
|
+
something that changed. A paired glyph is drawn once rather than cross-faded, so the glyph being left
|
|
119
|
+
carries the walk and its partner stays at nothing until it is being stood on exactly. Hang both
|
|
120
|
+
expressions from the same edge with `align`, or the part they share slides sideways as the difference
|
|
121
|
+
arrives.
|
|
122
|
+
|
|
95
123
|
Three things stop a typeset expression rather than being drawn, and each names what it found. A TeX
|
|
96
124
|
error carries the typesetter's own message. A character the font has no outline for arrives as text,
|
|
97
125
|
which would draw with whatever font a browser had and draw nothing at all in a recording. An
|
|
@@ -100,8 +128,8 @@ would otherwise ship as a red word inside the picture.
|
|
|
100
128
|
|
|
101
129
|
## The animations
|
|
102
130
|
|
|
103
|
-
`fadeIn`, `fadeOut`, `fadeTo`, `draw`, `morph`, `moveBy`, `rotate`, `scale`,
|
|
104
|
-
`indicate`, `flash` and `circumscribe`. A `Timeline` plays them in order, plays several `together`, or
|
|
131
|
+
`fadeIn`, `fadeOut`, `fadeTo`, `draw`, `morph`, `morphEquation`, `moveBy`, `rotate`, `scale`,
|
|
132
|
+
`growFrom`, `moveAlong`, `indicate`, `flash` and `circumscribe`. A `Timeline` plays them in order, plays several `together`, or
|
|
105
133
|
`stagger`s a row so its parts arrive one after another.
|
|
106
134
|
|
|
107
135
|
A turn and a growth happen about a point the marks decide for themselves, which is the middle of the
|
|
@@ -24,6 +24,25 @@ export declare function draw(target: string): Animation;
|
|
|
24
24
|
* until both hold the same points. A mark with no path is left alone.
|
|
25
25
|
*/
|
|
26
26
|
export declare function morph(target: string, into: Path): Animation;
|
|
27
|
+
/**
|
|
28
|
+
* One typeset expression walked into another, the shared glyphs staying put and
|
|
29
|
+
* only the difference moving.
|
|
30
|
+
*
|
|
31
|
+
* Both expressions are in the scene at every time and this moves one onto the
|
|
32
|
+
* other. A mark that arrived part way through a span would turn up in a
|
|
33
|
+
* comparison between two frames as something that changed, which is what a flash
|
|
34
|
+
* keeps its rays for.
|
|
35
|
+
*
|
|
36
|
+
* A paired glyph is drawn once rather than cross-faded. Two copies of one letter
|
|
37
|
+
* sitting on each other at half opacity through the middle of the span is a
|
|
38
|
+
* ghost, so the glyph being left carries the walk and its partner stays at
|
|
39
|
+
* nothing. At the end it is standing exactly on its partner, so nothing has to be
|
|
40
|
+
* swapped at any moment.
|
|
41
|
+
*
|
|
42
|
+
* An unpaired mark has its own opacity multiplied rather than set, so an
|
|
43
|
+
* expression still fading in when a morph starts does not jump to solid.
|
|
44
|
+
*/
|
|
45
|
+
export declare function morphEquation(from: string, to: string): Animation;
|
|
27
46
|
/** A mark's own opacity walked to a value, for a figure that wants a thing dimmed
|
|
28
47
|
* rather than gone. */
|
|
29
48
|
export declare function fadeTo(target: string, opacity: number): Animation;
|
package/dist/figure/animation.js
CHANGED
|
@@ -17,6 +17,7 @@ import { smoothstep } from '../values/ease.js';
|
|
|
17
17
|
import { circle, line, polygon, transformPath } from './path.js';
|
|
18
18
|
import { trimPath } from './trim.js';
|
|
19
19
|
import { lerpPath } from './morph.js';
|
|
20
|
+
import { matchGlyphs } from './equation-match.js';
|
|
20
21
|
import { pointAlong } from './length.js';
|
|
21
22
|
import { boundsOfMarks, centreOf } from './bounds.js';
|
|
22
23
|
import { interval } from '../values/interval.js';
|
|
@@ -79,6 +80,45 @@ export function morph(target, into) {
|
|
|
79
80
|
return { ...mark, path: lerpPath(mark.path, into, along) };
|
|
80
81
|
});
|
|
81
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* One typeset expression walked into another, the shared glyphs staying put and
|
|
85
|
+
* only the difference moving.
|
|
86
|
+
*
|
|
87
|
+
* Both expressions are in the scene at every time and this moves one onto the
|
|
88
|
+
* other. A mark that arrived part way through a span would turn up in a
|
|
89
|
+
* comparison between two frames as something that changed, which is what a flash
|
|
90
|
+
* keeps its rays for.
|
|
91
|
+
*
|
|
92
|
+
* A paired glyph is drawn once rather than cross-faded. Two copies of one letter
|
|
93
|
+
* sitting on each other at half opacity through the middle of the span is a
|
|
94
|
+
* ghost, so the glyph being left carries the walk and its partner stays at
|
|
95
|
+
* nothing. At the end it is standing exactly on its partner, so nothing has to be
|
|
96
|
+
* swapped at any moment.
|
|
97
|
+
*
|
|
98
|
+
* An unpaired mark has its own opacity multiplied rather than set, so an
|
|
99
|
+
* expression still fading in when a morph starts does not jump to solid.
|
|
100
|
+
*/
|
|
101
|
+
export function morphEquation(from, to) {
|
|
102
|
+
return (marks, along) => {
|
|
103
|
+
const leaving = marks.filter((mark) => touches(mark.id, from));
|
|
104
|
+
const arriving = marks.filter((mark) => touches(mark.id, to));
|
|
105
|
+
if (leaving.length === 0 || arriving.length === 0)
|
|
106
|
+
return marks;
|
|
107
|
+
const changed = new Map();
|
|
108
|
+
for (const [left, right] of matchGlyphs(leaving, arriving).pairs) {
|
|
109
|
+
changed.set(left.id, { ...left, path: lerpPath(left.path, right.path, along) });
|
|
110
|
+
changed.set(right.id, { ...right, opacity: 0 });
|
|
111
|
+
}
|
|
112
|
+
const faded = (mark, to) => ({ ...mark, opacity: (mark.opacity ?? 1) * to });
|
|
113
|
+
for (const mark of leaving)
|
|
114
|
+
if (!changed.has(mark.id))
|
|
115
|
+
changed.set(mark.id, faded(mark, 1 - along));
|
|
116
|
+
for (const mark of arriving)
|
|
117
|
+
if (!changed.has(mark.id))
|
|
118
|
+
changed.set(mark.id, faded(mark, along));
|
|
119
|
+
return marks.map((mark) => changed.get(mark.id) ?? mark);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
82
122
|
/** A mark's own opacity walked to a value, for a figure that wants a thing dimmed
|
|
83
123
|
* rather than gone. */
|
|
84
124
|
export function fadeTo(target, opacity) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which glyph of one typeset expression is which glyph of the other.
|
|
3
|
+
*
|
|
4
|
+
* A glyph is named for its place in the expression and the code point the
|
|
5
|
+
* typesetter wrote on it, so `3-1D465` is the fourth mark and the letter x, and
|
|
6
|
+
* a fraction bar is `4-rule`. The token two marks match on is the part of that
|
|
7
|
+
* name after the first dash, which needs no parsing and makes a rule a token of
|
|
8
|
+
* its own.
|
|
9
|
+
*
|
|
10
|
+
* The pairing is the longest common subsequence of the two token sequences,
|
|
11
|
+
* which is the longest run of tokens appearing in both lists in the same order.
|
|
12
|
+
* Matching in order is what stops the x of a numerator pairing with the x of a
|
|
13
|
+
* right-hand side, and it pairs each occurrence of a repeated glyph once rather
|
|
14
|
+
* than pairing several to one partner.
|
|
15
|
+
*
|
|
16
|
+
* The token is read off the id rather than carried beside the mark because a
|
|
17
|
+
* consumer stores a typeset equation rather than typesetting it again, and what
|
|
18
|
+
* it stores is a path and the id it belongs to. A code point kept elsewhere
|
|
19
|
+
* would have to be stored elsewhere too, and the matching would fail on
|
|
20
|
+
* everything read back from a cache that did not.
|
|
21
|
+
*/
|
|
22
|
+
import type { Mark, PathMark } from './mark.js';
|
|
23
|
+
export interface GlyphMatch {
|
|
24
|
+
/** Each glyph of the expression being left beside the one it becomes. */
|
|
25
|
+
readonly pairs: readonly (readonly [PathMark, PathMark])[];
|
|
26
|
+
/** Glyphs of the expression being left that nothing in the other answers. */
|
|
27
|
+
readonly leaving: readonly PathMark[];
|
|
28
|
+
/** Glyphs of the expression being arrived at that nothing in the first
|
|
29
|
+
* answers. */
|
|
30
|
+
readonly arriving: readonly PathMark[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* What a mark matches on: its leaf name after the first dash.
|
|
34
|
+
*
|
|
35
|
+
* A mark whose leaf carries no dash is not a glyph a typesetter wrote, and it
|
|
36
|
+
* pairs with nothing rather than pairing with every other mark that is also
|
|
37
|
+
* unnamed.
|
|
38
|
+
*/
|
|
39
|
+
export declare function glyphToken(id: string): string | undefined;
|
|
40
|
+
/** Two typeset expressions paired glyph by glyph, with what neither answers kept
|
|
41
|
+
* apart. Marks that are not paths are left out, since a glyph is an outline. */
|
|
42
|
+
export declare function matchGlyphs(from: readonly Mark[], to: readonly Mark[]): GlyphMatch;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a mark matches on: its leaf name after the first dash.
|
|
3
|
+
*
|
|
4
|
+
* A mark whose leaf carries no dash is not a glyph a typesetter wrote, and it
|
|
5
|
+
* pairs with nothing rather than pairing with every other mark that is also
|
|
6
|
+
* unnamed.
|
|
7
|
+
*/
|
|
8
|
+
export function glyphToken(id) {
|
|
9
|
+
const leaf = id.slice(id.lastIndexOf('/') + 1);
|
|
10
|
+
const dash = leaf.indexOf('-');
|
|
11
|
+
return dash < 0 ? undefined : leaf.slice(dash + 1);
|
|
12
|
+
}
|
|
13
|
+
/** The table of longest common subsequence lengths, read back from the end to
|
|
14
|
+
* give the pairs themselves. */
|
|
15
|
+
function pairedPlaces(from, to) {
|
|
16
|
+
const rows = from.length;
|
|
17
|
+
const columns = to.length;
|
|
18
|
+
const longest = Array.from({ length: rows + 1 }, () => new Array(columns + 1).fill(0));
|
|
19
|
+
for (let row = rows - 1; row >= 0; row--) {
|
|
20
|
+
for (let column = columns - 1; column >= 0; column--) {
|
|
21
|
+
const same = from[row] !== undefined && from[row] === to[column];
|
|
22
|
+
longest[row][column] = same
|
|
23
|
+
? longest[row + 1][column + 1] + 1
|
|
24
|
+
: Math.max(longest[row + 1][column], longest[row][column + 1]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const places = [];
|
|
28
|
+
let row = 0;
|
|
29
|
+
let column = 0;
|
|
30
|
+
while (row < rows && column < columns) {
|
|
31
|
+
if (from[row] !== undefined && from[row] === to[column]) {
|
|
32
|
+
places.push([row, column]);
|
|
33
|
+
row++;
|
|
34
|
+
column++;
|
|
35
|
+
}
|
|
36
|
+
else if (longest[row + 1][column] >= longest[row][column + 1])
|
|
37
|
+
row++;
|
|
38
|
+
else
|
|
39
|
+
column++;
|
|
40
|
+
}
|
|
41
|
+
return places;
|
|
42
|
+
}
|
|
43
|
+
const glyphsOf = (marks) => marks.filter((mark) => mark.kind === 'path');
|
|
44
|
+
/** Two typeset expressions paired glyph by glyph, with what neither answers kept
|
|
45
|
+
* apart. Marks that are not paths are left out, since a glyph is an outline. */
|
|
46
|
+
export function matchGlyphs(from, to) {
|
|
47
|
+
const left = glyphsOf(from);
|
|
48
|
+
const right = glyphsOf(to);
|
|
49
|
+
const places = pairedPlaces(left.map((mark) => glyphToken(mark.id)), right.map((mark) => glyphToken(mark.id)));
|
|
50
|
+
const takenLeft = new Set(places.map(([row]) => row));
|
|
51
|
+
const takenRight = new Set(places.map(([, column]) => column));
|
|
52
|
+
return {
|
|
53
|
+
pairs: places.map(([row, column]) => [left[row], right[column]]),
|
|
54
|
+
leaving: left.filter((_, at) => !takenLeft.has(at)),
|
|
55
|
+
arriving: right.filter((_, at) => !takenRight.has(at)),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -19,8 +19,12 @@ export declare function equationMarks(root: EquationElement): Equation;
|
|
|
19
19
|
* they are always used in. */
|
|
20
20
|
export declare function equationFromTex(tex: string): Promise<Equation>;
|
|
21
21
|
export interface EquationOptions {
|
|
22
|
-
/**
|
|
22
|
+
/** The point the expression is placed against, in the figure's own units. */
|
|
23
23
|
readonly at: Vec2;
|
|
24
|
+
/** Which edge of the expression sits on that point across, the middle of it
|
|
25
|
+
* unless named. Two expressions placed at one point by their start keep the
|
|
26
|
+
* part they share in the same place. */
|
|
27
|
+
readonly align?: 'start' | 'middle' | 'end';
|
|
24
28
|
/** The box the expression is fitted inside, in the figure's own units. */
|
|
25
29
|
readonly width: number;
|
|
26
30
|
readonly height: number;
|
package/dist/figure/equation.js
CHANGED
|
@@ -172,7 +172,10 @@ export async function equationFromTex(tex) {
|
|
|
172
172
|
export function equationNode(name, equation, options) {
|
|
173
173
|
const { box } = equation;
|
|
174
174
|
const fit = Math.min(options.width / box.width, options.height / box.height);
|
|
175
|
-
|
|
176
|
-
|
|
175
|
+
// The point the expression is hung from, in the typesetter's units, so that
|
|
176
|
+
// scaling about it lands the asked-for edge on the asked-for place.
|
|
177
|
+
const across = options.align === 'start' ? box.x : options.align === 'end' ? box.x + box.width : box.x + box.width / 2;
|
|
178
|
+
const hung = vec2(across, box.y + box.height / 2);
|
|
179
|
+
const transform = mat3.multiply(mat3.translation(options.at), mat3.multiply(mat3.scaling(vec2(fit, fit)), mat3.translation(vec2(-hung.x, -hung.y))));
|
|
177
180
|
return group(name, equation.marks.map((mark) => shape(mark.id, mark.path, { fill: options.fill })), { transform });
|
|
178
181
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export { labelFor, tickStep, ticksOn } from './figure/ticks.js';
|
|
|
38
38
|
export type { Tick } from './figure/ticks.js';
|
|
39
39
|
export { flatten, group, shape, text } from './figure/node.js';
|
|
40
40
|
export type { GroupNode, Node, ShapeNode, Style, TextNode, TextOptions } from './figure/node.js';
|
|
41
|
-
export { circumscribe, fadeIn, fadeOut, fadeTo, draw, flash, growFrom, indicate, morph, moveAlong, moveBy, rotate, scale } from './figure/animation.js';
|
|
41
|
+
export { circumscribe, fadeIn, fadeOut, fadeTo, draw, flash, growFrom, indicate, morph, morphEquation, moveAlong, moveBy, rotate, scale } from './figure/animation.js';
|
|
42
42
|
export type { AboutOptions, Animation, CircumscribeOptions, FlashOptions, IndicateOptions, ScaleOptions } from './figure/animation.js';
|
|
43
43
|
export { Timeline } from './figure/timeline.js';
|
|
44
44
|
export type { PlayOptions, Span, StaggerOptions } from './figure/timeline.js';
|
|
@@ -57,3 +57,5 @@ export { typesetElement } from './figure/typeset.js';
|
|
|
57
57
|
export type { EquationElement } from './figure/typeset.js';
|
|
58
58
|
export { equationFromTex, equationMarks, equationNode } from './figure/equation.js';
|
|
59
59
|
export type { Equation, EquationBox, EquationOptions } from './figure/equation.js';
|
|
60
|
+
export { glyphToken, matchGlyphs } from './figure/equation-match.js';
|
|
61
|
+
export type { GlyphMatch } from './figure/equation-match.js';
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ export { axes, numberLine, numberPlane } from './figure/axis.js';
|
|
|
23
23
|
export { coordsOf, pointOf, scaleOf, scaled, unscaled } from './figure/scale.js';
|
|
24
24
|
export { labelFor, tickStep, ticksOn } from './figure/ticks.js';
|
|
25
25
|
export { flatten, group, shape, text } from './figure/node.js';
|
|
26
|
-
export { circumscribe, fadeIn, fadeOut, fadeTo, draw, flash, growFrom, indicate, morph, moveAlong, moveBy, rotate, scale } from './figure/animation.js';
|
|
26
|
+
export { circumscribe, fadeIn, fadeOut, fadeTo, draw, flash, growFrom, indicate, morph, morphEquation, moveAlong, moveBy, rotate, scale } from './figure/animation.js';
|
|
27
27
|
export { Timeline } from './figure/timeline.js';
|
|
28
28
|
export { lengthOf, pointAlong } from './figure/length.js';
|
|
29
29
|
export { trimPath } from './figure/trim.js';
|
|
@@ -34,3 +34,4 @@ export { paintCanvas } from './paint/canvas.js';
|
|
|
34
34
|
export { arrow, callout, dot } from './figure/annotate.js';
|
|
35
35
|
export { typesetElement } from './figure/typeset.js';
|
|
36
36
|
export { equationFromTex, equationMarks, equationNode } from './figure/equation.js';
|
|
37
|
+
export { glyphToken, matchGlyphs } from './figure/equation-match.js';
|
package/package.json
CHANGED