-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
171 lines (152 loc) · 4.59 KB
/
editor.js
File metadata and controls
171 lines (152 loc) · 4.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
const http = new XMLHttpRequest();
let isDrawing = false;
let startPoint = null;
let points = [];
function ajax(url, method, data, callback) {
http.open(method, url, true);
http.setRequestHeader('Content-type', 'application/json');
if (method.toLowerCase() === 'post')
http.send(JSON.stringify(data));
else
http.send();
http.onload = function() {
callback(null, http.responseText);
}
}
function saveHandler() {
const detail = document.querySelector('#nameOfDetail').value;
const select = document.querySelector('#loadImageInput');
const uuid = select.options[select.selectedIndex].value;
const data = { uuid, detail, points };
ajax('/shape', 'post', data, () => {});
}
document.addEventListener("DOMContentLoaded", () => {
ajax('/image', 'get', null, (err, reply) => {
const data = JSON.parse(reply);
const select = document.querySelector('#loadImageInput');
const o = document.createElement("option");
o.text = '---';
select.add(o);
data.forEach(option => {
const o = document.createElement("option");
o.text = option.name;
o.value = option.uuid;
select.add(o)
});
run();
})
});
function cleanCanvas(id) {
let editorLayout = document.querySelector(id);
editorLayout
.getContext('2d')
.clearRect(0, 0, editorLayout.width, editorLayout.height);
editorLayout.getContext('2d').drawImage(image, 0, 0);
}
let image;
function run() {
// function changeDetailColor(e) {
// addCanvasPath('canvas', points, e.target.value)
// }
// const colorPicker = document.querySelector('#colorPicker');
// colorPicker.addEventListener("input", changeDetailColor, false);
// colorPicker.addEventListener("change", changeDetailColor, false);
// colorPicker.select();
document.querySelector('#loadImageInput').onchange = (event) => {
if (event.target.selectedIndex === 0) {
cleanCanvas('canvas');
cleanDetailsList();
return;
}
const imageId = event.target.options[event.target.selectedIndex].value;
fetchImage('/image/' + imageId + '/data');
fetchDetails('/shape/' + imageId, (err, reply) => {
const data = JSON.parse(reply);
const detailsList = document.querySelector('#detailList');
data.forEach(item => {
const img = document.createElement('img');
img.src = `/image/${imageId}/${item.name}`;
img.onclick = function() {
points = item.points;
cleanCanvas('canvas');
addCanvasPath('canvas', item.points);
};
detailsList.appendChild(img);
});
})
};
function addCanvasPath(id, points, fillColor) {
let editorLayout = document.querySelector(id);
const ctx = editorLayout.getContext('2d');
ctx.beginPath();
points.forEach(point => {
const {x, y} = point;
// console.log(x, y)
ctx.lineTo(x, y)
});
const {x, y} = points[0];
ctx.lineTo(x, y);
if (fillColor) {
ctx.fillStyle = fillColor;
ctx.fill();
} else {
ctx.stroke();
}
}
function fetchDetails(url, cb) {
ajax(url, 'get', null, (err, reply) => {
cb(err, reply)
})
}
function fetchImage(url) {
image = new Image();
image.onload = () => {
createEditor(image);
};
image.src = url;
}
function cleanDetailsList() {
const detailsList = document.querySelector('#detailList');
while(detailsList.firstChild)
detailsList.removeChild(detailsList.firstChild)
}
function createEditor(image) {
let editorLayout = document.querySelector('canvas');
if (editorLayout) {
cleanCanvas('canvas');
} else {
const canvasBlock = document.querySelector('#canvasBlock')
editorLayout = document.createElement('canvas');
canvasBlock.appendChild(editorLayout);
}
editorLayout.width = image.width;
editorLayout.height = image.height;
const context = editorLayout.getContext("2d");
context.drawImage(image, 0, 0);
editorLayout.addEventListener('click', event => {
const offsetLeft = editorLayout.offsetLeft;
const offsetTop = editorLayout.offsetTop;
const x = event.pageX - offsetLeft;
const y = event.pageY - offsetTop;
points.push({x, y});
if (!isDrawing) {
points = [];
isDrawing = true;
context.beginPath();
startPoint = {x, y};
}
context.lineTo(x, y);
context.stroke();
if (
points.length > 1 &&
Math.abs(startPoint.x - x) <= 6 &&
Math.abs(startPoint.y - y) <= 6
) {
isDrawing = false;
startPoint = {};
context.closePath();
context.stroke();
}
});
}
}