Handle cases when canvas is not defined (#1236)

This commit is contained in:
1chandu 2019-09-12 17:38:49 -07:00 committed by GitHub
parent f41d4a5d29
commit f7a200db76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -2,7 +2,11 @@
// multiplier need to convert CSS size to Device size
export function cssToDeviceRatio(gl) {
return gl.drawingBufferWidth / (gl.canvas.clientWidth || gl.canvas.width);
if (gl.canvas) {
return gl.drawingBufferWidth / (gl.canvas.clientWidth || gl.canvas.width || 1);
}
// use default device pixel ratio
return 1;
}
// Maps CSS pixel position to device pixel position

View File

@ -338,5 +338,12 @@ test('webgl#cssToDeviceRatio', t => {
MAP_TEST_CASES.forEach(tc => {
t.equal(cssToDeviceRatio(tc.gl), tc.ratio, 'cssToDeviceRatio should return correct value');
});
const glWithNoCanvas = {};
t.equal(
cssToDeviceRatio(glWithNoCanvas),
1,
'cssToDeviceRatio should return 1 when there is no canvas'
);
t.end();
});