-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas_test.js
More file actions
executable file
·486 lines (432 loc) · 11.5 KB
/
canvas_test.js
File metadata and controls
executable file
·486 lines (432 loc) · 11.5 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// get the canvas
var my_canvas = document.getElementById("canvas");
var context = my_canvas.getContext("2d");
// The following variables are all used as stacks for the undo function
var locs = []; // stores the location of all sugars on the canvas
var names = []; // stores the name of each sugar on the canvas
var parentBonds = []; // stores location of one endpoint of a bond
var childBonds = []; // stores the location of the other endpoint of a bond
var lastAction = []; // stores the last action made
var name = 'MannoseSq'; // name of the current selected molecule
backgroundGrid(1000,700, 20); // draws the grid on the canvas
/*
Adds a sugar to the stacks that maintain sugar locations and names on the canvas
*/
function addSugar(name, x, y) {
locs.push([x, y]);
names.push(name);
}
/*
Adds a bond to the stacks that maintain bond locations on the canvas
*/
function addBond(px, py, cx, cy) {
parentBonds.push([px, py]);
childBonds.push([cx, cy]);
}
/*
Adds the last action to the lastAction stack
*/
function addAction(action) {
if (action === 'sugar') {
lastAction.push('sugar');
}
else {
lastAction.push('bond');
}
}
/*
Clears all the stacks to reset the canvas
*/
function clearScreen() {
locs = [];
names = [];
parentBonds = [];
childBonds = [];
lastAction = [];
undo();
clearXY();
}
/*
Undoes the user's last action, removing either a sugar or a bond
*/
function undo() {
var action = lastAction.pop(); // determine if the last action placed a sugar or bond
// if sugar, remove last sugar's data
if (action === 'sugar' && names.length !== 0) {
locs.pop();
names.pop();
popXY();
}
// if bond, remove last bond's data
else {
if (parentBonds.length !== 0) {
parentBonds.pop();
childBonds.pop();
}
}
// reset the canvas by drawing a clear rectangle and redoing the grid
context.clearRect(0, 0, my_canvas.width, my_canvas.height);
backgroundGrid(1000,700, 20);
// redraw all remaining sugars
for (var i = 0; i < locs.length; i++) {
drawShape(names[i], locs[i][0], locs[i][1], 10);
}
// redraw all remaining bonds
for (var i = 0; i < parentBonds.length; i++) {
drawBond(parentBonds[i][0], parentBonds[i][1], childBonds[i][0], childBonds[i][1]);
};
}
/*
Draw a bond from one sugar to another sugar
*/
function drawBond(px, py, cx, cy) {
context.beginPath();
context.moveTo(px, py);
context.lineTo(cx, cy);
context.lineWidth = 3;
context.stroke();
context.closePath();
}
/*
Draw a sugar depending on the currently selected sugar name
used a switch statement to allow all sugars to be drawn with a single function
call depening on the input provided to the function.
*/
function drawShape(name, x, y, r) {
context.lineWidth = 1;
switch (name) {
case 'ManA':
context.beginPath()
context.moveTo(x,y+r);
context.lineTo(x,y-r);
context.lineTo(x+r,y);
context.lineTo(x,y+r);
context.fillStyle="56E87D";
context.fill();
context.lineTo(x-r,y);
context.lineTo(x,y-r);
context.stroke()
context.closePath()
break;
case 'Fucose':
context.beginPath()
context.moveTo(x+r, y+r)
context.lineTo(x, y-r)
context.lineTo(x-r, y+r)
context.lineTo(x+r, y+r)
context.fillStyle = "red";
context.fill()
context.stroke()
context.closePath()
break;
case 'NeuAc':
context.beginPath()
context.moveTo(x,y+r);
context.lineTo(x+r,y);
context.lineTo(x,y-r);
context.lineTo(x-r,y);
context.lineTo(x,y+r);
context.fillStyle="E359F0";
context.fill();
context.stroke()
context.closePath()
break;
case 'NeuGc':
context.beginPath()
context.moveTo(x,y+r);
context.lineTo(x+r,y);
context.lineTo(x,y-r);
context.lineTo(x-r,y);
context.lineTo(x,y+r);
context.fillStyle="C7E9ED";
context.fill();
context.stroke()
context.closePath()
break;
case 'KDN':
context.beginPath()
context.moveTo(x,y+r);
context.lineTo(x+r,y);
context.lineTo(x,y-r);
context.lineTo(x-r,y);
context.lineTo(x,y+r);
context.fillStyle="89F5BF";
context.fill();
context.stroke()
context.closePath()
break;
case 'MannoseDi':
context.beginPath();
context.moveTo(x-r,y-r);
context.lineTo(x+r,y+r);
context.lineTo(x+r, y-r);
context.lineTo(x-r,y-r);
context.fillStyle="56E87D";
context.fill();
context.lineTo(x-r,y+r);
context.lineTo(x+r,y+r);
context.stroke();
context.closePath();
break;
case 'MannoseCi':
context.beginPath();
context.arc(x, y, r, 0, 2*Math.PI);
context.fillStyle="56E87D";
context.fill();
context.stroke();
context.closePath();
break;
case 'MannoseSq':
context.beginPath();
context.moveTo(x+r,y+r);
context.lineTo(x-r,y+r);
context.lineTo(x-r,y-r);
context.lineTo(x+r,y-r);
context.lineTo(x+r,y+r);
context.fillStyle="56E87D";
context.fill();
context.stroke();
context.closePath();
break;
case 'GlucoseDi':
context.beginPath();
context.moveTo(x-r,y-r);
context.lineTo(x+r,y+r);
context.lineTo(x+r, y-r);
context.lineTo(x-r,y-r);
context.fillStyle="blue";
context.fill();
context.lineTo(x-r,y+r);
context.lineTo(x+r,y+r);
context.stroke();
context.closePath();
break;
case 'GlucoseCi':
context.beginPath();
context.arc(x, y, r, 0, 2*Math.PI);
context.fillStyle="blue";
context.fill();
context.stroke();
context.closePath();
break;
case 'GlucoseSq':
context.beginPath();
context.moveTo(x+r,y+r);
context.lineTo(x-r,y+r);
context.lineTo(x-r,y-r);
context.lineTo(x+r,y-r);
context.lineTo(x+r,y+r);
context.fillStyle="blue";
context.fill();
context.stroke();
context.closePath();
break;
case 'GalactoseDi':
context.beginPath();
context.moveTo(x-r,y-r);
context.lineTo(x+r,y+r);
context.lineTo(x+r, y-r);
context.lineTo(x-r,y-r);
context.fillStyle="yellow";
context.fill();
context.lineTo(x-r,y+r);
context.lineTo(x+r,y+r);
context.stroke();
context.closePath();
break;
case 'GalactoseCi':
context.beginPath();
context.arc(x, y, r, 0, 2*Math.PI);
context.fillStyle="yellow";
context.fill();
context.stroke();
context.closePath();
break;
case 'GalactoseSq':
context.beginPath();
context.moveTo(x+r,y+r);
context.lineTo(x-r,y+r);
context.lineTo(x-r,y-r);
context.lineTo(x+r,y-r);
context.lineTo(x+r,y+r);
context.fillStyle="yellow";
context.fill();
context.stroke();
context.closePath();
break;
case 'GalA':
context.beginPath()
context.moveTo(x,y+r);
context.lineTo(x,y-r);
context.lineTo(x-r,y);
context.lineTo(x,y+r);
context.fillStyle="yellow";
context.fill();
context.lineTo(x+r,y);
context.lineTo(x,y-r);
context.stroke()
context.closePath()
break;
case 'IdoA':
context.beginPath();
context.moveTo(x+r, y);
context.lineTo(x-r, y);
context.lineTo(x,y+r);
context.lineTo(x+r, y);
context.fillStyle="tan";
context.fill();
context.lineTo(x, y-r);
context.lineTo(x-r, y);
context.stroke();
context.closePath();
break;
case 'GlcA':
context.beginPath();
context.moveTo(x+r, y);
context.lineTo(x-r, y);
context.lineTo(x,y-r);
context.lineTo(x+r, y);
context.fillStyle="blue";
context.fill();
context.lineTo(x, y+r);
context.lineTo(x-r, y);
context.stroke();
context.closePath();
break;
case 'Xylose':
context.beginPath()
context.moveTo(x,y-r);
context.lineTo(x-r/4, y-r/4);
context.lineTo(x-r, y-r/4);
context.lineTo(x-r/3, y+r/6);
context.lineTo(x-2*r/3, y+r);
context.lineTo(x, y+r/3);
context.lineTo(x+2*r/3, y+r);
context.lineTo(x+r/3, y+r/6);
context.lineTo(x+r, y-r/4);
context.lineTo(x+r/4, y-r/4);
context.lineTo(x, y-r);
context.fillStyle="F2DCB8";
context.fill();
context.stroke();
context.closePath();
break;
}
}
/*
Allows the listener to get the name of the currently selected molecule
*/
function getName() {
return name;
}
/*
Allows html buttons to be used to change the name of the current sugar
*/
function setName(n) {
name = n;
}
//###############################################################################
//###############################################################################
/*
Draws the grid on the canvas
*/
function backgroundGrid (width, height, space) {
var rows = height/space;
var cols = width/space;
context.lineWidth = 1;
context.beginPath();
for (i = 0; i < rows; i++) {
context.moveTo(0, i*space);
context.lineTo(width,i*space);
}
for (j = 0; j < cols; j++) {
context.moveTo(j*space, 0);
context.lineTo(j*space, height);
}
context.strokeStyle="C9C8CC"; // makes the grid gray
context.stroke();
context.closePath();
}
//////////////////////////
// Molecule Object
//////////////////////////
/*
The Sugar class represents a single sugar that the user placed on the canvas. Used to
encode the image drawn by the user into an ID string that can be input to a SQL database.
Glycomic molcules will be represented as directed graphs, thus each sugar will have incoming
and outgoing bonds/edges.
A sugar object knows what type of sugar it is (name), the type of bonds it has
(inBonds and outBounds), and which sugars it shares bonds with (adj).
*/
function Sugar(name) {
this.n = name;
this.bonds = [null, null, null, null, null, null];
this.adj = [null, null, null, null, null, null];
}
/*
idstring function generates the id string for the sugar
*/
Sugar.prototype.idstring = function() {
bondCount = 0;
bondType = []; // used to generate the id string
for (var i = 0; i < this.bonds.length; i++) {
if (this.bonds[i]) { // if there is a bond in this position
bondCount += 1;
bondType.push(this.bonds[i] + str(i+1));
}
}
string = str(bondCount) + this.n; // add number of bonds and sugar name to string
for (var b = 0; b < bondType.length; b++) {
string += bondType[b]; // add bond name and position to id string
}
string += '-'; // use a dash to separate bonds from sugars
return string;
//final output looks like "2ManAalpha1beta2-"
}
/*
The molecule class stores sugars in a directed graph/tree structure
*/
function Molecule() {
this.root = null;
this.ident = "";
}
/*
Adds a root to the graph - the root must be added because the user decides what sugar
will be at the root of their molecule.
*/
Molecule.prototype.addRoot = function(sugar) {
this.root = sugar;
};
/*
Adds a bond between two sugars
It doesn't matter which sugar is the parent and which is the child, the direction
of the bond is handeled when the ID for the molecule is generated.
*/
Molecule.prototype.addBond = function (parent, child, pos, bond) {
parent.adj[pos] = child;
parent.bonds[pos] = bond;
};
/*
Generates the ID of the molecule using a recursive helper that performs
a pre order traversal of the molecule.
*/
Molecule.prototype.genId = function () {
this.genIdHelper(this.root, 7);
return this.ident;
};
/*
Helper function that generates the ID of the molecule
*/
Molecule.prototype.genIdHelper = function (node, inBond) {
this.ident += node.idString(); // add the current node's ID string to the molecule ID
hasChild = false;
for (var i = 0; i < node.adj.length; i++) { // for every potential node adjacent to the current node
if (node.adj[i] && i != inBond) { // if the adjacent node exists and is not the parent
hasChild = true;
this.genIdHelper(node.adj[i], i); // recursive case
}
}
if (!hasChild){ // if the node is a leaf, indicate the branch is done with a '/'
this.ident += '/';
}
}