-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
232 lines (195 loc) · 6.67 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<canvas id="myCanvas" width="785" height="877" style="border:1px solid #d3d3d3; background: url('./images/a.PNG')">
Your browser does not support the HTML5 canvas tag.</canvas>
<div>
<input type="checkbox" id="connected" name="connected"
checked>
<label for="connected">Connected</label>
</div>
<script>
var mouse_x;
var mouse_y;
var pointindex = 0;
var bez_move_index = 0;
var bez_move_point = 's';
var move_point = false;
var connected = true;
var BezierCurve = function(s_x, s_y, cp1_x, cp1_y, cp2_x, cp2_y, e_x, e_y, connected) {
this.s_x = s_x;
this.s_y = s_y;
this.cp1_x = cp1_x;
this.cp1_y = cp1_y;
this.cp2_x = cp2_x;
this.cp2_y = cp2_y;
this.e_x = e_x;
this.e_y = e_y;
this.connected = connected;
};
BezierCurve.prototype.draw = function() {
drawBezier(this.s_x, this.s_y, this.cp1_x, this.cp1_y, this.cp2_x, this.cp2_y, this.e_x, this.e_y);
drawPoint(this.s_x, this.s_y);
drawPoint(this.cp1_x, this.cp1_y);
drawPoint(this.cp2_x, this.cp2_y);
drawPoint(this.e_x, this.e_y);
};
BezierCurve.prototype.intersect = function(x, y) {
if ((Math.abs(x - this.s_x)**2 + Math.abs(y - this.s_y)**2) ** (1/2) < 7) {
return 's';
}
else if ((Math.abs(x - this.cp1_x)**2 + Math.abs(y - this.cp1_y)**2) ** (1/2) < 7) {
return 'cp1';
}
else if ((Math.abs(x - this.cp2_x)**2 + Math.abs(y - this.cp2_y)**2) ** (1/2) < 7) {
return 'cp2';
}
else if ((Math.abs(x - this.e_x)**2 + Math.abs(y - this.e_y)**2) ** (1/2) < 7) {
return 'e';
}
else {
return 'no';
}
};
var bezierCurves = [];
var bezierCurveBuffer;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
function onMouseMove( event ) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
//mouse_x = ( event.clientX / 800 ) * 2 - 1;
//mouse_y = - ( event.clientY / 400 ) * 2 + 1;
let pos = getMousePos(c, event);
mouse_x = pos.x;
mouse_y = pos.y;
if (move_point) {
let bezCurve = bezierCurves[bez_move_index];
if (bez_move_point === 's') {
bezCurve.s_x = mouse_x;
bezCurve.s_y = mouse_y;
} else if (bez_move_point === 'cp1') {
bezCurve.cp1_x = mouse_x;
bezCurve.cp1_y = mouse_y;
} else if (bez_move_point === 'cp2') {
bezCurve.cp2_x = mouse_x;
bezCurve.cp2_y = mouse_y;
} else if (bez_move_point === 'e') {
bezCurve.e_x = mouse_x;
bezCurve.e_y = mouse_y;
}
bezierCurves[bez_move_index] = bezCurve;
}
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
function onMouseDown( event ) {
if (mouse_x > 785 || mouse_y > 877) {
return;
}
console.log(mouse_x);
console.log(mouse_y);
if (intersects_point(mouse_x, mouse_y)) {
move_point = true;
} else {
addPoint(mouse_x, mouse_y);
}
}
function onDocumentMouseUp(event) {
move_point = false;
}
function intersects_point(x, y) {
for (let i = 0; i < bezierCurves.length; i++) {
let bezCurve = bezierCurves[i];
let intersect = bezCurve.intersect(x, y);
if (intersect === 's') {
bez_move_point = 's';
bez_move_index = i;
return 1;
} else if (intersect === 'cp1') {
bez_move_point = 'cp1';
bez_move_index = i;
return 1;
} else if (intersect === 'cp2') {
bez_move_point = 'cp2';
bez_move_index = i;
return 1;
} else if (intersect === 'e') {
bez_move_point = 'e';
bez_move_index = i;
return 1;
}
}
return 0;
}
function drawBezier(s_x, s_y, cp1_x, cp1_y, cp2_x, cp2_y, e_x, e_y) {
ctx.beginPath();
ctx.moveTo(s_x, s_y);
ctx.bezierCurveTo(cp1_x, cp1_y, cp2_x, cp2_y, e_x, e_y);
ctx.stroke();
}
function drawPoint(x, y) {
ctx.fillRect(x-5,y-5,10,10);
}
function addPoint(x, y) {
if (pointindex === 0) {
//begin a new bezier curve
connected = document.getElementById("connected").checked;
bezierCurveBuffer = new BezierCurve(0, 0, 0, 0, 0, 0, 0, 0, connected);
if (connected && bezierCurves.length > 0) {
let lastBezCurve = bezierCurves[bezierCurves.length - 1]
x = lastBezCurve.e_x;
y = lastBezCurve.e_y;
console.log('used last');
}
bezierCurveBuffer.s_x = x;
bezierCurveBuffer.s_y = y;
pointindex++;
} else if (pointindex === 1) {
bezierCurveBuffer.cp1_x = x;
bezierCurveBuffer.cp1_y = y;
pointindex++;
} else if (pointindex === 2) {
bezierCurveBuffer.cp2_x = x;
bezierCurveBuffer.cp2_y = y;
pointindex++;
} else if (pointindex === 3) {
bezierCurveBuffer.e_x = x;
bezierCurveBuffer.e_y = y;
bezierCurveBuffer.draw();
bezierCurves.push(bezierCurveBuffer);
pointindex = 0;
}
drawPoint(x, y);
}
window.addEventListener( 'mousemove', onMouseMove, false );
window.addEventListener( 'mousedown', onMouseDown, false );
window.addEventListener( 'mouseup', onDocumentMouseUp, false );
ctx.lineWidth = 3;
// set line color
ctx.strokeStyle = '#ff0000';
function render() {
ctx.clearRect(0, 0, c.width, c.height);
for (let i = 0; i < bezierCurves.length; i++) {
let bezCurve = bezierCurves[i];
if (i !== 0 && bezCurve.connected && bezierCurves[i-1].connected) {
bezCurve.s_x = bezierCurves[i-1].e_x;
bezCurve.s_y = bezierCurves[i-1].e_y;
}
bezCurve.draw();
}
window.requestAnimationFrame(render);
}
window.requestAnimationFrame(render);
</script>
</body>
</html>