Skip to content

Commit afabd67

Browse files
authored
add typings for offscreencanvas
INTERNAL
1 parent af3f975 commit afabd67

5 files changed

Lines changed: 19 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@types/jasmine": "~2.5.53",
2424
"@types/node": "~9.6.0",
2525
"@types/node-fetch": "~2.1.2",
26+
"@types/offscreencanvas": "~2019.3.0",
2627
"browserify": "~16.2.3",
2728
"clang-format": "~1.2.4",
2829
"jasmine": "~3.1.0",

src/backends/cpu/backend_cpu.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ interface TensorData<D extends DataType> {
6666
}
6767

6868
function createCanvas() {
69-
//@ts-ignore
70-
if (typeof(OffscreenCanvas) !== 'undefined') {
71-
//@ts-ignore
69+
if (typeof OffscreenCanvas !== 'undefined') {
7270
return new OffscreenCanvas(300, 150);
73-
} else if (typeof(document) !== 'undefined') {
71+
} else if (typeof document !== 'undefined') {
7472
return document.createElement('canvas');
7573
} else {
7674
throw new Error('Cannot create a canvas in this context');
@@ -81,7 +79,8 @@ export class MathBackendCPU implements KernelBackend {
8179
public blockSize = 48;
8280

8381
private data: DataStorage<TensorData<DataType>>;
84-
private fromPixels2DContext: CanvasRenderingContext2D;
82+
private fromPixels2DContext: CanvasRenderingContext2D|
83+
OffscreenCanvasRenderingContext2D;
8584
private firstUse = true;
8685

8786
constructor() {

src/backends/webgl/canvas_util.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ export function getWebGLContext(webGLVersion: number): WebGLRenderingContext {
5656
}
5757

5858
export function createCanvas(webGLVersion: number) {
59-
//@ts-ignore
60-
if (typeof(OffscreenCanvas) !== 'undefined' && webGLVersion === 2) {
61-
//@ts-ignore
59+
if (typeof OffscreenCanvas !== 'undefined' && webGLVersion === 2) {
6260
return new OffscreenCanvas(300, 150);
63-
} else if (typeof(document) !== 'undefined') {
61+
} else if (typeof document !== 'undefined') {
6462
return document.createElement('canvas');
6563
} else {
6664
throw new Error('Cannot create a canvas in this context');
@@ -73,7 +71,7 @@ function getWebGLRenderingContext(webGLVersion: number): WebGLRenderingContext {
7371
}
7472
const canvas = createCanvas(webGLVersion);
7573

76-
canvas.addEventListener('webglcontextlost', (ev : Event) => {
74+
canvas.addEventListener('webglcontextlost', (ev: Event) => {
7775
ev.preventDefault();
7876
delete contexts[webGLVersion];
7977
}, false);

src/backends/webgl/canvas_util_test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import {getWebGLContext} from './canvas_util';
2222

2323
describeWithFlags('canvas_util', BROWSER_ENVS, () => {
2424
it('Returns a valid canvas', () => {
25-
const canvas = getWebGLContext(ENV.getNumber('WEBGL_VERSION')).canvas;
26-
expect((canvas instanceof HTMLCanvasElement)
27-
//@ts-ignore
28-
|| (canvas instanceof OffscreenCanvas)).toBe(true);
25+
const canvas = getWebGLContext(ENV.getNumber('WEBGL_VERSION')).canvas as (
26+
HTMLCanvasElement | OffscreenCanvas);
27+
expect(
28+
(canvas instanceof HTMLCanvasElement) ||
29+
(canvas instanceof OffscreenCanvas))
30+
.toBe(true);
2931
});
3032

3133
it('Returns a valid gl context', () => {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@
142142
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.47.tgz#655b4a1cc52c8ab08f48cc055061990d6846bc97"
143143
integrity sha512-56wEJWXZs+3XXoTe/OCpdZ6czrONhy+6hT0GdPOb7HvudLTMJ1T5tuZPs37K5cPR5t+J9+vLPFDQgUQ8NWJE1w==
144144

145+
"@types/offscreencanvas@~2019.3.0":
146+
version "2019.3.0"
147+
resolved "https://registry.yarnpkg.com/@types/offscreencanvas/-/offscreencanvas-2019.3.0.tgz#3336428ec7e9180cf4566dfea5da04eb586a6553"
148+
integrity sha512-esIJx9bQg+QYF0ra8GnvfianIY8qWB0GBx54PK5Eps6m+xTj86KLavHv6qDhzKcu5UUOgNfJ2pWaIIV7TRUd9Q==
149+
145150
"@types/seedrandom@2.4.27":
146151
version "2.4.27"
147152
resolved "https://registry.yarnpkg.com/@types/seedrandom/-/seedrandom-2.4.27.tgz#9db563937dd86915f69092bc43259d2f48578e41"

0 commit comments

Comments
 (0)