-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruler.js
More file actions
203 lines (189 loc) · 5.59 KB
/
Copy pathruler.js
File metadata and controls
203 lines (189 loc) · 5.59 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
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
function reverse_color(colors, idx) {
colors[idx + 0] = 255 - colors[idx + 0];
colors[idx + 1] = 255 - colors[idx + 1];
colors[idx + 2] = 255 - colors[idx + 2];
// imgdata[idx + 3] = 255 - imgdata[idx + 3];
}
function same_color(color1, color2, fix) {
for (let i = 0; i < color1.length; i++) {
if (Math.abs(color1[i] - color2[i]) > fix) {
return false;
}
}
return true;
}
function subImageData(oriimgdata, x, y, width, height) {
let tarray = [];
for (let yy = 0; yy < height; yy++) {
let idx = oriimgdata.width * 4 * (y + yy) + x * 4;
tarray.push(...oriimgdata.data.slice(idx, idx + width * 4));
}
return new ImageData(new Uint8ClampedArray(tarray), width, height);
}
function mod_x(x, y, fix) {
var ret = [];
var linedata = subImageData(imagedata, 0, y, can.width, 1);
let i = 0;
for (i = (x - 1) * 4; i > 0; i -= 4) {
//left
let now_color = [
linedata.data[i + 0],
linedata.data[i + 1],
linedata.data[i + 2],
// linedata.data[i + 3],
];
if (same_color(rgba, now_color, fix)) {
reverse_color(linedata.data, i);
} else {
break;
}
}
ret[0] = i / 4;
for (i = (x + 1) * 4; i < linedata.data.length; i += 4) {
//right
let now_color = [
linedata.data[i + 0],
linedata.data[i + 1],
linedata.data[i + 2],
// linedata.data[i + 3],
];
if (same_color(rgba, now_color, fix)) {
reverse_color(linedata.data, i);
} else {
break;
}
}
ret[1] = i / 4;
ctx.putImageData(linedata, 0, y);
return ret;
}
function mod_y(x, y, fix) {
var ret = [];
var linedata = subImageData(imagedata, x, 0, 1, imagedata.height);
let i = 0;
for (i = (y - 1) * 4; i > 0; i -= 4) {
//top
let now_color = [
linedata.data[i + 0],
linedata.data[i + 1],
linedata.data[i + 2],
// linedata.data[i + 3],
];
if (same_color(rgba, now_color, fix)) {
reverse_color(linedata.data, i);
} else {
break;
}
}
ret[0] = i / 4;
for (i = (y + 1) * 4; i < linedata.data.length; i += 4) {
//bottom
let now_color = [
linedata.data[i + 0],
linedata.data[i + 1],
linedata.data[i + 2],
// linedata.data[i + 3],
];
if (same_color(rgba, now_color, fix)) {
reverse_color(linedata.data, i);
} else {
break;
}
}
ret[1] = i / 4;
ctx.putImageData(linedata, x, 0);
return ret;
}
function restore_region(x, y) {
let xregion = subImageData(imagedata, 0, y, imagedata.width, 1);
let yregion = subImageData(imagedata, x, 0, 1, imagedata.height);
ctx.putImageData(xregion, 0, y);
ctx.putImageData(yregion, x, 0);
}
var input_x = document.getElementById("mod_x");
var input_y = document.getElementById("mod_y");
var a_width = document.getElementById("width");
var a_height = document.getElementById("height");
var input_fix = document.getElementById("fix");
var div = document.getElementById("maindiv");
var can = document.getElementById("can");
can.width = document.documentElement.clientWidth;
can.height = document.documentElement.clientHeight;
var ctx = can.getContext("2d");
var div_down = false;
var offset_pos = [0, 0];
var imagedata;
var rgba;
var lastpos = [0, 0];
var act_mod = [true, true];
var fix = 0;
let img = new Image();
img.src = localStorage["brokenclient"];
// img.src = "back.png";
img.onload = () => {
ctx.drawImage(img, 0, 0);
imagedata = ctx.getImageData(0, 0, can.width, can.height);
};
can.onmousemove = (e) => {
restore_region(lastpos[0], lastpos[1]);
let idx = imagedata.width * 4 * e.pageY + e.pageX * 4;
rgba = [
imagedata.data[idx + 0],
imagedata.data[idx + 1],
imagedata.data[idx + 2],
// imagedata.data[idx + 3],
];
let t;
if (act_mod[0]) {
t = mod_x(e.pageX, e.pageY, fix);
a_width.text = t[1] - t[0];
}
if (act_mod[1]) {
t = mod_y(e.pageX, e.pageY, fix);
a_height.text = t[1] - t[0];
}
lastpos = [e.pageX, e.pageY];
};
div.onmousedown = (e) => {
div_down = true;
let left = parseInt(div.style.left || 10);
let top = parseInt(div.style.top || 10);
offset_pos = [e.pageX - left, e.pageY - top];
};
div.onmousemove = (e) => {
if (div_down) {
div.style.left = e.pageX - offset_pos[0] + "px";
div.style.top = e.pageY - offset_pos[1] + "px";
}
};
div.onmouseup = (e) => {
div_down = false;
};
input_x.onchange = (e) => {
a_width.text = 0;
act_mod[0] = input_x.checked;
};
input_y.onchange = (e) => {
a_height.text = 0;
act_mod[1] = input_y.checked;
};
input_fix.oninput = (e) => {
let n = parseInt(input_fix.value);
fix = Math.min(255, n);
fix = Math.max(0, fix);
};
// numble input 不支持选区
// input_fix.onfocus=(e)=>{
// input_fix.setSelectionRange(0,input_fix.value.length)
// }
can.onmousedown = (e) => {
window.utools.copyText(a_width.text + "," + a_height.text);
window.utools.showNotification("长宽已复制");
window.close();
};
can.onmouseenter = (e) => {
document.body.style.cursor = "none";
};
can.onmouseleave = (e) => {
document.body.style.cursor = "unset";
};