-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
370 lines (331 loc) · 16.4 KB
/
handler.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// this function is useful for selecting existing rectangle or creating a new one.
function handledown_rectangle( ctx, shapes, $this, event, selected ){
var rects = getRectangles( shapes );
var mouse = getPosition( $this, event );
var new_rect = undefined, i;
// var bx1, bx2, by1, by2, obj;
/* for( i = 0; i < rects.length; i++ ){
obj = rects[ i ];
obj.computeHeightWidth();
if( selected && mouse.x >= obj.points[ 0 ].x &&
mouse.x <= obj.width &&
mouse.y >= obj.points[ 0 ].y &&
mouse.y <= obj.height ){
new_rect = i;
global_rect = shapes[ i ];
break;
}
}*/
// new rectangle is being drawn
if( new_rect == undefined ){
shapes.push( new Rectangle( { points : [ new Point( mouse.x, mouse.y ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 0,
height : 0,
stroke_color: "black",
fill_color : undefined } ) );
global_rect = shapes[ shapes.length - 1 ];
}
}
function handlemove_rectangle( ctx, shapes, $this, event ){
if( global_rect != undefined ){
var mouse = getPosition( $this, event );
// get width
global_rect.width = mouse.x - global_rect.points[ 0 ].x;
// get height
global_rect.height = mouse.y - global_rect.points[ 0 ].y;
// compute other points too.
global_rect.points[ 1 ].x = global_rect.points[ 0 ].x + global_rect.width;
global_rect.points[ 1 ].y = global_rect.points[ 0 ].y;
global_rect.points[ 2 ].x = global_rect.points[ 1 ].x;
global_rect.points[ 2 ].y = global_rect.points[ 1 ].y + global_rect.height;
global_rect.points[ 3 ].x = global_rect.points[ 0 ].x;
global_rect.points[ 3 ].y = global_rect.points[ 0 ].y + global_rect.height;
}
}
function handledown_circle( ctx, shapes, $this, event ){
if( global_circle == undefined ){
var mouse = getPosition( $this, event );
global_circle = new Circle( { x : mouse.x,
y : mouse.y,
radius : 0,
fill_color : undefined,
stroke_color: "black"
});
shapes.push( global_circle );
}
}
function handlemove_circle( ctx, shapes, $this, event ){
if( global_circle != undefined ){
var mouse = getPosition( $this, event );
var radius = Math.floor( Math.sqrt( Math.pow( mouse.x - global_circle.x, 2 ) + Math.pow( mouse.y - global_circle.y, 2 ) ) );
global_circle.radius = radius;
}
}
function handledown_line( ctx, shapes, $this, event ){
// new line is being drawn.
if( global_line == undefined ){
var mouse = getPosition( $this, event );
global_line = new Line( { x1 : mouse.x, y1 : mouse.y, width : 1 } );
//shapes.push( global_line );
}
}
function handlemove_line( ctx, shapes, $this, event ){
if( global_line != undefined ){
if( shapes[ shapes.length - 1 ] != global_line )
shapes.push( global_line );
var mouse = getPosition( $this, event );
global_line.extend( mouse.x, mouse.y );
}
}
function handledown_fhand( ctx, shapes, $this, event ){
var mouse = getPosition( $this, event );
var p = new Path( {
x1 : mouse.x,
y1 : mouse.y
} );
shapes.push( p );
global_path = p;
start = true;
}
function handlemove_fhand( ctx, shapes, $this, event ){
if( start && global_path != undefined ){
var mouse = getPosition( $this, event );
var p = new Point( mouse.x, mouse.y );
global_path.points.push( p );
}
}
function handledown_select( ctx, shapes, $this, event ){
// get mouse coordinates.
var mouse = getPosition( $this, event );
var i;
var len = shapes.length;
// flag is used to track whether any object is selected when mouse down event fires.
var flag = false;
// check whether the mouse coordinates are within any shapes.
for( i = 0; i < len; i++ ){
if( shapes[ i ].checkPointsWithin( mouse ) ){
if( select_shape != undefined )
select_shape.unStroke();
select_shape = shapes[ i ];
select_shape.stroke( "red" );
global_position.x = mouse.x;
global_position.y = mouse.y;
flag = true;
break;
}
}
if( !flag && select_shape != undefined ){
select_shape.unStroke();
select_shape = undefined;
}
}
function handledown_fill( ctx, shapes, $this, event ){
if( select_shape != undefined ){
var mouse = getPosition( $this, event );
var i, len = shapes.length;
for( i = 0; i < len; i++ ){
if( shapes[ i ].checkPointsWithin( mouse ) && shapes[ i ] == select_shape ){
select_shape.fill( global_color );
console.log( global_color );
break;
}
}
}
}
function handlemove_select( ctx, shapes, $this, event ){
if( select_shape != undefined && mouse_control ){
// get the current coordinates.
var mouse = getPosition( $this, event );
var pos = new Point(), pos1 = new Point();
var dx = (mouse.x - global_position.x);
var dy = (mouse.y - global_position.y);
if( select_shape.id == 1 ){
pos.x = (mouse.x - global_position.x) + select_shape.points[ 0 ].x;
pos.y = (mouse.y - global_position.y) + select_shape.points[ 0 ].y;
}
else if( select_shape.id == 2 ){
pos.x = dx + select_shape.x1;
pos.y = dy + select_shape.y1;
pos1.x = dx + select_shape.x2;
pos1.y = dy + select_shape.y2;
}
else if( select_shape.id == 3 ){
pos.x = (mouse.x - global_position.x) + select_shape.x;
pos.y = (mouse.y - global_position.y) + select_shape.y;
}
global_position.y = mouse.y;
global_position.x = mouse.x;
// move the shape to the new place.
select_shape.move( pos, pos1 );
}
}
function handledown_resize( ctx, shapes, $this, event ){
if( global_point == undefined ){
if( select_shape != undefined ){
var mouse = getPosition( $this, event );
// if it is a rectangle.
if( select_shape.id == 1 ){
global_point = select_shape.withinBounds( mouse );
// console.log( global_point + "\n" + "hurr\n" );
}
else if( select_shape.id == 2 ){
global_point = select_shape.withinBounds( mouse );
}
else if( select_shape.id == 3 )
global_point = select_shape.withinBounds( mouse );
}
}
}
function handlemove_resize( ctx, shapes, $this, event ){
if( global_point != undefined ){
if( select_shape != undefined ){
var mouse = getPosition( $this, event );
if( select_shape.id == 1 ){
if( global_point == 0 ){
// points 1,3 needs modification.
select_shape.points[ 1 ].y = mouse.y;
select_shape.points[ 3 ].x = mouse.x;
// compute new width & height.
select_shape.width = Math.abs( select_shape.points[ 2 ].x - select_shape.points[ 3 ].x );
select_shape.height = Math.abs( select_shape.points[ 2 ].y - select_shape.points[ 1 ].y );
//select_shape.points[ 1 ].x = mouse.x + select_shape.width;
// new origin.
select_shape.points[ 0 ].x = mouse.x;
select_shape.points[ 0 ].y = mouse.y;
}
else if( global_point == 1 ){
// point 2s x needs to changed and 0s y needs to changed.
select_shape.points[ 2 ].x = mouse.x;
select_shape.width = Math.abs( mouse.x - select_shape.points[ 0 ].x );
select_shape.height = Math.abs( mouse.y - select_shape.points[ 2 ].y );
select_shape.points[ 1 ].x = select_shape.points[ 0 ].x + select_shape.width;
select_shape.points[ 1 ].y = select_shape.points[ 2 ].y - select_shape.height;
// set new origin
select_shape.points[ 0 ].x = select_shape.points[ 3 ].x;
select_shape.points[ 0 ].y = select_shape.points[ 3 ].y - select_shape.height;
}
else if( global_point == 2 ){
// point 1 and 3 needs to be changed.
select_shape.points[ 1 ].x = mouse.x;
select_shape.points[ 3 ].y = mouse.y;
select_shape.width = Math.abs( mouse.x - select_shape.points[ 3 ].x );
select_shape.height = Math.abs( mouse.y - select_shape.points[ 1 ].y );
select_shape.points[ 2 ].x = select_shape.points[ 3 ].x + select_shape.width;
select_shape.points[ 2 ].y = select_shape.points[ 1 ].y + select_shape.height;
// no need to compute new origin.
}
else if( global_point == 3 ){
select_shape.points[ 2 ].y = mouse.y;
select_shape.width = Math.abs( mouse.x - select_shape.points[ 2 ].x );
select_shape.height = Math.abs( mouse.y - select_shape.points[ 0 ].y );
select_shape.points[ 3 ].x = mouse.x;//select_shape.points[ 2 ].x - select_shape.width;
select_shape.points[ 3 ].y = mouse.y;//select_shape.points[ 2 ].x - select_shape.width;
// set new origin;
select_shape.points[ 0 ].x = select_shape.points[ 1 ].x - select_shape.width;
select_shape.points[ 0 ].y = select_shape.points[ 2 ].y - select_shape.height;
}
}
// Line resize.
else if( select_shape.id == 2 ){
if( global_point == 0 ){
select_shape.resize( mouse.x, mouse.y, select_shape.x2, select_shape.y2 );
}
else if( global_point == 1 ){
select_shape.resize( select_shape.x1, select_shape.y1, mouse.x, mouse.y );
}
}
else if( select_shape.id == 3 ){
if( global_point == 0 ){
select_shape.resize( mouse.x, mouse.y );
}
}
}
}
}
function getPosition( $this, event ){
var obj = new Point( event.pageX - $this.offsetLeft, event.pageY - $this.offsetTop );
return obj;
}
function getRectangles( shapes ){
var i, len = shapes.length;
var rects = new Array();
for( i = 0; i < len; i++ )
if( shapes[ i ].id == 1 )
rects.push( shapes[ i ] );
return rects;
}
function draw_bounds( id ){
select_shape.bounds = new Array();
if( id == 1 ){
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.points[ 0 ].x - 2, select_shape.points[ 0 ].y - 2 ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.points[ 1 ].x - 2, select_shape.points[ 1 ].y - 2 ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.points[ 2 ].x - 2, select_shape.points[ 2 ].y - 2 ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.points[ 3 ].x - 2, select_shape.points[ 3 ].y - 2 ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
}
else if( id == 2 ){
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.x1 - 2, select_shape.y1 - 2 ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.x2 - 2, select_shape.y2 - 2 ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
}
else if( id == 3 ){
select_shape.bounds.push( new Rectangle( { points : [ new Point( select_shape.x + select_shape.radius, select_shape.y ),
new Point( undefined, undefined ),
new Point( undefined, undefined ),
new Point( undefined, undefined ) ],
width : 4,
height : 4,
stroke_color: "black",
fill_color : undefined,
id : 10 } ) );
}
}