Skip to content

Commit 7f174f5

Browse files
committed
feat: Midjourney Redraw Helper
1 parent 21df7c6 commit 7f174f5

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

app/components/ImageMask.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = () => {

app/pages/Midjourney.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ const ModalForm = (props: {
907907
originalImageUrl={originalImageUrl}
908908
onClose={() => setShowImageMaskModal(false)}
909909
onFinished={(maskBase64) => {
910+
console.log("maskBase64", maskBase64);
910911
props.form.setFieldsValue({maskBase64});
911912
setShowImageMaskModal(false);
912913
}}

0 commit comments

Comments
 (0)