Skip to content

Commit f0823b1

Browse files
committed
Undo ImageData texture uploads
1 parent 50b4ccd commit f0823b1

File tree

5 files changed

+12
-28
lines changed

5 files changed

+12
-28
lines changed

src/BitmapSkin.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,6 @@ class BitmapSkin extends Skin {
7171
}
7272
const gl = this._renderer.gl;
7373

74-
// Preferably bitmapData is ImageData. ImageData speeds up updating
75-
// Silhouette and is better handled by more browsers in regards to
76-
// memory.
77-
let textureData = bitmapData;
78-
if (bitmapData instanceof HTMLCanvasElement) {
79-
// Given a HTMLCanvasElement get the image data to pass to webgl and
80-
// Silhouette.
81-
const context = bitmapData.getContext('2d');
82-
textureData = context.getImageData(0, 0, bitmapData.width, bitmapData.height);
83-
}
84-
8574
if (this._texture === null) {
8675
const textureOptions = {
8776
auto: false,
@@ -91,7 +80,8 @@ class BitmapSkin extends Skin {
9180
this._texture = twgl.createTexture(gl, textureOptions);
9281
}
9382

94-
this._setTexture(textureData);
83+
this._setTexture(bitmapData);
84+
this._silhouette.update(bitmapData);
9585

9686
// Do these last in case any of the above throws an exception
9787
this._costumeResolution = costumeResolution || 2;

src/SVGSkin.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,23 @@ class SVGSkin extends Skin {
6666
createMIP (scale) {
6767
this._svgRenderer.draw(scale);
6868

69-
// Pull out the ImageData from the canvas. ImageData speeds up
70-
// updating Silhouette and is better handled by more browsers in
71-
// regards to memory.
7269
const canvas = this._svgRenderer.canvas;
7370
// If one of the canvas dimensions is 0, set this MIP to an empty image texture.
7471
// This avoids an IndexSizeError from attempting to getImageData when one of the dimensions is 0.
7572
if (canvas.width === 0 || canvas.height === 0) return super.getTexture();
7673

77-
const context = canvas.getContext('2d');
78-
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
79-
8074
const textureOptions = {
8175
auto: false,
8276
wrap: this._renderer.gl.CLAMP_TO_EDGE,
83-
src: textureData,
77+
src: canvas,
8478
premultiplyAlpha: true
8579
};
8680

8781
const mip = twgl.createTexture(this._renderer.gl, textureOptions);
8882

8983
// Check if this is the largest MIP created so far. Currently, silhouettes only get scaled up.
9084
if (this._largestMIPScale < scale) {
91-
this._silhouette.update(textureData);
85+
this._silhouette.update(canvas);
9286
this._largestMIPScale = scale;
9387
}
9488

src/Silhouette.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,14 @@ class Silhouette {
134134
imageData = bitmapData;
135135
this._width = bitmapData.width;
136136
this._height = bitmapData.height;
137+
} else if (bitmapData instanceof HTMLCanvasElement) {
138+
// If passed a <canvas>, grab its image data.
139+
const ctx = bitmapData.getContext('2d');
140+
imageData = ctx.getImageData(0, 0, bitmapData.width, bitmapData.height);
141+
this._width = bitmapData.width;
142+
this._height = bitmapData.height;
137143
} else {
138-
// Draw about anything else to our update canvas and poll image data
139-
// from that.
144+
// Draw about anything else to our update canvas and poll image data from that.
140145
const canvas = Silhouette._updateCanvas();
141146
const width = this._width = canvas.width = bitmapData.width;
142147
const height = this._height = canvas.height = bitmapData.height;

src/Skin.js

-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ class Skin extends EventEmitter {
147147
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
148148
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
149149
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
150-
151-
this._silhouette.update(textureData);
152150
}
153151

154152
/**

src/TextBubbleSkin.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ class TextBubbleSkin extends Skin {
260260
this._renderTextBubble(requestedScale);
261261
this._textureDirty = false;
262262

263-
const context = this._canvas.getContext('2d');
264-
const textureData = context.getImageData(0, 0, this._canvas.width, this._canvas.height);
265-
266263
const gl = this._renderer.gl;
267264

268265
if (this._texture === null) {
@@ -274,7 +271,7 @@ class TextBubbleSkin extends Skin {
274271
this._texture = twgl.createTexture(gl, textureOptions);
275272
}
276273

277-
this._setTexture(textureData);
274+
this._setTexture(this._canvas);
278275
}
279276

280277
return this._texture;

0 commit comments

Comments
 (0)