maskeditor.js 27.8 KB
Newer Older
1
2
3
import { app } from "../../scripts/app.js";
import { ComfyDialog, $el } from "../../scripts/ui.js";
import { ComfyApp } from "../../scripts/app.js";
4
5
import { api } from "../../scripts/api.js"
import { ClipspaceDialog } from "./clipspace.js";
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

// Helper function to convert a data URL to a Blob object
function dataURLToBlob(dataURL) {
	const parts = dataURL.split(';base64,');
	const contentType = parts[0].split(':')[1];
	const byteString = atob(parts[1]);
	const arrayBuffer = new ArrayBuffer(byteString.length);
	const uint8Array = new Uint8Array(arrayBuffer);
	for (let i = 0; i < byteString.length; i++) {
		uint8Array[i] = byteString.charCodeAt(i);
	}
	return new Blob([arrayBuffer], { type: contentType });
}

function loadedImageToBlob(image) {
	const canvas = document.createElement('canvas');

	canvas.width = image.width;
	canvas.height = image.height;

	const ctx = canvas.getContext('2d');

	ctx.drawImage(image, 0, 0);

	const dataURL = canvas.toDataURL('image/png', 1);
	const blob = dataURLToBlob(dataURL);

	return blob;
}

Dr.Lt.Data's avatar
Dr.Lt.Data committed
36
37
38
39
40
41
42
43
44
45
46
47
function loadImage(imagePath) {
	return new Promise((resolve, reject) => {
		const image = new Image();

		image.onload = function() {
			resolve(image);
		};

		image.src = imagePath;
	});
}

48
async function uploadMask(filepath, formData) {
49
	await api.fetchApi('/upload/mask', {
50
51
52
53
54
55
56
		method: 'POST',
		body: formData
	}).then(response => {}).catch(error => {
		console.error('Error:', error);
	});

	ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']] = new Image();
Jairo Correa's avatar
Jairo Correa committed
57
	ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']].src = api.apiURL("/view?" + new URLSearchParams(filepath).toString() + app.getPreviewFormatParam() + app.getRandParam());
58
59
60
61
62
63
64

	if(ComfyApp.clipspace.images)
		ComfyApp.clipspace.images[ComfyApp.clipspace['selectedIndex']] = filepath;

	ClipspaceDialog.invalidatePreview();
}

65
function prepare_mask(image, maskCanvas, maskCtx, maskColor) {
66
	// paste mask data into alpha channel
Dr.Lt.Data's avatar
Dr.Lt.Data committed
67
68
	maskCtx.drawImage(image, 0, 0, maskCanvas.width, maskCanvas.height);
	const maskData = maskCtx.getImageData(0, 0, maskCanvas.width, maskCanvas.height);
69

Dr.Lt.Data's avatar
Dr.Lt.Data committed
70
71
72
73
	// invert mask
	for (let i = 0; i < maskData.data.length; i += 4) {
		if(maskData.data[i+3] == 255)
			maskData.data[i+3] = 0;
74
		else
Dr.Lt.Data's avatar
Dr.Lt.Data committed
75
			maskData.data[i+3] = 255;
76

77
78
79
		maskData.data[i] = maskColor.r;
		maskData.data[i+1] = maskColor.g;
		maskData.data[i+2] = maskColor.b;
80
81
	}

Dr.Lt.Data's avatar
Dr.Lt.Data committed
82
83
	maskCtx.globalCompositeOperation = 'source-over';
	maskCtx.putImageData(maskData, 0, 0);
84
85
86
87
}

class MaskEditorDialog extends ComfyDialog {
	static instance = null;
88
89
90
91
92
93
94
95
96
97
98

	static getInstance() {
		if(!MaskEditorDialog.instance) {
			MaskEditorDialog.instance = new MaskEditorDialog(app);
		}

		return MaskEditorDialog.instance;
	}

	is_layout_created =  false;

99
100
101
102
103
104
105
106
107
108
109
110
111
112
	constructor() {
		super();
		this.element = $el("div.comfy-modal", { parent: document.body }, 
			[ $el("div.comfy-modal-content", 
				[...this.createButtons()]),
			]);
	}

	createButtons() {
		return [];
	}

