Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo ImageData texture uploads #732

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/BitmapSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ class BitmapSkin extends Skin {
}
const gl = this._renderer.gl;

// Preferably bitmapData is ImageData. ImageData speeds up updating
// Silhouette and is better handled by more browsers in regards to
// memory.
let textureData = bitmapData;
if (bitmapData instanceof HTMLCanvasElement) {
// Given a HTMLCanvasElement get the image data to pass to webgl and
// Silhouette.
const context = bitmapData.getContext('2d');
textureData = context.getImageData(0, 0, bitmapData.width, bitmapData.height);
}

if (this._texture === null) {
const textureOptions = {
auto: false,
Expand All @@ -91,7 +80,7 @@ class BitmapSkin extends Skin {
this._texture = twgl.createTexture(gl, textureOptions);
}

this._setTexture(textureData);
this._setTexture(bitmapData);

// Do these last in case any of the above throws an exception
this._costumeResolution = costumeResolution || 2;
Expand Down
10 changes: 2 additions & 8 deletions src/SVGSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,23 @@ class SVGSkin extends Skin {
createMIP (scale) {
this._svgRenderer.draw(scale);

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

const context = canvas.getContext('2d');
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);

const textureOptions = {
auto: false,
wrap: this._renderer.gl.CLAMP_TO_EDGE,
src: textureData,
src: canvas,
premultiplyAlpha: true
};

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

// Check if this is the largest MIP created so far. Currently, silhouettes only get scaled up.
if (this._largestMIPScale < scale) {
this._silhouette.update(textureData);
this._silhouette.update(canvas);
this._largestMIPScale = scale;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Silhouette.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@ class Silhouette {
imageData = bitmapData;
this._width = bitmapData.width;
this._height = bitmapData.height;
} else if (bitmapData instanceof HTMLCanvasElement) {
// If passed a <canvas>, grab its image data.
const ctx = bitmapData.getContext('2d');
imageData = ctx.getImageData(0, 0, bitmapData.width, bitmapData.height);
this._width = bitmapData.width;
this._height = bitmapData.height;
} else {
// Draw about anything else to our update canvas and poll image data
// from that.
// Draw about anything else to our update canvas and poll image data from that.
const canvas = Silhouette._updateCanvas();
const width = this._width = canvas.width = bitmapData.width;
const height = this._height = canvas.height = bitmapData.height;
Expand Down
5 changes: 1 addition & 4 deletions src/TextBubbleSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ class TextBubbleSkin extends Skin {
this._renderTextBubble(requestedScale);
this._textureDirty = false;

const context = this._canvas.getContext('2d');
const textureData = context.getImageData(0, 0, this._canvas.width, this._canvas.height);

const gl = this._renderer.gl;

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

this._setTexture(textureData);
this._setTexture(this._canvas);
}

return this._texture;
Expand Down