Skip to content

Commit be5ab2e

Browse files
committed
receive ImageData directly in Silhouette.update
Given ImageData we can skip drawing the input and getting image data. This can help if update's color can also use the ImageData directly.
1 parent c9f86ef commit be5ab2e

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/Silhouette.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,27 @@ class Silhouette {
8888
* rendering can be queried from.
8989
*/
9090
update (bitmapData) {
91-
const canvas = Silhouette._updateCanvas();
92-
const width = this._width = canvas.width = bitmapData.width;
93-
const height = this._height = canvas.height = bitmapData.height;
94-
const ctx = canvas.getContext('2d');
95-
96-
if (!(width && height)) {
97-
return;
91+
let imageData;
92+
if (bitmapData instanceof ImageData) {
93+
// If handed ImageData directly, use it directly.
94+
imageData = bitmapData;
95+
this._width = bitmapData.width;
96+
this._height = bitmapData.height;
97+
} else {
98+
// Draw about anything else to our update canvas and poll image data
99+
// from that.
100+
const canvas = Silhouette._updateCanvas();
101+
const width = this._width = canvas.width = bitmapData.width;
102+
const height = this._height = canvas.height = bitmapData.height;
103+
const ctx = canvas.getContext('2d');
104+
105+
if (!(width && height)) {
106+
return;
107+
}
108+
ctx.clearRect(0, 0, width, height);
109+
ctx.drawImage(bitmapData, 0, 0, width, height);
110+
imageData = ctx.getImageData(0, 0, width, height);
98111
}
99-
ctx.clearRect(0, 0, width, height);
100-
ctx.drawImage(bitmapData, 0, 0, width, height);
101-
const imageData = ctx.getImageData(0, 0, width, height);
102112

103113
this._data = new Uint8ClampedArray(imageData.data.length / 4);
104114
this._colorData = imageData.data;

0 commit comments

Comments
 (0)