laymur 0.28.0 → 0.29.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.
Files changed (81) hide show
  1. package/README.md +53 -4
  2. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +14 -3
  3. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.js +38 -5
  4. package/dist/core/constraints/UICustomConstraint.js +1 -1
  5. package/dist/core/constraints/UIHeightConstraint/UIHeightConstraint.js +1 -1
  6. package/dist/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.js +1 -1
  7. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
  8. package/dist/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.js +1 -1
  9. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.d.ts +6 -0
  10. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.js +8 -0
  11. package/dist/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.js +1 -1
  12. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
  13. package/dist/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.js +1 -1
  14. package/dist/core/constraints/UIWidthConstraint/UIWidthConstraint.js +1 -1
  15. package/dist/core/elements/UICanvasElement/UICanvasElement.d.ts +54 -0
  16. package/dist/core/elements/UICanvasElement/UICanvasElement.js +70 -0
  17. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +2 -0
  18. package/dist/core/elements/UIDummy/UIDummy.js +3 -1
  19. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +42 -2
  20. package/dist/core/elements/UIGraphics/UIGraphics.Internal.js +2 -2
  21. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +48 -34
  22. package/dist/core/elements/UIGraphics/UIGraphics.js +196 -93
  23. package/dist/core/elements/UIImage/UIImage.d.ts +11 -1
  24. package/dist/core/elements/UIImage/UIImage.js +14 -0
  25. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +10 -1
  26. package/dist/core/elements/UIScene/UIScene.Internal.js +1 -0
  27. package/dist/core/elements/UIScene/UIScene.d.ts +11 -1
  28. package/dist/core/elements/UIScene/UIScene.js +29 -5
  29. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -2
  30. package/dist/core/elements/UIText/UIText.Internal.d.ts +14 -1
  31. package/dist/core/elements/UIText/UIText.Internal.js +12 -0
  32. package/dist/core/elements/UIText/UIText.Measuring.d.ts +9 -4
  33. package/dist/core/elements/UIText/UIText.Measuring.js +17 -6
  34. package/dist/core/elements/UIText/UIText.Rendering.d.ts +7 -16
  35. package/dist/core/elements/UIText/UIText.Rendering.js +212 -46
  36. package/dist/core/elements/UIText/UIText.d.ts +58 -28
  37. package/dist/core/elements/UIText/UIText.js +113 -138
  38. package/dist/core/index.d.ts +10 -4
  39. package/dist/core/index.js +5 -1
  40. package/dist/core/layers/UILayer/UILayer.d.ts +17 -3
  41. package/dist/core/layers/UILayer/UILayer.js +35 -4
  42. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +2 -0
  43. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +3 -0
  44. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.d.ts +46 -0
  45. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.js +86 -0
  46. package/dist/core/miscellaneous/color/UIColor.d.ts +17 -0
  47. package/dist/core/miscellaneous/color/UIColor.js +22 -0
  48. package/dist/core/miscellaneous/color/canvasColor.d.ts +9 -0
  49. package/dist/core/miscellaneous/color/canvasColor.js +15 -0
  50. package/dist/core/miscellaneous/insets/UIInsets.d.ts +3 -0
  51. package/dist/core/miscellaneous/insets/UIInsets.js +15 -0
  52. package/dist/core/miscellaneous/paint/UIGradient.Internal.d.ts +37 -0
  53. package/dist/core/miscellaneous/paint/UIGradient.Internal.js +14 -0
  54. package/dist/core/miscellaneous/paint/UIGradient.d.ts +18 -0
  55. package/dist/core/miscellaneous/paint/UIGradient.js +35 -0
  56. package/dist/core/miscellaneous/paint/UILinearGradient.d.ts +14 -0
  57. package/dist/core/miscellaneous/paint/UILinearGradient.js +22 -0
  58. package/dist/core/miscellaneous/paint/UIPaint.Internal.d.ts +21 -0
  59. package/dist/core/miscellaneous/paint/UIPaint.Internal.js +1 -0
  60. package/dist/core/miscellaneous/paint/UIPaint.d.ts +17 -0
  61. package/dist/core/miscellaneous/paint/UIPaint.js +21 -0
  62. package/dist/core/miscellaneous/paint/UIRadialGradient.d.ts +18 -0
  63. package/dist/core/miscellaneous/paint/UIRadialGradient.js +33 -0
  64. package/dist/core/miscellaneous/shared.d.ts +14 -0
  65. package/dist/core/miscellaneous/shared.js +11 -0
  66. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +88 -30
  67. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +12 -8
  68. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +41 -66
  69. package/dist/core/miscellaneous/text-style/UITextStyle.js +165 -294
  70. package/dist/core/miscellaneous/texture/UITextureView.Internal.d.ts +2 -1
  71. package/dist/core/miscellaneous/texture/UITextureView.Internal.js +12 -3
  72. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +1 -1
  73. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +1 -1
  74. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +1 -1
  75. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +6 -2
  76. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +41 -9
  77. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +97 -35
  78. package/dist/particles/elements/UIEmitter.Internal.d.ts +3 -0
  79. package/dist/particles/elements/UIEmitter.Internal.js +1 -0
  80. package/dist/particles/elements/UIEmitter.js +3 -2
  81. package/package.json +1 -1
