@@ -88,17 +88,27 @@ class Silhouette {
88
88
* rendering can be queried from.
89
89
*/
90
90
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 ) ;
98
111
}
99
- ctx . clearRect ( 0 , 0 , width , height ) ;
100
- ctx . drawImage ( bitmapData , 0 , 0 , width , height ) ;
101
- const imageData = ctx . getImageData ( 0 , 0 , width , height ) ;
102
112
103
113
this . _data = new Uint8ClampedArray ( imageData . data . length / 4 ) ;
104
114
this . _colorData = imageData . data ;
0 commit comments