@@ -99,7 +99,6 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
99
99
console . log ( emoji ) ;
100
100
101
101
return emoji === undefined ? true : textWidth > 0 ;
102
- // return textWidth > 0;
103
102
} ;
104
103
105
104
const handleDownload = ( ) => {
@@ -122,6 +121,8 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
122
121
123
122
ctx . drawImage ( img , 0 , 0 , img . width , img . height ) ;
124
123
124
+ const imagePromises = [ ] ;
125
+
125
126
faceAreas . forEach ( ( area ) => {
126
127
const { width, height, fontSize } = getAreaSize ( img , area ) ;
127
128
@@ -134,20 +135,16 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
134
135
blurredCanvas . width = img . width ;
135
136
blurredCanvas . height = img . height ;
136
137
137
- ctx . drawImage ( img , 0 , 0 , img . width , img . height ) ;
138
138
blurredCtx . filter = 'blur(20px)' ;
139
139
blurredCtx . drawImage ( img , 0 , 0 , img . width , img . height ) ;
140
140
141
- faceAreas . forEach ( ( area ) => {
142
- const faceImageData = blurredCtx . getImageData (
143
- area . leftCol ,
144
- area . topRow ,
145
- width ,
146
- height
147
- ) ;
148
-
149
- ctx . putImageData ( faceImageData , area . leftCol , area . topRow ) ;
150
- } ) ;
141
+ const faceImageData = blurredCtx . getImageData (
142
+ area . leftCol ,
143
+ area . topRow ,
144
+ width ,
145
+ height
146
+ ) ;
147
+ ctx . putImageData ( faceImageData , area . leftCol , area . topRow ) ;
151
148
}
152
149
153
150
if ( Object . keys ( emojiMap ) . includes ( filterType ) ) {
@@ -162,23 +159,59 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
162
159
area . leftCol + width / 2 ,
163
160
area . topRow + height / 2
164
161
) ;
162
+ } else {
163
+ const imgPromise = new Promise ( ( resolve , reject ) => {
164
+ const filterImg = new Image ( ) ;
165
+ filterImg . src = `/images/${ filterType } .png` ;
166
+ filterImg . onload = ( ) => {
167
+ const aspectRatio = filterImg . width / filterImg . height ;
168
+ let scaledWidth = width ;
169
+ let scaledHeight = height ;
170
+
171
+ if ( scaledWidth / aspectRatio > scaledHeight ) {
172
+ scaledHeight = scaledWidth / aspectRatio ;
173
+ } else {
174
+ scaledWidth = scaledHeight * aspectRatio ;
175
+ }
176
+
177
+ const offsetX = area . leftCol + ( width - scaledWidth ) / 2 ;
178
+ const offsetY = area . topRow + ( height - scaledHeight ) / 2 ;
179
+
180
+ ctx . drawImage (
181
+ filterImg ,
182
+ offsetX ,
183
+ offsetY ,
184
+ scaledWidth ,
185
+ scaledHeight
186
+ ) ;
187
+
188
+ resolve ( ) ;
189
+ } ;
190
+ filterImg . onerror = ( error ) => {
191
+ console . error ( 'Error loading filter image:' , error ) ;
192
+ reject ( error ) ;
193
+ } ;
194
+ } ) ;
195
+
196
+ imagePromises . push ( imgPromise ) ;
165
197
}
166
- } else {
167
- const img = new Image ( ) ;
168
- img . src = `/images/${ filterType } .png` ;
169
- img . onload = ( ) => {
170
- ctx . drawImage ( img , area . leftCol , area . topRow , width , height ) ;
171
- } ;
172
198
}
173
199
} ) ;
174
200
175
- canvas . toBlob ( ( blob ) => {
176
- const link = document . createElement ( 'a' ) ;
177
- link . href = URL . createObjectURL ( blob ) ;
178
- link . download = 'filtered_image.png' ;
179
- link . click ( ) ;
180
- setIsDownloading ( false ) ;
181
- } , 'image/png' ) ;
201
+ Promise . all ( imagePromises )
202
+ . then ( ( ) => {
203
+ canvas . toBlob ( ( blob ) => {
204
+ const link = document . createElement ( 'a' ) ;
205
+ link . href = URL . createObjectURL ( blob ) ;
206
+ link . download = 'filtered_image.png' ;
207
+ link . click ( ) ;
208
+ setIsDownloading ( false ) ;
209
+ } , 'image/png' ) ;
210
+ } )
211
+ . catch ( ( error ) => {
212
+ console . error ( 'Error loading filter images:' , error ) ;
213
+ setIsDownloading ( false ) ;
214
+ } ) ;
182
215
} ;
183
216
184
217
img . onerror = ( ) => {
0 commit comments