@luma.gl/core 9.3.0-alpha.2 → 9.3.0-alpha.6

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 (121) hide show
  1. package/dist/adapter/canvas-context.d.ts +6 -181
  2. package/dist/adapter/canvas-context.d.ts.map +1 -1
  3. package/dist/adapter/canvas-context.js +5 -450
  4. package/dist/adapter/canvas-context.js.map +1 -1
  5. package/dist/adapter/canvas-observer.d.ts +32 -0
  6. package/dist/adapter/canvas-observer.d.ts.map +1 -0
  7. package/dist/adapter/canvas-observer.js +90 -0
  8. package/dist/adapter/canvas-observer.js.map +1 -0
  9. package/dist/adapter/canvas-surface.d.ts +150 -0
  10. package/dist/adapter/canvas-surface.d.ts.map +1 -0
  11. package/dist/adapter/canvas-surface.js +392 -0
  12. package/dist/adapter/canvas-surface.js.map +1 -0
  13. package/dist/adapter/device.d.ts +64 -9
  14. package/dist/adapter/device.d.ts.map +1 -1
  15. package/dist/adapter/device.js +108 -4
  16. package/dist/adapter/device.js.map +1 -1
  17. package/dist/adapter/luma.js +1 -1
  18. package/dist/adapter/presentation-context.d.ts +11 -0
  19. package/dist/adapter/presentation-context.d.ts.map +1 -0
  20. package/dist/adapter/presentation-context.js +12 -0
  21. package/dist/adapter/presentation-context.js.map +1 -0
  22. package/dist/adapter/resources/buffer.d.ts +1 -1
  23. package/dist/adapter/resources/buffer.d.ts.map +1 -1
  24. package/dist/adapter/resources/buffer.js +14 -6
  25. package/dist/adapter/resources/buffer.js.map +1 -1
  26. package/dist/adapter/resources/command-encoder.d.ts +27 -6
  27. package/dist/adapter/resources/command-encoder.d.ts.map +1 -1
  28. package/dist/adapter/resources/command-encoder.js +65 -1
  29. package/dist/adapter/resources/command-encoder.js.map +1 -1
  30. package/dist/adapter/resources/fence.d.ts +1 -1
  31. package/dist/adapter/resources/fence.d.ts.map +1 -1
  32. package/dist/adapter/resources/fence.js +3 -1
  33. package/dist/adapter/resources/fence.js.map +1 -1
  34. package/dist/adapter/resources/framebuffer.d.ts.map +1 -1
  35. package/dist/adapter/resources/framebuffer.js +9 -11
  36. package/dist/adapter/resources/framebuffer.js.map +1 -1
  37. package/dist/adapter/resources/query-set.d.ts +17 -1
  38. package/dist/adapter/resources/query-set.d.ts.map +1 -1
  39. package/dist/adapter/resources/query-set.js.map +1 -1
  40. package/dist/adapter/resources/render-pipeline.d.ts +19 -7
  41. package/dist/adapter/resources/render-pipeline.d.ts.map +1 -1
  42. package/dist/adapter/resources/render-pipeline.js +20 -2
  43. package/dist/adapter/resources/render-pipeline.js.map +1 -1
  44. package/dist/adapter/resources/resource.d.ts +8 -0
  45. package/dist/adapter/resources/resource.d.ts.map +1 -1
  46. package/dist/adapter/resources/resource.js +240 -14
  47. package/dist/adapter/resources/resource.js.map +1 -1
  48. package/dist/adapter/resources/shader.js +27 -25
  49. package/dist/adapter/resources/shader.js.map +1 -1
  50. package/dist/adapter/resources/shared-render-pipeline.d.ts +22 -0
  51. package/dist/adapter/resources/shared-render-pipeline.d.ts.map +1 -0
  52. package/dist/adapter/resources/shared-render-pipeline.js +25 -0
  53. package/dist/adapter/resources/shared-render-pipeline.js.map +1 -0
  54. package/dist/adapter/resources/texture.d.ts +78 -12
  55. package/dist/adapter/resources/texture.d.ts.map +1 -1
  56. package/dist/adapter/resources/texture.js +182 -30
  57. package/dist/adapter/resources/texture.js.map +1 -1
  58. package/dist/adapter-utils/format-compiler-log.d.ts.map +1 -1
  59. package/dist/adapter-utils/format-compiler-log.js +23 -15
  60. package/dist/adapter-utils/format-compiler-log.js.map +1 -1
  61. package/dist/dist.dev.js +1265 -362
  62. package/dist/dist.min.js +10 -9
  63. package/dist/index.cjs +1027 -243
  64. package/dist/index.cjs.map +4 -4
  65. package/dist/index.d.ts +5 -1
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +3 -0
  68. package/dist/index.js.map +1 -1
  69. package/dist/shadertypes/data-types/decode-shader-types.d.ts +2 -2
  70. package/dist/shadertypes/data-types/decode-shader-types.d.ts.map +1 -1
  71. package/dist/shadertypes/data-types/decode-shader-types.js +11 -2
  72. package/dist/shadertypes/data-types/decode-shader-types.js.map +1 -1
  73. package/dist/shadertypes/textures/pixel-utils.js +4 -4
  74. package/dist/shadertypes/textures/pixel-utils.js.map +1 -1
  75. package/dist/shadertypes/textures/texture-format-decoder.d.ts.map +1 -1
  76. package/dist/shadertypes/textures/texture-format-decoder.js +53 -8
  77. package/dist/shadertypes/textures/texture-format-decoder.js.map +1 -1
  78. package/dist/shadertypes/textures/texture-format-table.d.ts.map +1 -1
  79. package/dist/shadertypes/textures/texture-format-table.js +10 -9
  80. package/dist/shadertypes/textures/texture-format-table.js.map +1 -1
  81. package/dist/shadertypes/textures/texture-formats.d.ts +5 -2
  82. package/dist/shadertypes/textures/texture-formats.d.ts.map +1 -1
  83. package/dist/shadertypes/textures/texture-formats.js.map +1 -1
  84. package/dist/shadertypes/textures/texture-layout.d.ts +1 -1
  85. package/dist/utils/array-equal.d.ts +1 -1
  86. package/dist/utils/array-equal.d.ts.map +1 -1
  87. package/dist/utils/array-equal.js +15 -9
  88. package/dist/utils/array-equal.js.map +1 -1
  89. package/dist/utils/assert.d.ts +5 -0
  90. package/dist/utils/assert.d.ts.map +1 -0
  91. package/dist/utils/assert.js +17 -0
  92. package/dist/utils/assert.js.map +1 -0
  93. package/dist/utils/stats-manager.d.ts.map +1 -1
  94. package/dist/utils/stats-manager.js +61 -1
  95. package/dist/utils/stats-manager.js.map +1 -1
  96. package/package.json +6 -6
  97. package/src/adapter/canvas-context.ts +7 -590
  98. package/src/adapter/canvas-observer.ts +130 -0
  99. package/src/adapter/canvas-surface.ts +521 -0
  100. package/src/adapter/device.ts +174 -13
  101. package/src/adapter/presentation-context.ts +16 -0
  102. package/src/adapter/resources/buffer.ts +13 -5
  103. package/src/adapter/resources/command-encoder.ts +96 -7
  104. package/src/adapter/resources/fence.ts +3 -1
  105. package/src/adapter/resources/framebuffer.ts +9 -11
  106. package/src/adapter/resources/query-set.ts +17 -1
  107. package/src/adapter/resources/render-pipeline.ts +42 -13
  108. package/src/adapter/resources/resource.ts +284 -14
  109. package/src/adapter/resources/shader.ts +28 -28
  110. package/src/adapter/resources/shared-render-pipeline.ts +40 -0
  111. package/src/adapter/resources/texture.ts +269 -40
  112. package/src/adapter-utils/format-compiler-log.ts +23 -15
  113. package/src/index.ts +8 -0
  114. package/src/shadertypes/data-types/decode-shader-types.ts +13 -4
  115. package/src/shadertypes/textures/pixel-utils.ts +4 -4
  116. package/src/shadertypes/textures/texture-format-decoder.ts +73 -8
  117. package/src/shadertypes/textures/texture-format-table.ts +10 -9
  118. package/src/shadertypes/textures/texture-formats.ts +6 -1
  119. package/src/utils/array-equal.ts +21 -9
  120. package/src/utils/assert.ts +18 -0
  121. package/src/utils/stats-manager.ts +76 -2
