@luma.gl/webgl 9.0.0-alpha.10 → 9.0.0-alpha.11

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 (45) hide show
  1. package/dist/adapter/webgl-canvas-context.d.ts.map +1 -1
  2. package/dist/adapter/webgl-canvas-context.js +1 -3
  3. package/dist/adapter/webgl-canvas-context.js.map +1 -1
  4. package/dist/adapter/webgl-device.d.ts +33 -27
  5. package/dist/adapter/webgl-device.d.ts.map +1 -1
  6. package/dist/adapter/webgl-device.js +32 -24
  7. package/dist/adapter/webgl-device.js.map +1 -1
  8. package/dist/context/context/context-state.d.ts +0 -5
  9. package/dist/context/context/context-state.d.ts.map +1 -1
  10. package/dist/context/context/context-state.js +0 -5
  11. package/dist/context/context/context-state.js.map +1 -1
  12. package/dist/es5/adapter/webgl-canvas-context.js +1 -4
  13. package/dist/es5/adapter/webgl-canvas-context.js.map +1 -1
  14. package/dist/es5/adapter/webgl-device.js +34 -24
  15. package/dist/es5/adapter/webgl-device.js.map +1 -1
  16. package/dist/es5/context/context/context-state.js +0 -5
  17. package/dist/es5/context/context/context-state.js.map +1 -1
  18. package/dist/es5/index.js +0 -14
  19. package/dist/es5/index.js.map +1 -1
  20. package/dist/esm/adapter/webgl-canvas-context.js +1 -3
  21. package/dist/esm/adapter/webgl-canvas-context.js.map +1 -1
  22. package/dist/esm/adapter/webgl-device.js +32 -24
  23. package/dist/esm/adapter/webgl-device.js.map +1 -1
  24. package/dist/esm/context/context/context-state.js +0 -5
  25. package/dist/esm/context/context/context-state.js.map +1 -1
  26. package/dist/esm/index.js +0 -1
  27. package/dist/esm/index.js.map +1 -1
  28. package/dist/index.d.ts +0 -1
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +0 -1
  31. package/dist/index.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/adapter/webgl-canvas-context.ts +1 -3
  34. package/src/adapter/webgl-device.ts +60 -48
  35. package/src/context/context/context-state.ts +0 -10
  36. package/src/index.ts +0 -3
  37. package/dist/context/context/device-pixels.d.ts +0 -25
  38. package/dist/context/context/device-pixels.d.ts.map +0 -1
  39. package/dist/context/context/device-pixels.js +0 -103
  40. package/dist/context/context/device-pixels.js.map +0 -1
  41. package/dist/es5/context/context/device-pixels.js +0 -121
  42. package/dist/es5/context/context/device-pixels.js.map +0 -1
  43. package/dist/esm/context/context/device-pixels.js +0 -103
  44. package/dist/esm/context/context/device-pixels.js.map +0 -1
  45. package/src/context/context/device-pixels.ts +0 -158
