Skip to content

Commit 5887d8e

Browse files
committed
Change ImageUtils.getDataURL() to accept a non-renderable object and return an empty string if a data URL cannot be generated. Allows the serialization of a scene with a DataTexture/WebGLRenderTarget after mrdoob#16651 landed without throwing. Fixes mrdoob#16763.
1 parent 44b2865 commit 5887d8e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/extras/ImageUtils.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { Texture } from '../textures/Texture';
77
* @deprecated Use {@link TextureLoader} instead.
88
*/
99
export namespace ImageUtils {
10+
/**
11+
*/
12+
export function getDataURL(
13+
image: any,
14+
): string;
15+
1016
/**
1117
* @deprecated
1218
*/

src/extras/ImageUtils.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ var ImageUtils = {
1212

1313
var canvas;
1414

15-
if ( typeof HTMLCanvasElement == 'undefined' ) {
15+
if ( ! image ) {
16+
17+
return '';
18+
19+
} else if ( typeof HTMLCanvasElement == 'undefined' ) {
1620

1721
return image.src;
1822

@@ -35,7 +39,18 @@ var ImageUtils = {
3539

3640
} else {
3741

38-
context.drawImage( image, 0, 0, image.width, image.height );
42+
try {
43+
44+
// Can throw if `image` is not a drawable type, e.g. HTMLImageElement, HTMLVideoElement,
45+
// ImageBitmap; occurs for DataTextures, Textures from WebGLRenderTarget etc..
46+
47+
context.drawImage( image, 0, 0, image.width, image.height );
48+
49+
} catch ( e ) {
50+
51+
return '';
52+
53+
}
3954

4055
}
4156

0 commit comments

Comments
 (0)