@@ -1,186 +1,11 @@
1
- import type { Device } from "./device.js";
2
- import type { Framebuffer } from "./resources/framebuffer.js";
3
- import type { TextureFormatDepthStencil } from "../shadertypes/textures/texture-formats.js";
4
- /** Properties for a CanvasContext */
5
- export type CanvasContextProps = {
6
- /** Identifier, for debugging */
7
- id?: string;
8
- /** If a canvas not supplied, one will be created and added to the DOM. If a string, a canvas with that id will be looked up in the DOM */
9
- canvas?: HTMLCanvasElement | OffscreenCanvas | string | null;
10
- /** If new canvas is created, it will be created in the specified container, otherwise is appended as a child of document.body */
11
- container?: HTMLElement | string | null;
12
- /** Width in pixels of the canvas - used when creating a new canvas */
13
- width?: number;
14
- /** Height in pixels of the canvas - used when creating a new canvas */
15
- height?: number;
16
- /** Visibility (only used if new canvas is created). */
17
- visible?: boolean;
18
- /** Whether to size the drawing buffer to the pixel size during auto resize. If a number is provided it is used as a static pixel ratio */
19
- useDevicePixels?: boolean | number;
20
- /** Whether to track window resizes. */
21
- autoResize?: boolean;
22
- /** @see https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext/configure#alphamode */
23
- alphaMode?: 'opaque' | 'premultiplied';
24
- /** @see https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext/configure#colorspace */
25
- colorSpace?: 'srgb';
26
- /** Whether to track position changes. Calls this.device.onPositionChange */
27
- trackPosition?: boolean;
28
- };
29
- export type MutableCanvasContextProps = {
30
- /** Whether to size the drawing buffer to the pixel size during auto resize. If a number is provided it is used as a static pixel ratio */
31
- useDevicePixels?: boolean | number;
32
- };
1
+ export type { CanvasContextProps, MutableCanvasContextProps } from "./canvas-surface.js";
2
+ import { CanvasSurface } from "./canvas-surface.js";
33
3
  /**
34
- * Manages a canvas. Supports both HTML or offscreen canvas
35
- * - Creates a new canvas or looks up a canvas from the DOM
36
- * - Provides check for DOM loaded
37
- * @todo commit() @see https://github.com/w3ctag/design-reviews/issues/288
38
- * @todo transferControlToOffscreen: @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/transferControlToOffscreen
4
+ * Manages a renderable backend canvas. Supports both HTML or offscreen canvas
5
+ * and returns backend framebuffers sourced from the canvas itself.
39
6
  */