@@ -1,158 +0,0 @@
1
- import {log} from '@luma.gl/api';
2
- import {getContextState} from './context-state';
3
-
4
- /**
5
- * Returns multiplier need to convert CSS size to Device size
6
- */
7
- export function cssToDeviceRatio(gl: WebGLRenderingContext): number {
8
- const state = getContextState(gl);
9
-
10
- if (gl.canvas && state) {
11
- // For headless gl we might have used custom width and height
12
- // hence use cached clientWidth
13
- const {clientWidth} = state._canvasSizeInfo;
14
- return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;
15
- }
16
- // use default device pixel ratio
17
- return 1;
18
- }
19
-
20
- /**
21
- * Maps CSS pixel position to device pixel position
22
- */
23
- export function cssToDevicePixels(
24
- gl: WebGLRenderingContext,
25
- cssPixel: number[],
26
- yInvert: boolean = true
27
- ): {
28
- x: number;
29
- y: number;
30
- width: number;
31
- height: number;
32
- } {
33
- const ratio = cssToDeviceRatio(gl);
34
- const width = gl.drawingBufferWidth;
35
- const height = gl.drawingBufferHeight;
36
- return scalePixels(cssPixel, ratio, width, height, yInvert);
37
- }
38
-
39
- /**
40
- * Calulates device pixel ratio, used during context creation
41
- *
42
- * @param useDevicePixels - boolean or a number
43
- * @return - device pixel ratio
44
- */
45
- export function getDevicePixelRatio(useDevicePixels: boolean | number): number {
46
- const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
47
- if (Number.isFinite(useDevicePixels)) {
48
- // @ts-expect-error Can no longer be boolean after previous line
49
- return useDevicePixels <= 0 ? 1 : useDevicePixels;
50
- }
51
- return useDevicePixels ? windowRatio : 1;
52
- }
53
-
54
- // use devicePixelRatio to set canvas width and height
55
- export function setDevicePixelRatio(gl: WebGLRenderingContext, devicePixelRatio: number, options: {width?: number, height?: number} = {}) {
56
- if (!(gl.canvas instanceof HTMLCanvasElement)) {
57
- return;
58
- }
59
-
60
- const canvas: HTMLCanvasElement = gl.canvas;
61
-
62
- // NOTE: if options.width and options.height not used remove in v8
63
- let clientWidth = 'width' in options ? options.width : canvas.clientWidth;
64
- let clientHeight = 'height' in options ? options.height : canvas.clientHeight;
65
-
66
- if (!clientWidth || !clientHeight) {
67
- log.log(1, 'Canvas clientWidth/clientHeight is 0')();
68
- // by forcing devicePixel ratio to 1, we do not scale canvas.width and height in each frame.
69
- devicePixelRatio = 1;
70
- clientWidth = canvas.width || 1;
71
- clientHeight = canvas.height || 1;
72
- }
73
-
74
- const contextState = getContextState(gl);
75
- const cachedSize = contextState._canvasSizeInfo;
76
- // Check if canvas needs to be resized
77
- if (
78
- cachedSize.clientWidth !== clientWidth ||
79
- cachedSize.clientHeight !== clientHeight ||
80
- cachedSize.devicePixelRatio !== devicePixelRatio
81
- ) {
82
- let clampedPixelRatio = devicePixelRatio;
83
-
84
- const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);
85
- const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);
86
- canvas.width = canvasWidth;
87
- canvas.height = canvasHeight;
88
-
89
- // Note: when devicePixelRatio is too high, it is possible we might hit system limit for
90
- // drawing buffer width and hight, in those cases they get clamped and resulting aspect ration may not be maintained
91
- // for those cases, reduce devicePixelRatio.
92
- if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {
93
- log.warn(`Device pixel ratio clamped`)();
94
- clampedPixelRatio = Math.min(
95
- gl.drawingBufferWidth / clientWidth,
96
- gl.drawingBufferHeight / clientHeight
97
- );
98
-
99
- canvas.width = Math.floor(clientWidth * clampedPixelRatio);
100
- canvas.height = Math.floor(clientHeight * clampedPixelRatio);
101
- }
102
-
103
- Object.assign(contextState._canvasSizeInfo, {clientWidth, clientHeight, devicePixelRatio});
104
- }
105
- }
106
-
107
-
108
- // PRIVATE
109
-
110
- function scalePixels(pixel: number[], ratio: number, width: number, height: number, yInvert: boolean): {
111
- x: number;
112
- y: number;
113
- width: number;
114
- height: number;
115
- } {
116
- const x = scaleX(pixel[0], ratio, width);
117
- let y = scaleY(pixel[1], ratio, height, yInvert);
118
-
119
- // Find boundaries of next pixel to provide valid range of device pixel locaitons
120
-
121
- let t = scaleX(pixel[0] + 1, ratio, width);
122
- // If next pixel's position is clamped to boundary, use it as is, otherwise subtract 1 for current pixel boundary
123
- const xHigh = t === width - 1 ? t : t - 1;
124
-
125
- t = scaleY(pixel[1] + 1, ratio, height, yInvert);
126
- let yHigh;
127
- if (yInvert) {
128
- // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range
129
- t = t === 0 ? t : t + 1;
130
- // swap y and yHigh
131
- yHigh = y;
132
- y = t;
133
- } else {
134
- // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range
135
- yHigh = t === height - 1 ? t : t - 1;
136
- // y remains same
137
- }
138
- return {
139
- x,
140
- y,
141
- // when ratio < 1, current css pixel and next css pixel may point to same device pixel, set width/height to 1 in those cases.
142
- width: Math.max(xHigh - x + 1, 1),
143
- height: Math.max(yHigh - y + 1, 1)
144
- };
145
- }
146
-
147
- function scaleX(x: number, ratio: number, width: number): number {
148
- // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit
149
- const r = Math.min(Math.round(x * ratio), width - 1);
150
- return r;
151
- }
152
-
153
- function scaleY(y: number, ratio: number, height: number, yInvert: boolean): number {
154
- // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit
155
- return yInvert
156
- ? Math.max(0, height - 1 - Math.round(y * ratio))
157
- : Math.min(Math.round(y * ratio), height - 1);
158
- }