Repository navigation
Expand file tree
/
Copy pathblur.js
More file actions
173 lines (147 loc) · 4.37 KB
/
Copy pathblur.js
File metadata and controls
173 lines (147 loc) · 4.37 KB
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
import config from './../config.js';
import Base_tools_class from './../core/base-tools.js';
import Base_layers_class from './../core/base-layers.js';
import alertify from './../../../node_modules/alertifyjs/build/alertify.min.js';
import ImageFilters from './../libs/imagefilters.js';
import Helper_class from './../libs/helpers.js';
class Blur_class extends Base_tools_class {
constructor(ctx) {
super();
this.Base_layers = new Base_layers_class();
this.Helper = new Helper_class();
this.ctx = ctx;
this.name = 'blur';
this.tmpCanvas = null;
this.tmpCanvasCtx = null;
this.started = false;
}
dragStart(event) {
var _this = this;
if (config.TOOL.name != _this.name)
return;
_this.mousedown(event);
}
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');
}
dragEnd(event) {
var _this = this;
if (config.TOOL.name != _this.name)
return;
_this.mouseup(event);
}
load() {
var _this = this;
//mouse events
document.addEventListener('mousedown', function (event) {
_this.dragStart(event);
});
document.addEventListener('mousemove', function (event) {
_this.dragMove(event);
});
document.addEventListener('mouseup', function (event) {
_this.dragEnd(event);
});
// collect touch events
document.addEventListener('touchstart', function (event) {
_this.dragStart(event);
});
document.addEventListener('touchmove', function (event) {
_this.dragMove(event);
});
document.addEventListener('touchend', function (event) {
_this.dragEnd(event);
});
}
mousedown(e) {
this.started = false;
var mouse = this.get_mouse_info(e);
var params = this.getParams();
if (mouse.valid == false || mouse.click_valid == false) {
return;
}
if (config.layer.type != 'image') {
alertify.error('Layer must be image, convert it to raster to apply this tool.');
return;
}
if (config.layer.rotate || 0 > 0) {
alertify.error('Erase on rotate object is disabled. Sorry.');
return;
}
this.started = true;
window.State.save();
//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);
//do blur
this.blur_general('click', mouse, params.size, params.strength);
//register tmp canvas for faster 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.valid == false || mouse.click_valid == false) {
return;
}
if (this.started == false) {
return;
}
//do blur
this.blur_general('move', mouse, params.size, params.strength);
//draw draft preview
config.need_render = true;
}
mouseup(e) {
if (this.started == false) {
return;
}
delete config.layer.link_canvas;
this.Base_layers.update_layer_image(this.tmpCanvas);
//decrease memory
this.tmpCanvas.width = 1;
this.tmpCanvas.height = 1;
this.tmpCanvas = null;
this.tmpCanvasCtx = null;
}
blur_general(type, mouse, size, strength) {
var ctx = this.tmpCanvasCtx;
var mouse_x = Math.round(mouse.x) - config.layer.x;
var mouse_y = Math.round(mouse.y) - config.layer.y;
//adapt to origin size
mouse_x = this.adaptSize(mouse_x, 'width');
mouse_y = this.adaptSize(mouse_y, 'height');
var size_w = this.adaptSize(size, 'width');
var size_h = this.adaptSize(size, 'height');
//find center
var center_x = mouse_x - Math.round(size_w / 2);
var center_y = mouse_y - Math.round(size_h / 2);
//convert float coords to integers
center_x = Math.round(center_x);
center_y = Math.round(center_y);
mouse_x = Math.round(mouse_x);
mouse_y = Math.round(mouse_y);
if (type == 'move') {
strength = strength / 2;
if (strength < 1)
strength = 1;
}
var imageData = ctx.getImageData(center_x, center_y, size_w, size_h);
var filtered = ImageFilters.StackBlur(imageData, strength); //add effect
this.Helper.image_round(this.tmpCanvasCtx, mouse_x, mouse_y, size_w, size_h, filtered);
}
}
export default Blur_class;