pixi-silk 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.
@@ -0,0 +1,139 @@
1
+ import { ColorSpace } from "./color.js";
2
+ import { ColorSource } from "pixi.js";
3
+ //#region src/gradient.d.ts
4
+ /**
5
+ * A color stop: a bare color (stops are spread evenly), `[offset, color]`,
6
+ * `[offset, color, alpha]` or `{ offset, color, alpha }`.
7
+ */
8
+ export type StopInput = ColorSource | [number, ColorSource] | [number, ColorSource, number] | {
9
+ /** Position on the ramp, from 0 to 1. */
10
+ offset: number;
11
+ /** Colour of the stop. */
12
+ color: ColorSource;
13
+ /** Opacity of the stop, from 0 to 1. Default 1. */
14
+ alpha?: number;
15
+ };
16
+ /**
17
+ * How the ramp moves between stops.
18
+ *
19
+ * - `linear`: piecewise linear between stops (CSS behaviour).
20
+ * - `smooth`: monotone cubic spline through the stops with eased ends. Removes the
21
+ * Mach bands you see at every stop of a linear ramp and never overshoots.
22
+ * - a function remaps the gradient parameter before lookup (e.g. an easing curve for scrims).
23
+ */
24
+ export type Easing = "linear" | "smooth" | ((t: number) => number);
25
+ /** Gradient coordinates: `shape` = fractions of each painted shape's box, `local` = the graphics' local units (one ramp across many shapes). */
26
+ export type GradientUnits = "shape" | "local";
27
+ /** What a gradient does outside 0..1: hold the end colours (`pad`), tile (`repeat`) or mirror (`reflect`). */
28
+ export type ExtendMode = "pad" | "repeat" | "reflect";
29
+ /** Options shared by every gradient: colour stops, interpolation space, easing, extend mode and units. */
30
+ export interface GradientOptions {
31
+ /** Colour stops. Bare colours are spread evenly. See {@link StopInput}. */
32
+ stops: StopInput[];
33
+ /** Interpolation space. OKLab keeps hue and lightness perceptually even. Default `oklab`. */
34
+ space?: ColorSpace;
35
+ /** How the ramp moves between stops. Default `linear`. */
36
+ easing?: Easing;
37
+ /** What the gradient does outside 0..1. Default `pad`. */
38
+ extend?: ExtendMode;
39
+ /** Coordinates of the gradient's points and radii: each shape's box (`shape`, default) or local units (`local`). */
40
+ units?: GradientUnits;
41
+ }
42
+ /**
43
+ * Base class of all gradients. Stops are interpolated premultiplied (OKLab by default), baked into a
44
+ * shared half-float atlas and dithered when sampled, so gradients stay smooth without banding.
45
+ */
46
+ export declare abstract class Gradient {
47
+ /** Interpolation space. */
48
+ readonly space: ColorSpace;
49
+ /** How the ramp moves between stops. */
50
+ readonly easing: Easing;
51
+ /** What the gradient does outside 0..1. */
52
+ readonly extend: ExtendMode;
53
+ /** Coordinates of the gradient's points and radii. */
54
+ readonly units: GradientUnits;
55
+ constructor(options: GradientOptions);
56
+ }
57
+ /** A point `[x, y]`. In `shape` units, x and y are fractions of the painted shape's box. */
58
+ export type Vec2 = [number, number];
59
+ /** Options of {@link linear}: stops plus the start and end points of the ramp. */
60
+ export interface LinearGradientOptions extends GradientOptions {
61
+ /** Start point. Default `[0, 0]` (top). */
62
+ from?: Vec2;
63
+ /** End point. Default `[0, 1]` (bottom). */
64
+ to?: Vec2;
65
+ }
66
+ /** A straight ramp between two points. Create it with {@link linear}, {@link vertical} or {@link horizontal}. */
67
+ export declare class LinearGradient extends Gradient {
68
+ /** Start point. */
69
+ from: Vec2;
70
+ /** End point. */
71
+ to: Vec2;
72
+ constructor(options: LinearGradientOptions);
73
+ }
74
+ /** Options of {@link radial}: stops plus centre, outer radius and inner radius. */
75
+ export interface RadialGradientOptions extends GradientOptions {
76
+ /** Default `[0.5, 0.5]`. */
77
+ center?: Vec2;
78
+ /** Outer radius. In shape units it is a fraction of the larger side. Default `0.5`. */
79
+ radius?: number;
80
+ /** Radius where the ramp starts. Default `0`. */
81
+ innerRadius?: number;
82
+ }
83
+ /** A ramp from a centre outwards, optionally starting at an inner radius. Create it with {@link radial}. */
84
+ export declare class RadialGradient extends Gradient {
85
+ /** Centre of the ramp. */
86
+ center: Vec2;
87
+ /** Radius where the ramp ends. */
88
+ radius: number;
89
+ /** Radius where the ramp starts. */
90
+ innerRadius: number;
91
+ constructor(options: RadialGradientOptions);
92
+ }
93
+ /** Options of {@link conic}: stops plus centre, start angle and sweep. */
94
+ export interface ConicGradientOptions extends GradientOptions {
95
+ /** Default `[0.5, 0.5]`. */
96
+ center?: Vec2;
97
+ /**
98
+ * Angle (radians, y-down so positive is clockwise) where the ramp starts.
99
+ * Defaults to the arc start for arcs/sectors, otherwise 12 o'clock.
100
+ */
101
+ startAngle?: number;
102
+ /** Angular length of the ramp. Defaults to the arc sweep for arcs/sectors, otherwise a full turn. */
103
+ sweep?: number;
104
+ }
105
+ /** An angular ramp around a centre. On arcs it follows the arc. Create it with {@link conic}. */
106
+ export declare class ConicGradient extends Gradient {
107
+ /** Centre of the ramp. */
108
+ center: Vec2;
109
+ /** Angle where the ramp starts. Unset follows the arc, or 12 o'clock for other shapes. */
110
+ startAngle?: number;
111
+ /** Angular length of the ramp. Unset follows the arc, or a full turn for other shapes. */
112
+ sweep?: number;
113
+ constructor(options: ConicGradientOptions);
114
+ }
115
+ /** A ramp that follows a stroke from its first point (t = 0) to its last (t = 1). Create it with {@link along}. */
116
+ export declare class PathGradient extends Gradient {}
117
+ /**
118
+ * Linear gradient. `from` / `to` default to top and bottom of the shape's box (`units: 'shape'`).
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * g.roundRect(0, 0, 200, 60, 16).fill(linear([0x0a84ff, 0xbf5af2], { from: [0, 0], to: [1, 1] }));
123
+ * ```
124
+ */
125
+ export declare const linear: (stops: StopInput[], options?: Omit<LinearGradientOptions, "stops">) => LinearGradient;
126
+ /** Top-to-bottom linear gradient across the painted shape. */
127
+ export declare const vertical: (stops: StopInput[], options?: Omit<LinearGradientOptions, "stops" | "from" | "to">) => LinearGradient;
128
+ /** Left-to-right linear gradient across the painted shape. */
129
+ export declare const horizontal: (stops: StopInput[], options?: Omit<LinearGradientOptions, "stops" | "from" | "to">) => LinearGradient;
130
+ /** Radial gradient from `center` (default the box centre) out to `radius`, optionally starting at `innerRadius`. */
131
+ export declare const radial: (stops: StopInput[], options?: Omit<RadialGradientOptions, "stops">) => RadialGradient;
132
+ /**
133
+ * Conic (angular) gradient. On arcs and sectors it spans the arc by default, so a ring's ramp
134
+ * always runs from its start cap to its end cap.
135
+ */
136
+ export declare const conic: (stops: StopInput[], options?: Omit<ConicGradientOptions, "stops">) => ConicGradient;
137
+ /** Gradient that follows a stroke from its first point (t = 0) to its last (t = 1). */
138
+ export declare const along: (stops: StopInput[], options?: Omit<GradientOptions, "stops" | "units">) => PathGradient;
139
+ //#endregion
@@ -0,0 +1,302 @@
1
+ import { fromSpace, parseColor, toSpace } from "./color.js";
2
+ import { GradKind, TAU } from "./constants.js";
3
+ //#region src/gradient.ts
4
+ const fnIds = /* @__PURE__ */ new WeakMap();
5
+ let nextFnId = 1;
6
+ /** 2- and 3-element arrays are stops (`[offset, color, alpha?]`). Pass rgb colors as hex, strings or objects. */
7
+ function isStopTuple(s) {
8
+ return Array.isArray(s) && (s.length === 2 || s.length === 3) && typeof s[0] === "number";
9
+ }
10
+ function normalizeStops(input) {
11
+ if (input.length === 0) return [{
12
+ offset: 0,
13
+ rgba: [
14
+ 0,
15
+ 0,
16
+ 0,
17
+ 0
18
+ ]
19
+ }, {
20
+ offset: 1,
21
+ rgba: [
22
+ 0,
23
+ 0,
24
+ 0,
25
+ 0
26
+ ]
27
+ }];
28
+ const stops = input.map((s, i) => {
29
+ const even = input.length === 1 ? 0 : i / (input.length - 1);
30
+ if (isStopTuple(s)) return {
31
+ offset: s[0],
32
+ rgba: parseColor(s[1], s[2] ?? 1)
33
+ };
34
+ if (typeof s === "object" && s !== null && !Array.isArray(s) && "offset" in s && "color" in s) return {
35
+ offset: s.offset,
36
+ rgba: parseColor(s.color, s.alpha ?? 1)
37
+ };
38
+ return {
39
+ offset: even,
40
+ rgba: parseColor(s)
41
+ };
42
+ });
43
+ let last = 0;
44
+ for (const s of stops) {
45
+ s.offset = Math.min(1, Math.max(last, s.offset));
46
+ last = s.offset;
47
+ }
48
+ if (stops.length === 1) stops.push({
49
+ offset: 1,
50
+ rgba: stops[0].rgba
51
+ });
52
+ return stops;
53
+ }
54
+ /** Fritsch-Butland tangents with zero slope at both ends (so two stops ease like smoothstep). */
55
+ function monotoneTangents(xs, ys) {
56
+ const n = xs.length;
57
+ const m = new Array(n).fill(0);
58
+ for (let k = 1; k < n - 1; k++) {
59
+ const h0 = xs[k] - xs[k - 1];
60
+ const h1 = xs[k + 1] - xs[k];
61
+ if (h0 <= 1e-9 || h1 <= 1e-9) continue;
62
+ const d0 = (ys[k] - ys[k - 1]) / h0;
63
+ const d1 = (ys[k + 1] - ys[k]) / h1;
64
+ if (d0 * d1 <= 0) continue;
65
+ const w1 = 2 * h1 + h0;
66
+ const w2 = h1 + 2 * h0;
67
+ m[k] = (w1 + w2) / (w1 / d0 + w2 / d1);
68
+ }
69
+ return m;
70
+ }
71
+ /**
72
+ * Base class of all gradients. Stops are interpolated premultiplied (OKLab by default), baked into a
73
+ * shared half-float atlas and dithered when sampled, so gradients stay smooth without banding.
74
+ */
75
+ var Gradient = class {
76
+ /** @internal Normalised stops (offsets clamped and sorted, colours parsed). */
77
+ stops;
78
+ /** Interpolation space. */
79
+ space;
80
+ /** How the ramp moves between stops. */
81
+ easing;
82
+ /** What the gradient does outside 0..1. */
83
+ extend;
84
+ /** Coordinates of the gradient's points and radii. */
85
+ units;
86
+ /** @internal Identity of the baked ramp (geometry excluded), used to share atlas rows. */
87
+ key;
88
+ constructor(options) {
89
+ this.stops = normalizeStops(options.stops);
90
+ this.space = options.space ?? "oklab";
91
+ this.easing = options.easing ?? "linear";
92
+ this.extend = options.extend ?? "pad";
93
+ this.units = options.units ?? "shape";
94
+ let easingKey;
95
+ if (typeof this.easing === "function") {
96
+ let id = fnIds.get(this.easing);
97
+ if (!id) {
98
+ id = nextFnId++;
99
+ fnIds.set(this.easing, id);
100
+ }
101
+ easingKey = `fn${id}`;
102
+ } else easingKey = this.easing;
103
+ this.key = `${this.space}|${easingKey}|${this.stops.map((s) => `${s.offset.toFixed(5)}:${s.rgba.map((c) => c.toFixed(5)).join(",")}`).join(";")}`;
104
+ }
105
+ /** @internal Bakes RAMP_WIDTH premultiplied sRGB samples into `out` (rgba floats). */
106
+ bake(out) {
107
+ const stops = this.stops;
108
+ const n = stops.length;
109
+ const xs = stops.map((s) => s.offset);
110
+ const ch = [
111
+ [],
112
+ [],
113
+ [],
114
+ []
115
+ ];
116
+ for (const s of stops) {
117
+ const w = toSpace(s.rgba, this.space);
118
+ const a = s.rgba[3];
119
+ ch[0].push(w[0] * a);
120
+ ch[1].push(w[1] * a);
121
+ ch[2].push(w[2] * a);
122
+ ch[3].push(a);
123
+ }
124
+ const tangents = this.easing === "smooth" ? ch.map((ys) => monotoneTangents(xs, ys)) : null;
125
+ const remap = typeof this.easing === "function" ? this.easing : null;
126
+ const v = [
127
+ 0,
128
+ 0,
129
+ 0,
130
+ 0
131
+ ];
132
+ for (let i = 0; i < 256; i++) {
133
+ let t = i / 255;
134
+ if (remap) t = Math.min(1, Math.max(0, remap(t)));
135
+ if (t <= xs[0]) for (let c = 0; c < 4; c++) v[c] = ch[c][0];
136
+ else if (t >= xs[n - 1]) for (let c = 0; c < 4; c++) v[c] = ch[c][n - 1];
137
+ else {
138
+ let k = 0;
139
+ while (k < n - 2 && t > xs[k + 1]) k++;
140
+ while (k < n - 2 && xs[k + 1] - xs[k] <= 1e-9) k++;
141
+ const h = xs[k + 1] - xs[k];
142
+ const s = h > 1e-9 ? (t - xs[k]) / h : 1;
143
+ for (let c = 0; c < 4; c++) {
144
+ const y0 = ch[c][k];
145
+ const y1 = ch[c][k + 1];
146
+ if (tangents) {
147
+ const s2 = s * s;
148
+ const s3 = s2 * s;
149
+ v[c] = (2 * s3 - 3 * s2 + 1) * y0 + (s3 - 2 * s2 + s) * h * tangents[c][k] + (-2 * s3 + 3 * s2) * y1 + (s3 - s2) * h * tangents[c][k + 1];
150
+ } else v[c] = y0 + (y1 - y0) * s;
151
+ }
152
+ }
153
+ const a = Math.min(1, Math.max(0, v[3]));
154
+ const o = i * 4;
155
+ if (a <= 1e-7) {
156
+ out[o] = out[o + 1] = out[o + 2] = out[o + 3] = 0;
157
+ continue;
158
+ }
159
+ const rgb = fromSpace([
160
+ v[0] / a,
161
+ v[1] / a,
162
+ v[2] / a
163
+ ], this.space);
164
+ out[o] = rgb[0] * a;
165
+ out[o + 1] = rgb[1] * a;
166
+ out[o + 2] = rgb[2] * a;
167
+ out[o + 3] = a;
168
+ }
169
+ }
170
+ };
171
+ function point(frame, units, p) {
172
+ return units === "shape" ? [frame.x + p[0] * frame.w, frame.y + p[1] * frame.h] : p;
173
+ }
174
+ /** A straight ramp between two points. Create it with {@link linear}, {@link vertical} or {@link horizontal}. */
175
+ var LinearGradient = class extends Gradient {
176
+ /** @internal */
177
+ kind = GradKind.Linear;
178
+ /** Start point. */
179
+ from;
180
+ /** End point. */
181
+ to;
182
+ constructor(options) {
183
+ super(options);
184
+ this.from = options.from ?? [0, 0];
185
+ this.to = options.to ?? [0, 1];
186
+ }
187
+ /** @internal */
188
+ resolve(frame, out, o) {
189
+ const a = point(frame, this.units, this.from);
190
+ const b = point(frame, this.units, this.to);
191
+ out[o] = a[0];
192
+ out[o + 1] = a[1];
193
+ out[o + 2] = b[0];
194
+ out[o + 3] = b[1];
195
+ }
196
+ };
197
+ /** A ramp from a centre outwards, optionally starting at an inner radius. Create it with {@link radial}. */
198
+ var RadialGradient = class extends Gradient {
199
+ /** @internal */
200
+ kind = GradKind.Radial;
201
+ /** Centre of the ramp. */
202
+ center;
203
+ /** Radius where the ramp ends. */
204
+ radius;
205
+ /** Radius where the ramp starts. */
206
+ innerRadius;
207
+ constructor(options) {
208
+ super(options);
209
+ this.center = options.center ?? [.5, .5];
210
+ this.radius = options.radius ?? .5;
211
+ this.innerRadius = options.innerRadius ?? 0;
212
+ }
213
+ /** @internal */
214
+ resolve(frame, out, o) {
215
+ const c = point(frame, this.units, this.center);
216
+ const scale = this.units === "shape" ? Math.max(frame.w, frame.h) : 1;
217
+ out[o] = c[0];
218
+ out[o + 1] = c[1];
219
+ out[o + 2] = this.radius * scale;
220
+ out[o + 3] = this.innerRadius * scale;
221
+ }
222
+ };
223
+ /** An angular ramp around a centre. On arcs it follows the arc. Create it with {@link conic}. */
224
+ var ConicGradient = class extends Gradient {
225
+ /** @internal */
226
+ kind = GradKind.Conic;
227
+ /** Centre of the ramp. */
228
+ center;
229
+ /** Angle where the ramp starts. Unset follows the arc, or 12 o'clock for other shapes. */
230
+ startAngle;
231
+ /** Angular length of the ramp. Unset follows the arc, or a full turn for other shapes. */
232
+ sweep;
233
+ constructor(options) {
234
+ super(options);
235
+ this.center = options.center ?? [.5, .5];
236
+ this.startAngle = options.startAngle;
237
+ this.sweep = options.sweep;
238
+ }
239
+ /** @internal */
240
+ resolve(frame, out, o) {
241
+ const c = point(frame, this.units, this.center);
242
+ out[o] = c[0];
243
+ out[o + 1] = c[1];
244
+ out[o + 2] = this.startAngle ?? frame.start ?? -Math.PI / 2;
245
+ out[o + 3] = this.sweep ?? frame.sweep ?? TAU;
246
+ }
247
+ };
248
+ /** A ramp that follows a stroke from its first point (t = 0) to its last (t = 1). Create it with {@link along}. */
249
+ var PathGradient = class extends Gradient {
250
+ /** @internal */
251
+ kind = GradKind.Path;
252
+ /** @internal */
253
+ resolve(_frame, out, o) {
254
+ out[o] = out[o + 1] = out[o + 2] = out[o + 3] = 0;
255
+ }
256
+ };
257
+ /**
258
+ * Linear gradient. `from` / `to` default to top and bottom of the shape's box (`units: 'shape'`).
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * g.roundRect(0, 0, 200, 60, 16).fill(linear([0x0a84ff, 0xbf5af2], { from: [0, 0], to: [1, 1] }));
263
+ * ```
264
+ */
265
+ const linear = (stops, options = {}) => new LinearGradient({
266
+ ...options,
267
+ stops
268
+ });
269
+ /** Top-to-bottom linear gradient across the painted shape. */
270
+ const vertical = (stops, options = {}) => new LinearGradient({
271
+ ...options,
272
+ stops,
273
+ from: [0, 0],
274
+ to: [0, 1]
275
+ });
276
+ /** Left-to-right linear gradient across the painted shape. */
277
+ const horizontal = (stops, options = {}) => new LinearGradient({
278
+ ...options,
279
+ stops,
280
+ from: [0, 0],
281
+ to: [1, 0]
282
+ });
283
+ /** Radial gradient from `center` (default the box centre) out to `radius`, optionally starting at `innerRadius`. */
284
+ const radial = (stops, options = {}) => new RadialGradient({
285
+ ...options,
286
+ stops
287
+ });
288
+ /**
289
+ * Conic (angular) gradient. On arcs and sectors it spans the arc by default, so a ring's ramp
290
+ * always runs from its start cap to its end cap.
291
+ */
292
+ const conic = (stops, options = {}) => new ConicGradient({
293
+ ...options,
294
+ stops
295
+ });
296
+ /** Gradient that follows a stroke from its first point (t = 0) to its last (t = 1). */
297
+ const along = (stops, options = {}) => new PathGradient({
298
+ ...options,
299
+ stops
300
+ });
301
+ //#endregion
302
+ export { ConicGradient, Gradient, LinearGradient, PathGradient, RadialGradient, along, conic, horizontal, linear, radial, vertical };
package/dist/hit.js ADDED
@@ -0,0 +1,89 @@
1
+ import { Flag, Prim } from "./constants.js";
2
+ //#region src/hit.ts
3
+ function roundBox(px, py, bx, by, r) {
4
+ const qx = Math.abs(px) - bx + r;
5
+ const qy = Math.abs(py) - by + r;
6
+ return Math.min(Math.max(qx, qy), 0) + Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) - r;
7
+ }
8
+ function segDist(px, py, ax, ay, bx, by) {
9
+ const abx = bx - ax;
10
+ const aby = by - ay;
11
+ const len2 = abx * abx + aby * aby;
12
+ const h = len2 > 0 ? Math.max(0, Math.min(1, ((px - ax) * abx + (py - ay) * aby) / len2)) : 0;
13
+ return Math.hypot(px - ax - abx * h, py - ay - aby * h);
14
+ }
15
+ /** Angle of (x, y) relative to an arc: true when inside its sweep. */
16
+ function inSweep(x, y, start, sweep) {
17
+ if (Math.abs(sweep) >= Math.PI * 2 - 1e-6) return true;
18
+ let a = (Math.atan2(y, x) - start) * Math.sign(sweep);
19
+ a = (a % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2);
20
+ return a <= Math.abs(sweep);
21
+ }
22
+ /** Undo a primitive's rotation. */
23
+ function local(x, y, rot) {
24
+ if (rot === 0) return [x, y];
25
+ const c = Math.cos(-rot);
26
+ const s = Math.sin(-rot);
27
+ return [c * x - s * y, s * x + c * y];
28
+ }
29
+ function hitTest(f, count, x, y) {
30
+ for (let i = count - 1; i >= 0; i--) {
31
+ const o = i * 40;
32
+ if (x < f[o] || y < f[o + 1] || x > f[o + 2] || y > f[o + 3]) continue;
33
+ const type = f[o + 16];
34
+ const flags = f[o + 17];
35
+ const hw = f[o + 18] / 2;
36
+ const fill = (flags & Flag.Fill) !== 0;
37
+ const stroke = (flags & Flag.Stroke) !== 0 && hw > 0;
38
+ const off = f[o + 39] * hw;
39
+ let d = Infinity;
40
+ switch (type) {
41
+ case Prim.Rect: {
42
+ const r = Math.max(f[o + 8], f[o + 9], f[o + 10], f[o + 11]);
43
+ const [px, py] = local(x - f[o + 4], y - f[o + 5], f[o + 15]);
44
+ d = roundBox(px, py, f[o + 6], f[o + 7], Math.min(r, f[o + 6], f[o + 7]));
45
+ break;
46
+ }
47
+ case Prim.Ellipse: {
48
+ const [px, py] = local(x - f[o + 4], y - f[o + 5], f[o + 15]);
49
+ const qx = px / f[o + 6];
50
+ const qy = py / f[o + 7];
51
+ const k0 = Math.hypot(qx, qy);
52
+ const k1 = Math.hypot(qx / f[o + 6], qy / f[o + 7]);
53
+ d = k1 > 0 ? k0 * (k0 - 1) / k1 : -Math.min(f[o + 6], f[o + 7]);
54
+ break;
55
+ }
56
+ case Prim.Arc: {
57
+ const qx = x - f[o + 4];
58
+ const qy = y - f[o + 5];
59
+ if (inSweep(qx, qy, f[o + 7], f[o + 8]) && Math.abs(Math.hypot(qx, qy) - f[o + 6]) <= hw) return true;
60
+ continue;
61
+ }
62
+ case Prim.Sector: {
63
+ const qx = x - f[o + 4];
64
+ const qy = y - f[o + 5];
65
+ const len = Math.hypot(qx, qy);
66
+ if (len <= f[o + 6] && len >= f[o + 9] && inSweep(qx, qy, f[o + 7], f[o + 8])) return true;
67
+ continue;
68
+ }
69
+ case Prim.Segment:
70
+ if (segDist(x, y, f[o + 4], f[o + 5], f[o + 6], f[o + 7]) <= Math.max(hw, 1)) return true;
71
+ continue;
72
+ case Prim.Area: {
73
+ const ax = f[o + 4];
74
+ const bx = f[o + 6];
75
+ if (x < ax || x > bx) continue;
76
+ const t = (x - ax) / (bx - ax || 1);
77
+ const top = f[o + 5] + (f[o + 7] - f[o + 5]) * t;
78
+ const bottom = f[o + 9] + (f[o + 11] - f[o + 9]) * t;
79
+ if (y >= Math.min(top, bottom) && y <= Math.max(top, bottom)) return true;
80
+ continue;
81
+ }
82
+ default: return true;
83
+ }
84
+ if (fill && d <= 0 || stroke && Math.abs(d - off) <= hw) return true;
85
+ }
86
+ return false;
87
+ }
88
+ //#endregion
89
+ export { hitTest };
@@ -0,0 +1,8 @@
1
+ import { SilkApp, SilkAppOptions, createSilkApp } from "./app.js";
2
+ import { ColorSpace, RGBA, linearToOklab, oklabToLinear, parseColor } from "./color.js";
3
+ import { ConicGradient, ConicGradientOptions, Easing, ExtendMode, Gradient, GradientOptions, GradientUnits, LinearGradient, LinearGradientOptions, PathGradient, RadialGradient, RadialGradientOptions, StopInput, Vec2, along, conic, horizontal, linear, radial, vertical } from "./gradient.js";
4
+ import { GradientAtlas } from "./atlas.js";
5
+ import { PointsInput, catmullRom, monotoneX, toFlat } from "./curves.js";
6
+ import { Spring, damp, dampAngle, ease } from "./motion.js";
7
+ import { AreaOptions, FillInput, FillStyle, LineCap, PolylineOptions, SilkGeometry, SilkGraphics, SilkGraphicsOptions, StrokeInput, StrokeStyle } from "./SilkGraphics.js";
8
+ export { type AreaOptions, type ColorSpace, ConicGradient, type ConicGradientOptions, type Easing, type ExtendMode, type FillInput, type FillStyle, Gradient, GradientAtlas, type GradientOptions, type GradientUnits, type LineCap, LinearGradient, type LinearGradientOptions, PathGradient, type PointsInput, type PolylineOptions, type RGBA, RadialGradient, type RadialGradientOptions, type SilkApp, type SilkAppOptions, SilkGeometry, SilkGraphics, type SilkGraphicsOptions, Spring, type StopInput, type StrokeInput, type StrokeStyle, type Vec2, along, catmullRom, conic, createSilkApp, damp, dampAngle, ease, horizontal, linear, linearToOklab, monotoneX, oklabToLinear, parseColor, radial, toFlat, vertical };
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import { createSilkApp } from "./app.js";
2
+ import { linearToOklab, oklabToLinear, parseColor } from "./color.js";
3
+ import { GradientAtlas } from "./atlas.js";
4
+ import { catmullRom, monotoneX, toFlat } from "./curves.js";
5
+ import { ConicGradient, Gradient, LinearGradient, PathGradient, RadialGradient, along, conic, horizontal, linear, radial, vertical } from "./gradient.js";
6
+ import { Spring, damp, dampAngle, ease } from "./motion.js";
7
+ import { SilkGeometry, SilkGraphics } from "./SilkGraphics.js";
8
+ export { ConicGradient, Gradient, GradientAtlas, LinearGradient, PathGradient, RadialGradient, SilkGeometry, SilkGraphics, Spring, along, catmullRom, conic, createSilkApp, damp, dampAngle, ease, horizontal, linear, linearToOklab, monotoneX, oklabToLinear, parseColor, radial, toFlat, vertical };
@@ -0,0 +1,42 @@
1
+ //#region src/motion.d.ts
2
+ /**
3
+ * Frame-rate independent motion helpers. Values glide the same at 60, 120 or 144 Hz,
4
+ * and never overshoot unless you ask a spring to.
5
+ */
6
+ /** Exponential approach: moves `current` towards `target`. `lambda` is about 1/seconds, higher is snappier. */
7
+ export declare function damp(current: number, target: number, lambda: number, dtSeconds: number): number;
8
+ /** Damped angle approach along the shortest path. */
9
+ export declare function dampAngle(current: number, target: number, lambda: number, dtSeconds: number): number;
10
+ /**
11
+ * Damped harmonic spring (semi-implicit Euler with fixed sub-steps).
12
+ * `stiffness` / `damping` follow the usual UI spring convention (e.g. 170 / 26).
13
+ */
14
+ export declare class Spring {
15
+ /** Current value. */
16
+ value: number;
17
+ /** Value the spring moves towards. */
18
+ target: number;
19
+ /** Current speed, in units per second. */
20
+ velocity: number;
21
+ /** Pull towards the target. Higher is faster. */
22
+ stiffness: number;
23
+ /** Friction. Lower bounces more. */
24
+ damping: number;
25
+ constructor(value?: number, stiffness?: number, damping?: number);
26
+ /** Advances the spring and returns the new value. */
27
+ step(dtSeconds: number): number;
28
+ /** True once the spring is at rest on its target. */
29
+ get settled(): boolean;
30
+ }
31
+ /** Easing curves for tweens (t from 0 to 1): inOutCubic, outCubic, inOutSine, outExpo. */
32
+ export declare const ease: {
33
+ /** Slow start, fast middle, slow end. */
34
+ inOutCubic: (t: number) => number;
35
+ /** Fast start, slow end. */
36
+ outCubic: (t: number) => number;
37
+ /** Gentle start and end. */
38
+ inOutSine: (t: number) => number;
39
+ /** Sharp start, long soft landing. */
40
+ outExpo: (t: number) => number;
41
+ };
42
+ //#endregion
package/dist/motion.js ADDED
@@ -0,0 +1,68 @@
1
+ //#region src/motion.ts
2
+ /**
3
+ * Frame-rate independent motion helpers. Values glide the same at 60, 120 or 144 Hz,
4
+ * and never overshoot unless you ask a spring to.
5
+ */
6
+ /** Exponential approach: moves `current` towards `target`. `lambda` is about 1/seconds, higher is snappier. */
7
+ function damp(current, target, lambda, dtSeconds) {
8
+ return target + (current - target) * Math.exp(-lambda * dtSeconds);
9
+ }
10
+ /** Damped angle approach along the shortest path. */
11
+ function dampAngle(current, target, lambda, dtSeconds) {
12
+ let delta = (target - current) % (Math.PI * 2);
13
+ if (delta > Math.PI) delta -= Math.PI * 2;
14
+ if (delta < -Math.PI) delta += Math.PI * 2;
15
+ return damp(current, current + delta, lambda, dtSeconds);
16
+ }
17
+ /**
18
+ * Damped harmonic spring (semi-implicit Euler with fixed sub-steps).
19
+ * `stiffness` / `damping` follow the usual UI spring convention (e.g. 170 / 26).
20
+ */
21
+ var Spring = class {
22
+ /** Current value. */
23
+ value;
24
+ /** Value the spring moves towards. */
25
+ target;
26
+ /** Current speed, in units per second. */
27
+ velocity = 0;
28
+ /** Pull towards the target. Higher is faster. */
29
+ stiffness;
30
+ /** Friction. Lower bounces more. */
31
+ damping;
32
+ constructor(value = 0, stiffness = 170, damping = 26) {
33
+ this.value = value;
34
+ this.target = value;
35
+ this.stiffness = stiffness;
36
+ this.damping = damping;
37
+ }
38
+ /** Advances the spring and returns the new value. */
39
+ step(dtSeconds) {
40
+ const h = 1 / 240;
41
+ let t = Math.min(dtSeconds, .1);
42
+ while (t > 0) {
43
+ const dt = Math.min(h, t);
44
+ const force = -this.stiffness * (this.value - this.target) - this.damping * this.velocity;
45
+ this.velocity += force * dt;
46
+ this.value += this.velocity * dt;
47
+ t -= dt;
48
+ }
49
+ return this.value;
50
+ }
51
+ /** True once the spring is at rest on its target. */
52
+ get settled() {
53
+ return Math.abs(this.value - this.target) < 1e-4 && Math.abs(this.velocity) < 1e-4;
54
+ }
55
+ };
56
+ /** Easing curves for tweens (t from 0 to 1): inOutCubic, outCubic, inOutSine, outExpo. */
57
+ const ease = {
58
+ /** Slow start, fast middle, slow end. */
59
+ inOutCubic: (t) => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2,
60
+ /** Fast start, slow end. */
61
+ outCubic: (t) => 1 - (1 - t) ** 3,
62
+ /** Gentle start and end. */
63
+ inOutSine: (t) => -(Math.cos(Math.PI * t) - 1) / 2,
64
+ /** Sharp start, long soft landing. */
65
+ outExpo: (t) => t === 1 ? 1 : 1 - 2 ** (-10 * t)
66
+ };
67
+ //#endregion
68
+ export { Spring, damp, dampAngle, ease };