gum-jsx 1.6.1 → 1.6.3
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 +2 -3
- package/docs/code/Arc.jsx +3 -3
- package/docs/code/Axis.jsx +1 -1
- package/docs/code/Element.jsx +5 -3
- package/docs/code/Ellipse.jsx +2 -2
- package/docs/code/Images.jsx +1 -1
- package/docs/code/Line.jsx +8 -13
- package/docs/code/Math.jsx +12 -14
- package/docs/code/Network.jsx +1 -1
- package/docs/code/Points.jsx +4 -1
- package/docs/code/RoundedLine.jsx +7 -11
- package/docs/code/Shape.jsx +8 -14
- package/docs/code/Spline.jsx +5 -3
- package/docs/code/SymFill.jsx +1 -1
- package/docs/code/SymShape.jsx +3 -4
- package/docs/code/Tables.jsx +3 -2
- package/docs/text/Arrays.md +1 -1
- package/docs/text/Box.md +1 -1
- package/docs/text/Element.md +11 -14
- package/docs/text/Gum.md +5 -5
- package/docs/text/Math.md +2 -1
- package/docs/text/SymFill.md +2 -1
- package/docs/text/SymLine.md +2 -1
- package/docs/text/SymPoints.md +2 -1
- package/docs/text/SymShape.md +2 -1
- package/docs/text/SymSpline.md +1 -0
- package/gala/code/atomic_orbitals.jsx +1 -2
- package/gala/code/complex_plot.jsx +1 -1
- package/gala/code/pendulum_physics.jsx +3 -3
- package/gala/code/plot_manual.jsx +2 -2
- package/gala/code/polygon_slide.jsx +3 -4
- package/gala/code/spline_star.jsx +2 -2
- package/package.json +1 -1
- package/prompt/intro.md +2 -3
- package/scripts/gum.ts +14 -11
- package/scripts/test.ts +0 -69
- package/skills/gum-jsx/SKILL.md +19 -21
- package/skills/gum-jsx/references/gala/atomic_orbitals.md +1 -2
- package/skills/gum-jsx/references/gala/complex_plot.md +1 -1
- package/skills/gum-jsx/references/gala/pendulum_physics.md +3 -3
- package/skills/gum-jsx/references/gala/plot_manual.md +2 -2
- package/skills/gum-jsx/references/gala/polygon_slide.md +3 -4
- package/skills/gum-jsx/references/gala/spline_star.md +2 -2
- package/skills/gum-jsx/references/geometry.md +32 -45
- package/skills/gum-jsx/references/layout.md +5 -2
- package/skills/gum-jsx/references/networks.md +1 -1
- package/skills/gum-jsx/references/plotting.md +1 -1
- package/skills/gum-jsx/references/symbolic.md +12 -8
- package/skills/gum-jsx/references/utilities.md +19 -19
- package/src/elems/core.ts +33 -13
- package/src/elems/geometry.ts +10 -12
- package/src/elems/layout.ts +5 -7
- package/src/elems/network.ts +4 -3
- package/src/elems/plot.ts +19 -35
- package/src/elems/symbolic.ts +30 -24
- package/src/eval.ts +13 -3
- package/src/gum.ts +3 -3
- package/src/lib/interp.ts +10 -4
- package/src/lib/types.ts +1 -2
- package/src/lib/utils.ts +37 -29
- package/src/render.ts +7 -27
package/README.md
CHANGED
|
@@ -91,11 +91,10 @@ CLI options:
|
|
|
91
91
|
| Option | Description | Default |
|
|
92
92
|
|--------|-------------|---------|
|
|
93
93
|
| `file` | Gum JSX file to render | stdin |
|
|
94
|
-
| `-s, --size <size>` |
|
|
94
|
+
| `-s, --size <size>` | SVG/viewBox size in pixels | 1000 |
|
|
95
95
|
| `-t, --theme <theme>` | Theme: `light` or `dark` | light |
|
|
96
96
|
| `-b, --background <color>` | Background color | white |
|
|
97
97
|
| `-f, --format <format>` | Format: `json`, `svg`, `png`, `kitty` | auto |
|
|
98
98
|
| `-o, --output <output>` | Output file | stdout |
|
|
99
|
-
| `-
|
|
100
|
-
| `-h, --height <height>` | Max height of the PNG | auto |
|
|
99
|
+
| `-r, --raster-size <size>` | Max rasterized PNG size | auto |
|
|
101
100
|
| `-d, --dev` | Live update display | off |
|
package/docs/code/Arc.jsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// elliptical and circular arcs using start and end angles
|
|
2
2
|
<Group>
|
|
3
|
-
<Arc pos={[0.
|
|
4
|
-
<Arc pos={[0.
|
|
5
|
-
</Group>
|
|
3
|
+
<Arc pos={[0.3, 0.5]} rad={[0.2, 0.15]} start={-45} end={210} stroke={blue} stroke-width={2} />
|
|
4
|
+
<Arc pos={[0.7, 0.5]} rad={0.15} start={90} end={-150} stroke={red} stroke-width={2} />
|
|
5
|
+
</Group>
|
package/docs/code/Axis.jsx
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
const emoji = ['🗻', '🚀', '🐳', '🍉', '🍩']
|
|
3
3
|
const ticks = zip(linspace(0, 1, emoji.length), emoji)
|
|
4
4
|
return <Box padding={[0.5, 1]}>
|
|
5
|
-
<HAxis aspect={10} ticks={ticks} tick-side="outer" label-size={1} />
|
|
5
|
+
<HAxis aspect={10} ticks={ticks} tick-side="outer" label-size={1} label-offset={0.25} />
|
|
6
6
|
</Box>
|
package/docs/code/Element.jsx
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// draw a 2x1 rectangle in red with a blue square embedded within it
|
|
2
|
+
<Group aspect={2}>
|
|
3
|
+
<Rect stroke={red} />
|
|
4
|
+
<Square stroke={blue} />
|
|
5
|
+
</Group>
|
package/docs/code/Ellipse.jsx
CHANGED
package/docs/code/Images.jsx
CHANGED
package/docs/code/Line.jsx
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
// draw a
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[0.3, 0.7],
|
|
10
|
-
[0.7, 0.7],
|
|
11
|
-
[0.7, 0.3],
|
|
12
|
-
]} />
|
|
13
|
-
</Group>
|
|
1
|
+
// draw a piecewise line spiraling outwards (with dots at vertices)
|
|
2
|
+
const spiral = linspace(0, 5, 25).map(t => polar(2*pi * t, t/5))
|
|
3
|
+
return <Box margin>
|
|
4
|
+
<Graph coord={[-1, -1, 1, 1]}>
|
|
5
|
+
<Line points={spiral} />
|
|
6
|
+
<Points points={spiral} point-size={0.03} />
|
|
7
|
+
</Graph>
|
|
8
|
+
</Box>
|
package/docs/code/Math.jsx
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
//
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
<
|
|
12
|
-
|
|
13
|
-
{ring.map(pos => <Dot pos={pos} size={0.03} fill={blue} />)}
|
|
14
|
-
</Group>
|
|
1
|
+
// draw a spirograph in a box for a set of example parameters
|
|
2
|
+
const [R, r, d, k] = [10, 7, 4, 7]
|
|
3
|
+
const fx = t => (R - r) * cos(t) + d * cos(((R - r) / r) * t)
|
|
4
|
+
const fy = t => (R - r) * sin(t) - d * sin(((R - r) / r) * t)
|
|
5
|
+
return <TitleFrame title="Spirograph" padding={0.2} margin rounded>
|
|
6
|
+
<Graph aspect coord={[-R, -R, R, R]}>
|
|
7
|
+
<Circle pos={[0, 0]} rad={R} stroke={darkgray} stroke-dasharray={10} />
|
|
8
|
+
<Circle pos={[0, 0]} rad={R - r} stroke-dasharray={5} />
|
|
9
|
+
<SymSpline fx={fx} fy={fy} tlim={[0, 2*pi*k]} stroke={blue} stroke-width={2} />
|
|
10
|
+
</Graph>
|
|
11
|
+
<Span pos={[0.5, 1.1]} ysize={0.05} font-family={mono}>R = 10 | r = 7 | d = 4</Span>
|
|
12
|
+
</TitleFrame>
|
package/docs/code/Network.jsx
CHANGED
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
<Node id="test" pos={[0.75, 0.25]} wrap={6}>This is a test of wrapping capabilities</Node>
|
|
5
5
|
<Node id="ball" pos={[0.75, 0.75]}><Ellipse aspect={1.5} fill={blue}/></Node>
|
|
6
6
|
<Edge start="hello" end="test" />
|
|
7
|
-
<Edge start="hello" end="ball" start-side="s"
|
|
7
|
+
<Edge start="hello" end="ball" start-side="s" />
|
|
8
8
|
</Network>
|
package/docs/code/Points.jsx
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// A plot of three different increasing curves of varying steepness and multiple points spaced at regular intervals. The x-axis label is "time (seconds)", the y-axis label is "space (meters)", and the title is "Spacetime Vibes". There are axis ticks in both directions with assiated faint grid lines.
|
|
2
|
-
<Plot
|
|
2
|
+
<Plot coord={[-1, -1, 1, 1]} margin={0.2} grid
|
|
3
|
+
xlabel="time (seconds)" ylabel="space (meters)"
|
|
4
|
+
title="Spacetime Vibes"
|
|
5
|
+
>
|
|
3
6
|
<Points point-size={0.04} points={[
|
|
4
7
|
[0, 0.5], [0.5, 0], [-0.5, 0], [0, -0.5]
|
|
5
8
|
]} />
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
// a city-block route in blue with rounded corners, with the underlying
|
|
2
2
|
// vertices marked as black dots to show how the corners are rounded
|
|
3
3
|
const points = [
|
|
4
|
-
[0.
|
|
5
|
-
[0.
|
|
6
|
-
[0.40, 0.80],
|
|
7
|
-
[0.70, 0.80],
|
|
8
|
-
[0.70, 0.50],
|
|
9
|
-
[0.90, 0.50],
|
|
4
|
+
[-0.8, 0.6], [-0.2, 0.6], [-0.2, -0.6],
|
|
5
|
+
[ 0.4, -0.6], [ 0.4, 0.0], [ 0.8, 0.0],
|
|
10
6
|
]
|
|
11
|
-
return <
|
|
12
|
-
<Line opacity={0.3}
|
|
13
|
-
<RoundedLine stroke={blue} stroke-width={2} radius={0.
|
|
14
|
-
<Points point-size={0.
|
|
15
|
-
</
|
|
7
|
+
return <Graph aspect padding>
|
|
8
|
+
<Line points={points} opacity={0.3} />
|
|
9
|
+
<RoundedLine points={points} stroke={blue} stroke-width={2} radius={0.1} />
|
|
10
|
+
<Points points={points} point-size={0.03} />
|
|
11
|
+
</Graph>
|
package/docs/code/Shape.jsx
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
// draw a
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
[0
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[0.3, 0.3],
|
|
10
|
-
[0.7, 0.3],
|
|
11
|
-
[0.7, 0.7],
|
|
12
|
-
[0.3, 0.7]
|
|
13
|
-
]} />
|
|
14
|
-
</Group>
|
|
1
|
+
// draw a stop sign
|
|
2
|
+
const hexagon = linspace(0, 2*pi, 8, false).map(t => polar(t))
|
|
3
|
+
return <Box fill="#bbb" rounded padding margin>
|
|
4
|
+
<Graph xlim={[-1, 1]} ylim={[-1, 1]} aspect={1}>
|
|
5
|
+
<Shape points={hexagon} fill="#CC0202" stroke={white} stroke_width={20} spin={180/8} />
|
|
6
|
+
<Text pos={[0, 0]} xsize={1.5} color={white} font-weight={bold}>STOP</Text>
|
|
7
|
+
</Graph>
|
|
8
|
+
</Box>
|
package/docs/code/Spline.jsx
CHANGED
|
@@ -7,7 +7,9 @@ const points = [
|
|
|
7
7
|
[0.50, 0.50],
|
|
8
8
|
]
|
|
9
9
|
return <Frame rounded margin>
|
|
10
|
-
<
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
<Group>
|
|
11
|
+
<Spline closed stroke={blue} fill={gray} points={points} />
|
|
12
|
+
<Shape stroke={red} points={points} />
|
|
13
|
+
<Points point-size={0.02} points={points} />
|
|
14
|
+
</Group>
|
|
13
15
|
</Frame>
|
package/docs/code/SymFill.jsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// a decaying sine wave filled in with blue
|
|
2
2
|
const decay = x => exp(-0.1*x) * sin(x)
|
|
3
3
|
return <Graph xlim={[0, 6*pi]} ylim={[-1, 1]} aspect={phi}>
|
|
4
|
-
<SymFill
|
|
4
|
+
<SymFill f1={t => [t, decay(t)]} f2={t => [t, 0]} tlim={[0, 6*pi]} fill={blue} fill-opacity={0.5} N={250} />
|
|
5
5
|
<SymLine fy={decay} N={250} />
|
|
6
6
|
</Graph>
|
package/docs/code/SymShape.jsx
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
// Draw a rounded star shape with a blue fill. Wrap it in a rounded frame.
|
|
2
|
-
const rad = t => 1 - 0.
|
|
2
|
+
const rad = t => 1 - 0.2 * cos(5 * (t - pi/2))
|
|
3
3
|
return <Frame rounded padding margin>
|
|
4
4
|
<SymShape aspect fill={blue}
|
|
5
5
|
tlim={[0, 2*pi]} N={200}
|
|
6
|
-
|
|
7
|
-
fy={t => rad(t) * cos(t)}
|
|
6
|
+
f={t => polar(t, rad(t))}
|
|
8
7
|
/>
|
|
9
|
-
</Frame>
|
|
8
|
+
</Frame>
|
package/docs/code/Tables.jsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// load "data.csv" and plot each row as a blue dot
|
|
2
|
-
return <Graph
|
|
2
|
+
return <Graph aspect coord={[0, 0, 10, 10]}>
|
|
3
|
+
<Mesh2D xlocs={10} ylocs={10} opacity={0.25} />
|
|
3
4
|
{loadTable('data.csv').map(({ x, y }) =>
|
|
4
|
-
<Dot pos={[x, y]}
|
|
5
|
+
<Dot pos={[x, y]} size={0.5} fill={blue} />
|
|
5
6
|
)}
|
|
6
7
|
</Graph>
|
package/docs/text/Arrays.md
CHANGED
|
@@ -16,7 +16,7 @@ There are a number of functions designed to make working with arrays easier. The
|
|
|
16
16
|
- `norm(arr, degree=1)` — compute the `degree`-norm of array `arr`
|
|
17
17
|
- `normalize(arr, degree=1)` — normalize array `arr` to have `degree`-norm one
|
|
18
18
|
- `range(i0, i1, step=1)` — generate an array of evenly spaced values from `i0` to `i1` with spacing `step`
|
|
19
|
-
- `linspace(x0, x1, n=
|
|
19
|
+
- `linspace(x0, x1, n, end=false)` — generate an array of `n` evenly spaced values between `x0` and `x1` (including `x1` if `end` is true)
|
|
20
20
|
- `enumerate(arr)` — pair each element of array `arr` with its index
|
|
21
21
|
- `repeat(x, n)` — repeat array `x` a total of `n` times
|
|
22
22
|
- `meshgrid(x, y)` — create a mesh grid from arrays `x` and `y`
|
package/docs/text/Box.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
This is a simple container class allowing you to add padding, margins, and a border to a single **Element**. It's pretty versatile and is often used to set up the outermost positioning of a figure. Mirroring the standard CSS definitions, padding is space inside the border and margin is space outside the border. This has no border by default, but there is a specialized subclass of this called **Frame** that defaults to `border = 1`.
|
|
6
6
|
|
|
7
|
-
**Box** can be pretty handly in various situations. It is differentiated from [Group](/docs/Group) in that it will adopt the `aspect` of the child element. This is useful if you want to do something like shift an element up or down by a certain amount while maintaining its aspect ratio. Simply wrap it in a **Box** and set child's `pos` to the desired offset.
|
|
7
|
+
**Box** can be pretty handly in various situations. It is differentiated from [Group](/docs/Group) in that it will adopt the `aspect` of the first child element. This is useful if you want to do something like shift an element up or down by a certain amount while maintaining its aspect ratio. Simply wrap it in a **Box** and set child's `pos` to the desired offset.
|
|
8
8
|
|
|
9
9
|
There are multiple ways to specify padding and margins. If given as a scalar, it is constant across all sides. If two values are given, they correspond to the horizontal and vertical sides. If four values are given, they correspond to `[left, top, right, bottom]`.
|
|
10
10
|
|
package/docs/text/Element.md
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
# Element
|
|
2
2
|
|
|
3
|
-
The base class for all
|
|
3
|
+
The base class for all Gum objects. You will usually not be working with this object directly unless you are implementing your own custom elements. However, many of the arguments are common to all elements and are documented here.
|
|
4
4
|
|
|
5
|
-
The position and size of an element are specified in the internal coordinates (`coord`) of its parent, which defaults to the unit square. Rectangles are always specified in `[left, top, right, bottom]` format.
|
|
5
|
+
The position and size of an element are specified in the internal coordinates (`coord`) of its parent, which defaults to the unit square. Rectangles are always specified in `[left, top, right, bottom]` format. Where the element is placed is ultimately determined by its `rect`. However, it is more common to specify the `rect` using one of the convenience parameters below.
|
|
6
|
+
|
|
7
|
+
There are several convenience parameters that can be used to specify the `rect` in a more intuitive way. These include `size`/`xsize`/`ysize` for controlling the extent of the rectangle around `pos` and the analogous `rad`/`xrad`/`yrad` for radial specification. You can also specify the limits in either or both dimensions with `xrect`/`yrect`, where a single number is interpreted as a zero-length limit at that position.
|
|
8
|
+
|
|
9
|
+
The `expand` parameter can be used to control whether the element should be expanded to fully contain its `rect`. This is useful if you have an aspected element with a desired size in one dimension. A very common case is a [Text](/docs/Text) element where you specify `pos` and `ysize` and leave the width to be determined by the aspect ratio.
|
|
6
10
|
|
|
7
11
|
Parameters:
|
|
8
|
-
- `tag` = `g` — the SVG tag associated with this element
|
|
9
|
-
- `unary` = `false` — whether there is inner text for this element
|
|
10
12
|
- `aspect` = `null` — the width to height ratio for this element
|
|
13
|
+
- `rect` — a fully specified rectangle to place the child in (takes precedence over other parameters)
|
|
11
14
|
- `pos` — the desired position of the center of the child's rectangle
|
|
12
|
-
- `size` — the desired size of the child's rectangle (can be single number or pair)
|
|
13
|
-
- `
|
|
14
|
-
- `
|
|
15
|
-
- `aspect` — the aspect ratio of the child's rectangle
|
|
15
|
+
- `size`/`xsize`/`ysize` — the desired size of the child's rectangle (`size` can be single number or pair)
|
|
16
|
+
- `rad`/`xrad`/`yrad` — the desired radius of the child's rectangle (`rad` can be single number or pair)
|
|
17
|
+
- `xrect`/`yrect` — the limits of the child's rectangle in the x and y dimensions (both can be single number or pair)
|
|
16
18
|
- `expand` — when `true`, instead of embedding the child within `rect`, it will make the child just large enough to fully contain `rect`
|
|
17
19
|
- `align` — how to align the child when it doesn't fit exactly within `rect`, options are `left`, `right`, `center`, or a fractional position (can set vertical and horizontal separately with a pair)
|
|
18
20
|
- `rotate` — how much to rotate the child by (degrees counterclockwise)
|
|
19
21
|
- `spin` — like rotate but will maintain the same size
|
|
20
22
|
- `flex` — override to set `aspect = null`
|
|
21
|
-
- `...` = `{}` — additional attributes
|
|
22
|
-
|
|
23
|
-
Methods:
|
|
24
|
-
- `props(ctx)` — returns a dictionary of attributes for the SVG element. The default implementation returns the non-null `attr` passed to the constructor
|
|
25
|
-
- `inner(ctx)` — returns the inner text of the SVG element (for non-unary). Defaults to returing empty string
|
|
26
|
-
- `svg(ctx)` — returns the rendered SVG of the element as a `String`. Default implementation constructs SVG from `tag`, `unary`, `props`, and `inner`
|
|
23
|
+
- `...` = `{}` — additional attributes are applied directly to the resulting SVG
|
package/docs/text/Gum.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# Gum
|
|
2
2
|
|
|
3
|
-
Welcome to the
|
|
3
|
+
Welcome to the Gum documentation! Click on an item in the list on the left to get more info about a particular class (usually an [Element](/docs/Element), function, or constant).
|
|
4
4
|
|
|
5
|
-
Each entry has a description of the operation and arguments of the item and an associated example code snippet. You can edit the code snippet, but note that these will get clobbered if you navigate to another entry! Go to the [main editor](/) for non-ephemeral work.
|
|
5
|
+
Each entry has a description of the operation and arguments of the item and an associated example code snippet. You can edit the code snippet, but note that these will get clobbered if you navigate to another entry! Go to the [main editor](/gum/studio) for non-ephemeral work.
|
|
6
6
|
|
|
7
|
-
The syntax is
|
|
7
|
+
The syntax is a JSX component style familiar to React developers. The output is pure SVG. You can nest objects in interesting ways and specify their parameters. Positions and sizes are specified proportionally (i.e. between `0` and `1`), but some quantities like `border` or `stroke-width` are still specified in pixel units.
|
|
8
8
|
|
|
9
9
|
## Common Patterns
|
|
10
10
|
|
|
11
11
|
*Parameter specification*: You can specify boolean parameters like `border` just by writing their name. Some parameters, such as `margin` or `padding` default to `0` when not specified, but also take a specific value when specified as boolean `true` (in both cases `0.1`). You can also pass SVG properties such as `stroke-width` directly.
|
|
12
12
|
|
|
13
|
-
*Subunit arguments*: for compound elements that inherit [Group](/docs/Group), some keyword arguments are passed down to the constituent parts. For instance, in [Plot](/docs/Plot), one can specify arguments intended for the
|
|
13
|
+
*Subunit arguments*: for compound elements that inherit [Group](/docs/Group), some keyword arguments are passed down to the constituent parts. For instance, in [Plot](/docs/Plot), one can specify arguments intended for the [XAxis](/docs/Axis) unit by prefixing them with `xaxis-`. For example, setting the `stroke-width` for this subunit can be achieved with `xaxis-stroke-width`.
|
|
14
14
|
|
|
15
|
-
*Constructive layout*: we try to avoid hard-coding absolute values as much as possible. Instead, we use proportions relative to the parent element's size. For instance, `margin = 0.1` means "10% of the parent's width/height". Similarly, instead of manually positioning elements in a row, we use [HStack](/docs/
|
|
15
|
+
*Constructive layout*: we try to avoid hard-coding absolute values as much as possible. Instead, we use proportions relative to the parent element's size. For instance, `margin = 0.1` means "10% of the parent's width/height". Similarly, instead of manually positioning elements in a row, we use [HStack](/docs/Stack) or [VStack](/docs/Stack) to automatically arrange them.
|
package/docs/text/Math.md
CHANGED
|
@@ -27,6 +27,7 @@ Here we collect a variety of global mathematical functions and constants. You ca
|
|
|
27
27
|
- `round(x)` — the rounding function
|
|
28
28
|
- `clamp(x, lim=[0, 1])` — clamp `x` to the range `lim`
|
|
29
29
|
- `rescale(x, lim=[0, 1])` — linearly rescale `x` to the range `lim`
|
|
30
|
-
- `polar(
|
|
30
|
+
- `polar(theta, radius=1, center=[0, 0])` — convert polar coordinates (`theta` in radians, `radius` scalar or size vector) to a 2D point around `center`
|
|
31
|
+
- `polard(angle, radius=1, center=[0, 0])` — same as `polar` but takes `angle` in degrees
|
|
31
32
|
|
|
32
33
|
Angles use gum's usual screen-space convention: `0` points right and `90` points down.
|
package/docs/text/SymFill.md
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
*Inherits*: [Fill](/docs/Fill) > [Shape](/docs/Shape) > **Pointstring** > [Element](/docs/Element)
|
|
4
4
|
|
|
5
|
-
Flexible interface to generate filled in paths symbolically or in combination with fixed inputs. This generates a polygon by running through the points generated by `fx1`/`fy1` and then backwards through the points generated by `fx2`/`fy2`. To generate a simple filled curve, pass your function to `fy1` and let `fy2` be `0`.
|
|
5
|
+
Flexible interface to generate filled in paths symbolically or in combination with fixed inputs. This generates a polygon by running through the points generated by `f1` or `fx1`/`fy1` and then backwards through the points generated by `f2` or `fx2`/`fy2`. To generate a simple filled curve, pass your function to `fy1` and let `fy2` be `0`.
|
|
6
6
|
|
|
7
7
|
Parameters:
|
|
8
|
+
- `f1`/`f2` — functions mapping t-values to `[x, y]` points for either bound
|
|
8
9
|
- `fx1`/`fy1` — a function generating one of the bounds for the fill (or a constant)
|
|
9
10
|
- `fx2`/`fy2` — a function generating the other bound for the fill (or a constant)
|
|
10
11
|
- `xlim`/`ylim`/`tlim` — a pair of numbers specifying variable limits
|
package/docs/text/SymLine.md
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
Flexible interface to generate two-dimensional paths symbolically or in combination with fixed inputs. There are variety of acceptable input combinations, but the most common usage is to specify the range to use for x-values with `xlim` and a function to plot with `fy`. To plot a polygon instead of a line, use [SymShape](/docs/SymShape).
|
|
6
6
|
|
|
7
|
-
Alternatively, you can specify the transpose with `ylim`/`fx`, or
|
|
7
|
+
Alternatively, you can specify the transpose with `ylim`/`fx`, or do a fully parametric path using `tlim` with either `f` or `fx`/`fy`. In any of these cases, one can either specify limits with `xlim`/`ylim`/`tlim` or specific values with `xvals`/`yvals`/`tvals`.
|
|
8
8
|
|
|
9
9
|
Parameters:
|
|
10
|
+
- `f` — a function mapping t-values to `[x, y]` points
|
|
10
11
|
- `fx`/`fy` — a function mapping from x-values, y-values, or t-values
|
|
11
12
|
- `xlim`/`ylim`/`tlim` — a pair of numbers specifying variable limits
|
|
12
13
|
- `xvals`/`yvals`/`tvals` — a list of x-values, y-values, or t-values to use
|
package/docs/text/SymPoints.md
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
*Inherits*: [Group](/docs/Group) > [Element](/docs/Element)
|
|
4
4
|
|
|
5
|
-
Flexible interface to generate sets of points symbolically or in combination with fixed inputs. The most common usage is to specify the range for x-values with `xlim` and a function to plot with `fy`. But you can specify the transpose with `ylim`/`fx`, or do a fully parametric path using `tlim
|
|
5
|
+
Flexible interface to generate sets of points symbolically or in combination with fixed inputs. The most common usage is to specify the range for x-values with `xlim` and a function to plot with `fy`. But you can specify the transpose with `ylim`/`fx`, or do a fully parametric path using `tlim` with either `f` or `fx`/`fy`.
|
|
6
6
|
|
|
7
7
|
You can also specify point size functionally with `point-size` and the shape with `point-shape`. Both of these functions take `(x, y, t, i)` values as inputs and return the desired value for each point. As with other groups, the **SymPoints** element itself can still be laid out with the normal `size`/`xsize`/`ysize` element parameters.
|
|
8
8
|
|
|
9
9
|
Parameters:
|
|
10
|
+
- `f` — a function mapping t-values to `[x, y]` points
|
|
10
11
|
- `fx`/`fy` — a function mapping from x-values, y-values, or t-values
|
|
11
12
|
- `point-size` = `0.05` — a size or a function mapping from `(x, y, t, i)` values to a size
|
|
12
13
|
- `point-shape` = `Dot` — a shape or function mapping from `(x, y, t, i)` values to a shape
|
package/docs/text/SymShape.md
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
*Inherits*: [Shape](/docs/Shape) > **Pointstring** > [Element](/docs/Element)
|
|
4
4
|
|
|
5
|
-
Flexible interface to generate shapes symbolically or in combination with fixed inputs. Operates similarly to [Shape](/docs/Shape), but generates a shape from
|
|
5
|
+
Flexible interface to generate shapes symbolically or in combination with fixed inputs. Operates similarly to [Shape](/docs/Shape), but generates a shape from points generated by `f` or `fx`/`fy`.
|
|
6
6
|
|
|
7
7
|
Parameters:
|
|
8
|
+
- `f` — a function mapping t-values to `[x, y]` points
|
|
8
9
|
- `fx`/`fy` — a function mapping from x-values, y-values, or t-values
|
|
9
10
|
- `xlim`/`ylim`/`tlim` — a pair of numbers specifying variable limits
|
|
10
11
|
- `xvals`/`yvals`/`tvals` — a list of x-values, y-values, or t-values to use
|
package/docs/text/SymSpline.md
CHANGED
|
@@ -6,6 +6,7 @@ Flexible interface to generate smooth two-dimensional spline curves symbolically
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Parameters:
|
|
9
|
+
- `f` — a function mapping t-values to `[x, y]` points
|
|
9
10
|
- `fx`/`fy` — a function mapping from x-values, y-values, or t-values
|
|
10
11
|
- `xlim`/`ylim`/`tlim` — a pair of numbers specifying variable limits
|
|
11
12
|
- `xvals`/`yvals`/`tvals` — a list of x-values, y-values, or t-values to use
|
|
@@ -27,8 +27,7 @@ const dz2_r = (t) => 0.28 * abs(3 * cos(t) ** 2 - 1)
|
|
|
27
27
|
// plot radial orbital lobes via SymSpline
|
|
28
28
|
const OrbitalLobe = ({ rfn, t0, t1, sign }) =>
|
|
29
29
|
<SymSpline N={50} tlim={[t0, t1]}
|
|
30
|
-
|
|
31
|
-
fy={t => rfn(t) * sin(t)}
|
|
30
|
+
f={t => polar(t, rfn(t))}
|
|
32
31
|
stroke={sign ? pos_col : neg_col}
|
|
33
32
|
fill={sign ? pos_fill : neg_fill}
|
|
34
33
|
stroke-width={2} opacity={0.6}
|
|
@@ -3,7 +3,7 @@ const Curve = ({ fx, stroke }) => <SymLine fx={fx} ylim={ylim} stroke={stroke} s
|
|
|
3
3
|
const xlabel = <Latex>x = a + bi</Latex>
|
|
4
4
|
const ylabel = <Latex>c</Latex>
|
|
5
5
|
return <Plot aspect={2} margin={0.3} xlim={xlim} ylabel={ylabel} xlabel={xlabel} ylabel-offset={0.075}>
|
|
6
|
-
<Mesh2D xlocs={41} ylocs={21} xlim={xlim} ylim={ylim} opacity={0.
|
|
6
|
+
<Mesh2D xlocs={41} ylocs={21} xlim={xlim} ylim={ylim} opacity={0.1} />
|
|
7
7
|
<HLine loc={0} lim={xlim} opacity={0.3} />
|
|
8
8
|
<VLine loc={0} lim={ylim} opacity={0.3} />
|
|
9
9
|
<Curve fx={y => -y + sqrt(maximum(0, y*y-1))} stroke={blue} />
|
|
@@ -18,9 +18,9 @@ const arcRad = 0.25
|
|
|
18
18
|
// Computed positions
|
|
19
19
|
const rodRot = 90 - rodAng
|
|
20
20
|
const eqnY = pivotY + rodLen
|
|
21
|
-
const [ bobX, bobY ] =
|
|
22
|
-
const [ midX, midY ] =
|
|
23
|
-
const [ tenX, tenY ] =
|
|
21
|
+
const [ bobX, bobY ] = polard(rodRot, rodLen, [pivotX, pivotY])
|
|
22
|
+
const [ midX, midY ] = polard(rodRot, 0.50 * rodLen, [pivotX, pivotY])
|
|
23
|
+
const [ tenX, tenY ] = polard(rodRot, 0.75 * rodLen, [pivotX, pivotY])
|
|
24
24
|
|
|
25
25
|
return <Box margin={0.06}>
|
|
26
26
|
<VStack spacing={0.05}>
|
|
@@ -2,8 +2,8 @@ const aspect = 2
|
|
|
2
2
|
const ratio = pi / aspect
|
|
3
3
|
return <Box margin={0.3}>
|
|
4
4
|
<Group coord={[0, 1, 2*pi, -1]} aspect={aspect}>
|
|
5
|
-
<HMesh locs={5}
|
|
6
|
-
<VMesh locs={5}
|
|
5
|
+
<HMesh locs={5} opacity={0.15} />
|
|
6
|
+
<VMesh locs={5} opacity={0.15} />
|
|
7
7
|
<HAxis ticks={5} lim={[0, 2*pi]} pos={[pi, -1]} size={[2*pi, 0.08]} />
|
|
8
8
|
<VAxis ticks={5} lim={[-1, 1]} pos={[0, 0]} size={[0.08*ratio, 2]} />
|
|
9
9
|
<SymLine fy={sin} xlim={[0, 2*pi]} />
|
|
@@ -5,11 +5,10 @@ const shapes = [
|
|
|
5
5
|
]
|
|
6
6
|
|
|
7
7
|
const RegularPolygon = ({ n, ...args }) =>
|
|
8
|
-
<SymShape {...args}
|
|
9
|
-
aspect spin={90*(n-2)/n}
|
|
8
|
+
<SymShape {...args} aspect
|
|
10
9
|
xlim={[-1, 1]} ylim={[-1, 1]}
|
|
11
|
-
tvals={linspace(0, 2*pi, n
|
|
12
|
-
|
|
10
|
+
tvals={linspace(0, 2*pi, n, false)}
|
|
11
|
+
f={t => polar(t+pi/2*(n-2)/n)}
|
|
13
12
|
/>
|
|
14
13
|
|
|
15
14
|
return <Slide title="Simple Regular Polygons" wrap={25}>
|
|
@@ -8,8 +8,8 @@ const theta0 = linspace(0, 2 * pi, n, false).map(t => t - pi / 2)
|
|
|
8
8
|
const theta1 = theta0.map(t => t + pi / n)
|
|
9
9
|
|
|
10
10
|
// get inner/outer point positions
|
|
11
|
-
const points0 = theta0.map(t => polar(
|
|
12
|
-
const points1 = theta1.map(t => polar(
|
|
11
|
+
const points0 = theta0.map(t => polar(t))
|
|
12
|
+
const points1 = theta1.map(t => polar(t, R))
|
|
13
13
|
const points = zip(points0, points1).flat()
|
|
14
14
|
|
|
15
15
|
// return full spline
|
package/package.json
CHANGED
package/prompt/intro.md
CHANGED
|
@@ -115,11 +115,10 @@ Without specifying `stack-size`, the `Text` element would be quite large (it has
|
|
|
115
115
|
Here are the handy array functions provided by the library. All of these mimic the behavior of their counterparts in Python and `numpy`. This can be useful for generating `Element` objects from arrays:
|
|
116
116
|
```typescript
|
|
117
117
|
function zip(...arrs: any[]): any[]
|
|
118
|
-
function range(
|
|
119
|
-
function linspace(
|
|
118
|
+
function range(ia: number, ib?: number, step?: number = 1): number[]
|
|
119
|
+
function linspace(x0: number, x1: number, num: number, end?: boolean = false): number[]
|
|
120
120
|
function enumerate(x: any[]): any[]
|
|
121
121
|
function repeat(x: any, n: number): any[]
|
|
122
|
-
function lingrid(xlim: range, ylim: range, N: number): number[][]
|
|
123
122
|
```
|
|
124
123
|
|
|
125
124
|
Some of the most commonly used mathematical constants are pre-defined in the global scope:
|
package/scripts/gum.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Command } from 'commander'
|
|
|
4
4
|
import { readFileSync, writeFileSync } from 'fs'
|
|
5
5
|
import { dirname, resolve } from 'path'
|
|
6
6
|
|
|
7
|
-
import { evaluateGum } from '../src/eval'
|
|
7
|
+
import { evaluateGum, fitSize } from '../src/eval'
|
|
8
8
|
import { rasterizeSvg, formatImage, readStdin } from '../src/render'
|
|
9
9
|
import { Element, Group } from '../src/elems/core'
|
|
10
10
|
import type { CliArgs, LoadFile } from '../src/lib/types'
|
|
@@ -16,7 +16,7 @@ import { devCommand } from './dev'
|
|
|
16
16
|
|
|
17
17
|
function transformArgs(cmd: Command) {
|
|
18
18
|
const [ file0 ] = cmd.args
|
|
19
|
-
let { format, output, theme, background, size,
|
|
19
|
+
let { format, output, theme, background, size, rasterSize, dev } = cmd.opts()
|
|
20
20
|
|
|
21
21
|
// add white background for light theme
|
|
22
22
|
if (theme == 'light' && background == null) background = 'white'
|
|
@@ -41,7 +41,7 @@ function transformArgs(cmd: Command) {
|
|
|
41
41
|
: readFileSync(file, encoding as BufferEncoding)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
return { file, format, output, theme, background, size,
|
|
44
|
+
return { file, format, output, theme, background, size, rasterSize, dev, loadFile }
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
//
|
|
@@ -64,7 +64,7 @@ function convertToTree(elem: Element): any {
|
|
|
64
64
|
//
|
|
65
65
|
|
|
66
66
|
async function runCommand(args: CliArgs) {
|
|
67
|
-
const { file, format, output, theme, background, size: size0 = 1000,
|
|
67
|
+
const { file, format, output, theme, background, size: size0 = 1000, rasterSize, dev, loadFile } = args
|
|
68
68
|
|
|
69
69
|
// divert to dev command if update is on
|
|
70
70
|
if (dev) {
|
|
@@ -77,8 +77,6 @@ async function runCommand(args: CliArgs) {
|
|
|
77
77
|
|
|
78
78
|
// evaluate gum with size
|
|
79
79
|
const elem = evaluateGum(code, { size: size0, theme, loadFile })
|
|
80
|
-
const svg = format != 'json' ? elem.svg() : ''
|
|
81
|
-
const size = elem.size
|
|
82
80
|
|
|
83
81
|
// rasterize output
|
|
84
82
|
let out: string | Buffer
|
|
@@ -86,9 +84,15 @@ async function runCommand(args: CliArgs) {
|
|
|
86
84
|
const tree = convertToTree(elem)
|
|
87
85
|
out = JSON.stringify(tree, null, 2)
|
|
88
86
|
} else if (format == 'svg') {
|
|
89
|
-
out = svg
|
|
87
|
+
out = elem.svg()
|
|
90
88
|
} else if (format == 'png' || format == 'kitty') {
|
|
91
|
-
|
|
89
|
+
let svg = elem.svg()
|
|
90
|
+
if (rasterSize != null) {
|
|
91
|
+
const [ rasterWidth, rasterHeight ] = fitSize(elem.size, rasterSize)
|
|
92
|
+
const elem1 = elem.clone({ width: rasterWidth, height: rasterHeight })
|
|
93
|
+
svg = elem1.svg()
|
|
94
|
+
}
|
|
95
|
+
const dat = await rasterizeSvg(svg, { background })
|
|
92
96
|
out = (format == 'kitty') ? (formatImage(dat) + '\n') : dat
|
|
93
97
|
} else {
|
|
94
98
|
throw new Error(`Unsupported output format: ${format}`)
|
|
@@ -112,9 +116,8 @@ program.name('gum')
|
|
|
112
116
|
.option('-f, --format <format>', 'format to output')
|
|
113
117
|
.option('-t, --theme <theme>', 'theme to use', 'light')
|
|
114
118
|
.option('-b, --background <background>', 'background color')
|
|
115
|
-
.option('-s, --size <size>', 'size
|
|
116
|
-
.option('-
|
|
117
|
-
.option('-h, --height <height>', 'height of the PNG', (value: string) => parseInt(value))
|
|
119
|
+
.option('-s, --size <size>', 'SVG/viewBox size', (value: string) => parseInt(value))
|
|
120
|
+
.option('-r, --raster-size <size>', 'max rasterized PNG size', (value: string) => parseInt(value))
|
|
118
121
|
.option('-o, --output <output>', 'output file')
|
|
119
122
|
.action(async function(this: Command) {
|
|
120
123
|
const args = transformArgs(this)
|