/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
9.5.1 by galaxyAbstractor
Moved shared js, css and php, updated all paths in DuggaSys.
1
/********************************************************************************
2
3
   Mouse coordinate and canvas globals
4
   
5
   Handles both Touch and Mouse/Keyboard input at the same time
6
   
7
*********************************************************************************/
8
9
		// Mouse coordinate globals
10
		var gridx,gridy;
11
		var clickstate=0;
12
13
		var acanvas;
14
		var context;
15
16
/********************************************************************************
17
18
   Canvas Setup and Click Handling Code
19
   
20
   Handles both Touch and Mouse/Keyboard input at the same time and executes 
21
   handler callbacks.
22
   Also declares canvas globals
23
24
*********************************************************************************/
25
 
26
		function setupcanvas()
27
		{
28
				acanvas=document.getElementById('a');
29
				context=acanvas.getContext("2d");		
30
				
31
				setTimeout("foo();",50);
32
				
33
				setupClickHandling();		
34
		}
35
		
36
		function setupClickHandling()
37
		{
38
				// Mouse and Keyboard Events
39
				acanvas.addEventListener('mousemove', ev_mousemove, false);
40
				acanvas.addEventListener('mouseup', ev_mouseup, false);
41
				acanvas.addEventListener('mousedown', ev_mousedown, false);
42
				
43
				// Touch Events
44
				acanvas.addEventListener('touchstart', ev_touchstart, false);
45
				acanvas.addEventListener('touchend', ev_touchend, false);
46
				acanvas.addEventListener('touchmove', ev_touchmove, false);		
47
		}						
48
				
49
		// Keyboard/Mouse Mouse Up Handler
50
		function ev_mouseup(ev)
51
		{
52
				handler_mouseup(ev);
53
		}
54
		
55
		// Keyboard/Mouse Mouse Down Handler
56
		function ev_mousedown(ev)
57
		{
58
				handler_mousedown(ev);		
59
		}
60
61
		// Keyboard/Mouse Mouse Move Handler
62
		function ev_mousemove (ev) 
63
		{
64
			  var cx,cy=0;
65
			  if (ev.layerX||ev.layerX==0) { // Firefox
66
				    cx=ev.layerX-acanvas.offsetLeft;
67
				    cy=ev.layerY-acanvas.offsetTop;
68
			  } else if (ev.offsetX || ev.offsetX == 0) { // Opera
69
				    cx=ev.offsetX-acanvas.offsetLeft;
70
				    cy=ev.offsetY-acanvas.offsetTop;
71
			  }
72
73
			  coord=findPos(acanvas);
74
				cx=cx-coord.x;
75
				cy=cy-coord.y;
76
77
				handler_mousemove(cx,cy);
78
		}		
79
80
		// Touch start event
81
		function ev_touchstart(event){  
82
    		event.preventDefault();  
83
     		var numtouch = event.touches.length;  
84
85
				targetEvent =  event.touches.item(0);
86
87
				var cx = targetEvent.pageX;
88
				var cy = targetEvent.pageY;
89
 				
90
				gridx=cx;
91
				gridy=cy;
92
				
93
 				handler_mousedown(event);
94
				
95
		}  
96
97
		// Touch end event
98
		function ev_touchend(event){  
99
    		event.preventDefault();  
100
     		var numtouch = event.touches.length;  
101
102
 				handler_mouseup();
103
		};  
104
		
105
		// Touch move event
106
		function ev_touchmove(event){  
107
    		event.preventDefault();  
108
     		var numtouch = event.touches.length;  
109
110
				targetEvent =  event.touches.item(0);
111
112
				var cx = targetEvent.pageX;
113
				var cy = targetEvent.pageY;
114
				
115
				handler_mousemove(cx,cy);				
116
		};  
117
118
		// Fix scrolling on touch devices
