Browse Source

Texture.toJSON(): Support <canvas> .image. See #6665.

Mr.doob 10 years ago
parent
commit
b5472856b1
1 changed files with 25 additions and 11 deletions
  1. 25 11
      src/textures/Texture.js

+ 25 - 11
src/textures/Texture.js

@@ -136,28 +136,42 @@ THREE.Texture.prototype = {
 
 			}
 
-			if ( meta.images[ this.image.uuid ] === undefined ) {
+			function getDataURL( image ) {
 
-				var canvas = document.createElement( 'canvas' );
-				canvas.width = image.width;
-				canvas.height = image.height;
+				var canvas;
 
-				var context = canvas.getContext( '2d' );
-				context.drawImage( image, 0, 0, image.width, image.height );
+				if ( image.toDataURL !== undefined ) {
 
-				var src;
+					canvas = image;
 
-				if ( image.width > 2048 || image.height > 2048 ) {
+				} else {
+
+					canvas = document.createElement( 'canvas' );
+					canvas.width = image.width;
+					canvas.height = image.height;
+
+					canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );
+
+				}
 
-					src = canvas.toDataURL( 'image/jpeg', 0.6 );
+				if ( canvas.width > 2048 || canvas.height > 2048 ) {
+
+					return canvas.toDataURL( 'image/jpeg', 0.6 );
 
 				} else {
 
-					src = canvas.toDataURL( 'image/png' );
+					return canvas.toDataURL( 'image/png' );
 
 				}
 
-				meta.images[ this.image.uuid ] = { uuid: this.image.uuid, url: src };
+			}
+
+			if ( meta.images[ image.uuid ] === undefined ) {
+
+				meta.images[ image.uuid ] = {
+					uuid: image.uuid,
+					url: getDataURL( image )
+				};
 
 			}