-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
139 lines (123 loc) · 3.72 KB
/
core.js
File metadata and controls
139 lines (123 loc) · 3.72 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
function $ (query) {
return document.querySelectorAll(query);
}
var file_ele = document.getElementById('fileEle');
var raw_img = $('#input .img')[0],
res_img = $('#result .img')[0],
btns = $('.btn'),
in_btn = btns[0],
co_btn = btns[1],
input = $('.input'),
shreshold = input[0],
points_num = input[1],
percent = input[2];
var img_width, img_height;
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var loaded = false;
var img = new Image();
in_btn.onclick = function () {
file_ele.click();
}
file_ele.onchange = function () {
loaded = false;
var src = window.URL.createObjectURL(file_ele.files[0]);
raw_img.src = src;
}
var init = true, ca = false;
raw_img.onload = function () {
img.src = raw_img.src;
img.onload = function () {
img_width = img.width;
img_height = img.height;
canvas.width = img_width;
canvas.height = img_height;
caculate();
loaded = true;
ca = true;
if (init) {
init = false;
co_btn.click();
}
}
}
window.onload = function () {
raw_img.src = "./default.jpg";
}
function caculate() {
context.drawImage(img, 0, 0, img_width, img_height);
var imageData = context.getImageData(0, 0, img_width, img_height);
var edge_points = Sobel(imageData, parseInt(shreshold.value)), points = [], random;
var num = parseInt(points_num.value);
var edge_points_num = ~~(num * parseFloat(percent.value) * 0.01);
for (var i = 0; i < num - edge_points_num; i++) {
points.push([Math.random() * img_width , Math.random() * img_height])
}
for (var l = 0; l < edge_points_num; l++) {
random = ~~(Math.random() * edge_points.length);
points.push(edge_points[random]);
edge_points.splice(random, 1);
}
points.push([0, 0] , [0, img_height] , [img_width, 0] , [img_width, img_height]);
var triangles = Delaunay.triangulate(points);
var x1,x2,x3,y1,y2,y3,cx,cy;
for(var i=0;i < triangles.length; i+=3) {
x1 = points[triangles[i]][0];
x2 = points[triangles[i+1]][0];
x3 = points[triangles[i+2]][0];
y1 = points[triangles[i]][1];
y2 = points[triangles[i+1]][1];
y3 = points[triangles[i+2]][1];
// 获取三角形中心点坐标
cx = ~~((x1 + x2 + x3) / 3);
cy = ~~((y1 + y2 + y3) / 3);
// 获取中心点坐标的颜色值
index = (cy * imageData.width + cx) * 4;
var color_r = imageData.data[index];
var color_g = imageData.data[index + 1];
var color_b = imageData.data[index + 2];
// 绘制三角形
context.save();
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.lineTo(x3, y3);
context.closePath();
context.fillStyle = "rgba("+color_r+","+color_g+","+color_b+", 1)";
context.fill();
context.restore();
}
}
co_btn.onclick = function () {
if (loaded) {
if (!ca) {
caculate();
}
res_img.width = raw_img.width;
res_img.height = raw_img.height;
res_img.src = canvas.toDataURL("image/png");
ca = false;
}
}
var mask = document.getElementById('mask');
var display = document.getElementById('display');
var close = document.getElementById('close');
var scale_up = $('.scale-up'),
raw_su = scale_up[0],
res_su = scale_up[1];
function handle_scale (src) {
if (src) {
mask.className = 'show';
display.src = src;
}
}
raw_su.onclick = function () {
handle_scale(raw_img.src)
}
close.onclick = function () {
mask.className = '';
display.src = '';
}
res_su.onclick = function () {
handle_scale(canvas.toDataURL("image/png"));
}