119
		var ScrollFix = function(elem) {
120
		    // Variables to track inputs
121
		    var startY, startTopScroll;
122
		
123
		    elem = elem || document.querySelector(elem);
124
		
125
		    // If there is no element, then do nothing  
126
		    if(!elem)
127
		        return;
128
		
129
		    // Handle the start of interactions
130
		    elem.addEventListener('touchstart', function(event){
131
		        startY = event.touches[0].pageY;
132
		        startTopScroll = elem.scrollTop;
133
		
134
		        if(startTopScroll <= 0)
135
		            elem.scrollTop = 1;
136
		
137
		        if(startTopScroll + elem.offsetHeight >= elem.scrollHeight)
138
		            elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
139
		    }, false);
140
		};
141
142
/********************************************************************************
143
144
   Canvas Diagram Drawing Code
145
146
*********************************************************************************/
147
148
		
149
150
		// Draws a perfect round circle
151
		
152
		function drawcircle(radius,color)
153
		{
154
				context.lineWidth   = 1.5;
155
				context.strokeStyle = color;
156
				context.arc(0, 0, radius, 0 , 2 * Math.PI, false);
157
		}
158
159
		// Draws 90 degree arc
160
		
161
		function drawellipse(x1,y1,x2,y2)
162
		{
163
164
					var rx=(x2-x1)/2.0;
165
					var ry=(y2-y1)/2.0;
166
167
					var x3=x1+rx;
168
					var y3=y1+ry;
169
170
					context.beginPath();					
171
172
					context.moveTo(x1,y1+ry);
173
					context.quadraticCurveTo(x1,y1,x1+rx,y1);
174
					context.quadraticCurveTo(x2,y1,x2,y1+ry);
175
					context.quadraticCurveTo(x2,y2,x2-rx,y2);
176
					context.quadraticCurveTo(x1,y2,x1,y1+ry);
177
178
					context.stroke();
179
		}	
180
		
181
		// Draw a point		
182
		
183
		function point(x,y,col)
184
		{
185
				context.strokeStyle="#000";
186
				context.lineWidth = 1;
187
			
188
				context.fillStyle=col;
189
				context.fillRect(x-4,y-4,8,8);		
190
				context.strokeRect(x-4,y-4,8,8);						
191
		}
192
		
193
		// Draw a box around a point to indicate highlight
194
		
195
		function highlight(px,py)
196
		{
197
				context.strokeStyle="#aaa";
198
				context.lineWidth = 1;
199
			
200
				context.strokeRect(px-8,py-8,16,16);						
201
				
202
		}
203
		
204
		// Draw a line using current context
205
		
206
		function drawline(x1,y1,x2,y2,strokestyle,linewidth)
207
		{
208
						context.strokeStyle = strokestyle;
209
						context.lineWidth   = linewidth;
210
						context.beginPath();
211
						context.moveTo(x1,y1);
212
						context.lineTo(x2,y2);
213
						context.stroke();							
214
		}
215
		
216
		function fourpoints(x1,y1,x2,y2,x3,y3,x4,y4,col)
217
		{
218
				point(x1,y1,col);
219
				point(x2,y2,col);
220
				point(x3,y3,col);
221
				point(x4,y4,col);				
222
		}
223
		
224
		function drawdiamond(x1,y1,rx,ry,expand)
225
		{
226
				context.beginPath();					
227
				context.moveTo(x1-expand,y1+ry);
228
				context.lineTo(x1+rx,y2+expand);
229
				context.lineTo(x2+expand,y1+ry);
230
				context.lineTo(x1+rx,y1-expand);
231
				context.lineTo(x1-expand,y1+ry);			
232
				context.stroke();											
233
		}
234
		
235
		// Dashed Line in Segments of given size
236
		
237
		function dashedline(sx,sy,ex,ey,dashlen,linewidth,col)
238
		{
239
						
240
			var dx=ex-sx;
241
			var dy=ey-sy;
242
							
243
			len=Math.sqrt((dx*dx)+(dy*dy));
244
			notimes=Math.round(len/dashlen);
245
			
246
			dx=dx/notimes;
247
			dy=dy/notimes;
248
			
249
			context.lineWidth = linewidth;
250
			context.strokeStyle=col;
251
252
			context.beginPath();
253
254
			var xk,yk;
255
			xk=sx;
256
			yk=sy;
257
			xh=dx/2.0;
258
			yh=dy/2.0;
259
			for(var i=0;i<notimes;i++){
260
261
					context.moveTo(xk,yk);				
262
					context.lineTo(xk+xh,yk+yh);
263
				
264
					xk+=dx;
265
					yk+=dy;
266
			}
267
							
268
			context.stroke();
269
				
270
		}
