Skip to content

Commit d17066b

Browse files
authoredApr 2, 2020
Merge pull request #475 from adroitwhiz/penskin-silhouette-from-data
Set PenSkin silhouette data directly
2 parents 6cde82d + b194394 commit d17066b

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed
 

‎src/PenSkin.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class PenSkin extends Skin {
109109
/** @type {boolean} */
110110
this._silhouetteDirty = false;
111111

112+
/** @type {Uint8Array} */
113+
this._silhouettePixels = null;
114+
115+
/** @type {ImageData} */
116+
this._silhouetteImageData = null;
117+
112118
/** @type {object} */
113119
this._lineOnBufferDrawRegionId = {
114120
enter: () => this._enterDrawLineOnBuffer(),
@@ -597,6 +603,9 @@ class PenSkin extends Skin {
597603
gl.clearColor(0, 0, 0, 0);
598604
gl.clear(gl.COLOR_BUFFER_BIT);
599605

606+
this._silhouettePixels = new Uint8Array(Math.floor(width * height * 4));
607+
this._silhouetteImageData = this._canvas.getContext('2d').createImageData(width, height);
608+
600609
this._silhouetteDirty = true;
601610
}
602611

@@ -634,24 +643,17 @@ class PenSkin extends Skin {
634643
// Render export texture to another framebuffer
635644
const gl = this._renderer.gl;
636645

637-
const bounds = this._bounds;
638-
639646
this._renderer.enterDrawRegion(this._toBufferDrawRegionId);
640647

641648
// Sample the framebuffer's pixels into the silhouette instance
642-
const skinPixels = new Uint8Array(Math.floor(this._canvas.width * this._canvas.height * 4));
643-
gl.readPixels(0, 0, this._canvas.width, this._canvas.height, gl.RGBA, gl.UNSIGNED_BYTE, skinPixels);
644-
645-
const skinCanvas = this._canvas;
646-
skinCanvas.width = bounds.width;
647-
skinCanvas.height = bounds.height;
648-
649-
const skinContext = skinCanvas.getContext('2d');
650-
const skinImageData = skinContext.createImageData(bounds.width, bounds.height);
651-
skinImageData.data.set(skinPixels);
652-
skinContext.putImageData(skinImageData, 0, 0);
653-
654-
this._silhouette.update(this._canvas, true /* isPremultiplied */);
649+
gl.readPixels(
650+
0, 0,
651+
this._canvas.width, this._canvas.height,
652+
gl.RGBA, gl.UNSIGNED_BYTE, this._silhouettePixels
653+
);
654+
655+
this._silhouetteImageData.data.set(this._silhouettePixels);
656+
this._silhouette.update(this._silhouetteImageData, true /* isPremultiplied */);
655657

656658
this._silhouetteDirty = false;
657659
}

0 commit comments

Comments
 (0)
Please sign in to comment.