2
/********************************************************************************
4
Canvas Setup and Click Handling Code
6
Handles both Touch and Mouse/Keyboard input at the same time and executes
8
Also declares canvas globals
10
*********************************************************************************/
12
function setupcanvas()
14
acanvas=document.getElementById('a');
15
context=acanvas.getContext("2d");
17
setTimeout("foo();",20);
22
function setupClickHandling()
24
// Mouse and Keyboard Events
25
acanvas.addEventListener('mousemove', ev_mousemove, false);
26
acanvas.addEventListener('mouseup', ev_mouseup, false);
27
acanvas.addEventListener('mousedown', ev_mousedown, false);
30
acanvas.addEventListener('touchstart', ev_touchstart, false);
31
acanvas.addEventListener('touchend', ev_touchend, false);
32
acanvas.addEventListener('touchmove', ev_touchmove, false);
35
// Keyboard/Mouse Mouse Up Handler
36
function ev_mouseup(ev)
41
// Keyboard/Mouse Mouse Down Handler
42
function ev_mousedown(ev)
47
// Keyboard/Mouse Mouse Move Handler
48
function ev_mousemove (ev)
51
if (ev.layerX||ev.layerX==0) { // Firefox
52
cx=ev.layerX-acanvas.offsetLeft;
53
cy=ev.layerY-acanvas.offsetTop;
54
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
55
cx=ev.offsetX-acanvas.offsetLeft;
56
cy=ev.offsetY-acanvas.offsetTop;
59
coord=findPos(acanvas);
63
handler_mousemove(cx,cy);
67
function ev_touchstart(event){
68
event.preventDefault();
69
var numtouch = event.touches.length;
71
targetEvent = event.touches.item(0);
73
var cx = targetEvent.pageX;
74
var cy = targetEvent.pageY;
84
function ev_touchend(event){
85
event.preventDefault();
86
var numtouch = event.touches.length;
92
function ev_touchmove(event){
93
event.preventDefault();
94
var numtouch = event.touches.length;
96
targetEvent = event.touches.item(0);
98
var cx = targetEvent.pageX;
99
var cy = targetEvent.pageY;
101
handler_mousemove(cx,cy);
104
// Fix scrolling on touch devices
105
var ScrollFix = function(elem) {
106
// Variables to track inputs
107
var startY, startTopScroll;
109
elem = elem || document.querySelector(elem);
111
// If there is no element, then do nothing
115
// Handle the start of interactions
116
elem.addEventListener('touchstart', function(event){
117
startY = event.touches[0].pageY;
118
startTopScroll = elem.scrollTop;
120
if(startTopScroll <= 0)
123
if(startTopScroll + elem.offsetHeight >= elem.scrollHeight)
124
elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
128
/********************************************************************************
130
Canvas Diagram Drawing Code
132
*********************************************************************************/
134
// Draws 90 degree arc
135
function drawellipse(x1,y1,x2,y2)
146
context.moveTo(x1,y1+ry);
147
context.quadraticCurveTo(x1,y1,x1+rx,y1);
148
context.quadraticCurveTo(x2,y1,x2,y1+ry);
149
context.quadraticCurveTo(x2,y2,x2-rx,y2);
150
context.quadraticCurveTo(x1,y2,x1,y1+ry);
156
function point(col,x,y)
158
context.strokeStyle="#000";
159
context.lineWidth = 1;
161
context.fillStyle=col;
162
context.fillRect(x-4,y-4,8,8);
163
context.strokeRect(x-4,y-4,8,8);
166
// Draw a box around a point to indicate highlight
167
function highlight(px,py)
169
context.strokeStyle="#aaa";
170
context.lineWidth = 1;
172
context.strokeRect(px-8,py-8,16,16);
176
// Draw a line using current context
177
function drawline(x1,y1,x2,y2,strokestyle,linewidth)
179
context.strokeStyle = strokestyle;
180
context.lineWidth = linewidth;
182
context.moveTo(x1,y1);
183
context.lineTo(x2,y2);
187
function fourpoints(x1,y1,x2,y2,x3,y3,x4,y4,col)
195
function drawdiamond(x1,y1,rx,ry,expand)
198
context.moveTo(x1-expand,y1+ry);
199
context.lineTo(x1+rx,y2+expand);
200
context.lineTo(x2+expand,y1+ry);
201
context.lineTo(x1+rx,y1-expand);
202
context.lineTo(x1-expand,y1+ry);
206
// Recursive Pos of div in document - should work in most browsers
207
function findPos(obj) {
208
var curleft = curtop = 0;
209
if (obj.offsetParent) {
210
curleft = obj.offsetLeft
211
curtop = obj.offsetTop
212
while (obj = obj.offsetParent) {
213
curleft += obj.offsetLeft
214
curtop += obj.offsetTop
223
// Make side coordinates for drawing Model
225
function makeside(side,x1,y1,x2,y2,perc){
232
yk=y1+((y2-y1)*perc);
234
xk=x1+((x2-x1)*perc);
238
yk=y1+((y2-y1)*perc);
251
// Computes side identifier for a mouse coordinate and object coordinates
253
function computeside(x,y,x1,y1,x2,y2,sidetol){
255
var obj_sidentifier="None";
257
var obj_centerdist=0;
260
if(x>x1-sidetol&&x<x1+sidetol&&y>y1-sidetol&&y<y2+sidetol){
262
obj_sideperc=makesideperc(y,y1,y2);
263
obj_centerdist=centerdist(y,y1,y2);
266
// Bottom Not Including Left Side or Right Side
267
if(x>x1+sidetol&&x<x2-sidetol&&y>y2-sidetol&&y<y2+sidetol){
269
obj_sideperc=makesideperc(x,x1,x2);
270
obj_centerdist=centerdist(x,x1,x2);
274
if(x>x2-sidetol&&x<x2+sidetol&&y>y1-sidetol&&y<y2+sidetol){
276
obj_sideperc=makesideperc(y,y1,y2);
277
obj_centerdist=centerdist(y,y1,y2);
280
// Top Not Including Left Side or Right Side
281
if(x>x1+sidetol&&x<x2-sidetol&&y>y1-sidetol&&y<y1+sidetol){
283
obj_sideperc=makesideperc(x,x1,x2);
284
obj_centerdist=centerdist(x,x1,x2);
288
side:obj_sidentifier,
297
// Make side perc for ER model
299
function makesideperc(x,x1,x2){
303
if(perc>1.0) perc=1.0;
304
if(perc<0.0) perc=0.0;
309
function centerdist(x,x1,x2){
315
// Euclidian distance - Yo!
317
function distance(x1,y1,x2,y2){
319
var dist=Math.sqrt(((y2-y1)*(y2-y1))+((x2-x1)*(x2-x1)));
324
// Dashed Line in Segments of given size
326
function dashedline(sx,sy,ex,ey,dashlen,linewidth,col)
332
len=Math.sqrt((dx*dx)+(dy*dy));
333
notimes=Math.round(len/dashlen);
338
context.lineWidth = linewidth;
339
context.strokeStyle=col;
348
for(var i=0;i<notimes;i++){
350
context.moveTo(xk,yk);
351
context.lineTo(xk+xh,yk+yh);
361
// Arcto only works if both x1 and y2 are on circle border
362
function arcto(x0,y0,x1,y1,x2,y2)
365
var r = Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
368
var startAngle = (180/Math.PI*Math.atan2(y1-y0, x1-x0));
369
var endAngle = (180/Math.PI*Math.atan2(y2-y0, x2-x0));
371
context.arc(x0, y0, r, 0, Math.PI*2.0, 1.0);
375
// Draws 90 degree arc
376
function arcdeg(x1,y1,x2,y2,x3,y3)
380
// First quadrant positive positive
381
dashedline(x1,y1,x2,y2,8,1.0,"#999");
382
dashedline(x3,y3,x2,y2,8,1.0,"#999");
397
context.strokeStyle = '#49f';
398
context.lineWidth = 1.0;
400
context.moveTo(x1,y1);
404
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));
406
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));
410
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));
412
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));
b'\\ No newline at end of file'