@luma.gl/gltools
Version:
WebGL2 API Polyfills for WebGL1 WebGLRenderingContext
63 lines (54 loc) • 1.81 kB
JavaScript
export function cssToDeviceRatio(gl) {
const {
luma
} = gl;
if (gl.canvas && luma) {
const cachedSize = luma.canvasSizeInfo;
const clientWidth = 'clientWidth' in cachedSize ? cachedSize.clientWidth : gl.canvas.clientWidth;
return clientWidth ? gl.drawingBufferWidth / clientWidth : 1;
}
return 1;
}
export function cssToDevicePixels(gl, cssPixel) {
let yInvert = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
const ratio = cssToDeviceRatio(gl);
const width = gl.drawingBufferWidth;
const height = gl.drawingBufferHeight;
return scalePixels(cssPixel, ratio, width, height, yInvert);
}
export function getDevicePixelRatio(useDevicePixels) {
const windowRatio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
if (Number.isFinite(useDevicePixels)) {
return useDevicePixels <= 0 ? 1 : useDevicePixels;
}
return useDevicePixels ? windowRatio : 1;
}
function scalePixels(pixel, ratio, width, height, yInvert) {
const x = scaleX(pixel[0], ratio, width);
let y = scaleY(pixel[1], ratio, height, yInvert);
let t = scaleX(pixel[0] + 1, ratio, width);
const xHigh = t === width - 1 ? t : t - 1;
t = scaleY(pixel[1] + 1, ratio, height, yInvert);
let yHigh;
if (yInvert) {
t = t === 0 ? t : t + 1;
yHigh = y;
y = t;
} else {
yHigh = t === height - 1 ? t : t - 1;
}
return {
x,
y,
width: Math.max(xHigh - x + 1, 1),
height: Math.max(yHigh - y + 1, 1)
};
}
function scaleX(x, ratio, width) {
const r = Math.min(Math.round(x * ratio), width - 1);
return r;
}
function scaleY(y, ratio, height, yInvert) {
return yInvert ? Math.max(0, height - 1 - Math.round(y * ratio)) : Math.min(Math.round(y * ratio), height - 1);
}
//# sourceMappingURL=device-pixels.js.map