Skip to content

Commit bc893f8

Browse files
authored
Merge pull request #512 from kchadha/skin-empty-image-data
Add Support for Empty Skins
2 parents 13b9ed7 + 74a7a8c commit bc893f8

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/BitmapSkin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class BitmapSkin extends Skin {
5656
*/
5757
// eslint-disable-next-line no-unused-vars
5858
getTexture (scale) {
59-
return this._texture;
59+
return this._texture || super.getTexture();
6060
}
6161

6262
/**
@@ -78,6 +78,10 @@ class BitmapSkin extends Skin {
7878
* @fires Skin.event:WasAltered
7979
*/
8080
setBitmap (bitmapData, costumeResolution, rotationCenter) {
81+
if (!bitmapData.width || !bitmapData.height) {
82+
super.setEmptyImageData();
83+
return;
84+
}
8185
const gl = this._renderer.gl;
8286

8387
// Preferably bitmapData is ImageData. ImageData speeds up updating

src/SVGSkin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class SVGSkin extends Skin {
6666
*/
6767
// eslint-disable-next-line no-unused-vars
6868
getTexture (scale) {
69+
if (!this._svgRenderer.canvas.width || !this._svgRenderer.canvas.height) {
70+
return super.getTexture();
71+
}
72+
6973
// The texture only ever gets uniform scale. Take the larger of the two axes.
7074
const scaleMax = scale ? Math.max(Math.abs(scale[0]), Math.abs(scale[1])) : 100;
7175
const requestedScale = Math.min(scaleMax / 100, this._maxTextureScale);
@@ -108,6 +112,12 @@ class SVGSkin extends Skin {
108112
// updating Silhouette and is better handled by more browsers in
109113
// regards to memory.
110114
const canvas = this._svgRenderer.canvas;
115+
116+
if (!canvas.width || !canvas.height) {
117+
super.setEmptyImageData();
118+
return;
119+
}
120+
111121
const context = canvas.getContext('2d');
112122
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
113123

src/Skin.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class Skin extends EventEmitter {
140140
*/
141141
// eslint-disable-next-line no-unused-vars
142142
getTexture (scale) {
143-
return null;
143+
return this._emptyImageTexture;
144144
}
145145

146146
/**
@@ -171,6 +171,38 @@ class Skin extends EventEmitter {
171171
*/
172172
updateSilhouette () {}
173173

174+
/**
175+
* Set the contents of this skin to an empty skin.
176+
* @fires Skin.event:WasAltered
177+
*/
178+
setEmptyImageData () {
179+
// Free up the current reference to the _texture
180+
this._texture = null;
181+
182+
if (!this._emptyImageData) {
183+
// Create a transparent pixel
184+
this._emptyImageData = new ImageData(1, 1);
185+
186+
// Create a new texture and update the silhouette
187+
const gl = this._renderer.gl;
188+
189+
const textureOptions = {
190+
auto: true,
191+
wrap: gl.CLAMP_TO_EDGE,
192+
src: this._emptyImageData
193+
};
194+
195+
// Note: we're using _emptyImageTexture here instead of _texture
196+
// so that we can cache this empty texture for later use as needed.
197+
// this._texture can get modified by other skins (e.g. BitmapSkin
198+
// and SVGSkin, so we can't use that same field for caching)
199+
this._emptyImageTexture = twgl.createTexture(gl, textureOptions);
200+
}
201+
202+
this._silhouette.update(this._emptyImageData);
203+
this.emit(Skin.Events.WasAltered);
204+
}
205+
174206
/**
175207
* Does this point touch an opaque or translucent point on this skin?
176208
* Nearest Neighbor version

0 commit comments

Comments
 (0)