@@ -0,0 +1,46 @@
1
+ import type { CanvasTexture } from "three";
2
+ import type { UICanvas, UICanvasRenderingContext2D } from "../canvas.js";
3
+ /**
4
+ * Calculates the scale content is rasterized at. The required scale is rounded up to a step,
5
+ * so content that resizes smoothly is redrawn only when it crosses a step, and at most about 19%
6
+ * sharper than needed. The scale is then lowered until the longer side fits the raster limit.
7
+ * @param width Content width in content units
8
+ * @param height Content height in content units
9
+ * @param requiredScale Raster pixels per content unit needed for the content to look sharp
10
+ * @returns Raster pixels per content unit
11
+ */
12
+ export declare function calculateRasterScale(width: number, height: number, requiredScale: number): number;
13
+ /**
14
+ * Canvas drawn into a texture. Owns the canvas, its 2D context and the texture. A texture of
15
+ * another size is created anew, since three.js cannot resize an uploaded texture.
16
+ */
17
+ export declare class UICanvasSurface {
18
+ readonly canvas: UICanvas;
19
+ readonly context: UICanvasRenderingContext2D;
20
+ private textureInternal;
21
+ private scaleXInternal;
22
+ private scaleYInternal;
23
+ /**
24
+ * @param subject Name of the owner, used in errors
25
+ * @throws If the browser gives no 2D context
26
+ */
27
+ constructor(subject: string);
28
+ /** Texture showing the canvas */
29
+ get texture(): CanvasTexture;
30
+ /** Canvas pixels per content unit horizontally, as set by the last `begin` */
31
+ get scaleX(): number;
32
+ /** Canvas pixels per content unit vertically, as set by the last `begin` */
33
+ get scaleY(): number;
34
+ /**
35
+ * Sizes the canvas for content rasterized at a scale and clears it. The canvas sides are whole
36
+ * pixels, so the transform the context is left with maps the content exactly onto them, and the
37
+ * content is drawn in content units.
38
+ * @param width Content width in content units
39
+ * @param height Content height in content units
40
+ * @param scale Raster pixels per content unit
41
+ * @returns True when the texture was replaced and the new one has to be passed to the plane
42
+ */
43
+ begin(width: number, height: number, scale: number): boolean;
44
+ /** Frees the texture */
45
+ dispose(): void;
46
+ }
@@ -0,0 +1,86 @@
1
+ import { createCanvas, createCanvasTexture, getCanvas2DContext } from "../canvas.js";
2
+ /** Raster scales are rounded up to powers of two in steps of a quarter */
3
+ const RASTER_SCALE_STEPS_PER_OCTAVE = 4;
4
+ /** Longest side of a raster in pixels, kept within canvas limits of mobile browsers */
5
+ const RASTER_MAX_SIDE = 4096;
6
+ /** Smallest side of a raster in pixels */
7
+ const RASTER_MIN_SIDE = 2;
8
+ /**
9
+ * Calculates the scale content is rasterized at. The required scale is rounded up to a step,
10
+ * so content that resizes smoothly is redrawn only when it crosses a step, and at most about 19%
11
+ * sharper than needed. The scale is then lowered until the longer side fits the raster limit.
12
+ * @param width Content width in content units
13
+ * @param height Content height in content units
14
+ * @param requiredScale Raster pixels per content unit needed for the content to look sharp
15
+ * @returns Raster pixels per content unit
16
+ */
17
+ export function calculateRasterScale(width, height, requiredScale) {
18
+ const steps = Math.ceil(Math.log2(requiredScale) * RASTER_SCALE_STEPS_PER_OCTAVE - 1e-6);
19
+ const scale = Math.pow(2, steps / RASTER_SCALE_STEPS_PER_OCTAVE);
20
+ return Math.min(scale, RASTER_MAX_SIDE / Math.max(width, height, 1));
21
+ }
22
+ /**
23
+ * Canvas drawn into a texture. Owns the canvas, its 2D context and the texture. A texture of
24
+ * another size is created anew, since three.js cannot resize an uploaded texture.
25
+ */
26
+ export class UICanvasSurface {
27
+ /**
28
+ * @param subject Name of the owner, used in errors
29
+ * @throws If the browser gives no 2D context
30
+ */
31
+ constructor(subject) {
32
+ this.scaleXInternal = 1;
33
+ this.scaleYInternal = 1;
34
+ this.canvas = createCanvas(RASTER_MIN_SIDE, RASTER_MIN_SIDE);
35
+ const context = getCanvas2DContext(this.canvas);
36
+ if (!context) {
37
+ throw new Error(`${subject}: failed to create canvas context`);
38
+ }
39
+ this.context = context;
40
+ this.textureInternal = createCanvasTexture(this.canvas);
41
+ this.textureInternal.needsUpdate = true;
42
+ }
43
+ /** Texture showing the canvas */
44
+ get texture() {
45
+ return this.textureInternal;
46
+ }
47
+ /** Canvas pixels per content unit horizontally, as set by the last `begin` */
48
+ get scaleX() {
49
+ return this.scaleXInternal;
50
+ }
51
+ /** Canvas pixels per content unit vertically, as set by the last `begin` */
52
+ get scaleY() {
53
+ return this.scaleYInternal;
54
+ }
55
+ /**
56
+ * Sizes the canvas for content rasterized at a scale and clears it. The canvas sides are whole
57
+ * pixels, so the transform the context is left with maps the content exactly onto them, and the
58
+ * content is drawn in content units.
59
+ * @param width Content width in content units
60
+ * @param height Content height in content units
61
+ * @param scale Raster pixels per content unit
62
+ * @returns True when the texture was replaced and the new one has to be passed to the plane
63
+ */
64
+ begin(width, height, scale) {
65
+ const canvasWidth = Math.max(Math.ceil(width * scale), RASTER_MIN_SIDE);
66
+ const canvasHeight = Math.max(Math.ceil(height * scale), RASTER_MIN_SIDE);
67
+ const resized = this.canvas.width !== canvasWidth || this.canvas.height !== canvasHeight;
68
+ if (resized) {
69
+ this.canvas.width = canvasWidth;
70
+ this.canvas.height = canvasHeight;
71
+ this.textureInternal.dispose();
72
+ this.textureInternal = createCanvasTexture(this.canvas);
73
+ }
74
+ this.context.setTransform(1, 0, 0, 1, 0, 0);
75
+ this.context.clearRect(0, 0, canvasWidth, canvasHeight);
76
+ this.scaleXInternal = canvasWidth / width;
77
+ this.scaleYInternal = canvasHeight / height;
78
+ this.context.setTransform(this.scaleXInternal, 0, 0, this.scaleYInternal, 0, 0);
79
+ this.textureInternal.needsUpdate = true;
80
+ return resized;
81
+ }
82
+ /** Frees the texture */
83
+ dispose() {
84
+ this.textureInternal.dispose();
85
+ }
86
+ }
@@ -189,6 +189,23 @@ export declare class UIColor {
189
189
  set(uiColorConfig?: UIColorConfig): this;
190
190
  /** Sets RGBA components directly */
191
191
  setRGBA(r: number, g: number, b: number, a?: number): this;
192
+ /**
193
+ * Moves toward another color by a fraction, componentwise in RGBA as the components are stored
194
+ * (sRGB, like CSS transitions)
195
+ *
196
+ * @param color - Target color
197
+ * @param t - Fraction of the way, 0 keeps this color and 1 gives the target
198
+ */
199
+ lerp(color: UIColor, t: number): this;
200
+ /**
201
+ * Sets to a color between two others, componentwise in RGBA as the components are stored
202
+ * (sRGB, like CSS transitions)
203
+ *
204
+ * @param from - Color at 0
205
+ * @param to - Color at 1
206
+ * @param t - Fraction of the way from `from` to `to`
207
+ */
208
+ lerpColors(from: UIColor, to: UIColor, t: number): this;
192
209
  /** Returns RGBA as 32-bit hex (RRGGBBAA) */
193
210
  getHexRGBA(): number;
194
211
  /** Returns RGB as 24-bit hex (RRGGBB) */
@@ -338,6 +338,28 @@ export class UIColor {
338
338
  }
339
339
  return this;
340
340
  }
