-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathclone.js
335 lines (287 loc) · 9.07 KB
/
clone.js
1
2
3
4
5
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import app from './../app.js';
import config from './../config.js';
import Base_tools_class from './../core/base-tools.js';
import Base_layers_class from './../core/base-layers.js';
import Layer_raster_class from './../modules/layer/raster.js';
import alertify from './../../../node_modules/alertifyjs/build/alertify.min.js';
class Clone_class extends Base_tools_class {
constructor(ctx) {
super();
this.Base_layers = new Base_layers_class();
this.Layer_raster = new Layer_raster_class();
this.ctx = ctx;
this.name = 'clone';
this.tmpCanvas = null;
this.tmpCanvasCtx = null;
this.started = false;
this.clone_coords = null;
this.pressTimer = null;
}
load() {
var _this = this;
var is_touch = false;
//mouse events
document.addEventListener('mousedown', function (event) {
if(is_touch)
return;
_this.dragStart(event);
});
document.addEventListener('mousemove', function (event) {
if(is_touch)
return;
_this.dragMove(event);
});
document.addEventListener('mouseup', function (event) {
if(is_touch)
return;
_this.dragEnd(event);
});
// collect touch events
document.addEventListener('touchstart', function (event) {
is_touch = true;
_this.dragStart(event);
});
document.addEventListener('touchmove', function (event) {
_this.dragMove(event);
});
document.addEventListener('touchend', function (event) {
_this.dragEnd(event);
});
document.addEventListener('contextmenu', function (event) {
_this.mouseRightClick(event);
});
}
dragStart(event) {
var _this = this;
if (config.TOOL.name != _this.name)
return;
_this.mousedown(event);
var mouse = this.get_mouse_info(event);
if (mouse.click_valid == true) {
this.pressTimer = window.setTimeout(function() {
//long press success
_this.mouseLongClick();
}, 2000);
}
}
dragMove(event) {
var _this = this;
if (config.TOOL.name != _this.name)
return;
_this.mousemove(event);
//mouse cursor
var mouse = _this.get_mouse_info(event);
var params = _this.getParams();
_this.show_mouse_cursor(mouse.x, mouse.y, params.size, 'circle');
clearTimeout(this.pressTimer);
}
dragEnd(event) {
var _this = this;
if (config.TOOL.name != _this.name)
return;
_this.mouseup(event);
clearTimeout(this.pressTimer);
}
on_params_update() {
var params = this.getParams();
var strict_element = document.getElementById('strict');
if (params.circle == false) {
//hide strict controls
strict_element.style.display = 'none';
}
else {
//show strict controls
strict_element.style.display = 'block';
}
}
mouseRightClick(e) {
if (config.TOOL.name != this.name)
return;
var mouse = this.get_mouse_info(e);
var params = this.getParams();
if (e.which == 3 && mouse.valid == true) {
e.preventDefault();
}
if (params.source_layer.value == 'Previous' && config.layer.type === null) {
this.Layer_raster.raster();
}
if (config.layer.type != 'image') {
alertify.error('This layer must contain an image. Please convert it to raster to apply this tool.');
return;
}
if (config.layer.rotate || 0 > 0) {
alertify.error('Erase on rotate object is disabled. Please rasterize first.');
return;
}
if (e.which == 3 && mouse.valid == true) {
//right click - save coords
var mouse_x = this.adaptSize(mouse.x, 'width');
var mouse_y = this.adaptSize(mouse.y, 'height');
this.clone_coords = {
x: mouse_x,
y: mouse_y,
};
alertify.success('Source coordinates saved.');
}
}
mouseLongClick(){
var params = this.getParams();
var mouse = this.get_mouse_info();
if (params.source_layer.value == 'Previous' && config.layer.type === null) {
this.Layer_raster.raster();
}
if (config.layer.type != 'image') {
alertify.error('This layer must contain an image. Please convert it to raster to apply this tool.');
return;
}
if (config.layer.rotate || 0 > 0) {
alertify.error('Erase on rotate object is disabled. Please rasterize first.');
return;
}
var mouse_x = this.adaptSize(mouse.x, 'width');
var mouse_y = this.adaptSize(mouse.y, 'height');
this.clone_coords = {
x: mouse_x,
y: mouse_y,
};
alertify.success('Source coordinates saved.');
}
mousedown(e) {
this.started = false;
var mouse = this.get_mouse_info(e);
var params = this.getParams();
var layer = config.layer;
var previous_layer = this.Base_layers.find_previous(config.layer.id);
if (mouse.click_valid == false) {
return;
}
if (params.source_layer.value == 'Previous' && config.layer.type === null) {
this.Layer_raster.raster();
}
if (config.layer.type != 'image') {
alertify.error('This layer must contain an image. Please convert it to raster to apply this tool.');
return;
}
if (config.layer.rotate || 0 > 0) {
alertify.error('Erase on rotate object is disabled. Please rasterize first.');
return;
}
if (this.clone_coords === null) {
alertify.error('Source is empty, right click on image or use long press to save source position.');
return;
}
if (layer.width != layer.width_original || layer.height != layer.height_original) {
alertify.error('Clone tool disabled for resized image. Please rasterize first.');
return;
}
if (params.source_layer.value == 'Previous' &&
(previous_layer.width != previous_layer.width_original
|| previous_layer.height != previous_layer.height_original)) {
alertify.error('Clone tool disabled for resized image. Please rasterize first.');
return;
}
if (params.source_layer.value == 'Previous') {
if (previous_layer == null) {
alertify.error('Can not find previous layer.');
return;
}
if (previous_layer.type != 'image') {
alertify.error('Previous layer must be image, convert it to raster to apply this tool.');
return;
}
}
this.started = true;
//get canvas from layer
this.tmpCanvas = document.createElement('canvas');
this.tmpCanvasCtx = this.tmpCanvas.getContext("2d");
this.tmpCanvas.width = config.layer.width_original;
this.tmpCanvas.height = config.layer.height_original;
this.tmpCanvasCtx.drawImage(config.layer.link, 0, 0);
//clone
this.clone_general(this.tmpCanvas, this.tmpCanvas, 'click', mouse);
//register tmp canvas for progress redraw
config.layer.link_canvas = this.tmpCanvas;
config.need_render = true;
}
mousemove(e) {
var mouse = this.get_mouse_info(e);
var params = this.getParams();
if (mouse.is_drag == false)
return;
if (mouse.click_valid == false) {
return;
}
if (this.started == false) {
return;
}
//clone
this.clone_general(this.tmpCanvas, this.tmpCanvas, 'move', mouse);
//draw draft preview
config.need_render = true;
}
mouseup(e) {
if (this.started == false) {
return;
}
delete config.layer.link_canvas;
app.State.do_action(
new app.Actions.Bundle_action('clone_tool', 'Clone Tool', [
new app.Actions.Update_layer_image_action(this.tmpCanvas)
])
);
//decrease memory
this.tmpCanvas.width = 1;
this.tmpCanvas.height = 1;
this.tmpCanvas = null;
this.tmpCanvasCtx = null;
}
clone_general(canvas_from, canvas_to, type, mouse) {
var params = this.getParams();
var mouse_x = Math.round(mouse.x) - config.layer.x;
var mouse_y = Math.round(mouse.y) - config.layer.y;
var half = Math.round(params.size / 2);
//adapt to origin size
mouse_x = this.adaptSize(mouse_x, 'width');
mouse_y = this.adaptSize(mouse_y, 'height');
//convert float coords to integers
mouse_x = Math.round(mouse_x);
mouse_y = Math.round(mouse_y);
//create source canvas
var canvas_source = document.createElement("canvas");
var ctx_source = canvas_source.getContext("2d");
var w = Math.ceil(params.size);
var h = Math.ceil(params.size);
canvas_source.width = w;
canvas_source.height = h;
//add data
var x_from = Math.round(this.clone_coords.x - (mouse.click_x - mouse_x));
var y_from = Math.round(this.clone_coords.y - (mouse.click_y - mouse_y));
if (params.anti_aliasing == false) {
ctx_source.arc(half, half, half, 0, Math.PI * 2, false);
ctx_source.clip();
}
if (params.source_layer.value == 'Previous') {
var previous_layer = this.Base_layers.find_previous(config.layer.id);
x_from = Math.round(this.clone_coords.x - (mouse.click_x - mouse_x)) - previous_layer.x + config.layer.x;
y_from = Math.round(this.clone_coords.y - (mouse.click_y - mouse_y)) - previous_layer.y + config.layer.y;
ctx_source.drawImage(previous_layer.link, x_from - half, y_from - half, w, h, 0, 0, w, h);
}
else {
ctx_source.drawImage(canvas_from, x_from - half, y_from - half, w, h, 0, 0, w, h);
}
//apply anti aliasing
if (params.anti_aliasing == true) {
var gradient = ctx_source.createRadialGradient(half, half, 0, half, half, half + 1);
gradient.addColorStop(0, 'white');
gradient.addColorStop(0.3, 'white');
gradient.addColorStop(1, 'transparent');
ctx_source.fillStyle = gradient;
ctx_source.globalCompositeOperation = 'destination-in';
ctx_source.fillRect(0, 0, params.size, params.size);
ctx_source.globalCompositeOperation = 'source-over';
}
//finish
canvas_to.getContext("2d").drawImage(canvas_source, mouse_x - half, mouse_y - half);
}
}
export default Clone_class;