@luma.gl/core 9.2.6 → 9.3.0-alpha.10

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 (280) hide show
  1. package/dist/adapter/canvas-context.d.ts +6 -162
  2. package/dist/adapter/canvas-context.d.ts.map +1 -1
  3. package/dist/adapter/canvas-context.js +5 -419
  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 +81 -16
  14. package/dist/adapter/device.d.ts.map +1 -1
  15. package/dist/adapter/device.js +191 -10
  16. package/dist/adapter/device.js.map +1 -1
  17. package/dist/adapter/luma.js +1 -1
  18. package/dist/adapter/luma.js.map +1 -1
  19. package/dist/adapter/presentation-context.d.ts +11 -0
  20. package/dist/adapter/presentation-context.d.ts.map +1 -0
  21. package/dist/adapter/presentation-context.js +12 -0
  22. package/dist/adapter/presentation-context.js.map +1 -0
  23. package/dist/adapter/resources/buffer.d.ts +1 -1
  24. package/dist/adapter/resources/buffer.d.ts.map +1 -1
  25. package/dist/adapter/resources/buffer.js +14 -6
  26. package/dist/adapter/resources/buffer.js.map +1 -1
  27. package/dist/adapter/resources/command-buffer.d.ts +3 -1
  28. package/dist/adapter/resources/command-buffer.d.ts.map +1 -1
  29. package/dist/adapter/resources/command-buffer.js +3 -1
  30. package/dist/adapter/resources/command-buffer.js.map +1 -1
  31. package/dist/adapter/resources/command-encoder.d.ts +30 -7
  32. package/dist/adapter/resources/command-encoder.d.ts.map +1 -1
  33. package/dist/adapter/resources/command-encoder.js +68 -2
  34. package/dist/adapter/resources/command-encoder.js.map +1 -1
  35. package/dist/adapter/resources/compute-pipeline.d.ts +2 -2
  36. package/dist/adapter/resources/compute-pipeline.d.ts.map +1 -1
  37. package/dist/adapter/resources/fence.d.ts +16 -0
  38. package/dist/adapter/resources/fence.d.ts.map +1 -0
  39. package/dist/adapter/resources/fence.js +17 -0
  40. package/dist/adapter/resources/fence.js.map +1 -0
  41. package/dist/adapter/resources/framebuffer.d.ts +1 -1
  42. package/dist/adapter/resources/framebuffer.d.ts.map +1 -1
  43. package/dist/adapter/resources/framebuffer.js +15 -12
  44. package/dist/adapter/resources/framebuffer.js.map +1 -1
  45. package/dist/adapter/resources/query-set.d.ts +17 -1
  46. package/dist/adapter/resources/query-set.d.ts.map +1 -1
  47. package/dist/adapter/resources/query-set.js.map +1 -1
  48. package/dist/adapter/resources/render-pipeline.d.ts +28 -10
  49. package/dist/adapter/resources/render-pipeline.d.ts.map +1 -1
  50. package/dist/adapter/resources/render-pipeline.js +21 -2
  51. package/dist/adapter/resources/render-pipeline.js.map +1 -1
  52. package/dist/adapter/resources/resource.d.ts +13 -0
  53. package/dist/adapter/resources/resource.d.ts.map +1 -1
  54. package/dist/adapter/resources/resource.js +243 -14
  55. package/dist/adapter/resources/resource.js.map +1 -1
  56. package/dist/adapter/resources/shader.js +27 -25
  57. package/dist/adapter/resources/shader.js.map +1 -1
  58. package/dist/adapter/resources/shared-render-pipeline.d.ts +22 -0
  59. package/dist/adapter/resources/shared-render-pipeline.d.ts.map +1 -0
  60. package/dist/adapter/resources/shared-render-pipeline.js +25 -0
  61. package/dist/adapter/resources/shared-render-pipeline.js.map +1 -0
  62. package/dist/adapter/resources/texture-view.d.ts +1 -1
  63. package/dist/adapter/resources/texture-view.d.ts.map +1 -1
  64. package/dist/adapter/resources/texture.d.ts +168 -28
  65. package/dist/adapter/resources/texture.d.ts.map +1 -1
  66. package/dist/adapter/resources/texture.js +284 -25
  67. package/dist/adapter/resources/texture.js.map +1 -1
  68. package/dist/adapter/types/attachments.d.ts +1 -1
  69. package/dist/adapter/types/attachments.d.ts.map +1 -1
  70. package/dist/adapter/types/buffer-layout.d.ts +1 -1
  71. package/dist/adapter/types/buffer-layout.d.ts.map +1 -1
  72. package/dist/adapter/types/parameters.d.ts +3 -1
  73. package/dist/adapter/types/parameters.d.ts.map +1 -1
  74. package/dist/adapter/types/parameters.js +1 -0
  75. package/dist/adapter/types/parameters.js.map +1 -1
  76. package/dist/adapter/types/shader-layout.d.ts +10 -6
  77. package/dist/adapter/types/shader-layout.d.ts.map +1 -1
  78. package/dist/adapter/types/uniforms.d.ts +6 -0
  79. package/dist/adapter/types/uniforms.d.ts.map +1 -1
  80. package/dist/adapter-utils/bind-groups.d.ts +9 -0
  81. package/dist/adapter-utils/bind-groups.d.ts.map +1 -0
  82. package/dist/adapter-utils/bind-groups.js +41 -0
  83. package/dist/adapter-utils/bind-groups.js.map +1 -0
  84. package/dist/adapter-utils/format-compiler-log.d.ts.map +1 -1
  85. package/dist/adapter-utils/format-compiler-log.js +23 -15
  86. package/dist/adapter-utils/format-compiler-log.js.map +1 -1
  87. package/dist/adapter-utils/get-attribute-from-layouts.d.ts +2 -2
  88. package/dist/adapter-utils/get-attribute-from-layouts.d.ts.map +1 -1
  89. package/dist/adapter-utils/get-attribute-from-layouts.js +6 -6
  90. package/dist/adapter-utils/get-attribute-from-layouts.js.map +1 -1
  91. package/dist/dist.dev.js +2686 -644
  92. package/dist/dist.min.js +10 -9
  93. package/dist/factories/bind-group-factory.d.ts +20 -0
  94. package/dist/factories/bind-group-factory.d.ts.map +1 -0
  95. package/dist/factories/bind-group-factory.js +79 -0
  96. package/dist/factories/bind-group-factory.js.map +1 -0
  97. package/dist/factories/core-module-state.d.ts +7 -0
  98. package/dist/factories/core-module-state.d.ts.map +1 -0
  99. package/dist/{shadertypes/data-types/shader-types.js → factories/core-module-state.js} +1 -1
  100. package/dist/factories/core-module-state.js.map +1 -0
  101. package/dist/factories/pipeline-factory.d.ts +54 -0
  102. package/dist/factories/pipeline-factory.d.ts.map +1 -0
  103. package/dist/factories/pipeline-factory.js +270 -0
  104. package/dist/factories/pipeline-factory.js.map +1 -0
  105. package/dist/factories/shader-factory.d.ts +20 -0
  106. package/dist/factories/shader-factory.d.ts.map +1 -0
  107. package/dist/factories/shader-factory.js +84 -0
  108. package/dist/factories/shader-factory.js.map +1 -0
  109. package/dist/index.cjs +2422 -554
  110. package/dist/index.cjs.map +4 -4
  111. package/dist/index.d.ts +30 -14
  112. package/dist/index.d.ts.map +1 -1
  113. package/dist/index.js +19 -7
  114. package/dist/index.js.map +1 -1
  115. package/dist/portable/shader-block-writer.d.ts +51 -0
  116. package/dist/portable/shader-block-writer.d.ts.map +1 -0
  117. package/dist/portable/shader-block-writer.js +185 -0
  118. package/dist/portable/shader-block-writer.js.map +1 -0
  119. package/dist/portable/uniform-block.d.ts +1 -1
  120. package/dist/portable/uniform-block.d.ts.map +1 -1
  121. package/dist/portable/uniform-store.d.ts +55 -24
  122. package/dist/portable/uniform-store.d.ts.map +1 -1
  123. package/dist/portable/uniform-store.js +73 -25
  124. package/dist/portable/uniform-store.js.map +1 -1
  125. package/dist/shadertypes/data-types/data-type-decoder.d.ts +20 -0
  126. package/dist/shadertypes/data-types/data-type-decoder.d.ts.map +1 -0
  127. package/dist/shadertypes/data-types/data-type-decoder.js +79 -0
  128. package/dist/shadertypes/data-types/data-type-decoder.js.map +1 -0
  129. package/dist/shadertypes/data-types/data-types.d.ts +31 -12
  130. package/dist/shadertypes/data-types/data-types.d.ts.map +1 -1
  131. package/dist/shadertypes/data-types/decode-data-types.d.ts.map +1 -1
  132. package/dist/shadertypes/data-types/decode-data-types.js +2 -1
  133. package/dist/shadertypes/data-types/decode-data-types.js.map +1 -1
  134. package/dist/{image-utils → shadertypes/image-types}/image-types.d.ts +0 -6
  135. package/dist/shadertypes/image-types/image-types.d.ts.map +1 -0
  136. package/dist/shadertypes/image-types/image-types.js.map +1 -0
  137. package/dist/shadertypes/shader-types/shader-block-layout.d.ts +72 -0
  138. package/dist/shadertypes/shader-types/shader-block-layout.d.ts.map +1 -0
  139. package/dist/shadertypes/shader-types/shader-block-layout.js +209 -0
  140. package/dist/shadertypes/shader-types/shader-block-layout.js.map +1 -0
  141. package/dist/shadertypes/shader-types/shader-type-decoder.d.ts +41 -0
  142. package/dist/shadertypes/shader-types/shader-type-decoder.d.ts.map +1 -0
  143. package/dist/shadertypes/{data-types/decode-shader-types.js → shader-types/shader-type-decoder.js} +43 -4
  144. package/dist/shadertypes/shader-types/shader-type-decoder.js.map +1 -0
  145. package/dist/shadertypes/shader-types/shader-types.d.ts +101 -0
  146. package/dist/shadertypes/shader-types/shader-types.d.ts.map +1 -0
  147. package/dist/shadertypes/shader-types/shader-types.js +30 -0
  148. package/dist/shadertypes/shader-types/shader-types.js.map +1 -0
  149. package/dist/shadertypes/texture-types/pixel-utils.d.ts.map +1 -0
  150. package/dist/shadertypes/{textures → texture-types}/pixel-utils.js +4 -4
  151. package/dist/shadertypes/texture-types/pixel-utils.js.map +1 -0
  152. package/dist/shadertypes/texture-types/texture-format-decoder.d.ts +36 -0
  153. package/dist/shadertypes/texture-types/texture-format-decoder.d.ts.map +1 -0
  154. package/dist/shadertypes/{textures → texture-types}/texture-format-decoder.js +109 -37
  155. package/dist/shadertypes/texture-types/texture-format-decoder.js.map +1 -0
  156. package/dist/shadertypes/texture-types/texture-format-generics.d.ts +34 -0
  157. package/dist/shadertypes/texture-types/texture-format-generics.d.ts.map +1 -0
  158. package/dist/shadertypes/texture-types/texture-format-generics.js.map +1 -0
  159. package/dist/shadertypes/texture-types/texture-format-table.d.ts.map +1 -0
  160. package/dist/shadertypes/{textures → texture-types}/texture-format-table.js +10 -9
  161. package/dist/shadertypes/texture-types/texture-format-table.js.map +1 -0
  162. package/dist/shadertypes/{textures → texture-types}/texture-formats.d.ts +51 -17
  163. package/dist/shadertypes/texture-types/texture-formats.d.ts.map +1 -0
  164. package/dist/shadertypes/{textures → texture-types}/texture-formats.js +1 -0
  165. package/dist/shadertypes/texture-types/texture-formats.js.map +1 -0
  166. package/dist/shadertypes/texture-types/texture-layout.d.ts +5 -0
  167. package/dist/shadertypes/texture-types/texture-layout.d.ts.map +1 -0
  168. package/dist/shadertypes/texture-types/texture-layout.js +41 -0
  169. package/dist/shadertypes/texture-types/texture-layout.js.map +1 -0
  170. package/dist/shadertypes/vertex-types/vertex-format-decoder.d.ts +24 -0
  171. package/dist/shadertypes/vertex-types/vertex-format-decoder.d.ts.map +1 -0
  172. package/dist/shadertypes/vertex-types/vertex-format-decoder.js +106 -0
  173. package/dist/shadertypes/vertex-types/vertex-format-decoder.js.map +1 -0
  174. package/dist/shadertypes/vertex-types/vertex-formats.d.ts +50 -0
  175. package/dist/shadertypes/vertex-types/vertex-formats.d.ts.map +1 -0
  176. package/dist/shadertypes/vertex-types/vertex-formats.js.map +1 -0
  177. package/dist/utils/array-equal.d.ts +1 -1
  178. package/dist/utils/array-equal.d.ts.map +1 -1
  179. package/dist/utils/array-equal.js +15 -9
  180. package/dist/utils/array-equal.js.map +1 -1
  181. package/dist/utils/assert.d.ts +5 -0
  182. package/dist/utils/assert.d.ts.map +1 -0
  183. package/dist/utils/assert.js +17 -0
  184. package/dist/utils/assert.js.map +1 -0
  185. package/dist/utils/stats-manager.d.ts.map +1 -1
  186. package/dist/utils/stats-manager.js +61 -1
  187. package/dist/utils/stats-manager.js.map +1 -1
  188. package/package.json +6 -6
  189. package/src/adapter/canvas-context.ts +7 -556
  190. package/src/adapter/canvas-observer.ts +130 -0
  191. package/src/adapter/canvas-surface.ts +521 -0
  192. package/src/adapter/device.ts +308 -24
  193. package/src/adapter/presentation-context.ts +16 -0
  194. package/src/adapter/resources/buffer.ts +13 -5
  195. package/src/adapter/resources/command-buffer.ts +4 -2
  196. package/src/adapter/resources/command-encoder.ts +101 -10
  197. package/src/adapter/resources/compute-pipeline.ts +2 -2
  198. package/src/adapter/resources/fence.ts +32 -0
  199. package/src/adapter/resources/framebuffer.ts +16 -13
  200. package/src/adapter/resources/query-set.ts +17 -1
  201. package/src/adapter/resources/render-pipeline.ts +52 -16
  202. package/src/adapter/resources/resource.ts +289 -14
  203. package/src/adapter/resources/shader.ts +28 -28
  204. package/src/adapter/resources/shared-render-pipeline.ts +40 -0
  205. package/src/adapter/resources/texture-view.ts +1 -1
  206. package/src/adapter/resources/texture.ts +427 -49
  207. package/src/adapter/types/attachments.ts +1 -1
  208. package/src/adapter/types/buffer-layout.ts +1 -1
  209. package/src/adapter/types/parameters.ts +4 -1
  210. package/src/adapter/types/shader-layout.ts +15 -9
  211. package/src/adapter/types/uniforms.ts +12 -0
  212. package/src/adapter-utils/bind-groups.ts +71 -0
  213. package/src/adapter-utils/format-compiler-log.ts +23 -15
  214. package/src/adapter-utils/get-attribute-from-layouts.ts +8 -11
  215. package/src/factories/bind-group-factory.ts +139 -0
  216. package/src/factories/core-module-state.ts +11 -0
  217. package/src/factories/pipeline-factory.ts +328 -0
  218. package/src/factories/shader-factory.ts +103 -0
  219. package/src/index.ts +70 -26
  220. package/src/portable/shader-block-writer.ts +254 -0
  221. package/src/portable/uniform-block.ts +1 -1
  222. package/src/portable/uniform-store.ts +98 -40
  223. package/src/shadertypes/data-types/data-type-decoder.ts +105 -0
  224. package/src/shadertypes/data-types/data-types.ts +100 -48
  225. package/src/shadertypes/data-types/decode-data-types.ts +2 -1
  226. package/src/{image-utils → shadertypes/image-types}/image-types.ts +0 -7
  227. package/src/shadertypes/shader-types/shader-block-layout.ts +340 -0
  228. package/src/shadertypes/{data-types/decode-shader-types.ts → shader-types/shader-type-decoder.ts} +88 -14
  229. package/src/shadertypes/shader-types/shader-types.ts +207 -0
  230. package/src/shadertypes/{textures → texture-types}/pixel-utils.ts +4 -4
  231. package/src/shadertypes/{textures → texture-types}/texture-format-decoder.ts +166 -45
  232. package/src/shadertypes/{textures → texture-types}/texture-format-generics.ts +42 -48
  233. package/src/shadertypes/{textures → texture-types}/texture-format-table.ts +10 -9
  234. package/src/shadertypes/{textures → texture-types}/texture-formats.ts +73 -17
  235. package/src/shadertypes/texture-types/texture-layout.ts +60 -0
  236. package/src/shadertypes/vertex-types/vertex-format-decoder.ts +131 -0
  237. package/src/shadertypes/vertex-types/vertex-formats.ts +183 -0
  238. package/src/utils/array-equal.ts +21 -9
  239. package/src/utils/assert.ts +18 -0
  240. package/src/utils/stats-manager.ts +76 -2
  241. package/dist/image-utils/image-types.d.ts.map +0 -1
  242. package/dist/image-utils/image-types.js.map +0 -1
  243. package/dist/portable/uniform-buffer-layout.d.ts +0 -28
  244. package/dist/portable/uniform-buffer-layout.d.ts.map +0 -1
  245. package/dist/portable/uniform-buffer-layout.js +0 -96
  246. package/dist/portable/uniform-buffer-layout.js.map +0 -1
  247. package/dist/shadertypes/data-types/decode-shader-types.d.ts +0 -17
  248. package/dist/shadertypes/data-types/decode-shader-types.d.ts.map +0 -1
  249. package/dist/shadertypes/data-types/decode-shader-types.js.map +0 -1
  250. package/dist/shadertypes/data-types/shader-types.d.ts +0 -45
  251. package/dist/shadertypes/data-types/shader-types.d.ts.map +0 -1
  252. package/dist/shadertypes/data-types/shader-types.js.map +0 -1
  253. package/dist/shadertypes/textures/pixel-utils.d.ts.map +0 -1
  254. package/dist/shadertypes/textures/pixel-utils.js.map +0 -1
  255. package/dist/shadertypes/textures/texture-format-decoder.d.ts +0 -18
  256. package/dist/shadertypes/textures/texture-format-decoder.d.ts.map +0 -1
  257. package/dist/shadertypes/textures/texture-format-decoder.js.map +0 -1
  258. package/dist/shadertypes/textures/texture-format-generics.d.ts +0 -33
  259. package/dist/shadertypes/textures/texture-format-generics.d.ts.map +0 -1
  260. package/dist/shadertypes/textures/texture-format-generics.js.map +0 -1
  261. package/dist/shadertypes/textures/texture-format-table.d.ts.map +0 -1
  262. package/dist/shadertypes/textures/texture-format-table.js.map +0 -1
  263. package/dist/shadertypes/textures/texture-formats.d.ts.map +0 -1
  264. package/dist/shadertypes/textures/texture-formats.js.map +0 -1
  265. package/dist/shadertypes/vertex-arrays/decode-vertex-format.d.ts +0 -18
  266. package/dist/shadertypes/vertex-arrays/decode-vertex-format.d.ts.map +0 -1
  267. package/dist/shadertypes/vertex-arrays/decode-vertex-format.js +0 -100
  268. package/dist/shadertypes/vertex-arrays/decode-vertex-format.js.map +0 -1
  269. package/dist/shadertypes/vertex-arrays/vertex-formats.d.ts +0 -27
  270. package/dist/shadertypes/vertex-arrays/vertex-formats.d.ts.map +0 -1
  271. package/dist/shadertypes/vertex-arrays/vertex-formats.js.map +0 -1
  272. package/src/portable/uniform-buffer-layout.ts +0 -118
  273. package/src/shadertypes/data-types/shader-types.ts +0 -87
  274. package/src/shadertypes/vertex-arrays/decode-vertex-format.ts +0 -124
  275. package/src/shadertypes/vertex-arrays/vertex-formats.ts +0 -91
  276. /package/dist/{image-utils → shadertypes/image-types}/image-types.js +0 -0
  277. /package/dist/shadertypes/{textures → texture-types}/pixel-utils.d.ts +0 -0
  278. /package/dist/shadertypes/{textures → texture-types}/texture-format-generics.js +0 -0
  279. /package/dist/shadertypes/{textures → texture-types}/texture-format-table.d.ts +0 -0
  280. /package/dist/shadertypes/{vertex-arrays → vertex-types}/vertex-formats.js +0 -0
