@@ -151,18 +151,35 @@ const ImageMaskModal = (props: {
151151
152152 const getMaskBase64 = ( ) => {
153153 if ( ! maskCanvasRef . current ) return '' ;
154- const tempCanvas = document . createElement ( 'canvas' ) ;
155- tempCanvas . width = maskCanvasRef . current . width ;
156- tempCanvas . height = maskCanvasRef . current . height ;
157- const tempCtx = tempCanvas . getContext ( '2d' ) ;
158- if ( tempCtx ) {
159- tempCtx . fillStyle = 'black' ;
160- tempCtx . fillRect ( 0 , 0 , tempCanvas . width , tempCanvas . height ) ;
161- tempCtx . globalCompositeOperation = 'destination-out' ;
162- tempCtx . drawImage ( maskCanvasRef . current , 0 , 0 ) ;
163- tempCtx . globalCompositeOperation = 'source-over' ;
154+ const canvas = maskCanvasRef . current ;
155+ const ctx = canvas . getContext ( '2d' ) ;
156+
157+ if ( ctx ) {
158+ // 获取图像数据
159+ const imageData = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
160+ const data = imageData . data ;
161+
162+ // 创建新的画布来生成最终的蒙版
163+ const tempCanvas = document . createElement ( 'canvas' ) ;
164+ tempCanvas . width = canvas . width ;
165+ tempCanvas . height = canvas . height ;
166+ const tempCtx = tempCanvas . getContext ( '2d' ) ;
167+
168+ if ( tempCtx ) {
169+ // 填充黑色背景
170+ tempCtx . fillStyle = 'black' ;
171+ tempCtx . fillRect ( 0 , 0 , tempCanvas . width , tempCanvas . height ) ;
172+
173+ // 设置合成操作为 "source-over"
174+ tempCtx . globalCompositeOperation = 'source-over' ;
175+
176+ // 将原始的白色绘制内容直接绘制到黑色背景上
177+ tempCtx . drawImage ( canvas , 0 , 0 ) ;
178+ }
179+
180+ return tempCanvas . toDataURL ( 'image/png' ) . split ( ',' ) [ 1 ] ;
164181 }
165- return tempCanvas . toDataURL ( 'image/png' ) . split ( ',' ) [ 1 ] ;
182+ return '' ;
166183 } ;
167184
168185 const handleFinish = ( ) => {
0 commit comments