341
+ /**
342
+ * Moves toward another color by a fraction, componentwise in RGBA as the components are stored
343
+ * (sRGB, like CSS transitions)
344
+ *
345
+ * @param color - Target color
346
+ * @param t - Fraction of the way, 0 keeps this color and 1 gives the target
347
+ */
348
+ lerp(color, t) {
349
+ return this.lerpColors(this, color, t);
350
+ }
351
+ /**
352
+ * Sets to a color between two others, componentwise in RGBA as the components are stored
353
+ * (sRGB, like CSS transitions)
354
+ *
355
+ * @param from - Color at 0
356
+ * @param to - Color at 1
357
+ * @param t - Fraction of the way from `from` to `to`
358
+ */
359
+ lerpColors(from, to, t) {
360
+ assertValidNumber(t, "UIColor.lerpColors.t");
361
+ return this.setRGBA(MathUtils.lerp(from.r, to.r, t), MathUtils.lerp(from.g, to.g, t), MathUtils.lerp(from.b, to.b, t), MathUtils.lerp(from.a, to.a, t));
362
+ }
341
363
  /** Returns RGBA as 32-bit hex (RRGGBBAA) */
342
364
  getHexRGBA() {
343
365
  this.ensureRGBUpdatedFromHSL();
@@ -0,0 +1,9 @@
1
+ import type { UIColorConfig } from "./UIColor.Internal.js";
2
+ /**
3
+ * Converts a color to a canvas color. A `UIColor` name means the color `UIColor` gives it, any
4
+ * other string is taken as a CSS color and passed as it is, and any other value is converted
5
+ * through `UIColor`.
6
+ * @param color Color to convert
7
+ * @returns CSS color string
8
+ */
9
+ export declare function toCanvasColor(color: UIColorConfig): string;
@@ -0,0 +1,15 @@
1
+ import { COLORS } from "./UIColor.Internal.js";
2
+ import { UIColor } from "./UIColor.js";
3
+ const TEMP_COLOR = new UIColor();
4
+ /**
5
+ * Converts a color to a canvas color. A `UIColor` name means the color `UIColor` gives it, any
6
+ * other string is taken as a CSS color and passed as it is, and any other value is converted
7
+ * through `UIColor`.
8
+ * @param color Color to convert
9
+ * @returns CSS color string
10
+ */
11
+ export function toCanvasColor(color) {
12
+ return typeof color === "string" && COLORS[color] === undefined
13
+ ? color
14
+ : TEMP_COLOR.set(color).toCSSColor();
15
+ }
@@ -7,6 +7,7 @@ export declare class UIInsets {
7
7
  private tInternal;
8
8
  private bInternal;
9
9
  private dirtyInternal;
10
+ private versionInternal;
10
11
  /**
11
12
  * Creates UIInsets instance.
12
13
  *
@@ -21,6 +22,8 @@ export declare class UIInsets {
21
22
  get top(): number;
22
23
  /** Bottom inset */
23
24
  get bottom(): number;
25
+ /** Bumped by every change of a value, to compare with a value seen before */
26
+ get version(): number;
24
27
  /** Left inset */
25
28
  set left(value: number);
26
29
  /** Right inset */
@@ -14,6 +14,7 @@ export class UIInsets {
14
14
  this.tInternal = INSETS_DEFAULT_VALUE;
15
15
  this.bInternal = INSETS_DEFAULT_VALUE;
16
16
  this.dirtyInternal = false;
17
+ this.versionInternal = 0;
17
18
  this.set(config);
18
19
  }
19
20
  /** Left inset */
@@ -32,6 +33,10 @@ export class UIInsets {
32
33
  get bottom() {
33
34
  return this.bInternal;
34
35
  }
36
+ /** Bumped by every change of a value, to compare with a value seen before */
37
+ get version() {
38
+ return this.versionInternal;
39
+ }
35
40
  /** @internal */
36
41
  get dirty() {
37
42
  return this.dirtyInternal;
@@ -42,6 +47,7 @@ export class UIInsets {
42
47
  if (this.lInternal !== value) {
43
48
  this.lInternal = value;
44
49
  this.dirtyInternal = true;
50
+ this.versionInternal++;
45
51
  }
46
52
  }
47
53
  /** Right inset */
@@ -50,6 +56,7 @@ export class UIInsets {
50
56
  if (this.rInternal !== value) {
51
57
  this.rInternal = value;
52
58
  this.dirtyInternal = true;
59
+ this.versionInternal++;
53
60
  }
54
61
  }
55
62
  /** Top inset */
@@ -58,6 +65,7 @@ export class UIInsets {
58
65
  if (this.tInternal !== value) {
59
66
  this.tInternal = value;
60
67
  this.dirtyInternal = true;
68
+ this.versionInternal++;
61
69
  }
62
70
  }
63
71
  /** Bottom inset */
@@ -66,6 +74,7 @@ export class UIInsets {
66
74
  if (this.bInternal !== value) {
67
75
  this.bInternal = value;
68
76
  this.dirtyInternal = true;
77
+ this.versionInternal++;
69
78
  }
70
79
  }
71
80
  /** Sets left and right to same value */
@@ -75,6 +84,7 @@ export class UIInsets {
75
84
  this.lInternal = value;
76
85
  this.rInternal = value;
77
86
  this.dirtyInternal = true;
87
+ this.versionInternal++;
78
88
  }
79
89
  }
80
90
  /** Sets top and bottom to same value */
@@ -84,6 +94,7 @@ export class UIInsets {
84
94
  this.tInternal = value;
85
95
  this.bInternal = value;
86
96
  this.dirtyInternal = true;
97
+ this.versionInternal++;
87
98
  }
88
99
  }
89
100
  /** Sets all sides to same value */
@@ -98,6 +109,7 @@ export class UIInsets {
98
109
  this.tInternal = value;
99
110
  this.bInternal = value;
100
111
  this.dirtyInternal = true;
112
+ this.versionInternal++;
101
113
  }
102
114
  }
103
115
  /** Sets insets */
@@ -174,6 +186,7 @@ export class UIInsets {
174
186
  this.tInternal !== tOld ||
175
187
  this.bInternal !== bOld) {
176
188
  this.dirtyInternal = true;
189
+ this.versionInternal++;
177
190
  }
178
191
  }
