-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjPolygon.js
167 lines (150 loc) · 4.43 KB
/
jPolygon.js
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
/*
jPolygon - a ligthweigth javascript library to draw polygons over HTML5 canvas images.
Project URL: http://www.matteomattei.com/projects/jpolygon
Author: Matteo Mattei <[email protected]>
Version: 1.0
License: MIT License
*/
var perimeter = new Array();
var complete = false;
var canvas = document.getElementById("jPolygon");
var ctx;
function line_intersects(p0, p1, p2, p3) {
var s1_x, s1_y, s2_x, s2_y;
s1_x = p1['x'] - p0['x'];
s1_y = p1['y'] - p0['y'];
s2_x = p3['x'] - p2['x'];
s2_y = p3['y'] - p2['y'];
var s, t;
s = (-s1_y * (p0['x'] - p2['x']) + s1_x * (p0['y'] - p2['y'])) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (p0['y'] - p2['y']) - s2_y * (p0['x'] - p2['x'])) / (-s2_x * s1_y + s1_x * s2_y);
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
// Collision detected
return true;
}
return false; // No collision
}
function point(x, y){
ctx.fillStyle="white";
ctx.strokeStyle = "white";
ctx.fillRect(x-2,y-2,4,4);
ctx.moveTo(x,y);
}
function undo(){
ctx = undefined;
perimeter.pop();
complete = false;
start(true);
}
function clear_canvas(){
ctx = undefined;
perimeter = new Array();
complete = false;
document.getElementById('coordinates').value = '';
start();
}
function draw(end){
ctx.lineWidth = 1;
ctx.strokeStyle = "white";
ctx.lineCap = "square";
ctx.beginPath();
for(var i=0; i<perimeter.length; i++){
if(i==0){
ctx.moveTo(perimeter[i]['x'],perimeter[i]['y']);
end || point(perimeter[i]['x'],perimeter[i]['y']);
} else {
ctx.lineTo(perimeter[i]['x'],perimeter[i]['y']);
end || point(perimeter[i]['x'],perimeter[i]['y']);
}
}
if(end){
ctx.lineTo(perimeter[0]['x'],perimeter[0]['y']);
ctx.closePath();
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
ctx.fill();
ctx.strokeStyle = 'blue';
complete = true;
}
ctx.stroke();
// print coordinates
if(perimeter.length == 0){
document.getElementById('coordinates').value = '';
} else {
document.getElementById('coordinates').value = JSON.stringify(perimeter);
}
}
function check_intersect(x,y){
if(perimeter.length < 4){
return false;
}
var p0 = new Array();
var p1 = new Array();
var p2 = new Array();
var p3 = new Array();
p2['x'] = perimeter[perimeter.length-1]['x'];
p2['y'] = perimeter[perimeter.length-1]['y'];
p3['x'] = x;
p3['y'] = y;
for(var i=0; i<perimeter.length-1; i++){
p0['x'] = perimeter[i]['x'];
p0['y'] = perimeter[i]['y'];
p1['x'] = perimeter[i+1]['x'];
p1['y'] = perimeter[i+1]['y'];
if(p1['x'] == p2['x'] && p1['y'] == p2['y']){ continue; }
if(p0['x'] == p3['x'] && p0['y'] == p3['y']){ continue; }
if(line_intersects(p0,p1,p2,p3)==true){
return true;
}
}
return false;
}
function point_it(event) {
if(complete){
alert('Polygon already created');
return false;
}
var rect, x, y;
if(event.ctrlKey || event.which === 3 || event.button === 2){
if(perimeter.length==2){
alert('You need at least three points for a polygon');
return false;
}
x = perimeter[0]['x'];
y = perimeter[0]['y'];
if(check_intersect(x,y)){
alert('The line you are drowing intersect another line');
return false;
}
draw(true);
alert('Polygon closed');
event.preventDefault();
return false;
} else {
rect = canvas.getBoundingClientRect();
x = event.clientX - rect.left;
y = event.clientY - rect.top;
if (perimeter.length>0 && x == perimeter[perimeter.length-1]['x'] && y == perimeter[perimeter.length-1]['y']){
// same point - double click
return false;
}
if(check_intersect(x,y)){
alert('The line you are drowing intersect another line');
return false;
}
perimeter.push({'x':x,'y':y});
draw(false);
return false;
}
}
function start(with_draw) {
var img = new Image();
img.src = canvas.getAttribute('data-imgsrc');
img.onload = function(){
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
if(with_draw == true){
draw(false);
}
}
}