40
- export declare abstract class CanvasContext {
41
- static isHTMLCanvas(canvas: unknown): canvas is HTMLCanvasElement;
42
- static isOffscreenCanvas(canvas: unknown): canvas is OffscreenCanvas;
43
- static defaultProps: Required<CanvasContextProps>;
44
- abstract readonly device: Device;
7
+ export declare abstract class CanvasContext extends CanvasSurface {
8
+ static defaultProps: Required<import("./canvas-surface").CanvasContextProps>;
45
9
  abstract readonly handle: unknown;
46
- readonly id: string;
47
- readonly props: Required<CanvasContextProps>;
48
- readonly canvas: HTMLCanvasElement | OffscreenCanvas;
49
- /** Handle to HTML canvas */
50
- readonly htmlCanvas?: HTMLCanvasElement;
51
- /** Handle to wrapped OffScreenCanvas */
52
- readonly offscreenCanvas?: OffscreenCanvas;
53
- readonly type: 'html-canvas' | 'offscreen-canvas' | 'node';
54
- /** Promise that resolved once the resize observer has updated the pixel size */
55
- initialized: Promise<void>;
56
- isInitialized: boolean;
57
- /** Visibility is automatically updated (via an IntersectionObserver) */
58
- isVisible: boolean;
59
- /** Width of canvas in CSS units (tracked by a ResizeObserver) */
60
- cssWidth: number;
61
- /** Height of canvas in CSS units (tracked by a ResizeObserver) */
62
- cssHeight: number;
63
- /** Device pixel ratio. Automatically updated via media queries */
64
- devicePixelRatio: number;
65
- /** Exact width of canvas in physical pixels (tracked by a ResizeObserver) */
66
- devicePixelWidth: number;
67
- /** Exact height of canvas in physical pixels (tracked by a ResizeObserver) */
68
- devicePixelHeight: number;
69
- /** Width of drawing buffer: automatically tracks this.pixelWidth if props.autoResize is true */
70
- drawingBufferWidth: number;
71
- /** Height of drawing buffer: automatically tracks this.pixelHeight if props.autoResize is true */
72
- drawingBufferHeight: number;
73
- /** Resolves when the canvas is initialized, i.e. when the ResizeObserver has updated the pixel size */
74
- protected _initializedResolvers: {
75
- promise: Promise<void>;
76
- resolve: (t: void) => void;
77
- reject: (error: Error) => void;
78
- };
79
- /** ResizeObserver to track canvas size changes */
80
- protected readonly _resizeObserver: ResizeObserver | undefined;
81
- /** IntersectionObserver to track canvas visibility changes */
82
- protected readonly _intersectionObserver: IntersectionObserver | undefined;
83
- /** Position of the canvas in the document, updated by a timer */
84
- protected _position: [number, number];
85
- /** Whether this canvas context has been destroyed */
86
- protected destroyed: boolean;
87
- /** Whether the drawing buffer size needs to be resized (deferred resizing to avoid flicker) */
88
- protected _needsDrawingBufferResize: boolean;
89
- abstract get [Symbol.toStringTag](): string;
90
- toString(): string;
91
- constructor(props?: CanvasContextProps);
92
- destroy(): void;
93
- setProps(props: MutableCanvasContextProps): this;
94
- /** Returns a framebuffer with properly resized current 'swap chain' textures */
95
- getCurrentFramebuffer(options?: {
96
- depthStencilFormat?: TextureFormatDepthStencil | false;
97
- }): Framebuffer;
98
- /**
99
- * Returns the size covered by the canvas in CSS pixels
100
- * @note This can be different from the actual device pixel size of a canvas due to DPR scaling, and rounding to integer pixels
101
- * @note This is independent of the canvas' internal drawing buffer size (.width, .height).
102
- */
103
- getCSSSize(): [number, number];
104
- getPosition(): [number, number];
105
- /**
106
- * Returns the size covered by the canvas in actual device pixels.
107
- * @note This can be different from the 'CSS' size of a canvas due to DPR scaling, and rounding to integer pixels
108
- * @note This is independent of the canvas' internal drawing buffer size (.width, .height).
109
- */
110
- getDevicePixelSize(): [number, number];
111
- /** Get the drawing buffer size (number of pixels GPU is rendering into, can be different from CSS size) */
112
- getDrawingBufferSize(): [number, number];
113
- /** Returns the biggest allowed framebuffer size. @todo Allow the application to limit this? */
114
- getMaxDrawingBufferSize(): [number, number];
115
- /**
116
- * Update the canvas drawing buffer size.
117
- * @note - Called automatically if props.autoResize is true.
118
- * @note - Defers update of drawing buffer size until framebuffer is requested to avoid flicker
119
- * (resizing clears the drawing buffer)!
120
- */
121
- setDrawingBufferSize(width: number, height: number): void;
122
- /**
123
- * Returns the current DPR (number of physical pixels per CSS pixel), if props.useDevicePixels is true
124
- * @note This can be a fractional (non-integer) number, e.g. when the user zooms in the browser.
125
- * @note This function handles the non-HTML canvas cases
126
- */
127
- getDevicePixelRatio(): number;
128
- /**
129
- * Maps CSS pixel position to device pixel position
130
- */
131
- cssToDevicePixels(cssPixel: [number, number], yInvert?: boolean): {
132
- x: number;
133
- y: number;
134
- width: number;
135
- height: number;
136
- };
137
- /** @deprecated - use .getDevicePixelSize() */
138
- getPixelSize(): [number, number];
139
- /** @deprecated - TODO which values should we use for aspect */
140
- getAspect(): number;
141
- /** @deprecated Returns multiplier need to convert CSS size to Device size */
142
- cssToDeviceRatio(): number;
143
- /** @deprecated Use canvasContext.setDrawingBufferSize() */
144
- resize(size: {
145
- width: number;
146
- height: number;
147
- }): void;
148
- /**
149
- * Performs platform specific updates (WebGPU vs WebGL)
150
- * Can be called after changes to size or props,
151
- * to give implementation an opportunity to update configurations.
152
- */
153
- protected abstract _configureDevice(): void;
154
- /** Returns a framebuffer with properly resized current 'swap chain' textures */
155
- protected abstract _getCurrentFramebuffer(options?: {
156
- depthStencilFormat?: TextureFormatDepthStencil | false;
157
- }): Framebuffer;
158
- /**
159
- * Allows subclass constructor to override the canvas id for auto created canvases.
160
- * This can really help when debugging DOM in apps that create multiple devices
161
- */
162
- protected _setAutoCreatedCanvasId(id: string): void;
163
- /** reacts to an observed intersection */
164
- protected _handleIntersection(entries: IntersectionObserverEntry[]): void;
165
- /**
166
- * Reacts to an observed resize by using the most accurate pixel size information the browser can provide
167
- * @see https://web.dev/articles/device-pixel-content-box
168
- * @see https://webgpufundamentals.org/webgpu/lessons/webgpu-resizing-the-canvas.html
169
- */
170
- protected _handleResize(entries: ResizeObserverEntry[]): void;
171
- /** Initiate a deferred update for the canvas drawing buffer size */
172
- protected _updateDrawingBufferSize(): void;
173
- /** Perform a deferred resize of the drawing buffer if needed */
174
- _resizeDrawingBufferIfNeeded(): void;
175
- /** Monitor DPR changes */
176
- _observeDevicePixelRatio(): void;
177
- /** Start tracking positions with a timer */
178
- _trackPosition(intervalMs?: number): void;
179
- /**
180
- * Calculated the absolute position of the canvas
181
- * @note - getBoundingClientRect() is normally cheap but can be expensive
182
- * if called before browser has finished a reflow. Should not be the case here.
183
- */
184
- updatePosition(): void;
185
10
  }
186
11
  //# sourceMappingURL=canvas-context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"canvas-context.d.ts","sourceRoot":"","sources":["../../src/adapter/canvas-context.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,MAAM,EAAC,oBAAiB;AACrC,OAAO,KAAK,EAAC,WAAW,EAAC,mCAAgC;AACzD,OAAO,KAAK,EAAC,yBAAyB,EAAC,mDAAgD;AAIvF,qCAAqC;AACrC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,gCAAgC;IAChC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,0IAA0I;IAC1I,MAAM,CAAC,EAAE,iBAAiB,GAAG,eAAe,GAAG,MAAM,GAAG,IAAI,CAAC;IAC7D,iIAAiI;IACjI,SAAS,CAAC,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI,CAAC;IACxC,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM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,CAAC,aAAa,CAAC,OAAO,EAAE,mBAAmB,EAAE;IAmCtD,oEAAoE;IACpE,SAAS,CAAC,wBAAwB;IAoBlC,gEAAgE;IAChE,4BAA4B;IAgB5B,0BAA0B;IAC1B,wBAAwB;IAgBxB,4CAA4C;IAC5C,cAAc,CAAC,UAAU,GAAE,MAAY,GAAG,IAAI;IAU9C;;;;OAIG;IACH,cAAc;CAef"}
1
+ {"version":3,"file":"canvas-context.d.ts","sourceRoot":"","sources":["../../src/adapter/canvas-context.ts"],"names":[],"mappings":"AAIA,YAAY,EAAC,kBAAkB,EAAE,yBAAyB,EAAC,4BAAyB;AACpF,OAAO,EAAC,aAAa,EAAC,4BAAyB;AAE/C;;;GAGG;AACH,8BAAsB,aAAc,SAAQ,aAAa;IACvD,OAAgB,YAAY,0DAA8B;IAE1D,kBAA2B,MAAM,EAAE,OAAO,CAAC;CAC5C"}
@@ -1,457 +1,12 @@
1
1
  // luma.gl
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
- import { isBrowser } from '@probe.gl/env';
5
- import { uid } from "../utils/uid.js";
6
- import { withResolvers } from "../utils/promise-utils.js";
4
+ import { CanvasSurface } from "./canvas-surface.js";
7
5
  /**
8
- * Manages a canvas. Supports both HTML or offscreen canvas
9
- * - Creates a new canvas or looks up a canvas from the DOM
10
- * - Provides check for DOM loaded
11
- * @todo commit() @see https://github.com/w3ctag/design-reviews/issues/288
12
- * @todo transferControlToOffscreen: @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/transferControlToOffscreen
6
+ * Manages a renderable backend canvas. Supports both HTML or offscreen canvas
7
+ * and returns backend framebuffers sourced from the canvas itself.
13
8
  */
14
- export class CanvasContext {
15
- static isHTMLCanvas(canvas) {
16
- return typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement;
17
- }
18
- static isOffscreenCanvas(canvas) {
19
- return typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas;
20
- }
21
- static defaultProps = {
22
- id: undefined,
23
- canvas: null,
24
- width: 800,
25
- height: 600,
26
- useDevicePixels: true,
27
- autoResize: true,
28
- container: null,
29
- visible: true,
30
- alphaMode: 'opaque',
31
- colorSpace: 'srgb',
32
- trackPosition: false
33
- };
34
- id;
35
- props;
36
- canvas;
37
- /** Handle to HTML canvas */
38
- htmlCanvas;
39
- /** Handle to wrapped OffScreenCanvas */
40
- offscreenCanvas;
41
- type;
42
- /** Promise that resolved once the resize observer has updated the pixel size */
43
- initialized;
44
- isInitialized = false;
45
- /** Visibility is automatically updated (via an IntersectionObserver) */
46
- isVisible = true;
47
- /** Width of canvas in CSS units (tracked by a ResizeObserver) */
48
- cssWidth;
49
- /** Height of canvas in CSS units (tracked by a ResizeObserver) */
50
- cssHeight;
51
- /** Device pixel ratio. Automatically updated via media queries */
52
- devicePixelRatio;
53
- /** Exact width of canvas in physical pixels (tracked by a ResizeObserver) */
54
- devicePixelWidth;
55
- /** Exact height of canvas in physical pixels (tracked by a ResizeObserver) */
56
- devicePixelHeight;
57
- /** Width of drawing buffer: automatically tracks this.pixelWidth if props.autoResize is true */
58
- drawingBufferWidth;
59
- /** Height of drawing buffer: automatically tracks this.pixelHeight if props.autoResize is true */
60
- drawingBufferHeight;
61
- /** Resolves when the canvas is initialized, i.e. when the ResizeObserver has updated the pixel size */
62
- _initializedResolvers = withResolvers();
63
- /** ResizeObserver to track canvas size changes */
64
- _resizeObserver;
65
- /** IntersectionObserver to track canvas visibility changes */
66
- _intersectionObserver;
67
- /** Position of the canvas in the document, updated by a timer */
68
- _position = [0, 0];
69
- /** Whether this canvas context has been destroyed */
70
- destroyed = false;
71
- /** Whether the drawing buffer size needs to be resized (deferred resizing to avoid flicker) */
72
- _needsDrawingBufferResize = true;
73
- toString() {
74
- return `${this[Symbol.toStringTag]}(${this.id})`;
75
- }
76
- constructor(props) {
77
- this.props = { ...CanvasContext.defaultProps, ...props };
78
- props = this.props;
79
- this.initialized = this._initializedResolvers.promise;
80
- // Create a canvas element if needed
81
- if (!isBrowser()) {
82
- // TODO - does this prevent app from using jsdom style polyfills?
83
- this.canvas = { width: props.width || 1, height: props.height || 1 };
84
- }
85
- else if (!props.canvas) {
86
- this.canvas = createCanvasElement(props);
87
- }
88
- else if (typeof props.canvas === 'string') {
89
- this.canvas = getCanvasFromDOM(props.canvas);
90
- }
91
- else {
92
- this.canvas = props.canvas;
93
- }
94
- if (CanvasContext.isHTMLCanvas(this.canvas)) {
95
- this.id = props.id || this.canvas.id;
96
- this.type = 'html-canvas';
97
- this.htmlCanvas = this.canvas;
98
- }
99
- else if (CanvasContext.isOffscreenCanvas(this.canvas)) {
100
- this.id = props.id || 'offscreen-canvas';
101
- this.type = 'offscreen-canvas';
102
- this.offscreenCanvas = this.canvas;
103
- }
104
- else {
105
- // TODO - Node.js support is currently untested (was used for headless-gl in luma v8)
106
- this.id = props.id || 'node-canvas-context';
107
- this.type = 'node';
108
- }
109
- // Initialize size variables to some sane values (these will be updated by ResizeObserver)
110
- this.cssWidth = this.htmlCanvas?.clientWidth || this.canvas.width;
111
- this.cssHeight = this.htmlCanvas?.clientHeight || this.canvas.height;
112
- this.devicePixelWidth = this.canvas.width;
113
- this.devicePixelHeight = this.canvas.height;
114
- this.drawingBufferWidth = this.canvas.width;
115
- this.drawingBufferHeight = this.canvas.height;
116
- this.devicePixelRatio = globalThis.devicePixelRatio || 1;
117
- this._position = [0, 0];
118
- if (CanvasContext.isHTMLCanvas(this.canvas)) {
119
- // Track visibility changes
120
- this._intersectionObserver = new IntersectionObserver(entries => this._handleIntersection(entries));
121
- this._intersectionObserver.observe(this.canvas);
122
- // Track size changes
123
- this._resizeObserver = new ResizeObserver(entries => this._handleResize(entries));
124
- try {
125
- this._resizeObserver.observe(this.canvas, { box: 'device-pixel-content-box' });
126
- }
127
- catch {
128
- // Safari fallback
129
- this._resizeObserver.observe(this.canvas, { box: 'content-box' });
130
- }
131
- // Track device pixel ratio changes.
132
- // Defer call to after construction completes to ensure `this.device` is available.
133
- setTimeout(() => this._observeDevicePixelRatio(), 0);
134
- // Track top/left position changes
135
- if (this.props.trackPosition) {
136
- this._trackPosition();
137
- }
138
- }
139
- }
140
- destroy() {
141
- this.destroyed = true;
142
- }
143
- setProps(props) {
144
- if ('useDevicePixels' in props) {
145
- this.props.useDevicePixels = props.useDevicePixels || false;
146
- this._updateDrawingBufferSize();
147
- }
148
- return this;
149
- }
150
- /** Returns a framebuffer with properly resized current 'swap chain' textures */
151
- getCurrentFramebuffer(options) {
152
- this._resizeDrawingBufferIfNeeded();
153
- return this._getCurrentFramebuffer(options);
154
- }
155
- // SIZE METHODS
156
- /**
157
- * Returns the size covered by the canvas in CSS pixels
158
- * @note This can be different from the actual device pixel size of a canvas due to DPR scaling, and rounding to integer pixels
159
- * @note This is independent of the canvas' internal drawing buffer size (.width, .height).
160
- */
161
- getCSSSize() {
162
- return [this.cssWidth, this.cssHeight];
163
- }
164
- getPosition() {
165
- return this._position;
166
- }
167
- /**
168
- * Returns the size covered by the canvas in actual device pixels.
169
- * @note This can be different from the 'CSS' size of a canvas due to DPR scaling, and rounding to integer pixels
170
- * @note This is independent of the canvas' internal drawing buffer size (.width, .height).
171
- */
172
- getDevicePixelSize() {
173
- return [this.devicePixelWidth, this.devicePixelHeight];
174
- }
175
- /** Get the drawing buffer size (number of pixels GPU is rendering into, can be different from CSS size) */
176
- getDrawingBufferSize() {
177
- return [this.drawingBufferWidth, this.drawingBufferHeight];
178
- }
179
- /** Returns the biggest allowed framebuffer size. @todo Allow the application to limit this? */
180
- getMaxDrawingBufferSize() {
181
- const maxTextureDimension = this.device.limits.maxTextureDimension2D;
182
- return [maxTextureDimension, maxTextureDimension];
183
- }
184
- /**
185
- * Update the canvas drawing buffer size.
186
- * @note - Called automatically if props.autoResize is true.
187
- * @note - Defers update of drawing buffer size until framebuffer is requested to avoid flicker
188
- * (resizing clears the drawing buffer)!
189
- */
190
- setDrawingBufferSize(width, height) {
191
- // TODO(ib) - temporarily makes drawingBufferWidth/Height out of sync with canvas.width/height, could that cause issues?
192
- this.drawingBufferWidth = Math.floor(width);
193
- this.drawingBufferHeight = Math.floor(height);
194
- this._needsDrawingBufferResize = true;
195
- }
196
- /**
197
- * Returns the current DPR (number of physical pixels per CSS pixel), if props.useDevicePixels is true
198
- * @note This can be a fractional (non-integer) number, e.g. when the user zooms in the browser.
199
- * @note This function handles the non-HTML canvas cases
200
- */
201
- getDevicePixelRatio() {
202
- const dpr = typeof window !== 'undefined' && window.devicePixelRatio;
203
- return dpr || 1;
204
- }
205
- // DEPRECATED METHODS
206
- /**
207
- * Maps CSS pixel position to device pixel position
208
- */
209
- cssToDevicePixels(cssPixel, yInvert = true) {
210
- const ratio = this.cssToDeviceRatio();
211
- const [width, height] = this.getDrawingBufferSize();
212
- return scalePixels(cssPixel, ratio, width, height, yInvert);
213
- }
214
- /** @deprecated - use .getDevicePixelSize() */
215
- getPixelSize() {
216
- return this.getDevicePixelSize();
217
- }
218
- /** @deprecated - TODO which values should we use for aspect */
219
- getAspect() {
220
- const [width, height] = this.getDevicePixelSize();
221
- return width / height;
222
- }
223
- /** @deprecated Returns multiplier need to convert CSS size to Device size */
224
- cssToDeviceRatio() {
225
- try {
226
- const [drawingBufferWidth] = this.getDrawingBufferSize();
227
- const [cssWidth] = this.getCSSSize();
228
- return cssWidth ? drawingBufferWidth / cssWidth : 1;
229
- }
230
- catch {
231
- return 1;
232
- }
233
- }
234
- /** @deprecated Use canvasContext.setDrawingBufferSize() */
235
- resize(size) {
236
- this.setDrawingBufferSize(size.width, size.height);
237
- }
238
- // IMPLEMENTATION
239
- /**
240
- * Allows subclass constructor to override the canvas id for auto created canvases.
241
- * This can really help when debugging DOM in apps that create multiple devices
242
- */
243
- _setAutoCreatedCanvasId(id) {
244
- if (this.htmlCanvas?.id === 'lumagl-auto-created-canvas') {
245
- this.htmlCanvas.id = id;
246
- }
247
- }
248
- /** reacts to an observed intersection */
249
- _handleIntersection(entries) {
250
- const entry = entries.find(entry_ => entry_.target === this.canvas);
251
- if (!entry) {
252
- return;
253
- }
254
- // TODO - store intersection rectangle?
255
- const isVisible = entry.isIntersecting;
256
- if (this.isVisible !== isVisible) {
257
- this.isVisible = isVisible;
258
- this.device.props.onVisibilityChange(this);
259
- }
260
- }
261
- /**
262
- * Reacts to an observed resize by using the most accurate pixel size information the browser can provide
263
- * @see https://web.dev/articles/device-pixel-content-box
264
- * @see https://webgpufundamentals.org/webgpu/lessons/webgpu-resizing-the-canvas.html
265
- */
266
- _handleResize(entries) {
267
- const entry = entries.find(entry_ => entry_.target === this.canvas);
268
- if (!entry) {
269
- return;
270
- }
271
- // Update CSS size using content box size
272
- this.cssWidth = entry.contentBoxSize[0].inlineSize;
273
- this.cssHeight = entry.contentBoxSize[0].blockSize;
274
- // Update our drawing buffer size variables, saving the old values for logging
275
- const oldPixelSize = this.getDevicePixelSize();
276
- // Use the most accurate drawing buffer size information the current browser can provide
277
- // Note: content box sizes are guaranteed to be integers
278
- // Note: Safari falls back to contentBoxSize
279
- const devicePixelWidth = entry.devicePixelContentBoxSize?.[0].inlineSize ||
280
- entry.contentBoxSize[0].inlineSize * devicePixelRatio;
281
- const devicePixelHeight = entry.devicePixelContentBoxSize?.[0].blockSize ||
282
- entry.contentBoxSize[0].blockSize * devicePixelRatio;
283
- // Make sure we don't overflow the maximum supported texture size
284
- const [maxDevicePixelWidth, maxDevicePixelHeight] = this.getMaxDrawingBufferSize();
285
- this.devicePixelWidth = Math.max(1, Math.min(devicePixelWidth, maxDevicePixelWidth));
286
- this.devicePixelHeight = Math.max(1, Math.min(devicePixelHeight, maxDevicePixelHeight));
287
- this._updateDrawingBufferSize();
288
- // Inform the device
289
- this.device.props.onResize(this, { oldPixelSize });
290
- }
291
- /** Initiate a deferred update for the canvas drawing buffer size */
292
- _updateDrawingBufferSize() {
293
- // Update the canvas drawing buffer size
294
- if (this.props.autoResize) {
295
- if (typeof this.props.useDevicePixels === 'number') {
296
- const dpr = this.props.useDevicePixels;
297
- this.setDrawingBufferSize(this.cssWidth * dpr, this.cssHeight * dpr);
298
- }
299
- else if (this.props.useDevicePixels) {
300
- this.setDrawingBufferSize(this.devicePixelWidth, this.devicePixelHeight);
301
- }
302
- else {
303
- this.setDrawingBufferSize(this.cssWidth, this.cssHeight);
304
- }
305
- }
306
- // Resolve the initialized promise
307
- this._initializedResolvers.resolve();
308
- this.isInitialized = true;
309
- this.updatePosition();
310
- }
311
- /** Perform a deferred resize of the drawing buffer if needed */
312
- _resizeDrawingBufferIfNeeded() {
313
- if (this._needsDrawingBufferResize) {
314
- this._needsDrawingBufferResize = false;
315
- const sizeChanged = this.drawingBufferWidth !== this.canvas.width ||
316
- this.drawingBufferHeight !== this.canvas.height;
317
- if (sizeChanged) {
318
- // Update the canvas size
319
- this.canvas.width = this.drawingBufferWidth;
320
- this.canvas.height = this.drawingBufferHeight;
321
- // Inform the subclass: WebGPU needs to call canvascontext.configure()
322
- this._configureDevice();
323
- }
324
- }
325
- }
326
- /** Monitor DPR changes */
327
- _observeDevicePixelRatio() {
328
- const oldRatio = this.devicePixelRatio;
329
- this.devicePixelRatio = window.devicePixelRatio;
330
- this.updatePosition();
331
- // Inform the device
332
- this.device.props.onDevicePixelRatioChange(this, { oldRatio });
333
- // Set up a one time query against the current resolution.
334
- matchMedia(`(resolution: ${this.devicePixelRatio}dppx)`).addEventListener('change', () => this._observeDevicePixelRatio(), { once: true });
335
- }
336
- /** Start tracking positions with a timer */
337
- _trackPosition(intervalMs = 100) {
338
- const intervalId = setInterval(() => {
339
- if (this.destroyed) {
340
- clearInterval(intervalId);
341
- }
342
- else {
343
- this.updatePosition();
344
- }
345
- }, intervalMs);
346
- }
347
- /**
348
- * Calculated the absolute position of the canvas
349
- * @note - getBoundingClientRect() is normally cheap but can be expensive
350
- * if called before browser has finished a reflow. Should not be the case here.
351
- */
352
- updatePosition() {
353
- const newRect = this.htmlCanvas?.getBoundingClientRect();
354
- if (newRect) {
355
- // We only track position since we rely on the more precise ResizeObserver for size
356
- const position = [newRect.left, newRect.top];
357
- this._position ??= position;
358
- const positionChanged = position[0] !== this._position[0] || position[1] !== this._position[1];
359
- if (positionChanged) {
360
- const oldPosition = this._position;
361
- this._position = position;
362
- this.device.props.onPositionChange?.(this, { oldPosition });
363
- }
364
- }
365
- }
366
- }
367
- // HELPER FUNCTIONS
368
- /** Get a container element from a string or DOM element */
369
- function getContainer(container) {
370
- if (typeof container === 'string') {
371
- const element = document.getElementById(container);
372
- if (!element) {
373
- throw new Error(`${container} is not an HTML element`);
374
- }
375
- return element;
376
- }
377
- if (container) {
378
- return container;
379
- }
380
- return document.body;
381
- }
382
- /** Get a Canvas element from DOM id */
383
- function getCanvasFromDOM(canvasId) {
384
- const canvas = document.getElementById(canvasId);
385
- if (!CanvasContext.isHTMLCanvas(canvas)) {
386
- throw new Error('Object is not a canvas element');
387
- }
388
- return canvas;
389
- }
390
- /** Create a new canvas */
391
- function createCanvasElement(props) {
392
- const { width, height } = props;
393
- const newCanvas = document.createElement('canvas');
394
- newCanvas.id = uid('lumagl-auto-created-canvas');
395
- newCanvas.width = width || 1;
396
- newCanvas.height = height || 1;
397
- newCanvas.style.width = Number.isFinite(width) ? `${width}px` : '100%';
398
- newCanvas.style.height = Number.isFinite(height) ? `${height}px` : '100%';
399
- if (!props?.visible) {
400
- newCanvas.style.visibility = 'hidden';
401
- }
402
- // Insert the canvas in the DOM
403
- const container = getContainer(props?.container || null);
404
- container.insertBefore(newCanvas, container.firstChild);
405
- return newCanvas;
406
- }
407
- /**
408
- * Scales pixels linearly, handles edge cases
409
- * @param pixel
410
- * @param ratio
411
- * @param width
412
- * @param height
413
- * @param yInvert
414
- * @returns
415
- */
416
- function scalePixels(pixel, ratio, width, height, yInvert) {
417
- const point = pixel;
418
- const x = scaleX(point[0], ratio, width);
419
- let y = scaleY(point[1], ratio, height, yInvert);
420
- // Find boundaries of next pixel to provide valid range of device pixel locations
421
- let t = scaleX(point[0] + 1, ratio, width);
422
- // If next pixel's position is clamped to boundary, use it as is, otherwise subtract 1 for current pixel boundary
423
- const xHigh = t === width - 1 ? t : t - 1;
424
- t = scaleY(point[1] + 1, ratio, height, yInvert);
425
- let yHigh;
426
- if (yInvert) {
427
- // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range
428
- t = t === 0 ? t : t + 1;
429
- // swap y and yHigh
430
- yHigh = y;
431
- y = t;
432
- }
433
- else {
434
- // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range
435
- yHigh = t === height - 1 ? t : t - 1;
436
- // y remains same
437
- }
438
- return {
439
- x,
440
- y,
441
- // when ratio < 1, current css pixel and next css pixel may point to same device pixel, set width/height to 1 in those cases.
442
- width: Math.max(xHigh - x + 1, 1),
443
- height: Math.max(yHigh - y + 1, 1)
444
- };
445
- }
446
- function scaleX(x, ratio, width) {
447
- // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit
448
- const r = Math.min(Math.round(x * ratio), width - 1);
449
- return r;
450
- }
451
- function scaleY(y, ratio, height, yInvert) {
452
- // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit
453
- return yInvert
454
- ? Math.max(0, height - 1 - Math.round(y * ratio))
455
- : Math.min(Math.round(y * ratio), height - 1);
9
+ export class CanvasContext extends CanvasSurface {
10
+ static defaultProps = CanvasSurface.defaultProps;
456
11
  }
457
12
  //# sourceMappingURL=canvas-context.js.map