@@ -2,564 +2,15 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import {isBrowser} from '@probe.gl/env';
6
- import type {Device} from './device';
7
- import type {Framebuffer} from './resources/framebuffer';
8
- import type {TextureFormatDepthStencil} from '../shadertypes/textures/texture-formats';
9
- import {uid} from '../utils/uid';
10
- import {withResolvers} from '../utils/promise-utils';
11
-
12
- /** Properties for a CanvasContext */
13
- export type CanvasContextProps = {
14
- /** Identifier, for debugging */
15
- id?: string;
16
- /** 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 */
17
- canvas?: HTMLCanvasElement | OffscreenCanvas | string | null;
18
- /** If new canvas is created, it will be created in the specified container, otherwise is appended as a child of document.body */
19
- container?: HTMLElement | string | null;
20
- /** Width in pixels of the canvas - used when creating a new canvas */
21
- width?: number;
22
- /** Height in pixels of the canvas - used when creating a new canvas */
23
- height?: number;
24
- /** Visibility (only used if new canvas is created). */
25
- visible?: boolean;
26
- /** 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 */
27
- useDevicePixels?: boolean | number;
28
- /** Whether to track window resizes. */
29
- autoResize?: boolean;
30
- /** @see https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext/configure#alphamode */
31
- alphaMode?: 'opaque' | 'premultiplied';
32
- /** @see https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext/configure#colorspace */
33
- colorSpace?: 'srgb'; // GPUPredefinedColorSpace
34
- /** Whether to track position changes. Calls this.device.onPositionChange */
35
- trackPosition?: boolean;
36
- };
37
-
38
- export type MutableCanvasContextProps = {
39
- /** 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 */
40
- useDevicePixels?: boolean | number;
41
- };
42
-
43
- /**
44
- * Manages a canvas. Supports both HTML or offscreen canvas
45
- * - Creates a new canvas or looks up a canvas from the DOM
46
- * - Provides check for DOM loaded
47
- * @todo commit() @see https://github.com/w3ctag/design-reviews/issues/288
48
- * @todo transferControlToOffscreen: @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/transferControlToOffscreen
49
- */
50
- export abstract class CanvasContext {
51
- static isHTMLCanvas(canvas: unknown): canvas is HTMLCanvasElement {
52
- return typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement;
53
- }
54
-
55
- static isOffscreenCanvas(canvas: unknown): canvas is OffscreenCanvas {
56
- return typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas;
57
- }
58
-
59
- static defaultProps: Required<CanvasContextProps> = {
60
- id: undefined!,
61
- canvas: null,
62
- width: 800,
63
- height: 600,
64
- useDevicePixels: true,
65
- autoResize: true,
66
- container: null,
67
- visible: true,
68
- alphaMode: 'opaque',
69
- colorSpace: 'srgb',
70
- trackPosition: false
71
- };
72
-
73
- abstract readonly device: Device;
74
- abstract readonly handle: unknown;
75
- readonly id: string;
76
-
77
- readonly props: Required<CanvasContextProps>;
78
- readonly canvas: HTMLCanvasElement | OffscreenCanvas;
79
- /** Handle to HTML canvas */
80
- readonly htmlCanvas?: HTMLCanvasElement;
81
- /** Handle to wrapped OffScreenCanvas */
82
- readonly offscreenCanvas?: OffscreenCanvas;
83
- readonly type: 'html-canvas' | 'offscreen-canvas' | 'node';
84
-
85
- /** Promise that resolved once the resize observer has updated the pixel size */
86
- initialized: Promise<void>;
87
- isInitialized: boolean = false;
88
-
89
- /** Visibility is automatically updated (via an IntersectionObserver) */
90
- isVisible: boolean = true;
91
-
92
- /** Width of canvas in CSS units (tracked by a ResizeObserver) */
93
- cssWidth: number;
94
- /** Height of canvas in CSS units (tracked by a ResizeObserver) */
95
- cssHeight: number;
96
-
97
- /** Device pixel ratio. Automatically updated via media queries */
98
- devicePixelRatio: number;
99
- /** Exact width of canvas in physical pixels (tracked by a ResizeObserver) */
100
- devicePixelWidth: number;
101
- /** Exact height of canvas in physical pixels (tracked by a ResizeObserver) */
102
- devicePixelHeight: number;
103
-
104
- /** Width of drawing buffer: automatically tracks this.pixelWidth if props.autoResize is true */
105
- drawingBufferWidth: number;
106
- /** Height of drawing buffer: automatically tracks this.pixelHeight if props.autoResize is true */
107
- drawingBufferHeight: number;
108
-
109
- protected _initializedResolvers = withResolvers<void>();
110
- protected readonly _resizeObserver: ResizeObserver | undefined;
111
- protected readonly _intersectionObserver: IntersectionObserver | undefined;
112
- protected _position: [number, number];
113
- protected destroyed = false;
114
-
115
- abstract get [Symbol.toStringTag](): string;
116
-
117
- toString(): string {
118
- return `${this[Symbol.toStringTag]}(${this.id})`;
119
- }
120
-
121
- constructor(props?: CanvasContextProps) {
122
- this.props = {...CanvasContext.defaultProps, ...props};
123
- props = this.props;
124
-
125
- this.initialized = this._initializedResolvers.promise;
126
-
127
- // Create a canvas element if needed
128
- if (!isBrowser()) {
129
- // TODO - does this prevent app from using jsdom style polyfills?
130
- this.canvas = {width: props.width || 1, height: props.height || 1} as OffscreenCanvas;
131
- } else if (!props.canvas) {
132
- this.canvas = createCanvasElement(props);
133
- } else if (typeof props.canvas === 'string') {
134
- this.canvas = getCanvasFromDOM(props.canvas);
135
- } else {
136
- this.canvas = props.canvas;
137
- }
138
-
139
- if (CanvasContext.isHTMLCanvas(this.canvas)) {
140
- this.id = props.id || this.canvas.id;
141
- this.type = 'html-canvas';
142
- this.htmlCanvas = this.canvas;
143
- } else if (CanvasContext.isOffscreenCanvas(this.canvas)) {
144
- this.id = props.id || 'offscreen-canvas';
145
- this.type = 'offscreen-canvas';
146
- this.offscreenCanvas = this.canvas;
147
- } else {
148
- // TODO - Node.js support is currently untested (was used for headless-gl in luma v8)
149
- this.id = props.id || 'node-canvas-context';
150
- this.type = 'node';
151
- }
152
-
153
- // Initialize size variables to some sane values (these will be updated by ResizeObserver)
154
- this.cssWidth = this.htmlCanvas?.clientWidth || this.canvas.width;
155
- this.cssHeight = this.htmlCanvas?.clientHeight || this.canvas.height;
156
- this.devicePixelWidth = this.canvas.width;
157
- this.devicePixelHeight = this.canvas.height;
158
- this.drawingBufferWidth = this.canvas.width;
159
- this.drawingBufferHeight = this.canvas.height;
160
- this.devicePixelRatio = globalThis.devicePixelRatio || 1;
161
- this._position = [0, 0];
162
-
163
- if (CanvasContext.isHTMLCanvas(this.canvas)) {
164
- // Track visibility changes
165
- this._intersectionObserver = new IntersectionObserver(entries =>
166
- this._handleIntersection(entries)
167
- );
168
- this._intersectionObserver.observe(this.canvas);
169
-
170
- // Track size changes
171
- this._resizeObserver = new ResizeObserver(entries => this._handleResize(entries));
172
- try {
173
- this._resizeObserver.observe(this.canvas, {box: 'device-pixel-content-box'});
174
- } catch {
175
- // Safari fallback
176
- this._resizeObserver.observe(this.canvas, {box: 'content-box'});
177
- }
178
-
179
- // Track device pixel ratio changes.
180
- // Defer call to after construction completes to ensure `this.device` is available.
181
- setTimeout(() => this._observeDevicePixelRatio(), 0);
182
-
183
- // Track top/left position changes
184
- if (this.props.trackPosition) {
185
- this._trackPosition();
186
- }
187
- }
188
- }
189
-
190
- destroy() {
191
- this.destroyed = true;
192
- }
193
-
194
- setProps(props: MutableCanvasContextProps): this {
195
- if ('useDevicePixels' in props) {
196
- this.props.useDevicePixels = props.useDevicePixels || false;
197
- this._updateDrawingBufferSize();
198
- }
199
- return this;
200
- }
201
-
202
- /** Returns a framebuffer with properly resized current 'swap chain' textures */
203
- abstract getCurrentFramebuffer(options?: {
204
- depthStencilFormat?: TextureFormatDepthStencil | false;
205
- }): Framebuffer;
206
-
207
- // SIZE METHODS
208
-
209
- /**
210
- * Returns the size covered by the canvas in CSS pixels
211
- * @note This can be different from the actual device pixel size of a canvas due to DPR scaling, and rounding to integer pixels
212
- * @note This is independent of the canvas' internal drawing buffer size (.width, .height).
213
- */
214
- getCSSSize(): [number, number] {
215
- return [this.cssWidth, this.cssHeight];
216
- }
217
-
218
- getPosition() {
219
- return this._position;
220
- }
221
-
222
- /**
223
- * Returns the size covered by the canvas in actual device pixels.
224
- * @note This can be different from the 'CSS' size of a canvas due to DPR scaling, and rounding to integer pixels
225
- * @note This is independent of the canvas' internal drawing buffer size (.width, .height).
226
- */
227
- getDevicePixelSize(): [number, number] {
228
- return [this.devicePixelWidth, this.devicePixelHeight];
229
- }
230
-
231
- /** Get the drawing buffer size (number of pixels GPU is rendering into, can be different from CSS size) */
232
- getDrawingBufferSize(): [number, number] {
233
- return [this.drawingBufferWidth, this.drawingBufferHeight];
234
- }
235
-
236
- /** Returns the biggest allowed framebuffer size. @todo Allow the application to limit this? */
237
- getMaxDrawingBufferSize(): [number, number] {
238
- const maxTextureDimension = this.device.limits.maxTextureDimension2D;
239
- return [maxTextureDimension, maxTextureDimension];
240
- }
241
-
242
- /** Update the canvas drawing buffer size. Called automatically if props.autoResize is true. */
243
- setDrawingBufferSize(width: number, height: number) {
244
- this.canvas.width = width;
245
- this.canvas.height = height;
246
-
247
- this.drawingBufferWidth = width;
248
- this.drawingBufferHeight = height;
249
- }
250
-
251
- /**
252
- * Returns the current DPR (number of physical pixels per CSS pixel), if props.useDevicePixels is true
253
- * @note This can be a fractional (non-integer) number, e.g. when the user zooms in the browser.
254
- * @note This function handles the non-HTML canvas cases
255
- */
256
- getDevicePixelRatio(): number {
257
- const dpr = typeof window !== 'undefined' && window.devicePixelRatio;
258
- return dpr || 1;
259
- }
260
-
261
- // DEPRECATED METHODS
262
-
263
- /**
264
- * Maps CSS pixel position to device pixel position
265
- */
266
- cssToDevicePixels(
267
- cssPixel: [number, number],
268
- yInvert: boolean = true
269
- ): {
270
- x: number;
271
- y: number;
272
- width: number;
273
- height: number;
274
- } {
275
- const ratio = this.cssToDeviceRatio();
276
- const [width, height] = this.getDrawingBufferSize();
277
- return scalePixels(cssPixel, ratio, width, height, yInvert);
278
- }
279
-
280
- /** @deprecated - use .getDevicePixelSize() */
281
- getPixelSize() {
282
- return this.getDevicePixelSize();
283
- }
284
-
285
- /** @deprecated - TODO which values should we use for aspect */
286
- getAspect(): number {
287
- const [width, height] = this.getDevicePixelSize();
288
- return width / height;
289
- }
290
-
291
- /** @deprecated Returns multiplier need to convert CSS size to Device size */
292
- cssToDeviceRatio(): number {
293
- try {
294
- const [drawingBufferWidth] = this.getDrawingBufferSize();
295
- const [cssWidth] = this.getCSSSize();
296
- return cssWidth ? drawingBufferWidth / cssWidth : 1;
297
- } catch {
298
- return 1;
299
- }
300
- }
301
-
302
- /** @deprecated Use canvasContext.setDrawingBufferSize() */
303
- resize(size: {width: number; height: number}): void {
304
- this.setDrawingBufferSize(size.width, size.height);
305
- }
306
-
307
- // SUBCLASS OVERRIDES
308
-
309
- /**
310
- * Performs platform specific updates (WebGPU vs WebGL)
311
- * Can be called after changes to size or props,
312
- * to give implementation an opportunity to update configurations.
313
- */
314
- protected abstract _updateDevice(): void;
315
-
316
- // IMPLEMENTATION
317
-
318
- /**
319
- * Allows subclass constructor to override the canvas id for auto created canvases.
320
- * This can really help when debugging DOM in apps that create multiple devices
321
- */
322
- protected _setAutoCreatedCanvasId(id: string) {
323
- if (this.htmlCanvas?.id === 'lumagl-auto-created-canvas') {
324
- this.htmlCanvas.id = id;
325
- }
326
- }
327
-
328
- /** reacts to an observed intersection */
329
- protected _handleIntersection(entries: IntersectionObserverEntry[]) {
330
- const entry = entries.find(entry_ => entry_.target === this.canvas);
331
- if (!entry) {
332
- return;
333
- }
334
- // TODO - store intersection rectangle?
335
- const isVisible = entry.isIntersecting;
336
- if (this.isVisible !== isVisible) {
337
- this.isVisible = isVisible;
338
- this.device.props.onVisibilityChange(this);
339
- }
340
- }
341
-
342
- /**
343
- * Reacts to an observed resize by using the most accurate pixel size information the browser can provide
344
- * @see https://web.dev/articles/device-pixel-content-box
345
- * @see https://webgpufundamentals.org/webgpu/lessons/webgpu-resizing-the-canvas.html
346
- */
347
- protected _handleResize(entries: ResizeObserverEntry[]) {
348
- const entry = entries.find(entry_ => entry_.target === this.canvas);
349
- if (!entry) {
350
- return;
351
- }
352
-
353
- // Update CSS size using content box size
354
- this.cssWidth = entry.contentBoxSize[0].inlineSize;
355
- this.cssHeight = entry.contentBoxSize[0].blockSize;
356
-
357
- // Update our drawing buffer size variables, saving the old values for logging
358
- const oldPixelSize = this.getDevicePixelSize();
359
-
360
- // Use the most accurate drawing buffer size information the current browser can provide
361
- // Note: content box sizes are guaranteed to be integers
362
- // Note: Safari falls back to contentBoxSize
363
- const devicePixelWidth =
364
- entry.devicePixelContentBoxSize?.[0].inlineSize ||
365
- entry.contentBoxSize[0].inlineSize * devicePixelRatio;
366
-
367
- const devicePixelHeight =
368
- entry.devicePixelContentBoxSize?.[0].blockSize ||
369
- entry.contentBoxSize[0].blockSize * devicePixelRatio;
370
-
371
- // Make sure we don't overflow the maximum supported texture size
372
- const [maxDevicePixelWidth, maxDevicePixelHeight] = this.getMaxDrawingBufferSize();
373
- this.devicePixelWidth = Math.max(1, Math.min(devicePixelWidth, maxDevicePixelWidth));
374
- this.devicePixelHeight = Math.max(1, Math.min(devicePixelHeight, maxDevicePixelHeight));
375
-
376
- this._updateDrawingBufferSize();
377
-
378
- // Inform the device
379
- this.device.props.onResize(this, {oldPixelSize});
380
- }
381
-
382
- protected _updateDrawingBufferSize() {
383
- // Update the canvas drawing buffer size
384
- if (this.props.autoResize) {
385
- if (typeof this.props.useDevicePixels === 'number') {
386
- const dpr = this.props.useDevicePixels;
387
- this.setDrawingBufferSize(this.cssWidth * dpr, this.cssHeight * dpr);
388
- } else if (this.props.useDevicePixels) {
389
- this.setDrawingBufferSize(this.devicePixelWidth, this.devicePixelHeight);
390
- } else {
391
- this.setDrawingBufferSize(this.cssWidth, this.cssHeight);
392
- }
393
-
394
- // Inform the subclass
395
- this._updateDevice();
396
- }
397
-
398
- // Resolve the initialized promise
399
- this._initializedResolvers.resolve();
400
- this.isInitialized = true;
401
-
402
- this.updatePosition();
403
- }
404
-
405
- /** Monitor DPR changes */
406
- _observeDevicePixelRatio() {
407
- const oldRatio = this.devicePixelRatio;
408
- this.devicePixelRatio = window.devicePixelRatio;
409
-
410
- this.updatePosition();
411
-
412
- // Inform the device
413
- this.device.props.onDevicePixelRatioChange(this, {oldRatio});
414
- // Set up a one time query against the current resolution.
415
- matchMedia(`(resolution: ${this.devicePixelRatio}dppx)`).addEventListener(
416
- 'change',
417
- () => this._observeDevicePixelRatio(),
418
- {once: true}
419
- );
420
- }
421
-
422
- /** Start tracking positions with a timer */
423
- _trackPosition(intervalMs: number = 100): void {
424
- const intervalId = setInterval(() => {
425
- if (this.destroyed) {
426
- clearInterval(intervalId);
427
- } else {
428
- this.updatePosition();
429
- }
430
- }, intervalMs);
431
- }
432
-
433
- /**
434
- * Calculated the absolute position of the canvas
435
- * @note - getBoundingClientRect() is normally cheap but can be expensive
436
- * if called before browser has finished a reflow. Should not be the case here.
437
- */
438
- updatePosition() {
439
- const newRect = this.htmlCanvas?.getBoundingClientRect();
440
- if (newRect) {
441
- // We only track position since we rely on the more precise ResizeObserver for size
442
- const position: [number, number] = [newRect.left, newRect.top];
443
- this._position ??= position;
444
- const positionChanged =
445
- position[0] !== this._position[0] || position[1] !== this._position[1];
446
- if (positionChanged) {
447
- const oldPosition = this._position;
448
- this._position = position;
449
- this.device.props.onPositionChange?.(this, {oldPosition});
450
- }
451
- }
452
- }
453
- }
454
-
455
- // HELPER FUNCTIONS
456
-
457
- /** Get a container element from a string or DOM element */
458
- function getContainer(container: HTMLElement | string | null): HTMLElement {
459
- if (typeof container === 'string') {
460
- const element = document.getElementById(container);
461
- if (!element) {
462
- throw new Error(`${container} is not an HTML element`);
463
- }
464
- return element;
465
- }
466
- if (container) {
467
- return container;
468
- }
469
- return document.body;
470
- }
471
-
472
- /** Get a Canvas element from DOM id */
473
- function getCanvasFromDOM(canvasId: string): HTMLCanvasElement {
474
- const canvas = document.getElementById(canvasId);
475
- if (!CanvasContext.isHTMLCanvas(canvas)) {
476
- throw new Error('Object is not a canvas element');
477
- }
478
- return canvas;
479
- }
480
-
481
- /** Create a new canvas */
482
- function createCanvasElement(props: CanvasContextProps) {
483
- const {width, height} = props;
484
- const newCanvas = document.createElement('canvas');
485
- newCanvas.id = uid('lumagl-auto-created-canvas');
486
- newCanvas.width = width || 1;
487
- newCanvas.height = height || 1;
488
- newCanvas.style.width = Number.isFinite(width) ? `${width}px` : '100%';
489
- newCanvas.style.height = Number.isFinite(height) ? `${height}px` : '100%';
490
- if (!props?.visible) {
491
- newCanvas.style.visibility = 'hidden';
492
- }
493
- // Insert the canvas in the DOM
494
- const container = getContainer(props?.container || null);
495
- container.insertBefore(newCanvas, container.firstChild);
496
-
497
- return newCanvas;
498
- }
5
+ export type {CanvasContextProps, MutableCanvasContextProps} from './canvas-surface';
6
+ import {CanvasSurface} from './canvas-surface';
499
7
 
