pixi-silk 0.1.0 → 1.0.2
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/CHANGELOG.md +114 -0
- package/README.md +48 -68
- package/dist/SilkGraphics.d.ts +270 -55
- package/dist/SilkGraphics.js +909 -147
- package/dist/app.d.ts +32 -16
- package/dist/app.js +44 -10
- package/dist/atlas.d.ts +13 -6
- package/dist/atlas.js +13 -6
- package/dist/batch.d.ts +63 -0
- package/dist/batch.js +327 -0
- package/dist/color.d.ts +4 -1
- package/dist/color.js +9 -4
- package/dist/constants.js +31 -5
- package/dist/curves.d.ts +9 -3
- package/dist/curves.js +11 -5
- package/dist/glass/Glass.d.ts +62 -0
- package/dist/glass/Glass.js +135 -0
- package/dist/glass/GlassFilter.d.ts +33 -0
- package/dist/glass/GlassFilter.js +264 -0
- package/dist/glass/glass-wgsl.js +129 -0
- package/dist/glass/options.d.ts +63 -0
- package/dist/glass/options.js +55 -0
- package/dist/glass.d.ts +4 -0
- package/dist/glass.js +4 -0
- package/dist/glow/Glow.d.ts +60 -0
- package/dist/glow/Glow.js +65 -0
- package/dist/glow/aurora.d.ts +66 -0
- package/dist/glow/aurora.js +133 -0
- package/dist/glow/light.d.ts +38 -0
- package/dist/glow/light.js +83 -0
- package/dist/glow/recipes.d.ts +37 -0
- package/dist/glow/recipes.js +154 -0
- package/dist/glow/shapes.d.ts +13 -0
- package/dist/glow/shapes.js +34 -0
- package/dist/glow.d.ts +6 -0
- package/dist/glow.js +6 -0
- package/dist/gradient.d.ts +57 -4
- package/dist/gradient.js +142 -4
- package/dist/history.d.ts +194 -0
- package/dist/history.js +350 -0
- package/dist/hit.js +160 -26
- package/dist/index.d.ts +6 -5
- package/dist/index.js +4 -3
- package/dist/layout/flex.js +409 -0
- package/dist/layout/node.d.ts +53 -0
- package/dist/layout/node.js +117 -0
- package/dist/layout/style.d.ts +115 -0
- package/dist/layout.d.ts +3 -0
- package/dist/layout.js +2 -0
- package/dist/motion.d.ts +14 -10
- package/dist/motion.js +14 -10
- package/dist/path.d.ts +150 -0
- package/dist/path.js +761 -0
- package/dist/selection.d.ts +133 -0
- package/dist/selection.js +194 -0
- package/dist/shader-slot.js +9 -0
- package/dist/shader-wgsl.js +946 -0
- package/dist/shader.js +480 -55
- package/dist/signal.d.ts +49 -0
- package/dist/signal.js +79 -0
- package/dist/stroke.js +271 -0
- package/dist/text/SilkText.d.ts +249 -0
- package/dist/text/SilkText.js +568 -0
- package/dist/text/atlas.d.ts +153 -0
- package/dist/text/atlas.js +316 -0
- package/dist/text/edt.d.ts +14 -0
- package/dist/text/edt.js +97 -0
- package/dist/text/label.d.ts +19 -0
- package/dist/text/label.js +38 -0
- package/dist/text/layout.d.ts +150 -0
- package/dist/text/layout.js +270 -0
- package/dist/text/segment.d.ts +34 -0
- package/dist/text/segment.js +98 -0
- package/dist/text/shader.js +355 -0
- package/dist/text.d.ts +7 -0
- package/dist/text.js +7 -0
- package/dist/ui/ButtonDock.d.ts +47 -0
- package/dist/ui/ButtonDock.js +110 -0
- package/dist/ui/RadialMenu.d.ts +222 -0
- package/dist/ui/RadialMenu.js +680 -0
- package/dist/ui/SilkButton.d.ts +149 -0
- package/dist/ui/SilkButton.js +323 -0
- package/dist/ui/ValuePill.d.ts +48 -0
- package/dist/ui/ValuePill.js +77 -0
- package/dist/ui/arc.d.ts +32 -0
- package/dist/ui/arc.js +58 -0
- package/dist/ui/brush.d.ts +117 -0
- package/dist/ui/brush.js +280 -0
- package/dist/ui/camera.d.ts +250 -0
- package/dist/ui/camera.js +634 -0
- package/dist/ui/color/ColorPicker.d.ts +72 -0
- package/dist/ui/color/ColorPicker.js +125 -0
- package/dist/ui/color/ColorSlider.d.ts +81 -0
- package/dist/ui/color/ColorSlider.js +227 -0
- package/dist/ui/color/ColorWheel.d.ts +34 -0
- package/dist/ui/color/ColorWheel.js +117 -0
- package/dist/ui/color/Eyedropper.d.ts +57 -0
- package/dist/ui/color/Eyedropper.js +159 -0
- package/dist/ui/color/OklchPicker.d.ts +45 -0
- package/dist/ui/color/OklchPicker.js +137 -0
- package/dist/ui/color/SwatchPalette.d.ts +60 -0
- package/dist/ui/color/SwatchPalette.js +160 -0
- package/dist/ui/color/SwatchPopup.d.ts +68 -0
- package/dist/ui/color/SwatchPopup.js +237 -0
- package/dist/ui/color/draw.d.ts +22 -0
- package/dist/ui/color/draw.js +128 -0
- package/dist/ui/color/model.d.ts +94 -0
- package/dist/ui/color/model.js +193 -0
- package/dist/ui/color/rings.d.ts +75 -0
- package/dist/ui/color/rings.js +247 -0
- package/dist/ui/color.d.ts +11 -0
- package/dist/ui/color.js +11 -0
- package/dist/ui/dial.d.ts +229 -0
- package/dist/ui/dial.js +553 -0
- package/dist/ui/driver.d.ts +24 -0
- package/dist/ui/driver.js +46 -0
- package/dist/ui/gesture.d.ts +66 -0
- package/dist/ui/gesture.js +116 -0
- package/dist/ui/gizmo.d.ts +199 -0
- package/dist/ui/gizmo.js +957 -0
- package/dist/ui/grid.d.ts +106 -0
- package/dist/ui/grid.js +228 -0
- package/dist/ui/group.d.ts +81 -0
- package/dist/ui/group.js +144 -0
- package/dist/ui/guides.d.ts +142 -0
- package/dist/ui/guides.js +379 -0
- package/dist/ui/halo.d.ts +164 -0
- package/dist/ui/halo.js +438 -0
- package/dist/ui/icon.d.ts +14 -0
- package/dist/ui/icon.js +40 -0
- package/dist/ui/index.d.ts +62 -0
- package/dist/ui/index.js +56 -0
- package/dist/ui/input.d.ts +28 -0
- package/dist/ui/input.js +49 -0
- package/dist/ui/inspector.d.ts +4 -0
- package/dist/ui/inspector.js +4 -0
- package/dist/ui/keys.d.ts +20 -0
- package/dist/ui/keys.js +65 -0
- package/dist/ui/label.d.ts +18 -0
- package/dist/ui/layers.d.ts +5 -0
- package/dist/ui/layers.js +5 -0
- package/dist/ui/layout/debug.d.ts +38 -0
- package/dist/ui/layout/debug.js +299 -0
- package/dist/ui/layout/flex-container.d.ts +150 -0
- package/dist/ui/layout/flex-container.js +441 -0
- package/dist/ui/layout/simulate.d.ts +49 -0
- package/dist/ui/layout/simulate.js +92 -0
- package/dist/ui/layout/smart-container.d.ts +180 -0
- package/dist/ui/layout/smart-container.js +302 -0
- package/dist/ui/layout.d.ts +5 -0
- package/dist/ui/layout.js +5 -0
- package/dist/ui/marquee.d.ts +124 -0
- package/dist/ui/marquee.js +321 -0
- package/dist/ui/measure.d.ts +138 -0
- package/dist/ui/measure.js +351 -0
- package/dist/ui/minimap.d.ts +2 -0
- package/dist/ui/minimap.js +2 -0
- package/dist/ui/modes.d.ts +186 -0
- package/dist/ui/modes.js +380 -0
- package/dist/ui/odometer.d.ts +104 -0
- package/dist/ui/odometer.js +244 -0
- package/dist/ui/outline.d.ts +104 -0
- package/dist/ui/outline.js +310 -0
- package/dist/ui/paint.js +22 -0
- package/dist/ui/panel.d.ts +4 -0
- package/dist/ui/panel.js +4 -0
- package/dist/ui/panels/Inspector.d.ts +228 -0
- package/dist/ui/panels/Inspector.js +516 -0
- package/dist/ui/panels/LayerTree.d.ts +193 -0
- package/dist/ui/panels/LayerTree.js +878 -0
- package/dist/ui/panels/Minimap.d.ts +140 -0
- package/dist/ui/panels/Minimap.js +412 -0
- package/dist/ui/panels/Panel.d.ts +273 -0
- package/dist/ui/panels/Panel.js +767 -0
- package/dist/ui/panels/ScrollView.d.ts +126 -0
- package/dist/ui/panels/ScrollView.js +358 -0
- package/dist/ui/panels/binding.d.ts +59 -0
- package/dist/ui/panels/binding.js +84 -0
- package/dist/ui/panels/fields.d.ts +265 -0
- package/dist/ui/panels/fields.js +760 -0
- package/dist/ui/panels/icons.d.ts +7 -0
- package/dist/ui/panels/icons.js +136 -0
- package/dist/ui/panels/pixi-tree.d.ts +35 -0
- package/dist/ui/panels/pixi-tree.js +82 -0
- package/dist/ui/panels/theme.d.ts +79 -0
- package/dist/ui/panels/theme.js +91 -0
- package/dist/ui/panels/tree.d.ts +130 -0
- package/dist/ui/panels/tree.js +199 -0
- package/dist/ui/pathgizmo.d.ts +253 -0
- package/dist/ui/pathgizmo.js +932 -0
- package/dist/ui/pill.d.ts +3 -0
- package/dist/ui/pill.js +2 -0
- package/dist/ui/ruler.d.ts +113 -0
- package/dist/ui/ruler.js +371 -0
- package/dist/ui/snap.d.ts +151 -0
- package/dist/ui/snap.js +380 -0
- package/dist/ui/spacing.d.ts +122 -0
- package/dist/ui/spacing.js +346 -0
- package/dist/ui/textedit.d.ts +304 -0
- package/dist/ui/textedit.js +1085 -0
- package/dist/ui/timeline.d.ts +175 -0
- package/dist/ui/timeline.js +488 -0
- package/dist/webgpu.d.ts +25 -0
- package/dist/webgpu.js +32 -0
- package/package.json +172 -1
- package/skills/pixi-silk/SKILL.md +138 -0
- package/skills/pixi-silk-app-rendering/SKILL.md +114 -0
- package/skills/pixi-silk-drawing/SKILL.md +154 -0
- package/skills/pixi-silk-editor/SKILL.md +200 -0
- package/skills/pixi-silk-layout/SKILL.md +150 -0
- package/skills/pixi-silk-migrate-from-graphics/SKILL.md +115 -0
- package/skills/pixi-silk-paint/SKILL.md +132 -0
- package/skills/pixi-silk-text/SKILL.md +127 -0
- package/skills/pixi-silk-widgets/SKILL.md +139 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Signal } from "../signal.js";
|
|
2
|
+
import { Camera } from "./camera.js";
|
|
3
|
+
import { ColorSource, Container, Ticker } from "pixi.js";
|
|
4
|
+
//#region src/ui/grid.d.ts
|
|
5
|
+
/** How a {@link CanvasGrid} draws: thin `lines` or `dots` at the crossings. */
|
|
6
|
+
export type GridStyle = "lines" | "dots";
|
|
7
|
+
/** One level of a {@link CanvasGrid}, as {@link gridLevels} returns it. */
|
|
8
|
+
export interface GridLevel {
|
|
9
|
+
/** Distance between lines, in world units. */
|
|
10
|
+
step: number;
|
|
11
|
+
/** Distance between lines on screen, in CSS pixels (`step` times the zoom). */
|
|
12
|
+
spacing: number;
|
|
13
|
+
/** Opacity this level adds, 0..1, so the lines it shares with coarser levels add up to theirs. */
|
|
14
|
+
alpha: number;
|
|
15
|
+
}
|
|
16
|
+
/** Options of {@link gridLevels}. */
|
|
17
|
+
export interface GridLevelOptions {
|
|
18
|
+
/** Finest step, in world units. Default 1, a pixel grid at 100%. Must be above 0. */
|
|
19
|
+
unit?: number;
|
|
20
|
+
/** Each level is this many times the one below. Default 10, at least 2. */
|
|
21
|
+
ratio?: number;
|
|
22
|
+
/** Opacity of a line at full strength, 0..1. Default 0.1. */
|
|
23
|
+
alpha?: number;
|
|
24
|
+
/** Screen spacing in CSS pixels where a level starts to show and where it reaches full strength. Default `[6, 120]`. */
|
|
25
|
+
fade?: [number, number];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Options of {@link CanvasGrid}. In `dots` style, `alpha` defaults to 0.32 and `fade` to
|
|
29
|
+
* `[12, 160]`, so dots read as strong as lines.
|
|
30
|
+
*/
|
|
31
|
+
export interface CanvasGridOptions extends GridLevelOptions {
|
|
32
|
+
/** The camera the grid follows. The grid redraws whenever the camera's signals change. */
|
|
33
|
+
camera: Camera;
|
|
34
|
+
/** Lines or dots. Default `lines`. Change it later through `state.style`, and the grid crossfades. */
|
|
35
|
+
style?: GridStyle;
|
|
36
|
+
/** Line and dot colour. Default white. */
|
|
37
|
+
color?: ColorSource;
|
|
38
|
+
/** Line width in CSS pixels. Default 1. A level is skipped when its spacing is below twice this. */
|
|
39
|
+
lineWidth?: number;
|
|
40
|
+
/** Dot diameter in CSS pixels. Default 2. A level is skipped when its spacing is below twice this. */
|
|
41
|
+
dotSize?: number;
|
|
42
|
+
/** Draws the x = 0 and y = 0 axes in `axisColor`, 1 pixel wide. Default false. Change it later through `state.axes`. */
|
|
43
|
+
axes?: boolean;
|
|
44
|
+
/** Colour of the axes. Default `0x64a8ff`. */
|
|
45
|
+
axisColor?: ColorSource;
|
|
46
|
+
/** Opacity of the axes, 0..1. Default 0.5. */
|
|
47
|
+
axisAlpha?: number;
|
|
48
|
+
/** Ticker that runs fades and redraws. Default `Ticker.shared`. With `null`, call {@link CanvasGrid.tick} yourself. */
|
|
49
|
+
ticker?: Ticker | null;
|
|
50
|
+
}
|
|
51
|
+
/** The reactive state of a {@link CanvasGrid}. Write to these signals to change the grid. */
|
|
52
|
+
export interface CanvasGridState {
|
|
53
|
+
/** Lines or dots. Changes crossfade. */
|
|
54
|
+
style: Signal<GridStyle>;
|
|
55
|
+
/** Shows or hides the grid with a fade. Starts true. Separate from the container's `visible`. */
|
|
56
|
+
visible: Signal<boolean>;
|
|
57
|
+
/** Shows the x = 0 and y = 0 axes. Switches at once, without a fade. */
|
|
58
|
+
axes: Signal<boolean>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The grid levels to draw at `zoom`, finest first. Each level is `ratio` times the one below. A
|
|
62
|
+
* level fades in as its spacing grows from `fade[0]` to `fade[1]` pixels. Levels nest, so a line
|
|
63
|
+
* of a coarse level is also drawn by the finer ones: each level's `alpha` is what it adds on top.
|
|
64
|
+
* Levels that would add almost nothing are left out. Returns an empty array for a zoom or unit of 0 or less.
|
|
65
|
+
*/
|
|
66
|
+
export declare function gridLevels(zoom: number, options?: GridLevelOptions): GridLevel[];
|
|
67
|
+
/**
|
|
68
|
+
* An unbounded grid behind a {@link Camera}'s world. Levels nest (1, 10, 100 by default) and fade
|
|
69
|
+
* in and out with the zoom. The grid lives in screen space and only draws what shows. All vertical
|
|
70
|
+
* lines of a level are one dashed stroke, and so are the horizontal ones. Dots take one stroke per row.
|
|
71
|
+
*
|
|
72
|
+
* Add it under the world, in screen space: `app.stage.addChild(grid, world, overlay)`. It covers
|
|
73
|
+
* the camera's element from (0, 0), ignores pointer events, and cannot be rotated with the view.
|
|
74
|
+
*
|
|
75
|
+
* Experimental: the API may change in a minor release.
|
|
76
|
+
* @experimental
|
|
77
|
+
*/
|
|
78
|
+
export declare class CanvasGrid extends Container {
|
|
79
|
+
/** Style, fade and axes, as signals. */
|
|
80
|
+
readonly state: CanvasGridState;
|
|
81
|
+
private readonly _g;
|
|
82
|
+
private readonly _camera;
|
|
83
|
+
private readonly _opts;
|
|
84
|
+
private readonly _driver;
|
|
85
|
+
private readonly _color;
|
|
86
|
+
private readonly _axisColor;
|
|
87
|
+
private readonly _off;
|
|
88
|
+
private _lines;
|
|
89
|
+
private _dots;
|
|
90
|
+
private _shown;
|
|
91
|
+
private _drawn;
|
|
92
|
+
constructor(options: CanvasGridOptions);
|
|
93
|
+
/** How many shapes the last frame drew, each one silk instance. For tests and stats. */
|
|
94
|
+
get shapes(): number;
|
|
95
|
+
/** The levels of the current style at the camera's zoom. Computed on each read. */
|
|
96
|
+
get levels(): GridLevel[];
|
|
97
|
+
/** Redraws on the next frame. Camera moves and resizes the camera sees redraw by themselves. */
|
|
98
|
+
refresh(): this;
|
|
99
|
+
/** Advances fades by `dt` seconds (clamped to 0..0.1) and redraws. Only needed with `ticker: null`. */
|
|
100
|
+
tick(dt: number): boolean;
|
|
101
|
+
override destroy(options?: Parameters<Container["destroy"]>[0]): void;
|
|
102
|
+
private _levelOptions;
|
|
103
|
+
private _step;
|
|
104
|
+
private _draw;
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
package/dist/ui/grid.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { SilkGraphics } from "../SilkGraphics.js";
|
|
2
|
+
import { damp } from "../motion.js";
|
|
3
|
+
import { signal } from "../signal.js";
|
|
4
|
+
import { Driver } from "./driver.js";
|
|
5
|
+
import { hex } from "./paint.js";
|
|
6
|
+
import { Container } from "pixi.js";
|
|
7
|
+
//#region src/ui/grid.ts
|
|
8
|
+
const smooth = (t) => {
|
|
9
|
+
const u = t < 0 ? 0 : t > 1 ? 1 : t;
|
|
10
|
+
return u * u * (3 - 2 * u);
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The grid levels to draw at `zoom`, finest first. Each level is `ratio` times the one below. A
|
|
14
|
+
* level fades in as its spacing grows from `fade[0]` to `fade[1]` pixels. Levels nest, so a line
|
|
15
|
+
* of a coarse level is also drawn by the finer ones: each level's `alpha` is what it adds on top.
|
|
16
|
+
* Levels that would add almost nothing are left out. Returns an empty array for a zoom or unit of 0 or less.
|
|
17
|
+
*/
|
|
18
|
+
function gridLevels(zoom, options = {}) {
|
|
19
|
+
const unit = options.unit ?? 1;
|
|
20
|
+
const ratio = Math.max(2, options.ratio ?? 10);
|
|
21
|
+
const max = options.alpha ?? .1;
|
|
22
|
+
const [lo, hi] = options.fade ?? [6, 120];
|
|
23
|
+
const strength = (spacing) => max * smooth(Math.log(spacing / lo) / Math.log(Math.max(hi / lo, 1.0001)));
|
|
24
|
+
const out = [];
|
|
25
|
+
if (!(zoom > 0) || !(unit > 0)) return out;
|
|
26
|
+
let n = Math.max(0, Math.ceil(Math.log(lo / (unit * zoom)) / Math.log(ratio) - 1e-9));
|
|
27
|
+
let below = 0;
|
|
28
|
+
for (let i = 0; i < 64; i++, n++) {
|
|
29
|
+
const step = unit * ratio ** n;
|
|
30
|
+
const spacing = step * zoom;
|
|
31
|
+
const total = strength(spacing);
|
|
32
|
+
const alpha = 1 - (1 - total) / (1 - below);
|
|
33
|
+
if (alpha > .002) out.push({
|
|
34
|
+
step,
|
|
35
|
+
spacing,
|
|
36
|
+
alpha
|
|
37
|
+
});
|
|
38
|
+
below = total;
|
|
39
|
+
if (total >= max - 1e-6) break;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* An unbounded grid behind a {@link Camera}'s world. Levels nest (1, 10, 100 by default) and fade
|
|
45
|
+
* in and out with the zoom. The grid lives in screen space and only draws what shows. All vertical
|
|
46
|
+
* lines of a level are one dashed stroke, and so are the horizontal ones. Dots take one stroke per row.
|
|
47
|
+
*
|
|
48
|
+
* Add it under the world, in screen space: `app.stage.addChild(grid, world, overlay)`. It covers
|
|
49
|
+
* the camera's element from (0, 0), ignores pointer events, and cannot be rotated with the view.
|
|
50
|
+
*
|
|
51
|
+
* Experimental: the API may change in a minor release.
|
|
52
|
+
* @experimental
|
|
53
|
+
*/
|
|
54
|
+
var CanvasGrid = class extends Container {
|
|
55
|
+
/** Style, fade and axes, as signals. */
|
|
56
|
+
state;
|
|
57
|
+
_g = new SilkGraphics({ label: "CanvasGrid" });
|
|
58
|
+
_camera;
|
|
59
|
+
_opts;
|
|
60
|
+
_driver;
|
|
61
|
+
_color;
|
|
62
|
+
_axisColor;
|
|
63
|
+
_off = [];
|
|
64
|
+
_lines;
|
|
65
|
+
_dots;
|
|
66
|
+
_shown;
|
|
67
|
+
_drawn = 0;
|
|
68
|
+
constructor(options) {
|
|
69
|
+
super({ label: "CanvasGrid" });
|
|
70
|
+
this._opts = options;
|
|
71
|
+
this._camera = options.camera;
|
|
72
|
+
this._color = hex(options.color ?? 16777215);
|
|
73
|
+
this._axisColor = hex(options.axisColor ?? 6596863);
|
|
74
|
+
const style = options.style ?? "lines";
|
|
75
|
+
this.state = {
|
|
76
|
+
style: signal(style),
|
|
77
|
+
visible: signal(true),
|
|
78
|
+
axes: signal(options.axes ?? false)
|
|
79
|
+
};
|
|
80
|
+
this._lines = style === "lines" ? 1 : 0;
|
|
81
|
+
this._dots = 1 - this._lines;
|
|
82
|
+
this._shown = 1;
|
|
83
|
+
this.eventMode = "none";
|
|
84
|
+
this.addChild(this._g);
|
|
85
|
+
this._driver = new Driver((dt) => this._step(dt), options.ticker);
|
|
86
|
+
const wake = () => this._driver.wake();
|
|
87
|
+
const cam = options.camera.state;
|
|
88
|
+
for (const s of [
|
|
89
|
+
cam.zoom,
|
|
90
|
+
cam.x,
|
|
91
|
+
cam.y,
|
|
92
|
+
cam.width,
|
|
93
|
+
cam.height
|
|
94
|
+
]) this._off.push(s.subscribe(wake));
|
|
95
|
+
this._off.push(this.state.style.subscribe(wake), this.state.visible.subscribe(wake), this.state.axes.subscribe(wake));
|
|
96
|
+
this._draw();
|
|
97
|
+
}
|
|
98
|
+
/** How many shapes the last frame drew, each one silk instance. For tests and stats. */
|
|
99
|
+
get shapes() {
|
|
100
|
+
return this._drawn;
|
|
101
|
+
}
|
|
102
|
+
/** The levels of the current style at the camera's zoom. Computed on each read. */
|
|
103
|
+
get levels() {
|
|
104
|
+
return gridLevels(this._camera.zoom, this._levelOptions());
|
|
105
|
+
}
|
|
106
|
+
/** Redraws on the next frame. Camera moves and resizes the camera sees redraw by themselves. */
|
|
107
|
+
refresh() {
|
|
108
|
+
this._driver.wake();
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/** Advances fades by `dt` seconds (clamped to 0..0.1) and redraws. Only needed with `ticker: null`. */
|
|
112
|
+
tick(dt) {
|
|
113
|
+
return this._driver.tick(dt);
|
|
114
|
+
}
|
|
115
|
+
destroy(options) {
|
|
116
|
+
for (const off of this._off) off();
|
|
117
|
+
this._off.length = 0;
|
|
118
|
+
this._driver.sleep();
|
|
119
|
+
super.destroy(options ?? { children: true });
|
|
120
|
+
}
|
|
121
|
+
_levelOptions(style = this.state.style.value) {
|
|
122
|
+
const o = this._opts;
|
|
123
|
+
return {
|
|
124
|
+
unit: o.unit,
|
|
125
|
+
ratio: o.ratio,
|
|
126
|
+
alpha: o.alpha ?? (style === "dots" ? .32 : .1),
|
|
127
|
+
fade: o.fade ?? (style === "dots" ? [12, 160] : [6, 120])
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
_step(dt) {
|
|
131
|
+
const dots = this.state.style.value === "dots" ? 1 : 0;
|
|
132
|
+
const show = this.state.visible.value ? 1 : 0;
|
|
133
|
+
let busy = false;
|
|
134
|
+
const ease = (v, to) => {
|
|
135
|
+
const next = damp(v, to, 14, dt);
|
|
136
|
+
if (Math.abs(next - to) < .004) return to;
|
|
137
|
+
busy = true;
|
|
138
|
+
return next;
|
|
139
|
+
};
|
|
140
|
+
this._dots = ease(this._dots, dots);
|
|
141
|
+
this._lines = ease(this._lines, 1 - dots);
|
|
142
|
+
this._shown = ease(this._shown, show);
|
|
143
|
+
this._draw();
|
|
144
|
+
return busy;
|
|
145
|
+
}
|
|
146
|
+
_draw() {
|
|
147
|
+
const g = this._g.clear();
|
|
148
|
+
const cam = this._camera;
|
|
149
|
+
const view = cam.view;
|
|
150
|
+
const k = cam.zoom;
|
|
151
|
+
const w = view.width * k;
|
|
152
|
+
const h = view.height * k;
|
|
153
|
+
const ox = cam.state.x.value;
|
|
154
|
+
const oy = cam.state.y.value;
|
|
155
|
+
let count = 0;
|
|
156
|
+
if (this._shown <= .002 || !(w > 0) || !(h > 0)) {
|
|
157
|
+
this._drawn = 0;
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const first = (origin, step) => origin - Math.floor(origin / step) * step - step;
|
|
161
|
+
if (this._lines > .002) {
|
|
162
|
+
const lw = this._opts.lineWidth ?? 1;
|
|
163
|
+
for (const level of gridLevels(k, this._levelOptions("lines"))) {
|
|
164
|
+
const p = level.spacing;
|
|
165
|
+
const alpha = level.alpha * this._lines * this._shown;
|
|
166
|
+
if (p < lw * 2 || alpha <= .002) continue;
|
|
167
|
+
const x0 = first(ox, p) - lw / 2;
|
|
168
|
+
const y0 = first(oy, p) - lw / 2;
|
|
169
|
+
const style = {
|
|
170
|
+
color: this._color,
|
|
171
|
+
alpha,
|
|
172
|
+
cap: "butt"
|
|
173
|
+
};
|
|
174
|
+
g.moveTo(x0, h / 2).lineTo(w + p, h / 2).stroke({
|
|
175
|
+
...style,
|
|
176
|
+
width: h,
|
|
177
|
+
dash: [lw, p - lw]
|
|
178
|
+
});
|
|
179
|
+
g.moveTo(w / 2, y0).lineTo(w / 2, h + p).stroke({
|
|
180
|
+
...style,
|
|
181
|
+
width: w,
|
|
182
|
+
dash: [lw, p - lw]
|
|
183
|
+
});
|
|
184
|
+
count += 2;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (this._dots > .002) {
|
|
188
|
+
const size = this._opts.dotSize ?? 2;
|
|
189
|
+
for (const level of gridLevels(k, this._levelOptions("dots"))) {
|
|
190
|
+
const p = level.spacing;
|
|
191
|
+
const alpha = level.alpha * this._dots * this._shown;
|
|
192
|
+
if (p < size * 2 || alpha <= .002) continue;
|
|
193
|
+
const x0 = first(ox, p);
|
|
194
|
+
const y0 = first(oy, p);
|
|
195
|
+
const style = {
|
|
196
|
+
color: this._color,
|
|
197
|
+
alpha,
|
|
198
|
+
width: size,
|
|
199
|
+
cap: "round",
|
|
200
|
+
dash: [0, p]
|
|
201
|
+
};
|
|
202
|
+
for (let y = y0; y <= h + p; y += p) {
|
|
203
|
+
g.moveTo(x0, y).lineTo(w + p, y).stroke(style);
|
|
204
|
+
count++;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (this.state.axes.value) {
|
|
209
|
+
const alpha = (this._opts.axisAlpha ?? .5) * this._shown;
|
|
210
|
+
const style = {
|
|
211
|
+
color: this._axisColor,
|
|
212
|
+
alpha,
|
|
213
|
+
width: 1
|
|
214
|
+
};
|
|
215
|
+
if (ox >= 0 && ox <= w) {
|
|
216
|
+
g.moveTo(ox, 0).lineTo(ox, h).stroke(style);
|
|
217
|
+
count++;
|
|
218
|
+
}
|
|
219
|
+
if (oy >= 0 && oy <= h) {
|
|
220
|
+
g.moveTo(0, oy).lineTo(w, oy).stroke(style);
|
|
221
|
+
count++;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
this._drawn = count;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
//#endregion
|
|
228
|
+
export { CanvasGrid, gridLevels };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Signal } from "../signal.js";
|
|
2
|
+
import { Container } from "pixi.js";
|
|
3
|
+
//#region src/ui/group.d.ts
|
|
4
|
+
/** The reactive state of a {@link SelectionGroup}. */
|
|
5
|
+
export interface SelectionGroupState {
|
|
6
|
+
/** The objects the group moves. Set it through {@link SelectionGroup.set}, which also refits the box. */
|
|
7
|
+
targets: Signal<readonly Container[]>;
|
|
8
|
+
}
|
|
9
|
+
/** The three callbacks a {@link TransformGizmo} takes, wired to a {@link SelectionGroup}. */
|
|
10
|
+
export interface SelectionGroupHooks {
|
|
11
|
+
/** Calls {@link SelectionGroup.begin}. */
|
|
12
|
+
onStart: () => void;
|
|
13
|
+
/** Calls {@link SelectionGroup.apply}. */
|
|
14
|
+
onChange: () => void;
|
|
15
|
+
/** Calls {@link SelectionGroup.end}. */
|
|
16
|
+
onEnd: () => void;
|
|
17
|
+
}
|
|
18
|
+
/** Options of {@link SelectionGroup}. */
|
|
19
|
+
export interface SelectionGroupOptions {
|
|
20
|
+
/** Objects to start with. The box fits them once the group has a parent and you call `refit()`. */
|
|
21
|
+
targets?: readonly Container[];
|
|
22
|
+
/** Called after {@link SelectionGroup.apply} wrote the new transforms, once per drag move. */
|
|
23
|
+
onApply?: (targets: readonly Container[]) => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Several selected objects as one: a stand-in box around all of them that a
|
|
27
|
+
* {@link TransformGizmo} can move, resize and rotate. Every change of the box is applied to each
|
|
28
|
+
* object about the common pivot, whatever their parents.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* const group = new SelectionGroup();
|
|
32
|
+
* world.addChild(group);
|
|
33
|
+
* const gizmo = new TransformGizmo({ mode: 'box', ...group.hooks() });
|
|
34
|
+
* group.set(selected);
|
|
35
|
+
* gizmo.attach(group.target);
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* The group draws nothing and catches no pointer. Add it to the space the objects live in
|
|
39
|
+
* (usually the camera's world). It works on plain arrays of containers, so any selection store
|
|
40
|
+
* can drive it. Scaling a rotated group unevenly gives its objects a skew.
|
|
41
|
+
*
|
|
42
|
+
* Experimental: the API may change in a minor release.
|
|
43
|
+
* @experimental
|
|
44
|
+
*/
|
|
45
|
+
export declare class SelectionGroup extends Container {
|
|
46
|
+
/** The grouped objects, as a signal. */
|
|
47
|
+
readonly state: SelectionGroupState;
|
|
48
|
+
private readonly _opts;
|
|
49
|
+
private _start;
|
|
50
|
+
private _snap;
|
|
51
|
+
constructor(options?: SelectionGroupOptions);
|
|
52
|
+
/** The objects the group moves, the value of `state.targets`. */
|
|
53
|
+
get targets(): readonly Container[];
|
|
54
|
+
/**
|
|
55
|
+
* What to attach a gizmo to: the object itself when one is selected (so it keeps its own
|
|
56
|
+
* rotated frame), the group for several, null for none.
|
|
57
|
+
*/
|
|
58
|
+
get target(): Container | null;
|
|
59
|
+
/**
|
|
60
|
+
* Groups `targets` and fits the box around them, square to the space. The signal only changes
|
|
61
|
+
* when the set of objects changes, in any order.
|
|
62
|
+
*/
|
|
63
|
+
set(targets: readonly Container[]): this;
|
|
64
|
+
/**
|
|
65
|
+
* Fits the box around the objects again, square to the space, after they moved on their own.
|
|
66
|
+
* Resets the group's rotation, skew, scale and pivot. Without a parent the box is empty.
|
|
67
|
+
*/
|
|
68
|
+
refit(): this;
|
|
69
|
+
/** Remembers where the box and every object are: the start of a drag. Does nothing with fewer than two objects. */
|
|
70
|
+
begin(): this;
|
|
71
|
+
/**
|
|
72
|
+
* Applies how the box changed since {@link SelectionGroup.begin} to every object, through
|
|
73
|
+
* `setFromMatrix`, then calls `onApply`. No-op before `begin()`.
|
|
74
|
+
*/
|
|
75
|
+
apply(): this;
|
|
76
|
+
/** Ends a drag. The box keeps its rotation and size until the next `set()` or `refit()`. */
|
|
77
|
+
end(): this;
|
|
78
|
+
/** `onStart`, `onChange` and `onEnd` for a {@link TransformGizmo}'s options. Allocates new closures each call. */
|
|
79
|
+
hooks(): SelectionGroupHooks;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
package/dist/ui/group.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { signal } from "../signal.js";
|
|
2
|
+
import { boundsQuad, unionQuad } from "./outline.js";
|
|
3
|
+
import { Container, Matrix, Rectangle } from "pixi.js";
|
|
4
|
+
//#region src/ui/group.ts
|
|
5
|
+
const sameList = (a, b) => a.length === b.length && a.every((t) => b.includes(t));
|
|
6
|
+
/** `m` then `n`: the matrix that applies `n` first, then `m`. */
|
|
7
|
+
function compose(m, n, out = new Matrix()) {
|
|
8
|
+
return out.set(m.a * n.a + m.c * n.b, m.b * n.a + m.d * n.b, m.a * n.c + m.c * n.d, m.b * n.c + m.d * n.d, m.a * n.tx + m.c * n.ty + m.tx, m.b * n.tx + m.d * n.ty + m.ty);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Several selected objects as one: a stand-in box around all of them that a
|
|
12
|
+
* {@link TransformGizmo} can move, resize and rotate. Every change of the box is applied to each
|
|
13
|
+
* object about the common pivot, whatever their parents.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const group = new SelectionGroup();
|
|
17
|
+
* world.addChild(group);
|
|
18
|
+
* const gizmo = new TransformGizmo({ mode: 'box', ...group.hooks() });
|
|
19
|
+
* group.set(selected);
|
|
20
|
+
* gizmo.attach(group.target);
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The group draws nothing and catches no pointer. Add it to the space the objects live in
|
|
24
|
+
* (usually the camera's world). It works on plain arrays of containers, so any selection store
|
|
25
|
+
* can drive it. Scaling a rotated group unevenly gives its objects a skew.
|
|
26
|
+
*
|
|
27
|
+
* Experimental: the API may change in a minor release.
|
|
28
|
+
* @experimental
|
|
29
|
+
*/
|
|
30
|
+
var SelectionGroup = class extends Container {
|
|
31
|
+
/** The grouped objects, as a signal. */
|
|
32
|
+
state = { targets: signal([]) };
|
|
33
|
+
_opts;
|
|
34
|
+
_start = null;
|
|
35
|
+
_snap = [];
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
super({ label: "SelectionGroup" });
|
|
38
|
+
this._opts = options;
|
|
39
|
+
this.eventMode = "none";
|
|
40
|
+
this.boundsArea = new Rectangle(0, 0, 0, 0);
|
|
41
|
+
if (options.targets) this.set(options.targets);
|
|
42
|
+
}
|
|
43
|
+
/** The objects the group moves, the value of `state.targets`. */
|
|
44
|
+
get targets() {
|
|
45
|
+
return this.state.targets.value;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* What to attach a gizmo to: the object itself when one is selected (so it keeps its own
|
|
49
|
+
* rotated frame), the group for several, null for none.
|
|
50
|
+
*/
|
|
51
|
+
get target() {
|
|
52
|
+
const list = this.targets;
|
|
53
|
+
return list.length === 0 ? null : list.length === 1 ? list[0] : this;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Groups `targets` and fits the box around them, square to the space. The signal only changes
|
|
57
|
+
* when the set of objects changes, in any order.
|
|
58
|
+
*/
|
|
59
|
+
set(targets) {
|
|
60
|
+
if (!sameList(targets, this.targets)) this.state.targets.set([...targets]);
|
|
61
|
+
return this.refit();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Fits the box around the objects again, square to the space, after they moved on their own.
|
|
65
|
+
* Resets the group's rotation, skew, scale and pivot. Without a parent the box is empty.
|
|
66
|
+
*/
|
|
67
|
+
refit() {
|
|
68
|
+
const space = this.parent;
|
|
69
|
+
const list = this.targets.filter((t) => !t.destroyed && t.parent);
|
|
70
|
+
this.rotation = 0;
|
|
71
|
+
this.skew.set(0, 0);
|
|
72
|
+
this.scale.set(1);
|
|
73
|
+
this.pivot.set(0, 0);
|
|
74
|
+
if (!space || !list.length) {
|
|
75
|
+
this.boundsArea = new Rectangle(0, 0, 0, 0);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
const q = unionQuad(list.map((t) => boundsQuad(t, space)));
|
|
79
|
+
const w = q[2].x - q[0].x;
|
|
80
|
+
const h = q[2].y - q[0].y;
|
|
81
|
+
this.position.set(q[0].x + w / 2, q[0].y + h / 2);
|
|
82
|
+
this.boundsArea = new Rectangle(-w / 2, -h / 2, w, h);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
/** Remembers where the box and every object are: the start of a drag. Does nothing with fewer than two objects. */
|
|
86
|
+
begin() {
|
|
87
|
+
const space = this.parent;
|
|
88
|
+
this._snap = [];
|
|
89
|
+
this._start = null;
|
|
90
|
+
if (!space || this.targets.length < 2) return this;
|
|
91
|
+
const inv = space.getGlobalTransform().invert();
|
|
92
|
+
this.updateLocalTransform();
|
|
93
|
+
this._start = this.localTransform.clone();
|
|
94
|
+
for (const target of this.targets) {
|
|
95
|
+
if (target.destroyed || !target.parent) continue;
|
|
96
|
+
this._snap.push({
|
|
97
|
+
target,
|
|
98
|
+
inSpace: compose(inv, target.getGlobalTransform()),
|
|
99
|
+
parent: compose(inv, target.parent.getGlobalTransform())
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Applies how the box changed since {@link SelectionGroup.begin} to every object, through
|
|
106
|
+
* `setFromMatrix`, then calls `onApply`. No-op before `begin()`.
|
|
107
|
+
*/
|
|
108
|
+
apply() {
|
|
109
|
+
const start = this._start;
|
|
110
|
+
if (!start) return this;
|
|
111
|
+
this.updateLocalTransform();
|
|
112
|
+
const delta = compose(this.localTransform, start.clone().invert());
|
|
113
|
+
const m = new Matrix();
|
|
114
|
+
for (const s of this._snap) {
|
|
115
|
+
if (s.target.destroyed) continue;
|
|
116
|
+
compose(delta, s.inSpace, m);
|
|
117
|
+
s.target.setFromMatrix(compose(s.parent.clone().invert(), m));
|
|
118
|
+
}
|
|
119
|
+
this._opts.onApply?.(this.targets);
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
/** Ends a drag. The box keeps its rotation and size until the next `set()` or `refit()`. */
|
|
123
|
+
end() {
|
|
124
|
+
this._start = null;
|
|
125
|
+
this._snap = [];
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
/** `onStart`, `onChange` and `onEnd` for a {@link TransformGizmo}'s options. Allocates new closures each call. */
|
|
129
|
+
hooks() {
|
|
130
|
+
return {
|
|
131
|
+
onStart: () => {
|
|
132
|
+
this.begin();
|
|
133
|
+
},
|
|
134
|
+
onChange: () => {
|
|
135
|
+
this.apply();
|
|
136
|
+
},
|
|
137
|
+
onEnd: () => {
|
|
138
|
+
this.end();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
//#endregion
|
|
144
|
+
export { SelectionGroup };
|