179
192
  /** Sets from Vector4 (x=left, y=right, z=top, w=bottom) */
@@ -191,6 +204,7 @@ export class UIInsets {
191
204
  this.tInternal = vector.z;
192
205
  this.bInternal = vector.w;
193
206
  this.dirtyInternal = true;
207
+ this.versionInternal++;
194
208
  }
195
209
  }
196
210
  /** Converts to Vector4 (left, right, top, bottom) */
@@ -212,6 +226,7 @@ export class UIInsets {
212
226
  this.tInternal = other.tInternal;
213
227
  this.bInternal = other.bInternal;
214
228
  this.dirtyInternal = true;
229
+ this.versionInternal++;
215
230
  }
216
231
  }
217
232
  /** @internal */
@@ -0,0 +1,37 @@
1
+ import type { UIColorConfig } from "../color/UIColor.Internal.js";
2
+ /** Color stop of a gradient */
3
+ export interface UIGradientStop {
4
+ /** Position along the gradient, from 0 to 1 */
5
+ offset: number;
6
+ /** Color at the position. A string is a `UIColor` name or a CSS color. */
7
+ color: UIColorConfig;
8
+ }
9
+ /** Geometry of a linear gradient, in fractions of the filled box from its top left corner */
10
+ export interface UILinearGradientOptions {
11
+ /** Horizontal position of the start */
12
+ startX: number;
13
+ /** Vertical position of the start */
14
+ startY: number;
15
+ /** Horizontal position of the end */
16
+ endX: number;
17
+ /** Vertical position of the end */
18
+ endY: number;
19
+ }
20
+ /**
21
+ * Geometry of a radial gradient. The center is in fractions of the filled box from its top left
22
+ * corner, the radii in fractions of its longer side.
23
+ */
24
+ export interface UIRadialGradientOptions {
25
+ /** Horizontal position of the center */
26
+ centerX: number;
27
+ /** Vertical position of the center */
28
+ centerY: number;
29
+ /** Radius where offset 0 is */
30
+ innerRadius: number;
31
+ /** Radius where offset 1 is */
32
+ outerRadius: number;
33
+ }
34
+ /** From the top edge to the bottom one */
35
+ export declare const LINEAR_GRADIENT_DEFAULT_OPTIONS: UILinearGradientOptions;
36
+ /** From the center to the ends of the longer axis, the middles of the shorter sides */
37
+ export declare const RADIAL_GRADIENT_DEFAULT_OPTIONS: UIRadialGradientOptions;
@@ -0,0 +1,14 @@
1
+ /** From the top edge to the bottom one */
2
+ export const LINEAR_GRADIENT_DEFAULT_OPTIONS = {
3
+ startX: 0.5,
4
+ startY: 0,
5
+ endX: 0.5,
6
+ endY: 1,
7
+ };
8
+ /** From the center to the ends of the longer axis, the middles of the shorter sides */
9
+ export const RADIAL_GRADIENT_DEFAULT_OPTIONS = {
10
+ centerX: 0.5,
11
+ centerY: 0.5,
12
+ innerRadius: 0,
13
+ outerRadius: 0.5,
14
+ };
@@ -0,0 +1,18 @@
1
+ import type { UICanvasRenderingContext2D } from "../canvas.js";
2
+ import type { UIGradientStop } from "./UIGradient.Internal.js";
3
+ import type { UIPaintBox } from "./UIPaint.Internal.js";
4
+ /**
5
+ * Base class for gradients that fill text, its strokes and the shapes of `UIGraphics`. A gradient
6
+ * is laid over the box of what it fills, so its geometry is given in fractions of that box, from
7
+ * the top left corner, y pointing down. A gradient does not change after it is created.
8
+ */
9
+ export declare abstract class UIGradient {
10
+ private readonly stops;
11
+ /**
12
+ * @param stops Color stops, at least one. Their order does not matter.
13
+ * @throws If there are no stops or an offset is outside 0 to 1
14
+ */
15
+ constructor(stops: readonly UIGradientStop[]);
16
+ /** Creates the canvas gradient of this geometry over the box, without stops */
17
+ protected abstract createCanvasGeometry(context: UICanvasRenderingContext2D, box: UIPaintBox): CanvasGradient;
18
+ }
@@ -0,0 +1,35 @@
1
+ import { assertValidUnitNumber } from "../asserts.js";
2
+ import { toCanvasColor } from "../color/canvasColor.js";
3
+ /**
4
+ * Base class for gradients that fill text, its strokes and the shapes of `UIGraphics`. A gradient
5
+ * is laid over the box of what it fills, so its geometry is given in fractions of that box, from
6
+ * the top left corner, y pointing down. A gradient does not change after it is created.
7
+ */
8
+ export class UIGradient {
9
+ /**
10
+ * @param stops Color stops, at least one. Their order does not matter.
11
+ * @throws If there are no stops or an offset is outside 0 to 1
12
+ */
13
+ constructor(stops) {
14
+ if (stops.length === 0) {
15
+ throw new Error("UIGradient.constructor.stops: at least one stop is required");
16
+ }
17
+ for (const stop of stops) {
18
+ assertValidUnitNumber(stop.offset, "UIGradient.constructor.stops.offset");
19
+ }
20
+ this.stops = stops
21
+ .map((stop) => ({ offset: stop.offset, color: toCanvasColor(stop.color) }))
22
+ .sort((a, b) => a.offset - b.offset);
23
+ }
24
+ /**
25
+ * Builds the canvas gradient over a box given in the current canvas coordinates.
26
+ * @internal
27
+ */
28
+ createCanvasGradient(context, box) {
29
+ const gradient = this.createCanvasGeometry(context, box);
30
+ for (const stop of this.stops) {
31
+ gradient.addColorStop(stop.offset, stop.color);
32
+ }
33
+ return gradient;
34
+ }
35
+ }
@@ -0,0 +1,14 @@
1
+ import type { UICanvasRenderingContext2D } from "../canvas.js";
2
+ import type { UIGradientStop, UILinearGradientOptions } from "./UIGradient.Internal.js";
3
+ import { UIGradient } from "./UIGradient.js";
4
+ import type { UIPaintBox } from "./UIPaint.Internal.js";
5
+ /** Gradient along a line from a start point to an end point, top to bottom by default */
6
+ export declare class UILinearGradient extends UIGradient {
7
+ private readonly options;
8
+ /**
9
+ * @param stops Color stops, at least one
10
+ * @param options Start and end in fractions of the filled box
11
+ */
12
+ constructor(stops: readonly UIGradientStop[], options?: Partial<UILinearGradientOptions>);
13
+ protected createCanvasGeometry(context: UICanvasRenderingContext2D, box: UIPaintBox): CanvasGradient;
14
+ }
@@ -0,0 +1,22 @@
1
+ import { assertValidNumber } from "../asserts.js";
2
+ import { LINEAR_GRADIENT_DEFAULT_OPTIONS } from "./UIGradient.Internal.js";
3
+ import { UIGradient } from "./UIGradient.js";
4
+ /** Gradient along a line from a start point to an end point, top to bottom by default */
5
+ export class UILinearGradient extends UIGradient {
6
+ /**
7
+ * @param stops Color stops, at least one
8
+ * @param options Start and end in fractions of the filled box
9
+ */
10
+ constructor(stops, options = {}) {
11
+ super(stops);
12
+ this.options = { ...LINEAR_GRADIENT_DEFAULT_OPTIONS, ...options };
13
+ assertValidNumber(this.options.startX, "UILinearGradient.constructor.options.startX");
14
+ assertValidNumber(this.options.startY, "UILinearGradient.constructor.options.startY");
15
+ assertValidNumber(this.options.endX, "UILinearGradient.constructor.options.endX");
16
+ assertValidNumber(this.options.endY, "UILinearGradient.constructor.options.endY");
17
+ }
18
+ createCanvasGeometry(context, box) {
19
+ const { startX, startY, endX, endY } = this.options;
20
+ return context.createLinearGradient(box.x + startX * box.width, box.y + startY * box.height, box.x + endX * box.width, box.y + endY * box.height);
21
+ }
22
+ }
@@ -0,0 +1,21 @@
1
+ import type { UIColorConfig } from "../color/UIColor.Internal.js";
2
+ import type { UIGradient } from "./UIGradient.js";
3
+ /** Fill of text, a stroke or a shape, either a color or a gradient. A string is a `UIColor` name or a CSS color. */
4
+ export type UIPaintConfig = UIColorConfig | UIGradient;
5
+ /** Paint as it is stored, a CSS color string or a gradient */
6
+ export type UIPaint = string | UIGradient;
7
+ /** How the corners of a stroke are joined */
8
+ export type UILineJoin = "round" | "bevel" | "miter";
9
+ /** How the open ends of a stroke look */
10
+ export type UILineCap = "butt" | "round" | "square";
11
+ /** Box a gradient is laid over, in canvas coordinates */
12
+ export interface UIPaintBox {
13
+ /** Left edge */
14
+ x: number;
15
+ /** Top edge */
16
+ y: number;
17
+ /** Width */
18
+ width: number;
19
+ /** Height */
20
+ height: number;
21
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import type { UICanvasRenderingContext2D } from "../canvas.js";
2
+ import type { UIPaint, UIPaintBox, UIPaintConfig } from "./UIPaint.Internal.js";
3
+ /**
4
+ * Converts a paint config to the stored paint. Colors become CSS strings, so a `UIColor` changed
5
+ * later does not change the paint.
6
+ * @param paint Paint to convert
7
+ * @returns CSS color string or the gradient itself
8
+ */
9
+ export declare function resolvePaint(paint: UIPaintConfig): UIPaint;
10
+ /**
11
+ * Builds the canvas fill or stroke style of a paint.
12
+ * @param context Canvas context the style is used on
13
+ * @param paint Stored paint
14
+ * @param box Box a gradient is laid over, in the current canvas coordinates
15
+ * @returns Value for `fillStyle` or `strokeStyle`
16
+ */
17
+ export declare function toCanvasPaint(context: UICanvasRenderingContext2D, paint: UIPaint, box: UIPaintBox): string | CanvasGradient;
@@ -0,0 +1,21 @@
1
+ import { toCanvasColor } from "../color/canvasColor.js";
2
+ import { UIGradient } from "./UIGradient.js";
3
+ /**
4
+ * Converts a paint config to the stored paint. Colors become CSS strings, so a `UIColor` changed
5
+ * later does not change the paint.
6
+ * @param paint Paint to convert
7
+ * @returns CSS color string or the gradient itself
8
+ */
9
+ export function resolvePaint(paint) {
10
+ return paint instanceof UIGradient ? paint : toCanvasColor(paint);
11
+ }
12
+ /**
13
+ * Builds the canvas fill or stroke style of a paint.
14
+ * @param context Canvas context the style is used on
15
+ * @param paint Stored paint
16
+ * @param box Box a gradient is laid over, in the current canvas coordinates
17
+ * @returns Value for `fillStyle` or `strokeStyle`
18
+ */
19
+ export function toCanvasPaint(context, paint, box) {
20
+ return typeof paint === "string" ? paint : paint.createCanvasGradient(context, box);
21
+ }
@@ -0,0 +1,18 @@
1
+ import type { UICanvasRenderingContext2D } from "../canvas.js";
2
+ import type { UIGradientStop, UIRadialGradientOptions } from "./UIGradient.Internal.js";
3
+ import { UIGradient } from "./UIGradient.js";
4
+ import type { UIPaintBox } from "./UIPaint.Internal.js";
5
+ /**
6
+ * Circular gradient between two radii around a center, by default from the center to the ends of
7
+ * the longer axis of the box. The circle stays round in a box that is not square.
8
+ */
9
+ export declare class UIRadialGradient extends UIGradient {
10
+ private readonly options;
11
+ /**
12
+ * @param stops Color stops, at least one
13
+ * @param options Center in fractions of the filled box, radii in fractions of its longer side
14
+ * @throws If the outer radius is not larger than the inner one
15
+ */
16
+ constructor(stops: readonly UIGradientStop[], options?: Partial<UIRadialGradientOptions>);
17
+ protected createCanvasGeometry(context: UICanvasRenderingContext2D, box: UIPaintBox): CanvasGradient;
18
+ }
@@ -0,0 +1,33 @@
1
+ import { assertValidNonNegativeNumber, assertValidNumber } from "../asserts.js";
2
+ import { RADIAL_GRADIENT_DEFAULT_OPTIONS } from "./UIGradient.Internal.js";
3
+ import { UIGradient } from "./UIGradient.js";
4
+ /**
5
+ * Circular gradient between two radii around a center, by default from the center to the ends of
6
+ * the longer axis of the box. The circle stays round in a box that is not square.
7
+ */
8
+ export class UIRadialGradient extends UIGradient {
9
+ /**
10
+ * @param stops Color stops, at least one
11
+ * @param options Center in fractions of the filled box, radii in fractions of its longer side
12
+ * @throws If the outer radius is not larger than the inner one
13
+ */
14
+ constructor(stops, options = {}) {
15
+ super(stops);
16
+ this.options = { ...RADIAL_GRADIENT_DEFAULT_OPTIONS, ...options };
17
+ const { centerX, centerY, innerRadius, outerRadius } = this.options;
18
+ assertValidNumber(centerX, "UIRadialGradient.constructor.options.centerX");
19
+ assertValidNumber(centerY, "UIRadialGradient.constructor.options.centerY");
20
+ assertValidNonNegativeNumber(innerRadius, "UIRadialGradient.constructor.options.innerRadius");
21
+ assertValidNonNegativeNumber(outerRadius, "UIRadialGradient.constructor.options.outerRadius");
22
+ if (outerRadius <= innerRadius) {
23
+ throw new Error("UIRadialGradient.constructor.options.outerRadius: must be larger than innerRadius");
24
+ }
25
+ }
26
+ createCanvasGeometry(context, box) {
27
+ const { centerX, centerY, innerRadius, outerRadius } = this.options;
28
+ const x = box.x + centerX * box.width;
29
+ const y = box.y + centerY * box.height;
30
+ const side = Math.max(box.width, box.height);
31
+ return context.createRadialGradient(x, y, innerRadius * side, x, y, outerRadius * side);
32
+ }
33
+ }
@@ -1,3 +1,4 @@
1
+ import type { FerrsignView1 } from "ferrsign";
1
2
  import { UILayer } from "../layers/UILayer/UILayer.js";
2
3
  /** Objects that belong to a UI layer */
3
4
  export interface UILayerElement {
@@ -34,6 +35,19 @@ export interface UIPlaneElement extends UIPointElement {
34
35
  /** Center Y coordinate in world units */
35
36
  readonly centerY: number;
36
37
  }
38
+ /** UI elements whose content has its own aspect ratio, such as a picture or laid out text */
39
+ export interface UIContentAspectElement {
40
+ /** Width of the content divided by its height */
41
+ readonly contentAspect: number;
42
+ /** Signal fired with the new aspect when the content changes it */
43
+ readonly signalContentAspectChanged: FerrsignView1<number>;
44
+ }
45
+ /**
46
+ * Checks if object implements UIContentAspectElement.
47
+ * @param obj Object to check
48
+ * @returns True if object reports the aspect of its content
49
+ */
50
+ export declare function isUIContentAspectElement(obj: unknown): obj is UIContentAspectElement;
37
51
  /**
38
52
  * Checks if object implements UILayerElement.
39
53
  * @param obj Object to check
@@ -1,4 +1,15 @@
1
1
  import { UILayer } from "../layers/UILayer/UILayer.js";
2
+ /**
3
+ * Checks if object implements UIContentAspectElement.
4
+ * @param obj Object to check
5
+ * @returns True if object reports the aspect of its content
6
+ */
7
+ export function isUIContentAspectElement(obj) {
8
+ return (typeof obj === "object" &&
9
+ obj !== null &&
10
+ "contentAspect" in obj &&
11
+ "signalContentAspectChanged" in obj);
12
+ }
2
13
  /**
3
14
  * Checks if object implements UILayerElement.
4
15
  * @param obj Object to check