271
								
272
		// Arcto only works if both x1 and y2 are on circle border
273
		
274
		function arcto(x0,y0,x1,y1,x2,y2)
275
		{
276
277
				var r = Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
278
				var x = x0-r;
279
				var y = y0-r;
280
				var startAngle = (180/Math.PI*Math.atan2(y1-y0, x1-x0));
281
				var endAngle = (180/Math.PI*Math.atan2(y2-y0, x2-x0));
282
				
283
				context.arc(x0, y0, r, 0, Math.PI*2.0, 1.0);
284
285
		}
286
287
		// Draws 90 degree arc
288
		
289
		function arcdeg(x1,y1,x2,y2,x3,y3)
290
		{
291
					
292
293
					// First quadrant positive positive			
294
					dashedline(x1,y1,x2,y2,8,1.0,"#999");
295
					dashedline(x3,y3,x2,y2,8,1.0,"#999");					
296
297
					point(x1,y1,"#ff5");
298
					point(x2,y2,"#f55");
299
					point(x3,y3,"#f55");
300
												
301
					k=(y3-y1)/(x3-x1);					
302
										
303
					yk=y1+((x2-x1)*k);
304
					
305
					rx=x3-x1;
306
					ry=y3-y1;
307
308
					point(x2,yk,"#f5f");
309
310
					context.strokeStyle = '#49f';
311
					context.lineWidth   = 1.0;					
312
					context.beginPath();					
313
					context.moveTo(x1,y1);
314
					for(i=0;i<48;i++){
315
							if(y3>=y1){
316
									if(yk>=y2){
317
											context.lineTo(x1+(Math.sin(((Math.PI/96.0)*-i)+(Math.PI*1.0))*rx),y3+(Math.cos(((Math.PI/96.0)*-i)+(Math.PI*1.0))*ry));
318
									}else{
319
											context.lineTo(x3+(Math.sin(((Math.PI/96.0)*i)+(Math.PI*1.5))*rx),y1+(Math.cos(((Math.PI/96.0)*i)+(Math.PI*1.5))*ry));
320
									}
321
							}else{
322
									if(yk<=y2){
323
											context.lineTo(x1+(Math.sin(((Math.PI/96.0)*-i)+(Math.PI*1.0))*rx),y3+(Math.cos(((Math.PI/96.0)*-i)+(Math.PI*1.0))*ry));
324
									}else{
325
											context.lineTo(x3+(Math.sin(((Math.PI/96.0)*i)+(Math.PI*1.5))*rx),y1+(Math.cos(((Math.PI/96.0)*i)+(Math.PI*1.5))*ry));
326
									}							
327
							}
328
					}
329
					context.stroke();
330
331
		}		
332
	
333
		// function that draws one part of the sun	
334
		
335
		function sundial(radius,angle,scale)
336
		{
337
				
338
					cosv=Math.cos(angle);
339
					sinv=Math.sin(angle);
340
										
341
					yaddx=scale*cosv;
342
					yaddy=scale*sinv;
343
					
344
					xaddx=-scale*sinv;
345
					xaddy=scale*cosv;
346
347
					xk=cosv*radius;
348
					yk=sinv*radius;
349
350
					context.bezierCurveTo((-1.5*xaddx)+(yaddx*1.5)+xk,(-1.5*xaddy)+(yaddy*1.5)+yk,xaddx+(yaddx*2.0)+xk,xaddy+(yaddy*2.0)+yk,xaddx+(yaddx*3.0)+xk,xaddy+(yaddy*3.0)+yk);
351
					context.bezierCurveTo(xaddx+yaddx+xk,xaddy+yaddy+yk,(1.5*xaddx)+yaddx+xk,(1.5*xaddy)+yaddy+yk,(3.0*xaddx)+xk,(3.0*xaddy)+yk);
352
		}
353
354
		// function that daws the sun
355
		
356
		function drawsun()
