1
/********************************************************************************
3
Mouse coordinate and canvas globals
5
Handles both Touch and Mouse/Keyboard input at the same time
7
*********************************************************************************/
9
// Mouse coordinate globals
16
/********************************************************************************
18
Canvas Setup and Click Handling Code
20
Handles both Touch and Mouse/Keyboard input at the same time and executes
22
Also declares canvas globals
24
*********************************************************************************/
26
function setupcanvas()
28
acanvas=document.getElementById('a');
29
context=acanvas.getContext("2d");
31
setTimeout("foo();",50);
36
function setupClickHandling()
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);
44
acanvas.addEventListener('touchstart', ev_touchstart, false);
45
acanvas.addEventListener('touchend', ev_touchend, false);
46
acanvas.addEventListener('touchmove', ev_touchmove, false);
49
// Keyboard/Mouse Mouse Up Handler
50
function ev_mouseup(ev)
55
// Keyboard/Mouse Mouse Down Handler
56
function ev_mousedown(ev)
58
handler_mousedown(ev);
61
// Keyboard/Mouse Mouse Move Handler
62
function ev_mousemove (ev)
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;
73
coord=findPos(acanvas);
77
handler_mousemove(cx,cy);
81
function ev_touchstart(event){
82
event.preventDefault();
83
var numtouch = event.touches.length;
85
targetEvent = event.touches.item(0);
87
var cx = targetEvent.pageX;
88
var cy = targetEvent.pageY;
98
function ev_touchend(event){
99
event.preventDefault();
100
var numtouch = event.touches.length;
106
function ev_touchmove(event){
107
event.preventDefault();
108
var numtouch = event.touches.length;
110
targetEvent = event.touches.item(0);
112
var cx = targetEvent.pageX;
113
var cy = targetEvent.pageY;
115
handler_mousemove(cx,cy);
118
// Fix scrolling on touch devices
119
var ScrollFix = function(elem) {
120
// Variables to track inputs
121
var startY, startTopScroll;
123
elem = elem || document.querySelector(elem);
125
// If there is no element, then do nothing
129
// Handle the start of interactions
130
elem.addEventListener('touchstart', function(event){
131
startY = event.touches[0].pageY;
132
startTopScroll = elem.scrollTop;
134
if(startTopScroll <= 0)
137
if(startTopScroll + elem.offsetHeight >= elem.scrollHeight)
138
elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
142
/********************************************************************************
144
Canvas Diagram Drawing Code
146
*********************************************************************************/
148
// Draws a stroked rectangle of certain thicknes and color
150
function drawrect(x1,y1,x2,y2,color)
152
context.lineWidth = 1.5;
153
context.strokeStyle = color;
154
context.strokeRect(x1,y1,x2-x1,y2-y1);
159
// Draws a perfect round circle
161
function drawcircle(radius,color)
163
context.lineWidth = 1.5;
164
context.strokeStyle = color;
165
context.arc(0, 0, radius, 0 , 2 * Math.PI, false);
168
// Draws 90 degree arc
170
function drawellipse(x1,y1,x2,y2)
178
context.moveTo(x1,y1+ry);
179
context.quadraticCurveTo(x1,y1,x1+rx,y1);
180
context.quadraticCurveTo(x2,y1,x2,y1+ry);
181
context.quadraticCurveTo(x2,y2,x2-rx,y2);
182
context.quadraticCurveTo(x1,y2,x1,y1+ry);
189
function point(x,y,col)
191
context.strokeStyle="#000";
192
context.lineWidth = 1;
194
context.fillStyle=col;
195
context.fillRect(x-4,y-4,8,8);
196
context.strokeRect(x-4,y-4,8,8);
199
// Draw a box around a point to indicate highlight
201
function highlight(px,py)
203
context.strokeStyle="#aaa";
204
context.lineWidth = 1;
206
context.strokeRect(px-8,py-8,16,16);
210
// Draw a line using current context
212
function drawline(x1,y1,x2,y2,strokestyle,linewidth)
214
context.strokeStyle = strokestyle;
215
context.lineWidth = linewidth;
217
context.moveTo(x1,y1);
218
context.lineTo(x2,y2);
222
function fourpoints(x1,y1,x2,y2,x3,y3,x4,y4,col)
230
function drawdiamond(x1,y1,x2,y2)
236
context.moveTo(x1,y1+ry);
237
context.lineTo(x1+rx,y2);
238
context.lineTo(x2,y1+ry);
239
context.lineTo(x1+rx,y1);
240
context.lineTo(x1,y1+ry);
244
// Dashed Line in Segments of given size
246
function dashedline(sx,sy,ex,ey,dashlen,linewidth,col)
252
len=Math.sqrt((dx*dx)+(dy*dy));
253
notimes=Math.round(len/dashlen);
258
context.lineWidth = linewidth;
259
context.strokeStyle=col;
268
for(var i=0;i<notimes;i++){
270
context.moveTo(xk,yk);
271
context.lineTo(xk+xh,yk+yh);
281
// Arcto only works if both x1 and y2 are on circle border
283
function arcto(x0,y0,x1,y1,x2,y2)
286
var r = Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
289
var startAngle = (180/Math.PI*Math.atan2(y1-y0, x1-x0));
290
var endAngle = (180/Math.PI*Math.atan2(y2-y0, x2-x0));
292
context.arc(x0, y0, r, 0, Math.PI*2.0, 1.0);
296
// Draws 90 degree arc
298
function arcdeg(x1,y1,x2,y2,x3,y3)
302
// First quadrant positive positive
303
dashedline(x1,y1,x2,y2,8,1.0,"#999");
304
dashedline(x3,y3,x2,y2,8,1.0,"#999");
319
context.strokeStyle = '#49f';
320
context.lineWidth = 1.0;
322
context.moveTo(x1,y1);
326
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));
328
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));
332
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));
334
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));
342
// function that draws one part of the sun
344
function sundial(radius,angle,scale)
347
cosv=Math.cos(angle);
348
sinv=Math.sin(angle);
359
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);
360
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);
363
// function that daws the sun
367
context.fillStyle = "#fe9";
368
context.strokeStyle = "#d82";
369
context.lineWidth = 1.5;
372
context.moveTo(30,0);
373
for(i=0.0;i<360.0;i+=22.5){
374
angle=(i/360.0)*2*Math.PI;
381
// Draws the ball (used in various examples)
383
function drawball(cx,cy,radie,innerradie,ballradie,col1,inangle,inangleadd)
386
angleadd=(inangleadd/360.0)*2*Math.PI;
388
context.fillStyle = col1;
390
for(i=0;i<360;i+=inangle){
392
angle=(i/360.0)*2*Math.PI;
393
angle2=angle+angleadd;
394
angle3=angle+(angleadd*2.0);
395
angle4=angle-angleadd;
397
cosv=Math.cos(angle);
398
sinv=Math.sin(angle);
400
cosv2=Math.cos(angle2);
401
sinv2=Math.sin(angle2);
403
cosv4=Math.cos(angle4);
404
sinv4=Math.sin(angle4);
408
context.moveTo(cx,cy);
409
context.quadraticCurveTo(cx+(cosv*innerradie),cy+(sinv*innerradie),cx+(cosv2*radie),cy+(sinv2*radie));
410
context.arc(cx,cy,radie,angle2,angle,1.0);
411
context.quadraticCurveTo(cx+(cosv4*innerradie),cy+(sinv4*innerradie),cx,cy);
418
context.arc(cx,cy,radie,0,Math.PI*2.0,1.0);
423
// Draws underlined/dashed underlined text clipped inside a rectangle, a quadratic curve ellipsis or a diamond
425
function cliptext(x1,y1,x2,y2,tex,font,align,edgeoffs,baseline,color,clipkind,underlinekind)
438
// Make Rectangle / Ellipse / Diamond Clipping Paths
440
context.moveTo(x1,y1);
441
context.lineTo(x1,y2);
442
context.lineTo(x2-edgeoffs,y2);
443
context.lineTo(x2-edgeoffs,y1);
444
context.lineTo(x1,y1);
445
}else if(clipkind==2){
446
context.moveTo(x1,y1+ry);
447
context.quadraticCurveTo(x1+edgeoffs,y1+edgeoffs,x1+edgeoffs+rx,y1+edgeoffs);
448
context.quadraticCurveTo(x2-edgeoffs,y1+edgeoffs,x2-edgeoffs,y1+ry+edgeoffs);
449
context.quadraticCurveTo(x2-edgeoffs,y2-edgeoffs,x2-rx-edgeoffs,y2-edgeoffs);
450
context.quadraticCurveTo(x1+edgeoffs,y2-edgeoffs,x1+edgeoffs,y1+ry+edgeoffs);
451
}else if(clipkind==3){
452
context.moveTo(x1,y1+ry);
453
context.lineTo(x1+rx,y2);
454
context.lineTo(x2,y1+ry);
455
context.lineTo(x1+rx,y1);
456
context.lineTo(x1,y1+ry);
464
context.textAlign = "left";
465
context.fillStyle = color;
467
var metrics = context.measureText(tex);
468
var hwidth = metrics.width*0.5;
470
// Compute left-justified centered coordinate
471
if((rx-hwidth-edgeoffs)<0){
477
context.fillText(tex, tx, y2-ry+baseline);
479
// Draw underlining - can handle dashed underline!
480
if(underlinekind==1){
481
drawline(tx,y2-ry+baseline+5.0,tx+(hwidth*2),y2-ry+baseline+5.0,"#000",2.0);
484
if(clipkind!=0) context.restore();
488
// Draws cardinality at a certain offset from a coordinate
489
function drawcardinality(x,y,side,tex,xoffs,yoffs,font,baseline,sign,color)
491
// Xoffs is along line
493
// Yoffs is distance from line
496
var metrics = context.measureText(tex);
497
var twidth = metrics.width;
503
context.textAlign = "left";
504
context.fillStyle = color;
506
if(side==1&&sign==1){
507
context.fillText(tex, x-twidth-xoffs, y-yoffs);
508
drawrect(x-twidth-xoffs, y-yoffs,x-xoffs, y-yoffs-theight);
509
}else if(side==2&&sign==1){
510
context.fillText(tex, x+yoffs, y+xoffs+theight);
511
drawrect(x+yoffs, y+xoffs+theight,x+yoffs+twidth, y+xoffs);
512
}else if(side==3&&sign==1){
513
context.fillText(tex, x+xoffs, y-yoffs);
514
}else if(side==4&&sign==1){
515
context.fillText(tex, x+yoffs, y-xoffs);
516
}else if(side==1&&sign==2){
517
context.fillText(tex, x-twidth-xoffs, y+theight+yoffs);
518
drawrect(x-twidth-xoffs, y+yoffs,x-xoffs, y+theight+yoffs);
519
}else if(side==2&&sign==2){
520
context.fillText(tex, x-yoffs-twidth, y+xoffs+theight);
521
drawrect(x-yoffs-twidth, y+xoffs,x-yoffs, y+xoffs+theight);
522
}else if(side==3&&sign==2){
523
context.fillText(tex, x+xoffs, y+theight+yoffs);
524
}else if(side==4&&sign==2){
525
context.fillText(tex, x-yoffs-twidth, y-xoffs);
531
/********************************************************************************
533
Canvas and Diagram Measuring Functions
535
These functions allow us to measure pixels in diagram and other apps
537
*********************************************************************************/
540
// Recursive Pos of div in document - should work in most browsers
542
function findPos(obj) {
543
var curleft = curtop = 0;
544
if (obj.offsetParent) {
545
curleft = obj.offsetLeft
546
curtop = obj.offsetTop
547
while (obj = obj.offsetParent) {
548
curleft += obj.offsetLeft
549
curtop += obj.offsetTop
558
// Make side coordinates for drawing Model
560
function makeside(side,x1,y1,x2,y2,perc){
567
yk=y1+((y2-y1)*perc);
569
xk=x1+((x2-x1)*perc);
573
yk=y1+((y2-y1)*perc);
586
// Computes side identifier for a mouse coordinate and object coordinates
588
function computeside(x,y,x1,y1,x2,y2,sidetol){
590
var obj_sidentifier="None";
592
var obj_centerdist=0;
595
if(x>x1-sidetol&&x<x1+sidetol&&y>y1-sidetol&&y<y2+sidetol){
597
obj_sideperc=makesideperc(y,y1,y2);
598
obj_centerdist=centerdist(y,y1,y2);
601
// Bottom Not Including Left Side or Right Side
602
if(x>x1+sidetol&&x<x2-sidetol&&y>y2-sidetol&&y<y2+sidetol){
604
obj_sideperc=makesideperc(x,x1,x2);
605
obj_centerdist=centerdist(x,x1,x2);
609
if(x>x2-sidetol&&x<x2+sidetol&&y>y1-sidetol&&y<y2+sidetol){
611
obj_sideperc=makesideperc(y,y1,y2);
612
obj_centerdist=centerdist(y,y1,y2);
615
// Top Not Including Left Side or Right Side
616
if(x>x1+sidetol&&x<x2-sidetol&&y>y1-sidetol&&y<y1+sidetol){
618
obj_sideperc=makesideperc(x,x1,x2);
619
obj_centerdist=centerdist(x,x1,x2);
623
side:obj_sidentifier,
630
// Make side perc for ER model
632
function makesideperc(x,x1,x2){
636
if(perc>1.0) perc=1.0;
637
if(perc<0.0) perc=0.0;
642
function centerdist(x,x1,x2){
648
// Euclidian distance - Yo!
650
function distance(x1,y1,x2,y2){
652
var dist=Math.sqrt(((y2-y1)*(y2-y1))+((x2-x1)*(x2-x1)));
657
// Are we over a line or not.
659
function overline(x,y,x1,y1,x2,y2,tolerance)
667
straighttolerance=2.0;
669
var box1,boy1,box2,boy2;
687
// drawrect(box1-tolerance,boy1-tolerance,box2+tolerance,boy2+tolerance,"#aaa");
689
if((x>(box1-tolerance))&&(x<(box2+tolerance))&&(y>(boy1-tolerance))&&(y<(boy2+tolerance))){
690
// Straight X, Straight Y or Positive or Negative Incline
691
if(Math.abs(dx)<straighttolerance){
693
if(y>y1-tolerance&&y<y2+tolerance){
694
distance=Math.abs(x1-x);
697
if(y>y2-tolerance&&y<y1+tolerance){
698
distance=Math.abs(x1-x);
701
}else if(Math.abs(dy)<straighttolerance){
703
if(x>x1-tolerance&&x<x2+tolerance){
704
distance=Math.abs(y1-y);
707
if(x>x2-tolerance&&x<x1+tolerance){
708
distance=Math.abs(y1-y);
711
}else if(Math.abs(dx)>=Math.abs(dy)){
714
distance=Math.abs(yk-y);
718
distance=Math.abs(xk-x);
b'\\ No newline at end of file'