@@ -159,27 +159,36 @@ class pixelit {
159
159
* Draws a pixelated version of an image in a given canvas
160
160
*/
161
161
pixelate ( ) {
162
- this . drawto . width = this . drawfrom . width ;
163
- this . drawto . height = this . drawfrom . height ;
164
-
165
- const scaledW = this . drawto . width * this . scale ;
166
- const scaledH = this . drawto . height * this . scale ;
167
-
168
- //var ctx = canvas.getContext("2d");
169
-
170
- this . ctx . mozImageSmoothingEnabled = false ;
171
- this . ctx . webkitImageSmoothingEnabled = false ;
172
- this . ctx . imageSmoothingEnabled = false ;
162
+ this . drawto . width = this . drawfrom . naturalWidth ;
163
+ this . drawto . height = this . drawfrom . naturalHeight ;
173
164
174
- //previous method, would failt for background transparent images
175
- // this.ctx.drawImage(this.drawfrom, 0, 0, scaledW, scaledH) ;
165
+ let scaledW = this . drawto . width * this . scale ;
166
+ let scaledH = this . drawto . height * this . scale ;
176
167
177
168
//make temporary canvas to make new scaled copy
178
169
const tempCanvas = document . createElement ( "canvas" ) ;
170
+
171
+ //corner case of bigger images, increase the temporary canvas size to fit everything
172
+ if ( this . drawto . width > 800 || this . drawto . width > 800 ) {
173
+ //fix sclae to pixelate bigger images
174
+ this . scale *= 0.25 ;
175
+ scaledW = this . drawto . width * this . scale ;
176
+ scaledH = this . drawto . height * this . scale ;
177
+ //make it big enough to fit
178
+ tempCanvas . width = Math . max ( scaledW , scaledH ) + 50 ;
179
+ tempCanvas . height = Math . max ( scaledW , scaledH ) + 50 ;
180
+ }
179
181
// get the context
180
182
const tempContext = tempCanvas . getContext ( "2d" ) ;
181
183
// draw the image into the canvas
182
184
tempContext . drawImage ( this . drawfrom , 0 , 0 , scaledW , scaledH ) ;
185
+ document . body . appendChild ( tempCanvas ) ;
186
+
187
+ //configs to pixelate
188
+ this . ctx . mozImageSmoothingEnabled = false ;
189
+ this . ctx . webkitImageSmoothingEnabled = false ;
190
+ this . ctx . imageSmoothingEnabled = false ;
191
+
183
192
//draw to final canvas
184
193
this . ctx . drawImage (
185
194
tempCanvas ,
@@ -189,8 +198,8 @@ class pixelit {
189
198
scaledH ,
190
199
0 ,
191
200
0 ,
192
- this . drawfrom . width ,
193
- this . drawfrom . height
201
+ this . drawfrom . naturalWidth ,
202
+ this . drawfrom . naturalHeight
194
203
) ;
195
204
//remove temp element
196
205
tempCanvas . remove ( ) ;
0 commit comments