500
8
  /**
501
- * Scales pixels linearly, handles edge cases
502
- * @param pixel
503
- * @param ratio
504
- * @param width
505
- * @param height
506
- * @param yInvert
507
- * @returns
9
+ * Manages a renderable backend canvas. Supports both HTML or offscreen canvas
10
+ * and returns backend framebuffers sourced from the canvas itself.
508
11
  */
509
- function scalePixels(
510
- pixel: [number, number],
511
- ratio: number,
512
- width: number,
513
- height: number,
514
- yInvert: boolean
515
- ): {
516
- x: number;
517
- y: number;
518
- width: number;
519
- height: number;
520
- } {
521
- const point = pixel;
522
-
523
- const x = scaleX(point[0], ratio, width);
524
- let y = scaleY(point[1], ratio, height, yInvert);
525
-
526
- // Find boundaries of next pixel to provide valid range of device pixel locations
527
-
528
- let t = scaleX(point[0] + 1, ratio, width);
529
- // If next pixel's position is clamped to boundary, use it as is, otherwise subtract 1 for current pixel boundary
530
- const xHigh = t === width - 1 ? t : t - 1;
531
-
532
- t = scaleY(point[1] + 1, ratio, height, yInvert);
533
- let yHigh;
534
- if (yInvert) {
535
- // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range
536
- t = t === 0 ? t : t + 1;
537
- // swap y and yHigh
538
- yHigh = y;
539
- y = t;
540
- } else {
541
- // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range
542
- yHigh = t === height - 1 ? t : t - 1;
543
- // y remains same
544
- }
545
- return {
546
- x,
547
- y,
548
- // when ratio < 1, current css pixel and next css pixel may point to same device pixel, set width/height to 1 in those cases.
549
- width: Math.max(xHigh - x + 1, 1),
550
- height: Math.max(yHigh - y + 1, 1)
551
- };
552
- }
553
-
554
- function scaleX(x: number, ratio: number, width: number): number {
555
- // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit
556
- const r = Math.min(Math.round(x * ratio), width - 1);
557
- return r;
558
- }
12
+ export abstract class CanvasContext extends CanvasSurface {
13
+ static override defaultProps = CanvasSurface.defaultProps;
559
14
 
560
- function scaleY(y: number, ratio: number, height: number, yInvert: boolean): number {
561
- // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit
562
- return yInvert
563
- ? Math.max(0, height - 1 - Math.round(y * ratio))
564
- : Math.min(Math.round(y * ratio), height - 1);
15
+ abstract override readonly handle: unknown;
565
16
  }
@@ -0,0 +1,130 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ type CanvasObserverProps = {
6
+ canvas?: HTMLCanvasElement;
7
+ trackPosition: boolean;
8
+ onResize: (entries: ResizeObserverEntry[]) => void;
9
+ onIntersection: (entries: IntersectionObserverEntry[]) => void;
10
+ onDevicePixelRatioChange: () => void;
11
+ onPositionChange: () => void;
12
+ };
13
+
14
+ /**
15
+ * Internal DOM observer orchestration for HTML canvas surfaces.
16
+ *
17
+ * CanvasSurface owns the tracked state and device callback dispatch. This helper only manages
18
+ * browser observers, timers, and polling loops, then reports events through callbacks.
19
+ */
20
+ export class CanvasObserver {
21
+ readonly props: CanvasObserverProps;
22
+
23
+ private _resizeObserver: ResizeObserver | undefined;
24
+ private _intersectionObserver: IntersectionObserver | undefined;
25
+ private _observeDevicePixelRatioTimeout: ReturnType<typeof setTimeout> | null = null;
26
+ private _observeDevicePixelRatioMediaQuery: MediaQueryList | null = null;
27
+ private readonly _handleDevicePixelRatioChange = () => this._refreshDevicePixelRatio();
28
+ private _trackPositionInterval: ReturnType<typeof setInterval> | null = null;
29
+ private _started = false;
30
+
31
+ get started(): boolean {
32
+ return this._started;
33
+ }
34
+
35
+ constructor(props: CanvasObserverProps) {
36
+ this.props = props;
37
+ }
38
+
39
+ start(): void {
40
+ if (this._started || !this.props.canvas) {
41
+ return;
42
+ }
43
+
44
+ this._started = true;
45
+ this._intersectionObserver ||= new IntersectionObserver(entries =>
46
+ this.props.onIntersection(entries)
47
+ );
48
+ this._resizeObserver ||= new ResizeObserver(entries => this.props.onResize(entries));
49
+
50
+ this._intersectionObserver.observe(this.props.canvas);
51
+ try {
52
+ this._resizeObserver.observe(this.props.canvas, {box: 'device-pixel-content-box'});
53
+ } catch {
54
+ this._resizeObserver.observe(this.props.canvas, {box: 'content-box'});
55
+ }
56
+
57
+ this._observeDevicePixelRatioTimeout = setTimeout(() => this._refreshDevicePixelRatio(), 0);
58
+
59
+ if (this.props.trackPosition) {
60
+ this._trackPosition();
61
+ }
62
+ }
63
+
64
+ stop(): void {
65
+ if (!this._started) {
66
+ return;
67
+ }
68
+
69
+ this._started = false;
70
+
71
+ if (this._observeDevicePixelRatioTimeout) {
72
+ clearTimeout(this._observeDevicePixelRatioTimeout);
73
+ this._observeDevicePixelRatioTimeout = null;
74
+ }
75
+
76
+ if (this._observeDevicePixelRatioMediaQuery) {
77
+ this._observeDevicePixelRatioMediaQuery.removeEventListener(
78
+ 'change',
79
+ this._handleDevicePixelRatioChange
80
+ );
81
+ this._observeDevicePixelRatioMediaQuery = null;
82
+ }
83
+
84
+ if (this._trackPositionInterval) {
85
+ clearInterval(this._trackPositionInterval);
86
+ this._trackPositionInterval = null;
87
+ }
88
+
89
+ this._resizeObserver?.disconnect();
90
+ this._intersectionObserver?.disconnect();
91
+ }
92
+
93
+ private _refreshDevicePixelRatio(): void {
94
+ if (!this._started) {
95
+ return;
96
+ }
97
+
98
+ this.props.onDevicePixelRatioChange();
99
+
100
+ this._observeDevicePixelRatioMediaQuery?.removeEventListener(
101
+ 'change',
102
+ this._handleDevicePixelRatioChange
103
+ );
104
+ this._observeDevicePixelRatioMediaQuery = matchMedia(
105
+ `(resolution: ${window.devicePixelRatio}dppx)`
106
+ );
107
+ this._observeDevicePixelRatioMediaQuery.addEventListener(
108
+ 'change',
109
+ this._handleDevicePixelRatioChange,
110
+ {once: true}
111
+ );
112
+ }
113
+
114
+ private _trackPosition(intervalMs: number = 100): void {
115
+ if (this._trackPositionInterval) {
116
+ return;
117
+ }
118
+
119
+ this._trackPositionInterval = setInterval(() => {
120
+ if (!this._started) {
121
+ if (this._trackPositionInterval) {
122
+ clearInterval(this._trackPositionInterval);
123
+ this._trackPositionInterval = null;
124
+ }
125
+ } else {
126
+ this.props.onPositionChange();
127
+ }
128
+ }, intervalMs);
129
+ }
130
+ }