@@ -77,10 +77,14 @@ class SVGSkin extends Skin {
77
77
this . _textureScale = newScale ;
78
78
this . _svgRenderer . _draw ( this . _textureScale , ( ) => {
79
79
if ( this . _textureScale === newScale ) {
80
+ const canvas = this . _svgRenderer . canvas ;
81
+ const context = canvas . getContext ( '2d' ) ;
82
+ const textureData = context . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
83
+
80
84
const gl = this . _renderer . gl ;
81
85
gl . bindTexture ( gl . TEXTURE_2D , this . _texture ) ;
82
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , gl . RGBA , gl . UNSIGNED_BYTE , this . _svgRenderer . canvas ) ;
83
- this . _silhouette . update ( this . _svgRenderer . canvas ) ;
86
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , gl . RGBA , gl . UNSIGNED_BYTE , textureData ) ;
87
+ this . _silhouette . update ( textureData ) ;
84
88
}
85
89
} ) ;
86
90
}
@@ -99,20 +103,31 @@ class SVGSkin extends Skin {
99
103
this . _svgRenderer . fromString ( svgData , 1 , ( ) => {
100
104
const gl = this . _renderer . gl ;
101
105
this . _textureScale = this . _maxTextureScale = 1 ;
106
+
107
+ // Pull out the ImageData from the canvas. ImageData speeds up
108
+ // updating Silhouette and is better handled by more browsers in
109
+ // regards to memory.
110
+ const canvas = this . _svgRenderer . canvas ;
111
+ const context = canvas . getContext ( '2d' ) ;
112
+ const textureData = context . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
113
+
102
114
if ( this . _texture ) {
103
115
gl . bindTexture ( gl . TEXTURE_2D , this . _texture ) ;
104
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , gl . RGBA , gl . UNSIGNED_BYTE , this . _svgRenderer . canvas ) ;
105
- this . _silhouette . update ( this . _svgRenderer . canvas ) ;
116
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , gl . RGBA , gl . UNSIGNED_BYTE , textureData ) ;
117
+ this . _silhouette . update ( textureData ) ;
106
118
} else {
107
119
// TODO: mipmaps?
108
120
const textureOptions = {
109
121
auto : true ,
110
122
wrap : gl . CLAMP_TO_EDGE ,
111
- src : this . _svgRenderer . canvas
123
+ src : textureData
112
124
} ;
113
125
114
126
this . _texture = twgl . createTexture ( gl , textureOptions ) ;
115
- this . _silhouette . update ( this . _svgRenderer . canvas ) ;
127
+ this . _silhouette . update ( textureData ) ;
128
+
129
+ // Unset src to make garbage collecting textureData easier.
130
+ textureData . src = null ;
116
131
}
117
132
118
133
const maxDimension = Math . max ( this . _svgRenderer . canvas . width , this . _svgRenderer . canvas . height ) ;
0 commit comments