357
		{
358
				context.fillStyle = "#fe9";
359
				context.strokeStyle = "#d82";
360
				context.lineWidth   = 1.5;
361
								 
362
				context.beginPath();
363
				context.moveTo(30,0);
364
				for(i=0.0;i<360.0;i+=22.5){
365
						angle=(i/360.0)*2*Math.PI;
366
						sundial(30,angle,3);
367
				}
368
				context.stroke();
369
				context.fill();															
370
		}
371
		
372
		// Draws the ball (used in various examples)
373
		
374
		function drawball(cx,cy,radie,innerradie,ballradie,col1,inangle,inangleadd)
375
		{
376
					
377
					angleadd=(inangleadd/360.0)*2*Math.PI;
378
									
379
					context.fillStyle = col1;					
380
					
381
					for(i=0;i<360;i+=inangle){
382
														
383
							angle=(i/360.0)*2*Math.PI;
384
							angle2=angle+angleadd;
385
							angle3=angle+(angleadd*2.0);
386
							angle4=angle-angleadd;
387
388
							cosv=Math.cos(angle);
389
							sinv=Math.sin(angle);
390
391
							cosv2=Math.cos(angle2);
392
							sinv2=Math.sin(angle2);
393
394
							cosv4=Math.cos(angle4);
395
							sinv4=Math.sin(angle4);
396
							
397
							context.beginPath();
398
399
							context.moveTo(cx,cy);
400
							context.quadraticCurveTo(cx+(cosv*innerradie),cy+(sinv*innerradie),cx+(cosv2*radie),cy+(sinv2*radie));							
401
							context.arc(cx,cy,radie,angle2,angle,1.0);
402
							context.quadraticCurveTo(cx+(cosv4*innerradie),cy+(sinv4*innerradie),cx,cy);							
403
														
404
							context.fill();															
405
							
406
					}	
407
													
408
					context.beginPath();
409
					context.arc(cx,cy,radie,0,Math.PI*2.0,1.0);												
410
					context.stroke();															
411
		
412
		}
413
		
414
		
415
/********************************************************************************
416
417
   Canvas and Diagram Measuring Functions
418
   
419
	These functions allow us to measure pixels in diagram and other apps
420
421
*********************************************************************************/
422
		
423
		
424
		// Recursive Pos of div in document - should work in most browsers
425
		
426
		function findPos(obj) {
427
			var curleft = curtop = 0;
428
			if (obj.offsetParent) {
429
				curleft = obj.offsetLeft
430
				curtop = obj.offsetTop
431
				while (obj = obj.offsetParent) {
432
					curleft += obj.offsetLeft
433
					curtop += obj.offsetTop
434
				}
435
			}
436
			return {
437
						x:curleft,
438
						y:curtop
439
				}
440
		}
441
		
442
		// Make side coordinates for drawing Model
443
444
		function makeside(side,x1,y1,x2,y2,perc){
445
	
446
				var xk=0;
447
				var yk=0;
448
	
449
				if(side==1){
450
						xk=x1;
451
						yk=y1+((y2-y1)*perc);
452
				}else if(side==2){
453
						xk=x1+((x2-x1)*perc);
454
						yk=y2;
455
				}else if(side==3){
456
						xk=x2;
457
						yk=y1+((y2-y1)*perc);						
458
				}else if(side==4){
459
						xk=x1+((x2-x1)*perc)
460
						yk=y1;
461
				}
462
				
463
				return {
464
							x:xk,
465
							y:yk
466
					}
467
		
468
		}		
469
		
470
		// Computes side identifier for a mouse coordinate and object coordinates
471
		
