/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();
53
		}
54
		
55
		// Keyboard/Mouse Mouse Down Handler
56
		function ev_mousedown(ev)
57
		{
58
				handler_mousedown();		
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();
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
409
					context.beginPath();
410
					context.lineWidth   = 1.5;
411
					context.fillStyle = "#fff";
412
					context.arc(0, 0, ballradie, 0 , 2 * Math.PI, false);
413
					context.fill();															
414
													
415
					context.beginPath();
416
					context.arc(cx,cy,radie,0,Math.PI*2.0,1.0);												
417
					context.stroke();															
418
		
419
		}
420
		
421
		
422
/********************************************************************************
423
424
   Canvas and Diagram Measuring Functions
425
   
426
	These functions allow us to measure pixels in diagram and other apps
427
428
*********************************************************************************/
429
		
430
		
431
		// Recursive Pos of div in document - should work in most browsers
432
		
433
		function findPos(obj) {
434
			var curleft = curtop = 0;
435
			if (obj.offsetParent) {
436
				curleft = obj.offsetLeft
437
				curtop = obj.offsetTop
438
				while (obj = obj.offsetParent) {
439
					curleft += obj.offsetLeft
440
					curtop += obj.offsetTop
441
				}
442
			}
443
			return {
444
						x:curleft,
445
						y:curtop
446
				}
447
		}
448
		
449
		// Make side coordinates for drawing Model
450
451
		function makeside(side,x1,y1,x2,y2,perc){
452
	
453
				var xk=0;
454
				var yk=0;
455
	
456
				if(side==1){
457
						xk=x1;
458
						yk=y1+((y2-y1)*perc);
459
				}else if(side==2){
460
						xk=x1+((x2-x1)*perc);
461
						yk=y2;
462
				}else if(side==3){
463
						xk=x2;
464
						yk=y1+((y2-y1)*perc);						
465
				}else if(side==4){
466
						xk=x1+((x2-x1)*perc)
467
						yk=y1;
468
				}
469
				
470
				return {
471
							x:xk,
472
							y:yk
473
					}
474
		
475
		}		
476
		
477
		// Computes side identifier for a mouse coordinate and object coordinates
478
		
479
		function computeside(x,y,x1,y1,x2,y2,sidetol){
480
	
481
				var obj_sidentifier="None";
482
				var obj_sideperc=0;
483
				var obj_centerdist=0;
484
	
485
				// Left Side
486
				if(x>x1-sidetol&&x<x1+sidetol&&y>y1-sidetol&&y<y2+sidetol){
487
						obj_sidentifier=1;
488
						obj_sideperc=makesideperc(y,y1,y2);
489
						obj_centerdist=centerdist(y,y1,y2);
490
				}
491
492
				// Bottom Not Including Left Side or Right Side
493
				if(x>x1+sidetol&&x<x2-sidetol&&y>y2-sidetol&&y<y2+sidetol){
494
						obj_sidentifier=2;
495
						obj_sideperc=makesideperc(x,x1,x2);
496
						obj_centerdist=centerdist(x,x1,x2);
497
				}
498
499
				// Right Side
500
				if(x>x2-sidetol&&x<x2+sidetol&&y>y1-sidetol&&y<y2+sidetol){
501
						obj_sidentifier=3;
502
						obj_sideperc=makesideperc(y,y1,y2);
503
						obj_centerdist=centerdist(y,y1,y2);
504
				}
505
				
506
				// Top Not Including Left Side or Right Side
507
				if(x>x1+sidetol&&x<x2-sidetol&&y>y1-sidetol&&y<y1+sidetol){
508
						obj_sidentifier=4;
509
						obj_sideperc=makesideperc(x,x1,x2);
510
						obj_centerdist=centerdist(x,x1,x2);
511
				}
512
				
513
				return {
514
							side:obj_sidentifier,
515
							perc:obj_sideperc,
516
							dist:obj_centerdist
517
					}
518
		
519
		}		
520
						
521
		// Make side perc for ER model
522
		
523
		function makesideperc(x,x1,x2){
524
				r=x2-x1;
525
				perc=(x-x1)/r;
526
527
				if(perc>1.0) perc=1.0;
528
				if(perc<0.0) perc=0.0;
529
530
				return perc;
531
		}
532
		
533
		function centerdist(x,x1,x2){
534
				r=x1+((x2-x1)*0.5);
535
				
536
				return (x-r);
537
		}
538
		
539
		// Euclidian distance - Yo!
540
		
541
		function distance(x1,y1,x2,y2){
542
				
543
				var dist=Math.sqrt(((y2-y1)*(y2-y1))+((x2-x1)*(x2-x1)));
544
				
545
				return dist;
546
		}
547
548
		// Are we over a line or not.
549
550
		function overline(x,y,x1,y1,x2,y2,tolerance)
551
		{
552
				var distance=10000;
553
				
554
				
555
				dx=x2-x1;
556
				dy=y2-y1;
557
				
558
				straighttolerance=2.0;
559
				
560
				// Straight X, Straight Y or Positive or Negative Incline
561
				if(Math.abs(dx)<straighttolerance){
562
						if(y1<y2){
563
								if(y>y1-tolerance&&y<y2+tolerance){
564
										distance=Math.abs(x1-x);
565
								}
566
						}else{
567
								if(y>y2-tolerance&&y<y1+tolerance){
568
										distance=Math.abs(x1-x);
569
								}						
570
						}
571
						
572
						drawline(x1,y1,x2,y2,"#ff0",2.0);
573
574
				}else if(Math.abs(dy)<straighttolerance){
575
						if(x1<x2){
576
								if(x>x1-tolerance&&x<x2+tolerance){
577
										distance=Math.abs(y1-y);
578
								}
579
						}else{
580
								if(x>x2-tolerance&&x<x1+tolerance){
581
										distance=Math.abs(y1-y);
582
								}
583
						}
584
						drawline(x1,y1,x2,y2,"#ff0",2.0);
585
				}else if(Math.abs(dx)>=Math.abs(dy)){
586
						k=dy/dx;
587
						
588
						yk=y1+((x-x1)*k);
589
						distance=Math.abs(yk-y);
590
						
591
						point(x,yk,"#f5f");
592
						point(x1,y1,"#fff");						
593
						point(x2,y2,"#fff");						
594
						
595
						drawline(x1,y1,x2,y2,"#f00",2.0);
596
				}else if(Math.abs(dx)>=Math.abs(dy)){
597
						k=dy/dx;
598
						
599
						yk=y1+((x-x1)*k);
600
						distance=Math.abs(yk-y);
601
						
602
						point(x,yk,"#f5f");
603
						point(x1,y1,"#fff");						
604
						point(x2,y2,"#fff");
605
						
606
						drawline(x1,y1,x2,y2,"#0f0",2.0);						
607
				
608
				}
609
								
610
				return distance;		
611
		}