	createButton(name, callback) {
		var button = document.createElement("button");
113
		button.style.pointerEvents = "auto";
114
115
116
117
		button.innerText = name;
		button.addEventListener("click", callback);
		return button;
	}
118

119
120
121
122
123
124
	createLeftButton(name, callback) {
		var button = this.createButton(name, callback);
		button.style.cssFloat = "left";
		button.style.marginRight = "4px";
		return button;
	}
125

126
127
128
129
130
131
	createRightButton(name, callback) {
		var button = this.createButton(name, callback);
		button.style.cssFloat = "right";
		button.style.marginLeft = "4px";
		return button;
	}
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	createLeftSlider(self, name, callback) {
		const divElement = document.createElement('div');
		divElement.id = "maskeditor-slider";
		divElement.style.cssFloat = "left";
		divElement.style.fontFamily = "sans-serif";
		divElement.style.marginRight = "4px";
		divElement.style.color = "var(--input-text)";
		divElement.style.backgroundColor = "var(--comfy-input-bg)";
		divElement.style.borderRadius = "8px";
		divElement.style.borderColor = "var(--border-color)";
		divElement.style.borderStyle = "solid";
		divElement.style.fontSize = "15px";
		divElement.style.height = "21px";
		divElement.style.padding = "1px 6px";
		divElement.style.display = "flex";
		divElement.style.position = "relative";
		divElement.style.top = "2px";
150
		divElement.style.pointerEvents = "auto";
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
		self.brush_slider_input = document.createElement('input');
		self.brush_slider_input.setAttribute('type', 'range');
		self.brush_slider_input.setAttribute('min', '1');
		self.brush_slider_input.setAttribute('max', '100');
		self.brush_slider_input.setAttribute('value', '10');
		const labelElement = document.createElement("label");
		labelElement.textContent = name;

		divElement.appendChild(labelElement);
		divElement.appendChild(self.brush_slider_input);

		self.brush_slider_input.addEventListener("change", callback);

		return divElement;
	}

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
	createOpacitySlider(self, name, callback) {
		const divElement = document.createElement('div');
		divElement.id = "maskeditor-opacity-slider";
		divElement.style.cssFloat = "left";
		divElement.style.fontFamily = "sans-serif";
		divElement.style.marginRight = "4px";
		divElement.style.color = "var(--input-text)";
		divElement.style.backgroundColor = "var(--comfy-input-bg)";
		divElement.style.borderRadius = "8px";
		divElement.style.borderColor = "var(--border-color)";
		divElement.style.borderStyle = "solid";
		divElement.style.fontSize = "15px";
		divElement.style.height = "21px";
		divElement.style.padding = "1px 6px";
		divElement.style.display = "flex";
		divElement.style.position = "relative";
		divElement.style.top = "2px";
		divElement.style.pointerEvents = "auto";
		self.opacity_slider_input = document.createElement('input');
		self.opacity_slider_input.setAttribute('type', 'range');
		self.opacity_slider_input.setAttribute('min', '0.1');
		self.opacity_slider_input.setAttribute('max', '1.0');
		self.opacity_slider_input.setAttribute('step', '0.01')
		self.opacity_slider_input.setAttribute('value', '0.7');
		const labelElement = document.createElement("label");
		labelElement.textContent = name;

		divElement.appendChild(labelElement);
		divElement.appendChild(self.opacity_slider_input);

		self.opacity_slider_input.addEventListener("input", callback);

		return divElement;
	}

202
203
204
205
206
207
208
209
210
211
212
	setlayout(imgCanvas, maskCanvas) {
		const self = this;

		// If it is specified as relative, using it only as a hidden placeholder for padding is recommended
		// to prevent anomalies where it exceeds a certain size and goes outside of the window.
		var bottom_panel = document.createElement("div");
		bottom_panel.style.position = "absolute";
		bottom_panel.style.bottom = "0px";
		bottom_panel.style.left = "20px";
		bottom_panel.style.right = "20px";
		bottom_panel.style.height = "50px";
213
		bottom_panel.style.pointerEvents = "none";
214
215
216
217
218
219
220
221
222
223

		var brush = document.createElement("div");
		brush.id = "brush";
		brush.style.backgroundColor = "transparent";
		brush.style.outline = "1px dashed black";
		brush.style.boxShadow = "0 0 0 1px white";
		brush.style.borderRadius = "50%";
		brush.style.MozBorderRadius = "50%";
		brush.style.WebkitBorderRadius = "50%";
		brush.style.position = "absolute";
224
		brush.style.zIndex = 8889;
225
226
227
228
229
230
231
		brush.style.pointerEvents = "none";
		this.brush = brush;
		this.element.appendChild(imgCanvas);
		this.element.appendChild(maskCanvas);
		this.element.appendChild(bottom_panel);
		document.body.appendChild(brush);

232
233
234
235
		var clearButton = this.createLeftButton("Clear", () => {
			self.maskCtx.clearRect(0, 0, self.maskCanvas.width, self.maskCanvas.height);
		});
		
Dr.Lt.Data's avatar
Dr.Lt.Data committed
236
		this.brush_size_slider = this.createLeftSlider(self, "Thickness", (event) => {
237
238
239
			self.brush_size = event.target.value;
			self.updateBrushPreview(self, null, null);
		});
240

241
242
243
244
245
246
247
		this.brush_opacity_slider = this.createOpacitySlider(self, "Opacity", (event) => {
			self.brush_opacity = event.target.value;
			if (self.brush_color_mode !== "negative") {
			    self.maskCanvas.style.opacity = self.brush_opacity;
			}
		});

248
249
250
251
252
253
254
255
256
257
258
259
260
261
		this.colorButton = this.createLeftButton(this.getColorButtonText(), () => {
			if (self.brush_color_mode === "black") {
				self.brush_color_mode = "white";
			}
			else if (self.brush_color_mode === "white") {
				self.brush_color_mode = "negative";
			}
			else {
				self.brush_color_mode = "black";
			}

			self.updateWhenBrushColorModeChanged();
		});

262
263
264
265
266
		var cancelButton = this.createRightButton("Cancel", () => {
			document.removeEventListener("mouseup", MaskEditorDialog.handleMouseUp);
			document.removeEventListener("keydown", MaskEditorDialog.handleKeyDown);
			self.close();
		});
267
268

		this.saveButton = this.createRightButton("Save", () => {
269
270
271
272
273
274
275
276
277
278
			document.removeEventListener("mouseup", MaskEditorDialog.handleMouseUp);
			document.removeEventListener("keydown", MaskEditorDialog.handleKeyDown);
				self.save();
			});

		this.element.appendChild(imgCanvas);
		this.element.appendChild(maskCanvas);
		this.element.appendChild(bottom_panel);

		bottom_panel.appendChild(clearButton);
279
		bottom_panel.appendChild(this.saveButton);
280
		bottom_panel.appendChild(cancelButton);
Dr.Lt.Data's avatar
Dr.Lt.Data committed
281
		bottom_panel.appendChild(this.brush_size_slider);
282
		bottom_panel.appendChild(this.brush_opacity_slider);
283
		bottom_panel.appendChild(this.colorButton);
Dr.Lt.Data's avatar
Dr.Lt.Data committed
284
285
286

		imgCanvas.style.position = "absolute";
		maskCanvas.style.position = "absolute";
287
288
289
290

		imgCanvas.style.top = "200";
		imgCanvas.style.left = "0";

Dr.Lt.Data's avatar
Dr.Lt.Data committed
291
292
		maskCanvas.style.top = imgCanvas.style.top;
		maskCanvas.style.left = imgCanvas.style.left;
293
294
295
296

		const maskCanvasStyle = this.getMaskCanvasStyle();
		maskCanvas.style.mixBlendMode = maskCanvasStyle.mixBlendMode;
		maskCanvas.style.opacity = maskCanvasStyle.opacity;
297
298
	}

Dr.Lt.Data's avatar
Dr.Lt.Data committed
299
300
301
302
303
	async show() {
		this.zoom_ratio = 1.0;
		this.pan_x = 0;
		this.pan_y = 0;

304
305
306
307
308
309
310
311
312
313
314
315
316
		if(!this.is_layout_created) {
			// layout
			const imgCanvas = document.createElement('canvas');
			const maskCanvas = document.createElement('canvas');

			imgCanvas.id = "imageCanvas";
			maskCanvas.id = "maskCanvas";

			this.setlayout(imgCanvas, maskCanvas);

			// prepare content
			this.imgCanvas = imgCanvas;
			this.maskCanvas = maskCanvas;
Dr.Lt.Data's avatar
Dr.Lt.Data committed
317
			this.maskCtx = maskCanvas.getContext('2d', {willReadFrequently: true });
318
319
320
321
322
323
324
325
326
327
328

			this.setEventHandler(maskCanvas);

			this.is_layout_created = true;

			// replacement of onClose hook since close is not real close
			const self = this;
			const observer = new MutationObserver(function(mutations) {
			mutations.forEach(function(mutation) {
					if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
						if(self.last_display_style && self.last_display_style != 'none' && self.element.style.display == 'none') {
Dr.Lt.Data's avatar
Dr.Lt.Data committed
329
330
							document.removeEventListener("mouseup", MaskEditorDialog.handleMouseUp);
							self.brush.style.display = "none";
331
332
333
334
335
336
337
							ComfyApp.onClipspaceEditorClosed();
						}

						self.last_display_style = self.element.style.display;
					}
				});
			});
338

339
340
341
			const config = { attributes: true };
			observer.observe(this.element, config);
		}
342

Dr.Lt.Data's avatar
Dr.Lt.Data committed
343
344
		// The keydown event needs to be reconfigured when closing the dialog as it gets removed.
		document.addEventListener('keydown', MaskEditorDialog.handleKeyDown);
345

346
347
348
349
350
351
352
		if(ComfyApp.clipspace_return_node) {
			this.saveButton.innerText = "Save to node";
		}
		else {
			this.saveButton.innerText = "Save";
		}
		this.saveButton.disabled = false;
353

354
		this.element.style.display = "block";
Dr.Lt.Data's avatar
Dr.Lt.Data committed
355
356
357
358
359
		this.element.style.width = "85%";
		this.element.style.margin = "0 7.5%";
		this.element.style.height = "100vh";
		this.element.style.top = "50%";
		this.element.style.left = "42%";
360
		this.element.style.zIndex = 8888; // NOTE: alert dialog must be high priority.
Dr.Lt.Data's avatar
Dr.Lt.Data committed
361
362
363
364

