@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.
- package/dist/adapter/webgl-canvas-context.d.ts.map +1 -1
- package/dist/adapter/webgl-canvas-context.js +1 -3
- package/dist/adapter/webgl-canvas-context.js.map +1 -1
- package/dist/adapter/webgl-device.d.ts +33 -27
- package/dist/adapter/webgl-device.d.ts.map +1 -1
- package/dist/adapter/webgl-device.js +32 -24
- package/dist/adapter/webgl-device.js.map +1 -1
- package/dist/context/context/context-state.d.ts +0 -5
- package/dist/context/context/context-state.d.ts.map +1 -1
- package/dist/context/context/context-state.js +0 -5
- package/dist/context/context/context-state.js.map +1 -1
- package/dist/es5/adapter/webgl-canvas-context.js +1 -4
- package/dist/es5/adapter/webgl-canvas-context.js.map +1 -1
- package/dist/es5/adapter/webgl-device.js +34 -24
- package/dist/es5/adapter/webgl-device.js.map +1 -1
- package/dist/es5/context/context/context-state.js +0 -5
- package/dist/es5/context/context/context-state.js.map +1 -1
- package/dist/es5/index.js +0 -14
- package/dist/es5/index.js.map +1 -1
- package/dist/esm/adapter/webgl-canvas-context.js +1 -3
- package/dist/esm/adapter/webgl-canvas-context.js.map +1 -1
- package/dist/esm/adapter/webgl-device.js +32 -24
- package/dist/esm/adapter/webgl-device.js.map +1 -1
- package/dist/esm/context/context/context-state.js +0 -5
- package/dist/esm/context/context/context-state.js.map +1 -1
- package/dist/esm/index.js +0 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/adapter/webgl-canvas-context.ts +1 -3
- package/src/adapter/webgl-device.ts +60 -48
- package/src/context/context/context-state.ts +0 -10
- package/src/index.ts +0 -3
- package/dist/context/context/device-pixels.d.ts +0 -25
- package/dist/context/context/device-pixels.d.ts.map +0 -1
- package/dist/context/context/device-pixels.js +0 -103
- package/dist/context/context/device-pixels.js.map +0 -1
- package/dist/es5/context/context/device-pixels.js +0 -121
- package/dist/es5/context/context/device-pixels.js.map +0 -1
- package/dist/esm/context/context/device-pixels.js +0 -103
- package/dist/esm/context/context/device-pixels.js.map +0 -1
- package/src/context/context/device-pixels.ts +0 -158
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luma.gl/webgl",
|
|
3
|
-
"version": "9.0.0-alpha.
|
|
3
|
+
"version": "9.0.0-alpha.11",
|
|
4
4
|
"description": "WebGL2 adapter for the luma.gl API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@babel/runtime": "^7.0.0",
|
|
38
|
-
"@luma.gl/api": "9.0.0-alpha.
|
|
39
|
-
"@luma.gl/constants": "9.0.0-alpha.
|
|
38
|
+
"@luma.gl/api": "9.0.0-alpha.11",
|
|
39
|
+
"@luma.gl/constants": "9.0.0-alpha.11",
|
|
40
40
|
"@probe.gl/env": "^3.5.0"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "3a99f57970a609d50948851aec7a56d56685ab62"
|
|
43
43
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// luma.gl, MIT license
|
|
2
2
|
import type {CanvasContextProps} from '@luma.gl/api';
|
|
3
3
|
import {CanvasContext} from '@luma.gl/api';
|
|
4
|
-
import {getDevicePixelRatio, setDevicePixelRatio} from '../context/context/device-pixels';
|
|
5
4
|
import WebGLDevice from './webgl-device';
|
|
6
5
|
import WEBGLFramebuffer from './resources/webgl-framebuffer';
|
|
7
6
|
|
|
@@ -52,8 +51,7 @@ export default class WebGLCanvasContext extends CanvasContext {
|
|
|
52
51
|
resize(options?: {width?: number; height?: number; useDevicePixels?: boolean | number}): void {
|
|
53
52
|
// Resize browser context .
|
|
54
53
|
if (this.canvas) {
|
|
55
|
-
|
|
56
|
-
setDevicePixelRatio(this.device.gl, devicePixelRatio, options);
|
|
54
|
+
this.setDevicePixelRatio(devicePixelRatio, options);
|
|
57
55
|
return;
|
|
58
56
|
}
|
|
59
57
|
|
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
import {Device, CanvasContext, log, assert} from '@luma.gl/api';
|
|
11
11
|
import {isBrowser} from '@probe.gl/env';
|
|
12
12
|
import {polyfillContext} from '../context/polyfill/polyfill-context';
|
|
13
|
-
import {trackContextState} from '../context/state-tracker/track-context-state';
|
|
13
|
+
import {popContextState, pushContextState, trackContextState} from '../context/state-tracker/track-context-state';
|
|
14
14
|
import {ContextState} from '../context/context/context-state';
|
|
15
15
|
import {createBrowserContext} from '../context/context/create-browser-context';
|
|
16
16
|
import {
|
|
@@ -95,37 +95,6 @@ export default class WebGLDevice extends Device implements ContextState {
|
|
|
95
95
|
private _features?: Set<DeviceFeature>;
|
|
96
96
|
private _limits?: DeviceLimits;
|
|
97
97
|
|
|
98
|
-
//
|
|
99
|
-
// WebGL-only API (not part of `Device` API)
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
/** WebGL1 typed context. Can always be used. */
|
|
103
|
-
readonly gl: WebGLRenderingContext;
|
|
104
|
-
/** WebGL2 typed context. Need to check isWebGL2 or isWebGL1 before using. */
|
|
105
|
-
readonly gl2: WebGL2RenderingContext | null = null;
|
|
106
|
-
readonly debug: boolean = false;
|
|
107
|
-
|
|
108
|
-
/** `true` if this is a WebGL1 context. @note `false` if WebGL2 */
|
|
109
|
-
readonly isWebGL1: boolean;
|
|
110
|
-
/** `true` if this is a WebGL2 context. @note `false` if WebGL1 */
|
|
111
|
-
readonly isWebGL2: boolean;
|
|
112
|
-
|
|
113
|
-
get webglLimits(): WebGLLimits {
|
|
114
|
-
this._webglLimits = this._webglLimits || getWebGLLimits(this.gl);
|
|
115
|
-
return this._webglLimits;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private _webglLimits?: WebGLLimits;
|
|
119
|
-
|
|
120
|
-
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
121
|
-
readonly _canvasSizeInfo = {clientWidth: 0, clientHeight: 0, devicePixelRatio: 1};
|
|
122
|
-
/** State used by luma.gl classes */
|
|
123
|
-
readonly _extensions: Record<string, any> = {};
|
|
124
|
-
_polyfilled: boolean = false;
|
|
125
|
-
|
|
126
|
-
/** Instance of Spector.js (if initialized) */
|
|
127
|
-
spector;
|
|
128
|
-
|
|
129
98
|
//
|
|
130
99
|
// Static methods, expected to be present by `luma.createDevice()`
|
|
131
100
|
//
|
|
@@ -277,22 +246,6 @@ ${this.info.vendor}, ${this.info.renderer} for canvas: ${this.canvasContext.id}`
|
|
|
277
246
|
}
|
|
278
247
|
}
|
|
279
248
|
|
|
280
|
-
/**
|
|
281
|
-
* Loses the context
|
|
282
|
-
* @note Triggers context loss, mainly for testing
|
|
283
|
-
*/
|
|
284
|
-
loseDevice() {
|
|
285
|
-
const ext = this.gl.getExtension('WEBGL_lose_context');
|
|
286
|
-
if (ext) {
|
|
287
|
-
ext.loseContext();
|
|
288
|
-
}
|
|
289
|
-
// loseContext should trigger context loss callback but
|
|
290
|
-
this._resolveContextLost?.({
|
|
291
|
-
reason: 'destroyed',
|
|
292
|
-
message: 'Application triggered context loss'
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
|
|
296
249
|
get isLost(): boolean {
|
|
297
250
|
return this.gl.isContextLost();
|
|
298
251
|
}
|
|
@@ -390,6 +343,65 @@ ${this.info.vendor}, ${this.info.renderer} for canvas: ${this.canvasContext.id}`
|
|
|
390
343
|
this.renderPass = null;
|
|
391
344
|
// this.canvasContext.commit();
|
|
392
345
|
}
|
|
346
|
+
|
|
347
|
+
//
|
|
348
|
+
// WebGL-only API (not part of `Device` API)
|
|
349
|
+
//
|
|
350
|
+
|
|
351
|
+
/** WebGL1 typed context. Can always be used. */
|
|
352
|
+
readonly gl: WebGLRenderingContext;
|
|
353
|
+
/** WebGL2 typed context. Need to check isWebGL2 or isWebGL1 before using. */
|
|
354
|
+
readonly gl2: WebGL2RenderingContext | null = null;
|
|
355
|
+
readonly debug: boolean = false;
|
|
356
|
+
|
|
357
|
+
/** `true` if this is a WebGL1 context. @note `false` if WebGL2 */
|
|
358
|
+
readonly isWebGL1: boolean;
|
|
359
|
+
/** `true` if this is a WebGL2 context. @note `false` if WebGL1 */
|
|
360
|
+
readonly isWebGL2: boolean;
|
|
361
|
+
|
|
362
|
+
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
363
|
+
readonly _canvasSizeInfo = {clientWidth: 0, clientHeight: 0, devicePixelRatio: 1};
|
|
364
|
+
/** State used by luma.gl classes */
|
|
365
|
+
readonly _extensions: Record<string, any> = {};
|
|
366
|
+
_polyfilled: boolean = false;
|
|
367
|
+
|
|
368
|
+
/** Instance of Spector.js (if initialized) */
|
|
369
|
+
spector;
|
|
370
|
+
|
|
371
|
+
private _webglLimits?: WebGLLimits;
|
|
372
|
+
|
|
373
|
+
/** Return WebGL specific limits */
|
|
374
|
+
get webglLimits() : WebGLLimits {
|
|
375
|
+
this._webglLimits = this._webglLimits || getWebGLLimits(this.gl);
|
|
376
|
+
return this._webglLimits;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Loses the context
|
|
381
|
+
* @note Triggers context loss, mainly for testing
|
|
382
|
+
* @todo Promote to `Device` API?
|
|
383
|
+
*/
|
|
384
|
+
loseDevice(): void {
|
|
385
|
+
const ext = this.gl.getExtension('WEBGL_lose_context');
|
|
386
|
+
if (ext) {
|
|
387
|
+
ext.loseContext();
|
|
388
|
+
}
|
|
389
|
+
// loseContext should trigger context loss callback but
|
|
390
|
+
this._resolveContextLost?.({
|
|
391
|
+
reason: 'destroyed',
|
|
392
|
+
message: 'Application triggered context loss'
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Save current WebGL context state onto an internal stack */
|
|
397
|
+
pushState(): void {
|
|
398
|
+
pushContextState(this.gl);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Restores previously saved context state */
|
|
402
|
+
popState(): void {
|
|
403
|
+
popContextState(this.gl);
|
|
404
|
+
}
|
|
393
405
|
}
|
|
394
406
|
|
|
395
407
|
/** Check if supplied parameter is a WebGLRenderingContext */
|
|
@@ -2,11 +2,6 @@
|
|
|
2
2
|
* Stores luma.gl specific state associated with a context
|
|
3
3
|
*/
|
|
4
4
|
export interface ContextState {
|
|
5
|
-
_canvasSizeInfo: {
|
|
6
|
-
clientWidth: number;
|
|
7
|
-
clientHeight: number;
|
|
8
|
-
devicePixelRatio: number;
|
|
9
|
-
};
|
|
10
5
|
_polyfilled: boolean;
|
|
11
6
|
_extensions: Record<string, any>;
|
|
12
7
|
}
|
|
@@ -23,11 +18,6 @@ export function getContextState(gl: WebGLRenderingContext): ContextState {
|
|
|
23
18
|
}
|
|
24
19
|
if (!luma) {
|
|
25
20
|
const contextState: ContextState = {
|
|
26
|
-
_canvasSizeInfo: {
|
|
27
|
-
clientWidth: 0,
|
|
28
|
-
clientHeight: 0,
|
|
29
|
-
devicePixelRatio: 1,
|
|
30
|
-
},
|
|
31
21
|
_polyfilled: false,
|
|
32
22
|
_extensions: {}
|
|
33
23
|
};
|
package/src/index.ts
CHANGED
|
@@ -51,9 +51,6 @@ export {
|
|
|
51
51
|
assertWebGL2Context
|
|
52
52
|
} from './context/context/webgl-checks';
|
|
53
53
|
|
|
54
|
-
// Device ratio
|
|
55
|
-
export {cssToDeviceRatio, cssToDevicePixels} from './context/context/device-pixels';
|
|
56
|
-
|
|
57
54
|
// Unified parameter API
|
|
58
55
|
|
|
59
56
|
export {setDeviceParameters, withDeviceParameters} from './adapter/converters/device-parameters';
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns multiplier need to convert CSS size to Device size
|
|
3
|
-
*/
|
|
4
|
-
export declare function cssToDeviceRatio(gl: WebGLRenderingContext): number;
|
|
5
|
-
/**
|
|
6
|
-
* Maps CSS pixel position to device pixel position
|
|
7
|
-
*/
|
|
8
|
-
export declare function cssToDevicePixels(gl: WebGLRenderingContext, cssPixel: number[], yInvert?: boolean): {
|
|
9
|
-
x: number;
|
|
10
|
-
y: number;
|
|
11
|
-
width: number;
|
|
12
|
-
height: number;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Calulates device pixel ratio, used during context creation
|
|
16
|
-
*
|
|
17
|
-
* @param useDevicePixels - boolean or a number
|
|
18
|
-
* @return - device pixel ratio
|
|
19
|
-
*/
|
|
20
|
-
export declare function getDevicePixelRatio(useDevicePixels: boolean | number): number;
|
|
21
|
-
export declare function setDevicePixelRatio(gl: WebGLRenderingContext, devicePixelRatio: number, options?: {
|
|
22
|
-
width?: number;
|
|
23
|
-
height?: number;
|
|
24
|
-
}): void;
|
|
25
|
-
//# sourceMappingURL=device-pixels.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"device-pixels.d.ts","sourceRoot":"","sources":["../../../src/context/context/device-pixels.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,qBAAqB,GAAG,MAAM,CAWlE;AAED;;GAEG;AACF,wBAAgB,iBAAiB,CAChC,EAAE,EAAE,qBAAqB,EACzB,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,GAAE,OAAc,GACtB;IACD,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB,CAKA;AAED;;;;;GAKG;AACF,wBAAgB,mBAAmB,CAAC,eAAe,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAO9E;AAGD,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,EAAE,OAAO,GAAE;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAM,QAkDvI"}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { log } from '@luma.gl/api';
|
|
2
|
-
import { getContextState } from './context-state';
|
|
3
|
-
export function cssToDeviceRatio(gl) {
|
|
4
|
-
const state = getContextState(gl);
|
|
5
|
-
|
|
6
|
-
if (gl.canvas && state) {
|
|
7
|
-
const {
|
|
8
|
-
clientWidth
|
|
9
|
-
} = state._canvasSizeInfo;
|
|
10
|
-
return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return 1;
|
|
14
|
-
}
|
|
15
|
-
export function cssToDevicePixels(gl, cssPixel, yInvert = true) {
|
|
16
|
-
const ratio = cssToDeviceRatio(gl);
|
|
17
|
-
const width = gl.drawingBufferWidth;
|
|
18
|
-
const height = gl.drawingBufferHeight;
|
|
19
|
-
return scalePixels(cssPixel, ratio, width, height, yInvert);
|
|
20
|
-
}
|
|
21
|
-
export function getDevicePixelRatio(useDevicePixels) {
|
|
22
|
-
const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
23
|
-
|
|
24
|
-
if (Number.isFinite(useDevicePixels)) {
|
|
25
|
-
return useDevicePixels <= 0 ? 1 : useDevicePixels;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return useDevicePixels ? windowRatio : 1;
|
|
29
|
-
}
|
|
30
|
-
export function setDevicePixelRatio(gl, devicePixelRatio, options = {}) {
|
|
31
|
-
if (!(gl.canvas instanceof HTMLCanvasElement)) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const canvas = gl.canvas;
|
|
36
|
-
let clientWidth = 'width' in options ? options.width : canvas.clientWidth;
|
|
37
|
-
let clientHeight = 'height' in options ? options.height : canvas.clientHeight;
|
|
38
|
-
|
|
39
|
-
if (!clientWidth || !clientHeight) {
|
|
40
|
-
log.log(1, 'Canvas clientWidth/clientHeight is 0')();
|
|
41
|
-
devicePixelRatio = 1;
|
|
42
|
-
clientWidth = canvas.width || 1;
|
|
43
|
-
clientHeight = canvas.height || 1;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const contextState = getContextState(gl);
|
|
47
|
-
const cachedSize = contextState._canvasSizeInfo;
|
|
48
|
-
|
|
49
|
-
if (cachedSize.clientWidth !== clientWidth || cachedSize.clientHeight !== clientHeight || cachedSize.devicePixelRatio !== devicePixelRatio) {
|
|
50
|
-
let clampedPixelRatio = devicePixelRatio;
|
|
51
|
-
const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);
|
|
52
|
-
const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);
|
|
53
|
-
canvas.width = canvasWidth;
|
|
54
|
-
canvas.height = canvasHeight;
|
|
55
|
-
|
|
56
|
-
if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {
|
|
57
|
-
log.warn("Device pixel ratio clamped")();
|
|
58
|
-
clampedPixelRatio = Math.min(gl.drawingBufferWidth / clientWidth, gl.drawingBufferHeight / clientHeight);
|
|
59
|
-
canvas.width = Math.floor(clientWidth * clampedPixelRatio);
|
|
60
|
-
canvas.height = Math.floor(clientHeight * clampedPixelRatio);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
Object.assign(contextState._canvasSizeInfo, {
|
|
64
|
-
clientWidth,
|
|
65
|
-
clientHeight,
|
|
66
|
-
devicePixelRatio
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function scalePixels(pixel, ratio, width, height, yInvert) {
|
|
72
|
-
const x = scaleX(pixel[0], ratio, width);
|
|
73
|
-
let y = scaleY(pixel[1], ratio, height, yInvert);
|
|
74
|
-
let t = scaleX(pixel[0] + 1, ratio, width);
|
|
75
|
-
const xHigh = t === width - 1 ? t : t - 1;
|
|
76
|
-
t = scaleY(pixel[1] + 1, ratio, height, yInvert);
|
|
77
|
-
let yHigh;
|
|
78
|
-
|
|
79
|
-
if (yInvert) {
|
|
80
|
-
t = t === 0 ? t : t + 1;
|
|
81
|
-
yHigh = y;
|
|
82
|
-
y = t;
|
|
83
|
-
} else {
|
|
84
|
-
yHigh = t === height - 1 ? t : t - 1;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
x,
|
|
89
|
-
y,
|
|
90
|
-
width: Math.max(xHigh - x + 1, 1),
|
|
91
|
-
height: Math.max(yHigh - y + 1, 1)
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function scaleX(x, ratio, width) {
|
|
96
|
-
const r = Math.min(Math.round(x * ratio), width - 1);
|
|
97
|
-
return r;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function scaleY(y, ratio, height, yInvert) {
|
|
101
|
-
return yInvert ? Math.max(0, height - 1 - Math.round(y * ratio)) : Math.min(Math.round(y * ratio), height - 1);
|
|
102
|
-
}
|
|
103
|
-
//# sourceMappingURL=device-pixels.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/context/context/device-pixels.ts"],"names":["log","getContextState","cssToDeviceRatio","gl","state","canvas","clientWidth","_canvasSizeInfo","drawingBufferWidth","cssToDevicePixels","cssPixel","yInvert","ratio","width","height","drawingBufferHeight","scalePixels","getDevicePixelRatio","useDevicePixels","windowRatio","window","devicePixelRatio","Number","isFinite","setDevicePixelRatio","options","HTMLCanvasElement","clientHeight","contextState","cachedSize","clampedPixelRatio","canvasWidth","Math","floor","canvasHeight","warn","min","Object","assign","pixel","x","scaleX","y","scaleY","t","xHigh","yHigh","max","r","round"],"mappings":"AAAA,SAAQA,GAAR,QAAkB,cAAlB;AACA,SAAQC,eAAR,QAA8B,iBAA9B;AAKA,OAAO,SAASC,gBAAT,CAA0BC,EAA1B,EAA6D;AAClE,QAAMC,KAAK,GAAGH,eAA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AAmB8B,CAAnB,GAAuBA,CAAC,GAAG,CAAnC;AAED;;AACD,SAAO;AACLJ,IAAAA,CADK;AAELE,IAAAA,CAFK;AAIL7B,IAAAA,KAAK,EAAEmB,IAAI,CAACe,GAAL,CAASF,KAAK,GAAGL,CAAR,GAAY,CAArB,EAAwB,CAAxB,CAJF;AAKL1B,IAAAA,MAAM,EAAEkB,IAAI,CAACe,GAAL,CAASD,KAAK,GAAGJ,CAAR,GAAY,CAArB,EAAwB,CAAxB;AALH,GAAP;AAOD;;AAED,SAASD,MAAT,CAAgBD,CAAhB,EAA2B5B,KAA3B,EAA0CC,KAA1C,EAAiE;AAE/D,QAAMmC,CAAC,GAAGhB,IAAI,CAACI,GAAL,CAASJ,IAAI,CAACiB,KAAL,CAAWT,CAAC,GAAG5B,KAAf,CAAT,EAAgCC,KAAK,GAAG,CAAxC,CAAV;AACA,SAAOmC,CAAP;AACD;;AAED,SAASL,MAAT,CAAgBD,CAAhB,EAA2B9B,KAA3B,EAA0CE,MAA1C,EAA0DH,OAA1D,EAAoF;AAElF,SAAOA,OAAO,GACVqB,IAAI,CAACe,GAAL,CAAS,CAAT,EAAYjC,MAAM,GAAG,CAAT,GAAakB,IAAI,CAACiB,KAAL,CAAWP,CAAC,GAAG9B,KAAf,CAAzB,CADU,GAEVoB,IAAI,CAACI,GAAL,CAASJ,IAAI,CAACiB,KAAL,CAAWP,CAAC,GAAG9B,KAAf,CAAT,EAAgCE,MAAM,GAAG,CAAzC,CAFJ;AAGD","sourcesContent":["import {log} from '@luma.gl/api';\nimport {getContextState} from './context-state';\n\n/**\n * Returns multiplier need to convert CSS size to Device size\n */\nexport function cssToDeviceRatio(gl: WebGLRenderingContext): number {\n const state = getContextState(gl);\n\n if (gl.canvas && state) {\n // For headless gl we might have used custom width and height\n // hence use cached clientWidth\n const {clientWidth} = state._canvasSizeInfo;\n return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;\n }\n // use default device pixel ratio\n return 1;\n}\n\n/**\n * Maps CSS pixel position to device pixel position\n */\n export function cssToDevicePixels(\n gl: WebGLRenderingContext,\n cssPixel: number[],\n yInvert: boolean = true\n): {\n x: number;\n y: number;\n width: number;\n height: number;\n} {\n const ratio = cssToDeviceRatio(gl);\n const width = gl.drawingBufferWidth;\n const height = gl.drawingBufferHeight;\n return scalePixels(cssPixel, ratio, width, height, yInvert);\n}\n\n/**\n * Calulates device pixel ratio, used during context creation\n *\n * @param useDevicePixels - boolean or a number\n * @return - device pixel ratio\n */\n export function getDevicePixelRatio(useDevicePixels: boolean | number): number {\n const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;\n if (Number.isFinite(useDevicePixels)) {\n // @ts-expect-error Can no longer be boolean after previous line\n return useDevicePixels <= 0 ? 1 : useDevicePixels;\n }\n return useDevicePixels ? windowRatio : 1;\n}\n\n// use devicePixelRatio to set canvas width and height\nexport function setDevicePixelRatio(gl: WebGLRenderingContext, devicePixelRatio: number, options: {width?: number, height?: number} = {}) {\n if (!(gl.canvas instanceof HTMLCanvasElement)) {\n return;\n }\n\n const canvas: HTMLCanvasElement = gl.canvas;\n\n // NOTE: if options.width and options.height not used remove in v8\n let clientWidth = 'width' in options ? options.width : canvas.clientWidth;\n let clientHeight = 'height' in options ? options.height : canvas.clientHeight;\n\n if (!clientWidth || !clientHeight) {\n log.log(1, 'Canvas clientWidth/clientHeight is 0')();\n // by forcing devicePixel ratio to 1, we do not scale canvas.width and height in each frame.\n devicePixelRatio = 1;\n clientWidth = canvas.width || 1;\n clientHeight = canvas.height || 1;\n }\n\n const contextState = getContextState(gl);\n const cachedSize = contextState._canvasSizeInfo;\n // Check if canvas needs to be resized\n if (\n cachedSize.clientWidth !== clientWidth ||\n cachedSize.clientHeight !== clientHeight ||\n cachedSize.devicePixelRatio !== devicePixelRatio\n ) {\n let clampedPixelRatio = devicePixelRatio;\n\n const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);\n const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);\n canvas.width = canvasWidth;\n canvas.height = canvasHeight;\n\n // Note: when devicePixelRatio is too high, it is possible we might hit system limit for\n // drawing buffer width and hight, in those cases they get clamped and resulting aspect ration may not be maintained\n // for those cases, reduce devicePixelRatio.\n if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {\n log.warn(`Device pixel ratio clamped`)();\n clampedPixelRatio = Math.min(\n gl.drawingBufferWidth / clientWidth,\n gl.drawingBufferHeight / clientHeight\n );\n\n canvas.width = Math.floor(clientWidth * clampedPixelRatio);\n canvas.height = Math.floor(clientHeight * clampedPixelRatio);\n }\n\n Object.assign(contextState._canvasSizeInfo, {clientWidth, clientHeight, devicePixelRatio});\n }\n}\n\n\n// PRIVATE\n\nfunction scalePixels(pixel: number[], ratio: number, width: number, height: number, yInvert: boolean): {\n x: number;\n y: number;\n width: number;\n height: number;\n} {\n const x = scaleX(pixel[0], ratio, width);\n let y = scaleY(pixel[1], ratio, height, yInvert);\n\n // Find boundaries of next pixel to provide valid range of device pixel locaitons\n\n let t = scaleX(pixel[0] + 1, ratio, width);\n // If next pixel's position is clamped to boundary, use it as is, otherwise subtract 1 for current pixel boundary\n const xHigh = t === width - 1 ? t : t - 1;\n\n t = scaleY(pixel[1] + 1, ratio, height, yInvert);\n let yHigh;\n if (yInvert) {\n // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range\n t = t === 0 ? t : t + 1;\n // swap y and yHigh\n yHigh = y;\n y = t;\n } else {\n // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range\n yHigh = t === height - 1 ? t : t - 1;\n // y remains same\n }\n return {\n x,\n y,\n // when ratio < 1, current css pixel and next css pixel may point to same device pixel, set width/height to 1 in those cases.\n width: Math.max(xHigh - x + 1, 1),\n height: Math.max(yHigh - y + 1, 1)\n };\n}\n\nfunction scaleX(x: number, ratio: number, width: number): number {\n // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit\n const r = Math.min(Math.round(x * ratio), width - 1);\n return r;\n}\n\nfunction scaleY(y: number, ratio: number, height: number, yInvert: boolean): number {\n // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit\n return yInvert\n ? Math.max(0, height - 1 - Math.round(y * ratio))\n : Math.min(Math.round(y * ratio), height - 1);\n}\n"],"file":"device-pixels.js"}
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.cssToDeviceRatio = cssToDeviceRatio;
|
|
7
|
-
exports.cssToDevicePixels = cssToDevicePixels;
|
|
8
|
-
exports.getDevicePixelRatio = getDevicePixelRatio;
|
|
9
|
-
exports.setDevicePixelRatio = setDevicePixelRatio;
|
|
10
|
-
|
|
11
|
-
var _api = require("@luma.gl/api");
|
|
12
|
-
|
|
13
|
-
var _contextState = require("./context-state");
|
|
14
|
-
|
|
15
|
-
function cssToDeviceRatio(gl) {
|
|
16
|
-
var state = (0, _contextState.getContextState)(gl);
|
|
17
|
-
|
|
18
|
-
if (gl.canvas && state) {
|
|
19
|
-
var clientWidth = state._canvasSizeInfo.clientWidth;
|
|
20
|
-
return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return 1;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function cssToDevicePixels(gl, cssPixel) {
|
|
27
|
-
var yInvert = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
28
|
-
var ratio = cssToDeviceRatio(gl);
|
|
29
|
-
var width = gl.drawingBufferWidth;
|
|
30
|
-
var height = gl.drawingBufferHeight;
|
|
31
|
-
return scalePixels(cssPixel, ratio, width, height, yInvert);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function getDevicePixelRatio(useDevicePixels) {
|
|
35
|
-
var windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
36
|
-
|
|
37
|
-
if (Number.isFinite(useDevicePixels)) {
|
|
38
|
-
return useDevicePixels <= 0 ? 1 : useDevicePixels;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return useDevicePixels ? windowRatio : 1;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function setDevicePixelRatio(gl, devicePixelRatio) {
|
|
45
|
-
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
46
|
-
|
|
47
|
-
if (!(gl.canvas instanceof HTMLCanvasElement)) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
var canvas = gl.canvas;
|
|
52
|
-
var clientWidth = 'width' in options ? options.width : canvas.clientWidth;
|
|
53
|
-
var clientHeight = 'height' in options ? options.height : canvas.clientHeight;
|
|
54
|
-
|
|
55
|
-
if (!clientWidth || !clientHeight) {
|
|
56
|
-
_api.log.log(1, 'Canvas clientWidth/clientHeight is 0')();
|
|
57
|
-
|
|
58
|
-
devicePixelRatio = 1;
|
|
59
|
-
clientWidth = canvas.width || 1;
|
|
60
|
-
clientHeight = canvas.height || 1;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
var contextState = (0, _contextState.getContextState)(gl);
|
|
64
|
-
var cachedSize = contextState._canvasSizeInfo;
|
|
65
|
-
|
|
66
|
-
if (cachedSize.clientWidth !== clientWidth || cachedSize.clientHeight !== clientHeight || cachedSize.devicePixelRatio !== devicePixelRatio) {
|
|
67
|
-
var clampedPixelRatio = devicePixelRatio;
|
|
68
|
-
var canvasWidth = Math.floor(clientWidth * clampedPixelRatio);
|
|
69
|
-
var canvasHeight = Math.floor(clientHeight * clampedPixelRatio);
|
|
70
|
-
canvas.width = canvasWidth;
|
|
71
|
-
canvas.height = canvasHeight;
|
|
72
|
-
|
|
73
|
-
if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {
|
|
74
|
-
_api.log.warn("Device pixel ratio clamped")();
|
|
75
|
-
|
|
76
|
-
clampedPixelRatio = Math.min(gl.drawingBufferWidth / clientWidth, gl.drawingBufferHeight / clientHeight);
|
|
77
|
-
canvas.width = Math.floor(clientWidth * clampedPixelRatio);
|
|
78
|
-
canvas.height = Math.floor(clientHeight * clampedPixelRatio);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
Object.assign(contextState._canvasSizeInfo, {
|
|
82
|
-
clientWidth: clientWidth,
|
|
83
|
-
clientHeight: clientHeight,
|
|
84
|
-
devicePixelRatio: devicePixelRatio
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function scalePixels(pixel, ratio, width, height, yInvert) {
|
|
90
|
-
var x = scaleX(pixel[0], ratio, width);
|
|
91
|
-
var y = scaleY(pixel[1], ratio, height, yInvert);
|
|
92
|
-
var t = scaleX(pixel[0] + 1, ratio, width);
|
|
93
|
-
var xHigh = t === width - 1 ? t : t - 1;
|
|
94
|
-
t = scaleY(pixel[1] + 1, ratio, height, yInvert);
|
|
95
|
-
var yHigh;
|
|
96
|
-
|
|
97
|
-
if (yInvert) {
|
|
98
|
-
t = t === 0 ? t : t + 1;
|
|
99
|
-
yHigh = y;
|
|
100
|
-
y = t;
|
|
101
|
-
} else {
|
|
102
|
-
yHigh = t === height - 1 ? t : t - 1;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return {
|
|
106
|
-
x: x,
|
|
107
|
-
y: y,
|
|
108
|
-
width: Math.max(xHigh - x + 1, 1),
|
|
109
|
-
height: Math.max(yHigh - y + 1, 1)
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function scaleX(x, ratio, width) {
|
|
114
|
-
var r = Math.min(Math.round(x * ratio), width - 1);
|
|
115
|
-
return r;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function scaleY(y, ratio, height, yInvert) {
|
|
119
|
-
return yInvert ? Math.max(0, height - 1 - Math.round(y * ratio)) : Math.min(Math.round(y * ratio), height - 1);
|
|
120
|
-
}
|
|
121
|
-
//# sourceMappingURL=device-pixels.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/context/context/device-pixels.ts"],"names":["cssToDeviceRatio","gl","state","canvas","clientWidth","_canvasSizeInfo","drawingBufferWidth","cssToDevicePixels","cssPixel","yInvert","ratio","width","height","drawingBufferHeight","scalePixels","getDevicePixelRatio","useDevicePixels","windowRatio","window","devicePixelRatio","Number","isFinite","setDevicePixelRatio","options","HTMLCanvasElement","clientHeight","log","contextState","cachedSize","clampedPixelRatio","canvasWidth","Math","floor","canvasHeight","warn","min","Object","assign","pixel","x","scaleX","y","scaleY","t","xHigh","yHigh","max","r","round"],"mappings":";;;;;;;;;;AAAA;;AACA;;AAKO,SAASA,gBAAT,CAA0BC,EAA1B,EAA6D;AAClE,MAAMC,KAAK,GAAG,mCAAgBD,EAAhB,CAAd;;AAEA,MAAIA,EAAE,CAACE,MAAH,IAAaD,KAAjB,EAAwB;AAGtB,QAAOE,WAAP,GAAsBF,KAAK,CAACG,eAA5B,CAAOD,WAAP;AACA,WAAOA,WAAW,GAAGH,EAAE,CAACK,kBAAH,GAAwBF,WAA3B,GAAyC,CAA3D;AACD;;AAED,SAAO,CAAP;AACD;;AAKO,SAASG,iBAAT,CACNN,EADM,EAENO,QAFM,EASN;AAAA,MANAC,OAMA,uEANmB,IAMnB;AACA,MAAMC,KAAK,GAAGV,gBAAgB,CAACC,EAAD,CAA9B;AACA,MAAMU,KAAK,GAAGV,EAAE,CAACK,kBAAjB;AACA,MAAMM,MAAM,GAAGX,EAAE,CAACY,mBAAlB;AACA,SAAOC,WAAW,CAACN,QAAD,EAAWE,KAAX,EAAkBC,KAAlB,EAAyBC,MAAzB,EAAiCH,OAAjC,CAAlB;AACD;;AAQO,SAASM,mBAAT,CAA6BC,eAA7B,EAAwE;AAC9E,MAAMC,WAAW,GAAG,OAAOC,MAAP,KAAkB,WAAlB,GAAgC,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,EAA0DH,OAA1D,EAAoF;AAElF,SAAOA,OAAO,GACVsB,IAAI,CAACe,GAAL,CAAS,CAAT,EAAYlC,MAAM,GAAG,CAAT,GAAamB,IAAI,CAACiB,KAAL,CAAWP,CAAC,GAAG/B,KAAf,CAAzB,CADU,GAEVqB,IAAI,CAACI,GAAL,CAASJ,IAAI,CAACiB,KAAL,CAAWP,CAAC,GAAG/B,KAAf,CAAT,EAAgCE,MAAM,GAAG,CAAzC,CAFJ;AAGD","sourcesContent":["import {log} from '@luma.gl/api';\nimport {getContextState} from './context-state';\n\n/**\n * Returns multiplier need to convert CSS size to Device size\n */\nexport function cssToDeviceRatio(gl: WebGLRenderingContext): number {\n const state = getContextState(gl);\n\n if (gl.canvas && state) {\n // For headless gl we might have used custom width and height\n // hence use cached clientWidth\n const {clientWidth} = state._canvasSizeInfo;\n return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;\n }\n // use default device pixel ratio\n return 1;\n}\n\n/**\n * Maps CSS pixel position to device pixel position\n */\n export function cssToDevicePixels(\n gl: WebGLRenderingContext,\n cssPixel: number[],\n yInvert: boolean = true\n): {\n x: number;\n y: number;\n width: number;\n height: number;\n} {\n const ratio = cssToDeviceRatio(gl);\n const width = gl.drawingBufferWidth;\n const height = gl.drawingBufferHeight;\n return scalePixels(cssPixel, ratio, width, height, yInvert);\n}\n\n/**\n * Calulates device pixel ratio, used during context creation\n *\n * @param useDevicePixels - boolean or a number\n * @return - device pixel ratio\n */\n export function getDevicePixelRatio(useDevicePixels: boolean | number): number {\n const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;\n if (Number.isFinite(useDevicePixels)) {\n // @ts-expect-error Can no longer be boolean after previous line\n return useDevicePixels <= 0 ? 1 : useDevicePixels;\n }\n return useDevicePixels ? windowRatio : 1;\n}\n\n// use devicePixelRatio to set canvas width and height\nexport function setDevicePixelRatio(gl: WebGLRenderingContext, devicePixelRatio: number, options: {width?: number, height?: number} = {}) {\n if (!(gl.canvas instanceof HTMLCanvasElement)) {\n return;\n }\n\n const canvas: HTMLCanvasElement = gl.canvas;\n\n // NOTE: if options.width and options.height not used remove in v8\n let clientWidth = 'width' in options ? options.width : canvas.clientWidth;\n let clientHeight = 'height' in options ? options.height : canvas.clientHeight;\n\n if (!clientWidth || !clientHeight) {\n log.log(1, 'Canvas clientWidth/clientHeight is 0')();\n // by forcing devicePixel ratio to 1, we do not scale canvas.width and height in each frame.\n devicePixelRatio = 1;\n clientWidth = canvas.width || 1;\n clientHeight = canvas.height || 1;\n }\n\n const contextState = getContextState(gl);\n const cachedSize = contextState._canvasSizeInfo;\n // Check if canvas needs to be resized\n if (\n cachedSize.clientWidth !== clientWidth ||\n cachedSize.clientHeight !== clientHeight ||\n cachedSize.devicePixelRatio !== devicePixelRatio\n ) {\n let clampedPixelRatio = devicePixelRatio;\n\n const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);\n const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);\n canvas.width = canvasWidth;\n canvas.height = canvasHeight;\n\n // Note: when devicePixelRatio is too high, it is possible we might hit system limit for\n // drawing buffer width and hight, in those cases they get clamped and resulting aspect ration may not be maintained\n // for those cases, reduce devicePixelRatio.\n if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {\n log.warn(`Device pixel ratio clamped`)();\n clampedPixelRatio = Math.min(\n gl.drawingBufferWidth / clientWidth,\n gl.drawingBufferHeight / clientHeight\n );\n\n canvas.width = Math.floor(clientWidth * clampedPixelRatio);\n canvas.height = Math.floor(clientHeight * clampedPixelRatio);\n }\n\n Object.assign(contextState._canvasSizeInfo, {clientWidth, clientHeight, devicePixelRatio});\n }\n}\n\n\n// PRIVATE\n\nfunction scalePixels(pixel: number[], ratio: number, width: number, height: number, yInvert: boolean): {\n x: number;\n y: number;\n width: number;\n height: number;\n} {\n const x = scaleX(pixel[0], ratio, width);\n let y = scaleY(pixel[1], ratio, height, yInvert);\n\n // Find boundaries of next pixel to provide valid range of device pixel locaitons\n\n let t = scaleX(pixel[0] + 1, ratio, width);\n // If next pixel's position is clamped to boundary, use it as is, otherwise subtract 1 for current pixel boundary\n const xHigh = t === width - 1 ? t : t - 1;\n\n t = scaleY(pixel[1] + 1, ratio, height, yInvert);\n let yHigh;\n if (yInvert) {\n // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range\n t = t === 0 ? t : t + 1;\n // swap y and yHigh\n yHigh = y;\n y = t;\n } else {\n // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range\n yHigh = t === height - 1 ? t : t - 1;\n // y remains same\n }\n return {\n x,\n y,\n // when ratio < 1, current css pixel and next css pixel may point to same device pixel, set width/height to 1 in those cases.\n width: Math.max(xHigh - x + 1, 1),\n height: Math.max(yHigh - y + 1, 1)\n };\n}\n\nfunction scaleX(x: number, ratio: number, width: number): number {\n // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit\n const r = Math.min(Math.round(x * ratio), width - 1);\n return r;\n}\n\nfunction scaleY(y: number, ratio: number, height: number, yInvert: boolean): number {\n // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit\n return yInvert\n ? Math.max(0, height - 1 - Math.round(y * ratio))\n : Math.min(Math.round(y * ratio), height - 1);\n}\n"],"file":"device-pixels.js"}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { log } from '@luma.gl/api';
|
|
2
|
-
import { getContextState } from './context-state';
|
|
3
|
-
export function cssToDeviceRatio(gl) {
|
|
4
|
-
const state = getContextState(gl);
|
|
5
|
-
|
|
6
|
-
if (gl.canvas && state) {
|
|
7
|
-
const {
|
|
8
|
-
clientWidth
|
|
9
|
-
} = state._canvasSizeInfo;
|
|
10
|
-
return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return 1;
|
|
14
|
-
}
|
|
15
|
-
export function cssToDevicePixels(gl, cssPixel, yInvert = true) {
|
|
16
|
-
const ratio = cssToDeviceRatio(gl);
|
|
17
|
-
const width = gl.drawingBufferWidth;
|
|
18
|
-
const height = gl.drawingBufferHeight;
|
|
19
|
-
return scalePixels(cssPixel, ratio, width, height, yInvert);
|
|
20
|
-
}
|
|
21
|
-
export function getDevicePixelRatio(useDevicePixels) {
|
|
22
|
-
const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
23
|
-
|
|
24
|
-
if (Number.isFinite(useDevicePixels)) {
|
|
25
|
-
return useDevicePixels <= 0 ? 1 : useDevicePixels;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return useDevicePixels ? windowRatio : 1;
|
|
29
|
-
}
|
|
30
|
-
export function setDevicePixelRatio(gl, devicePixelRatio, options = {}) {
|
|
31
|
-
if (!(gl.canvas instanceof HTMLCanvasElement)) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const canvas = gl.canvas;
|
|
36
|
-
let clientWidth = 'width' in options ? options.width : canvas.clientWidth;
|
|
37
|
-
let clientHeight = 'height' in options ? options.height : canvas.clientHeight;
|
|
38
|
-
|
|
39
|
-
if (!clientWidth || !clientHeight) {
|
|
40
|
-
log.log(1, 'Canvas clientWidth/clientHeight is 0')();
|
|
41
|
-
devicePixelRatio = 1;
|
|
42
|
-
clientWidth = canvas.width || 1;
|
|
43
|
-
clientHeight = canvas.height || 1;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const contextState = getContextState(gl);
|
|
47
|
-
const cachedSize = contextState._canvasSizeInfo;
|
|
48
|
-
|
|
49
|
-
if (cachedSize.clientWidth !== clientWidth || cachedSize.clientHeight !== clientHeight || cachedSize.devicePixelRatio !== devicePixelRatio) {
|
|
50
|
-
let clampedPixelRatio = devicePixelRatio;
|
|
51
|
-
const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);
|
|
52
|
-
const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);
|
|
53
|
-
canvas.width = canvasWidth;
|
|
54
|
-
canvas.height = canvasHeight;
|
|
55
|
-
|
|
56
|
-
if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {
|
|
57
|
-
log.warn("Device pixel ratio clamped")();
|
|
58
|
-
clampedPixelRatio = Math.min(gl.drawingBufferWidth / clientWidth, gl.drawingBufferHeight / clientHeight);
|
|
59
|
-
canvas.width = Math.floor(clientWidth * clampedPixelRatio);
|
|
60
|
-
canvas.height = Math.floor(clientHeight * clampedPixelRatio);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
Object.assign(contextState._canvasSizeInfo, {
|
|
64
|
-
clientWidth,
|
|
65
|
-
clientHeight,
|
|
66
|
-
devicePixelRatio
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function scalePixels(pixel, ratio, width, height, yInvert) {
|
|
72
|
-
const x = scaleX(pixel[0], ratio, width);
|
|
73
|
-
let y = scaleY(pixel[1], ratio, height, yInvert);
|
|
74
|
-
let t = scaleX(pixel[0] + 1, ratio, width);
|
|
75
|
-
const xHigh = t === width - 1 ? t : t - 1;
|
|
76
|
-
t = scaleY(pixel[1] + 1, ratio, height, yInvert);
|
|
77
|
-
let yHigh;
|
|
78
|
-
|
|
79
|
-
if (yInvert) {
|
|
80
|
-
t = t === 0 ? t : t + 1;
|
|
81
|
-
yHigh = y;
|
|
82
|
-
y = t;
|
|
83
|
-
} else {
|
|
84
|
-
yHigh = t === height - 1 ? t : t - 1;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
x,
|
|
89
|
-
y,
|
|
90
|
-
width: Math.max(xHigh - x + 1, 1),
|
|
91
|
-
height: Math.max(yHigh - y + 1, 1)
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function scaleX(x, ratio, width) {
|
|
96
|
-
const r = Math.min(Math.round(x * ratio), width - 1);
|
|
97
|
-
return r;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function scaleY(y, ratio, height, yInvert) {
|
|
101
|
-
return yInvert ? Math.max(0, height - 1 - Math.round(y * ratio)) : Math.min(Math.round(y * ratio), height - 1);
|
|
102
|
-
}
|
|
103
|
-
//# sourceMappingURL=device-pixels.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/context/context/device-pixels.ts"],"names":["log","getContextState","cssToDeviceRatio","gl","state","canvas","clientWidth","_canvasSizeInfo","drawingBufferWidth","cssToDevicePixels","cssPixel","yInvert","ratio","width","height","drawingBufferHeight","scalePixels","getDevicePixelRatio","useDevicePixels","windowRatio","window","devicePixelRatio","Number","isFinite","setDevicePixelRatio","options","HTMLCanvasElement","clientHeight","contextState","cachedSize","clampedPixelRatio","canvasWidth","Math","floor","canvasHeight","warn","min","Object","assign","pixel","x","scaleX","y","scaleY","t","xHigh","yHigh","max","r","round"],"mappings":"AAAA,SAAQA,GAAR,QAAkB,cAAlB;AACA,SAAQC,eAAR,QAA8B,iBAA9B;AAKA,OAAO,SAASC,gBAAT,CAA0BC,EAA1B,EAA6D;AAClE,QAAMC,KAAK,GAAGH,eAAe,CAACE,EAAD,CAA7B;;AAEA,MAAIA,EAAE,CAACE,MAAH,IAAaD,KAAjB,EAAwB;AAGtB,UAAM;AAACE,MAAAA;AAAD,QAAgBF,KAAK,CAACG,eAA5B;AACA,WAAOD,WAAW,GAAGH,EAAE,CAACK,kBAAH,GAAwBF,WAA3B,GAAyC,CAA3D;AACD;;AAED,SAAO,CAAP;AACD;AAKA,OAAO,SAASG,iBAAT,CACNN,EADM,EAENO,QAFM,EAGNC,OAAgB,GAAG,IAHb,EASN;AACA,QAAMC,KAAK,GAAGV,gBAAgB,CAACC,EAAD,CAA9B;AACA,QAAMU,KAAK,GAAGV,EAAE,CAACK,kBAAjB;AACA,QAAMM,MAAM,GAAGX,EAAE,CAACY,mBAAlB;AACA,SAAOC,WAAW,CAACN,QAAD,EAAWE,KAAX,EAAkBC,KAA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,GAAG,CAAxC,CAAV;AACA,SAAOmC,CAAP;AACD;;AAED,SAASL,MAAT,CAAgBD,CAAhB,EAA2B9B,KAA3B,EAA0CE,MAA1C,EAA0DH,OAA1D,EAAoF;AAElF,SAAOA,OAAO,GACVqB,IAAI,CAACe,GAAL,CAAS,CAAT,EAAYjC,MAAM,GAAG,CAAT,GAAakB,IAAI,CAACiB,KAAL,CAAWP,CAAC,GAAG9B,KAAf,CAAzB,CADU,GAEVoB,IAAI,CAACI,GAAL,CAASJ,IAAI,CAACiB,KAAL,CAAWP,CAAC,GAAG9B,KAAf,CAAT,EAAgCE,MAAM,GAAG,CAAzC,CAFJ;AAGD","sourcesContent":["import {log} from '@luma.gl/api';\nimport {getContextState} from './context-state';\n\n/**\n * Returns multiplier need to convert CSS size to Device size\n */\nexport function cssToDeviceRatio(gl: WebGLRenderingContext): number {\n const state = getContextState(gl);\n\n if (gl.canvas && state) {\n // For headless gl we might have used custom width and height\n // hence use cached clientWidth\n const {clientWidth} = state._canvasSizeInfo;\n return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;\n }\n // use default device pixel ratio\n return 1;\n}\n\n/**\n * Maps CSS pixel position to device pixel position\n */\n export function cssToDevicePixels(\n gl: WebGLRenderingContext,\n cssPixel: number[],\n yInvert: boolean = true\n): {\n x: number;\n y: number;\n width: number;\n height: number;\n} {\n const ratio = cssToDeviceRatio(gl);\n const width = gl.drawingBufferWidth;\n const height = gl.drawingBufferHeight;\n return scalePixels(cssPixel, ratio, width, height, yInvert);\n}\n\n/**\n * Calulates device pixel ratio, used during context creation\n *\n * @param useDevicePixels - boolean or a number\n * @return - device pixel ratio\n */\n export function getDevicePixelRatio(useDevicePixels: boolean | number): number {\n const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;\n if (Number.isFinite(useDevicePixels)) {\n // @ts-expect-error Can no longer be boolean after previous line\n return useDevicePixels <= 0 ? 1 : useDevicePixels;\n }\n return useDevicePixels ? windowRatio : 1;\n}\n\n// use devicePixelRatio to set canvas width and height\nexport function setDevicePixelRatio(gl: WebGLRenderingContext, devicePixelRatio: number, options: {width?: number, height?: number} = {}) {\n if (!(gl.canvas instanceof HTMLCanvasElement)) {\n return;\n }\n\n const canvas: HTMLCanvasElement = gl.canvas;\n\n // NOTE: if options.width and options.height not used remove in v8\n let clientWidth = 'width' in options ? options.width : canvas.clientWidth;\n let clientHeight = 'height' in options ? options.height : canvas.clientHeight;\n\n if (!clientWidth || !clientHeight) {\n log.log(1, 'Canvas clientWidth/clientHeight is 0')();\n // by forcing devicePixel ratio to 1, we do not scale canvas.width and height in each frame.\n devicePixelRatio = 1;\n clientWidth = canvas.width || 1;\n clientHeight = canvas.height || 1;\n }\n\n const contextState = getContextState(gl);\n const cachedSize = contextState._canvasSizeInfo;\n // Check if canvas needs to be resized\n if (\n cachedSize.clientWidth !== clientWidth ||\n cachedSize.clientHeight !== clientHeight ||\n cachedSize.devicePixelRatio !== devicePixelRatio\n ) {\n let clampedPixelRatio = devicePixelRatio;\n\n const canvasWidth = Math.floor(clientWidth * clampedPixelRatio);\n const canvasHeight = Math.floor(clientHeight * clampedPixelRatio);\n canvas.width = canvasWidth;\n canvas.height = canvasHeight;\n\n // Note: when devicePixelRatio is too high, it is possible we might hit system limit for\n // drawing buffer width and hight, in those cases they get clamped and resulting aspect ration may not be maintained\n // for those cases, reduce devicePixelRatio.\n if (gl.drawingBufferWidth !== canvasWidth || gl.drawingBufferHeight !== canvasHeight) {\n log.warn(`Device pixel ratio clamped`)();\n clampedPixelRatio = Math.min(\n gl.drawingBufferWidth / clientWidth,\n gl.drawingBufferHeight / clientHeight\n );\n\n canvas.width = Math.floor(clientWidth * clampedPixelRatio);\n canvas.height = Math.floor(clientHeight * clampedPixelRatio);\n }\n\n Object.assign(contextState._canvasSizeInfo, {clientWidth, clientHeight, devicePixelRatio});\n }\n}\n\n\n// PRIVATE\n\nfunction scalePixels(pixel: number[], ratio: number, width: number, height: number, yInvert: boolean): {\n x: number;\n y: number;\n width: number;\n height: number;\n} {\n const x = scaleX(pixel[0], ratio, width);\n let y = scaleY(pixel[1], ratio, height, yInvert);\n\n // Find boundaries of next pixel to provide valid range of device pixel locaitons\n\n let t = scaleX(pixel[0] + 1, ratio, width);\n // If next pixel's position is clamped to boundary, use it as is, otherwise subtract 1 for current pixel boundary\n const xHigh = t === width - 1 ? t : t - 1;\n\n t = scaleY(pixel[1] + 1, ratio, height, yInvert);\n let yHigh;\n if (yInvert) {\n // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range\n t = t === 0 ? t : t + 1;\n // swap y and yHigh\n yHigh = y;\n y = t;\n } else {\n // If next pixel's position is clamped to boundary, use it as is, otherwise clamp it to valid range\n yHigh = t === height - 1 ? t : t - 1;\n // y remains same\n }\n return {\n x,\n y,\n // when ratio < 1, current css pixel and next css pixel may point to same device pixel, set width/height to 1 in those cases.\n width: Math.max(xHigh - x + 1, 1),\n height: Math.max(yHigh - y + 1, 1)\n };\n}\n\nfunction scaleX(x: number, ratio: number, width: number): number {\n // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit\n const r = Math.min(Math.round(x * ratio), width - 1);\n return r;\n}\n\nfunction scaleY(y: number, ratio: number, height: number, yInvert: boolean): number {\n // since we are rounding to nearest, when ratio > 1, edge pixels may point to out of bounds value, clamp to the limit\n return yInvert\n ? Math.max(0, height - 1 - Math.round(y * ratio))\n : Math.min(Math.round(y * ratio), height - 1);\n}\n"],"file":"device-pixels.js"}
|