472
		function computeside(x,y,x1,y1,x2,y2,sidetol){
473
	
474
				var obj_sidentifier="None";
475
				var obj_sideperc=0;
476
				var obj_centerdist=0;
477
	
478
				// Left Side
479
				if(x>x1-sidetol&&x<x1+sidetol&&y>y1-sidetol&&y<y2+sidetol){
480
						obj_sidentifier=1;
481
						obj_sideperc=makesideperc(y,y1,y2);
482
						obj_centerdist=centerdist(y,y1,y2);
483
				}
484
485
				// Bottom Not Including Left Side or Right Side
486
				if(x>x1+sidetol&&x<x2-sidetol&&y>y2-sidetol&&y<y2+sidetol){
487
						obj_sidentifier=2;
488
						obj_sideperc=makesideperc(x,x1,x2);
489
						obj_centerdist=centerdist(x,x1,x2);
490
				}
491
492
				// Right Side
493
				if(x>x2-sidetol&&x<x2+sidetol&&y>y1-sidetol&&y<y2+sidetol){
494
						obj_sidentifier=3;
495
						obj_sideperc=makesideperc(y,y1,y2);
496
						obj_centerdist=centerdist(y,y1,y2);
497
				}
498
				
499
				// Top Not Including Left Side or Right Side
500
				if(x>x1+sidetol&&x<x2-sidetol&&y>y1-sidetol&&y<y1+sidetol){
501
						obj_sidentifier=4;
502
						obj_sideperc=makesideperc(x,x1,x2);
503
						obj_centerdist=centerdist(x,x1,x2);
504
				}
505
				
506
				return {
507
							side:obj_sidentifier,
508
							perc:obj_sideperc,
509
							dist:obj_centerdist
510
					}
511
		
512
		}		
513
						
514
		// Make side perc for ER model
515
		
516
		function makesideperc(x,x1,x2){
517
				r=x2-x1;
518
				perc=(x-x1)/r;
519
520
				if(perc>1.0) perc=1.0;
521
				if(perc<0.0) perc=0.0;
522
523
				return perc;
524
		}
525
		
526
		function centerdist(x,x1,x2){
527
				r=x1+((x2-x1)*0.5);
528
				
529
				return (x-r);
530
		}
531
		
532
		// Euclidian distance - Yo!
533
		
534
		function distance(x1,y1,x2,y2){
535
				
536
				var dist=Math.sqrt(((y2-y1)*(y2-y1))+((x2-x1)*(x2-x1)));
537
				
538
				return dist;
539
		}
540
541
		// Are we over a line or not.
542
543
		function overline(x,y,x1,y1,x2,y2,tolerance)
544
		{
545
				var distance=10000;
546
				
547
				
548
				dx=x2-x1;
549
				dy=y2-y1;
550
				
551
				straighttolerance=2.0;
552
				
553
				// Straight X, Straight Y or Positive or Negative Incline
554
				if(Math.abs(dx)<straighttolerance){
555
						if(y1<y2){
556
								if(y>y1-tolerance&&y<y2+tolerance){
557
										distance=Math.abs(x1-x);
558
								}
559
						}else{
560
								if(y>y2-tolerance&&y<y1+tolerance){
561
										distance=Math.abs(x1-x);
562
								}						
563
						}
564
						
565
						drawline(x1,y1,x2,y2,"#ff0",2.0);
566
567
				}else if(Math.abs(dy)<straighttolerance){
568
						if(x1<x2){
569
								if(x>x1-tolerance&&x<x2+tolerance){
570
										distance=Math.abs(y1-y);
571
								}
572
						}else{
573
								if(x>x2-tolerance&&x<x1+tolerance){
574
										distance=Math.abs(y1-y);
575
								}
576
						}
577
						drawline(x1,y1,x2,y2,"#ff0",2.0);
578
				}else if(Math.abs(dx)>=Math.abs(dy)){
579
						k=dy/dx;
580
						
581
						yk=y1+((x-x1)*k);
582
						distance=Math.abs(yk-y);
583
						
584
						point(x,yk,"#f5f");
585
						point(x1,y1,"#fff");						
586
						point(x2,y2,"#fff");						
587
						
588
						drawline(x1,y1,x2,y2,"#f00",2.0);
589
				}else if(Math.abs(dx)>=Math.abs(dy)){
590
						k=dy/dx;
591
						
592
						yk=y1+((x-x1)*k);
593
						distance=Math.abs(yk-y);
594
						
595
						point(x,yk,"#f5f");
596
						point(x1,y1,"#fff");						
597
						point(x2,y2,"#fff");
598
						
599
						drawline(x1,y1,x2,y2,"#0f0",2.0);						
600
				
601
				}
602
								
603
				return distance;		
604
		}