		await this.setImages(this.imgCanvas);

		this.is_visible = true;
365
366
367
368
	}

	isOpened() {
		return this.element.style.display == "block";
369
370
	}

Dr.Lt.Data's avatar
Dr.Lt.Data committed
371
372
373
374
375
376
377
378
379
380
381
	invalidateCanvas(orig_image, mask_image) {
		this.imgCanvas.width = orig_image.width;
		this.imgCanvas.height = orig_image.height;

		this.maskCanvas.width = orig_image.width;
		this.maskCanvas.height = orig_image.height;

		let imgCtx = this.imgCanvas.getContext('2d', {willReadFrequently: true });
		let maskCtx = this.maskCanvas.getContext('2d', {willReadFrequently: true });

		imgCtx.drawImage(orig_image, 0, 0, orig_image.width, orig_image.height);
382
		prepare_mask(mask_image, this.maskCanvas, maskCtx, this.getMaskColor());
Dr.Lt.Data's avatar
Dr.Lt.Data committed
383
384
385
386
387
388
	}

	async setImages(imgCanvas) {
		let self = this;

		const imgCtx = imgCanvas.getContext('2d', {willReadFrequently: true });
389
390
391
		const maskCtx = this.maskCtx;
		const maskCanvas = this.maskCanvas;

392
393
394
		imgCtx.clearRect(0,0,this.imgCanvas.width,this.imgCanvas.height);
		maskCtx.clearRect(0,0,this.maskCanvas.width,this.maskCanvas.height);

395
396
397
398
399
		// image load
		const filepath = ComfyApp.clipspace.images;

		const alpha_url = new URL(ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']].src)
		alpha_url.searchParams.delete('channel');
400
		alpha_url.searchParams.delete('preview');
401
		alpha_url.searchParams.set('channel', 'a');
Dr.Lt.Data's avatar
Dr.Lt.Data committed
402
		let mask_image = await loadImage(alpha_url);
403
404
405
406
407

		// original image load
		const rgb_url = new URL(ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']].src);
		rgb_url.searchParams.delete('channel');
		rgb_url.searchParams.set('channel', 'rgb');
Dr.Lt.Data's avatar
Dr.Lt.Data committed
408
409
410
411
412
413
414
415
416
		this.image = new Image();
		this.image.onload = function() {
			maskCanvas.width = self.image.width;
			maskCanvas.height = self.image.height;

			self.invalidateCanvas(self.image, mask_image);
			self.initializeCanvasPanZoom();
		};
		this.image.src = rgb_url;
417
	}
418

Dr.Lt.Data's avatar
Dr.Lt.Data committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
	initializeCanvasPanZoom() {
		// set initialize
		let drawWidth = this.image.width;
		let drawHeight = this.image.height;

		let width = this.element.clientWidth;
		let height = this.element.clientHeight;

		if (this.image.width > width) {
			drawWidth = width;
			drawHeight = (drawWidth / this.image.width) * this.image.height;
		}

		if (drawHeight > height) {
			drawHeight = height;
			drawWidth = (drawHeight / this.image.height) * this.image.width;
		}

		this.zoom_ratio = drawWidth/this.image.width;

		const canvasX = (width - drawWidth) / 2;
		const canvasY = (height - drawHeight) / 2;
		this.pan_x = canvasX;
		this.pan_y = canvasY;

		this.invalidatePanZoom();
	}


	invalidatePanZoom() {
		let raw_width = this.image.width * this.zoom_ratio;
		let raw_height = this.image.height * this.zoom_ratio;

		if(this.pan_x + raw_width < 10) {
			this.pan_x = 10 - raw_width;
		}

		if(this.pan_y + raw_height < 10) {
			this.pan_y = 10 - raw_height;
		}

		let width = `${raw_width}px`;
		let height = `${raw_height}px`;
462

Dr.Lt.Data's avatar
Dr.Lt.Data committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
		let left = `${this.pan_x}px`;
		let top = `${this.pan_y}px`;

		this.maskCanvas.style.width = width;
		this.maskCanvas.style.height = height;
		this.maskCanvas.style.left = left;
		this.maskCanvas.style.top = top;

		this.imgCanvas.style.width = width;
		this.imgCanvas.style.height = height;
		this.imgCanvas.style.left = left;
		this.imgCanvas.style.top = top;
	}


	setEventHandler(maskCanvas) {
479
		const self = this;
Dr.Lt.Data's avatar
Dr.Lt.Data committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

		if(!this.handler_registered) {
			maskCanvas.addEventListener("contextmenu", (event) => {
				event.preventDefault();
			});

			this.element.addEventListener('wheel', (event) => this.handleWheelEvent(self,event));
			this.element.addEventListener('pointermove', (event) => this.pointMoveEvent(self,event));
			this.element.addEventListener('touchmove', (event) => this.pointMoveEvent(self,event));

			this.element.addEventListener('dragstart', (event) => {
				if(event.ctrlKey) {
					event.preventDefault();
				}
			});

			maskCanvas.addEventListener('pointerdown', (event) => this.handlePointerDown(self,event));
			maskCanvas.addEventListener('pointermove', (event) => this.draw_move(self,event));
			maskCanvas.addEventListener('touchmove', (event) => this.draw_move(self,event));
			maskCanvas.addEventListener('pointerover', (event) => { this.brush.style.display = "block"; });
			maskCanvas.addEventListener('pointerleave', (event) => { this.brush.style.display = "none"; });

			document.addEventListener('pointerup', MaskEditorDialog.handlePointerUp);

			this.handler_registered = true;
		}
506
507
	}

508
509
510
511
512
513
514
515
516
517
	getMaskCanvasStyle() {
		if (this.brush_color_mode === "negative") {
			return {
				mixBlendMode: "difference",
				opacity: "1",
			};
		}
		else {
			return {
				mixBlendMode: "initial",
518
				opacity: this.brush_opacity,
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
			};
		}
	}

	getMaskColor() {
		if (this.brush_color_mode === "black") {
			return { r: 0, g: 0, b: 0 };
		}
		if (this.brush_color_mode === "white") {
			return { r: 255, g: 255, b: 255 };
		}
		if (this.brush_color_mode === "negative") {
			// negative effect only works with white color
			return { r: 255, g: 255, b: 255 };
		}

		return { r: 0, g: 0, b: 0 };
	}

	getMaskFillStyle() {
		const maskColor = this.getMaskColor();

		return "rgb(" + maskColor.r + "," + maskColor.g + "," + maskColor.b + ")";
	}

	getColorButtonText() {
		let colorCaption = "unknown";

		if (this.brush_color_mode === "black") {
			colorCaption = "black";
		}
		else if (this.brush_color_mode === "white") {
			colorCaption = "white";
		}
		else if (this.brush_color_mode === "negative") {
			colorCaption = "negative";
		}

		return "Color: " + colorCaption;
	}

	updateWhenBrushColorModeChanged() {
		this.colorButton.innerText = this.getColorButtonText();

		// update mask canvas css styles

		const maskCanvasStyle = this.getMaskCanvasStyle();
		this.maskCanvas.style.mixBlendMode = maskCanvasStyle.mixBlendMode;
		this.maskCanvas.style.opacity = maskCanvasStyle.opacity;

		// update mask canvas rgb colors

		const maskColor = this.getMaskColor();

		const maskData = this.maskCtx.getImageData(0, 0, this.maskCanvas.width, this.maskCanvas.height);
		
		for (let i = 0; i < maskData.data.length; i += 4) {
			maskData.data[i] = maskColor.r;
			maskData.data[i+1] = maskColor.g;
			maskData.data[i+2] = maskColor.b;
		}
	
		this.maskCtx.putImageData(maskData, 0, 0);
	}

584
	brush_opacity = 0.7;
585
	brush_size = 10;
586
	brush_color_mode = "black";
587
588
589
590
591
592
593
594
595
	drawing_mode = false;
	lastx = -1;
	lasty = -1;
	lasttime = 0;

	static handleKeyDown(event) {
		const self = MaskEditorDialog.instance;
		if (event.key === ']') {
			self.brush_size = Math.min(self.brush_size+2, 100);
Dr.Lt.Data's avatar
Dr.Lt.Data committed
596
			self.brush_slider_input.value = self.brush_size;
597
598
		} else if (event.key === '[') {
			self.brush_size = Math.max(self.brush_size-2, 1);
Dr.Lt.Data's avatar
Dr.Lt.Data committed
599
			self.brush_slider_input.value = self.brush_size;
600
601
		} else if(event.key === 'Enter') {
			self.save();
602
603
604
605
606
607
608
		}

		self.updateBrushPreview(self);
	}

	static handlePointerUp(event) {
		event.preventDefault();
Dr.Lt.Data's avatar
Dr.Lt.Data committed
609
610
611
612

		this.mousedown_x = null;
		this.mousedown_y = null;

613
614
615
616
617
618
619
620
621
		MaskEditorDialog.instance.drawing_mode = false;
	}

	updateBrushPreview(self) {
		const brush = self.brush;

		var centerX = self.cursorX;
		var centerY = self.cursorY;

Dr.Lt.Data's avatar
Dr.Lt.Data committed
622
623
624
625
		brush.style.width = self.brush_size * 2 * this.zoom_ratio + "px";
		brush.style.height = self.brush_size * 2 * this.zoom_ratio + "px";
		brush.style.left = (centerX - self.brush_size * this.zoom_ratio) + "px";
		brush.style.top = (centerY - self.brush_size * this.zoom_ratio) + "px";
626
627
628
	}

	handleWheelEvent(self, event) {
Dr.Lt.Data's avatar
Dr.Lt.Data committed
629
630
631
632
633
634
635
636
637
638
		event.preventDefault();

		if(event.ctrlKey) {
			// zoom canvas
			if(event.deltaY < 0) {
				this.zoom_ratio = Math.min(10.0, this.zoom_ratio+0.2);
			}
			else {
				this.zoom_ratio = Math.max(0.2, this.zoom_ratio-0.2);
			}
639

Dr.Lt.Data's avatar
Dr.Lt.Data committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
			this.invalidatePanZoom();
		}
		else {
			// adjust brush size
			if(event.deltaY < 0)
				this.brush_size = Math.min(this.brush_size+2, 100);
			else
				this.brush_size = Math.max(this.brush_size-2, 1);

			this.brush_slider_input.value = this.brush_size;

			this.updateBrushPreview(this);
		}
	}

	pointMoveEvent(self, event) {
		this.cursorX = event.pageX;
		this.cursorY = event.pageY;
658
659

		self.updateBrushPreview(self);
Dr.Lt.Data's avatar
Dr.Lt.Data committed
660
661
662
663
664

		if(event.ctrlKey) {
			event.preventDefault();
			self.pan_move(self, event);
		}
665
666
667
668
669
670
671
672
673
674
675
676
677

		let left_button_down = window.TouchEvent && event instanceof TouchEvent || event.buttons == 1;

		if(event.shiftKey && left_button_down) {
			self.drawing_mode = false;

			const y = event.clientY;
			let delta = (self.zoom_lasty - y)*0.005;
			self.zoom_ratio = Math.max(Math.min(10.0, self.last_zoom_ratio - delta), 0.2);

			this.invalidatePanZoom();
			return;
		}
Dr.Lt.Data's avatar
Dr.Lt.Data committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
	}

	pan_move(self, event) {
		if(event.buttons == 1) {
			if(this.mousedown_x) {
				let deltaX = this.mousedown_x - event.clientX;
				let deltaY = this.mousedown_y - event.clientY;

				self.pan_x = this.mousedown_pan_x - deltaX;
				self.pan_y = this.mousedown_pan_y - deltaY;

				self.invalidatePanZoom();
			}
		}
692
693
694
	}

	draw_move(self, event) {
695
		if(event.ctrlKey || event.shiftKey) {
Dr.Lt.Data's avatar
Dr.Lt.Data committed
696
697
698
			return;
		}

699
700
701
702
703
704
705
		event.preventDefault();

		this.cursorX = event.pageX;
		this.cursorY = event.pageY;

		self.updateBrushPreview(self);

706
707
708
709
		let left_button_down = window.TouchEvent && event instanceof TouchEvent || event.buttons == 1;
		let right_button_down = [2, 5, 32].includes(event.buttons);

		if (!event.altKey && left_button_down) {
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
			var diff = performance.now() - self.lasttime;

			const maskRect = self.maskCanvas.getBoundingClientRect();

			var x = event.offsetX;
			var y = event.offsetY

			if(event.offsetX == null) {
				x = event.targetTouches[0].clientX - maskRect.left;
			}

			if(event.offsetY == null) {
				y = event.targetTouches[0].clientY - maskRect.top;
			}

Dr.Lt.Data's avatar
Dr.Lt.Data committed
725
726
727
			x /= self.zoom_ratio;
			y /= self.zoom_ratio;

728
729
730
731
732
			var brush_size = this.brush_size;
			if(event instanceof PointerEvent && event.pointerType == 'pen') {
				brush_size *= event.pressure;
				this.last_pressure = event.pressure;
			}
733
			else if(window.TouchEvent && event instanceof TouchEvent && diff < 20){
734
735
736
737
738
739
740
741
742
743
				// The firing interval of PointerEvents in Pen is unreliable, so it is supplemented by TouchEvents.
				brush_size *= this.last_pressure;
			}
			else {
				brush_size = this.brush_size;
			}

			if(diff > 20 && !this.drawing_mode)
				requestAnimationFrame(() => {
					self.maskCtx.beginPath();
744
					self.maskCtx.fillStyle = this.getMaskFillStyle();
745
746
747
748
749
750
751
752
753
					self.maskCtx.globalCompositeOperation = "source-over";
					self.maskCtx.arc(x, y, brush_size, 0, Math.PI * 2, false);
					self.maskCtx.fill();
					self.lastx = x;
					self.lasty = y;
				});
			else
				requestAnimationFrame(() => {
					self.maskCtx.beginPath();
754
					self.maskCtx.fillStyle = this.getMaskFillStyle();
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
					self.maskCtx.globalCompositeOperation = "source-over";

					var dx = x - self.lastx;
					var dy = y - self.lasty;

					var distance = Math.sqrt(dx * dx + dy * dy);
					var directionX = dx / distance;
					var directionY = dy / distance;

					for (var i = 0; i < distance; i+=5) {
						var px = self.lastx + (directionX * i);
						var py = self.lasty + (directionY * i);
						self.maskCtx.arc(px, py, brush_size, 0, Math.PI * 2, false);
						self.maskCtx.fill();
					}
					self.lastx = x;
					self.lasty = y;
				});

			self.lasttime = performance.now();
		}
776
		else if((event.altKey && left_button_down) || right_button_down) {
777
			const maskRect = self.maskCanvas.getBoundingClientRect();
Dr.Lt.Data's avatar
Dr.Lt.Data committed
778
779
			const x = (event.offsetX || event.targetTouches[0].clientX - maskRect.left) / self.zoom_ratio;
			const y = (event.offsetY || event.targetTouches[0].clientY - maskRect.top) / self.zoom_ratio;
780
781
782
783
784
785

			var brush_size = this.brush_size;
			if(event instanceof PointerEvent && event.pointerType == 'pen') {
				brush_size *= event.pressure;
				this.last_pressure = event.pressure;
			}
786
			else if(window.TouchEvent && event instanceof TouchEvent && diff < 20){
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
				brush_size *= this.last_pressure;
			}
			else {
				brush_size = this.brush_size;
			}

			if(diff > 20 && !drawing_mode) // cannot tracking drawing_mode for touch event
				requestAnimationFrame(() => {
					self.maskCtx.beginPath();
					self.maskCtx.globalCompositeOperation = "destination-out";
					self.maskCtx.arc(x, y, brush_size, 0, Math.PI * 2, false);
					self.maskCtx.fill();
					self.lastx = x;
					self.lasty = y;
				});
			else
				requestAnimationFrame(() => {
					self.maskCtx.beginPath();
					self.maskCtx.globalCompositeOperation = "destination-out";
					
					var dx = x - self.lastx;
					var dy = y - self.lasty;

					var distance = Math.sqrt(dx * dx + dy * dy);
					var directionX = dx / distance;
					var directionY = dy / distance;

					for (var i = 0; i < distance; i+=5) {
						var px = self.lastx + (directionX * i);
						var py = self.lasty + (directionY * i);
						self.maskCtx.arc(px, py, brush_size, 0, Math.PI * 2, false);
						self.maskCtx.fill();
					}
					self.lastx = x;
					self.lasty = y;
				});

				self.lasttime = performance.now();
		}
	}

	handlePointerDown(self, event) {
Dr.Lt.Data's avatar
Dr.Lt.Data committed
829
830
831
832
833
834
835
836
837
838
839
		if(event.ctrlKey) {
			if (event.buttons == 1) {
				this.mousedown_x = event.clientX;
				this.mousedown_y = event.clientY;

				this.mousedown_pan_x = this.pan_x;
				this.mousedown_pan_y = this.pan_y;
			}
			return;
		}

840
841
842
843
844
845
846
847
848
849
		var brush_size = this.brush_size;
		if(event instanceof PointerEvent && event.pointerType == 'pen') {
			brush_size *= event.pressure;
			this.last_pressure = event.pressure;
		}

		if ([0, 2, 5].includes(event.button)) {
			self.drawing_mode = true;

			event.preventDefault();
850
851
852
853
854
855
856

			if(event.shiftKey) {
				self.zoom_lasty = event.clientY;
				self.last_zoom_ratio = self.zoom_ratio;
				return;
			}

857
			const maskRect = self.maskCanvas.getBoundingClientRect();
Dr.Lt.Data's avatar
Dr.Lt.Data committed
858
859
			const x = (event.offsetX || event.targetTouches[0].clientX - maskRect.left) / self.zoom_ratio;
			const y = (event.offsetY || event.targetTouches[0].clientY - maskRect.top) / self.zoom_ratio;
860
861

			self.maskCtx.beginPath();
862
			if (!event.altKey && event.button == 0) {
863
				self.maskCtx.fillStyle = this.getMaskFillStyle();
864
865
866
867
868
869
870
871
872
873
874
875
				self.maskCtx.globalCompositeOperation = "source-over";
			} else {
				self.maskCtx.globalCompositeOperation = "destination-out";
			}
			self.maskCtx.arc(x, y, brush_size, 0, Math.PI * 2, false);
			self.maskCtx.fill();
			self.lastx = x;
			self.lasty = y;
			self.lasttime = performance.now();
		}
	}

876
	async save() {
Dr.Lt.Data's avatar
Dr.Lt.Data committed
877
878
879
880
		const backupCanvas = document.createElement('canvas');
		const backupCtx = backupCanvas.getContext('2d', {willReadFrequently:true});
		backupCanvas.width = this.image.width;
		backupCanvas.height = this.image.height;
881

Dr.Lt.Data's avatar
Dr.Lt.Data committed
882
		backupCtx.clearRect(0,0, backupCanvas.width, backupCanvas.height);
883
884
		backupCtx.drawImage(this.maskCanvas,
			0, 0, this.maskCanvas.width, this.maskCanvas.height,
Dr.Lt.Data's avatar
Dr.Lt.Data committed
885
			0, 0, backupCanvas.width, backupCanvas.height);
886
887

		// paste mask data into alpha channel
Dr.Lt.Data's avatar
Dr.Lt.Data committed
888
		const backupData = backupCtx.getImageData(0, 0, backupCanvas.width, backupCanvas.height);
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910

		// refine mask image
		for (let i = 0; i < backupData.data.length; i += 4) {
			if(backupData.data[i+3] == 255)
				backupData.data[i+3] = 0;
			else
				backupData.data[i+3] = 255;

			backupData.data[i] = 0;
			backupData.data[i+1] = 0;
			backupData.data[i+2] = 0;
		}

		backupCtx.globalCompositeOperation = 'source-over';
		backupCtx.putImageData(backupData, 0, 0);

		const formData = new FormData();
		const filename = "clipspace-mask-" + performance.now() + ".png";

		const item =
			{
				"filename": filename,
comfyanonymous's avatar
comfyanonymous committed
911
912
				"subfolder": "clipspace",
				"type": "input",
913
914
915
916
917
918
919
920
921
922
923
924
			};

		if(ComfyApp.clipspace.images)
			ComfyApp.clipspace.images[0] = item;

		if(ComfyApp.clipspace.widgets) {
			const index = ComfyApp.clipspace.widgets.findIndex(obj => obj.name === 'image');

			if(index >= 0)
				ComfyApp.clipspace.widgets[index].value = item;
		}

Dr.Lt.Data's avatar
Dr.Lt.Data committed
925
		const dataURL = backupCanvas.toDataURL();
926
927
		const blob = dataURLToBlob(dataURL);

928
929
930
931
932
933
934
935
936
937
938
		let original_url = new URL(this.image.src);

		const original_ref = { filename: original_url.searchParams.get('filename') };

		let original_subfolder = original_url.searchParams.get("subfolder");
		if(original_subfolder)
			original_ref.subfolder = original_subfolder;

		let original_type = original_url.searchParams.get("type");
		if(original_type)
			original_ref.type = original_type;
939
940

		formData.append('image', blob, filename);
941
		formData.append('original_ref', JSON.stringify(original_ref));
comfyanonymous's avatar
comfyanonymous committed
942
943
		formData.append('type', "input");
		formData.append('subfolder', "clipspace");
944

945
946
947
948
		this.saveButton.innerText = "Saving...";
		this.saveButton.disabled = true;
		await uploadMask(item, formData);
		ComfyApp.onClipspaceEditorSave();
949
950
951
952
953
954
955
		this.close();
	}
}

app.registerExtension({
	name: "Comfy.MaskEditor",
	init(app) {
956
		ComfyApp.open_maskeditor =
957
			function () {
958
959
960
961
				const dlg = MaskEditorDialog.getInstance();
				if(!dlg.isOpened()) {
					dlg.show();
				}
962
963
964
			};

		const context_predicate = () => ComfyApp.clipspace && ComfyApp.clipspace.imgs && ComfyApp.clipspace.imgs.length > 0
965
		ClipspaceDialog.registerButton("MaskEditor", context_predicate, ComfyApp.open_maskeditor);
966
	}
Jairo Correa's avatar
Jairo Correa committed
967
});