-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvasfn.js
1891 lines (1576 loc) · 67.7 KB
/
canvasfn.js
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2021 Joseph Mason
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//Used throughout entire script to get the coordinates of the mouse following mousedown.
Glass.prototype.getCurrentCoords = function(e) {
let x = e.pageX - $(' #'+this.id+'-canvas').offset().left
let y = e.pageY - $(' #'+this.id+'-canvas').offset().top
return [x, y]
}
//Clears the canvas, and redraws. This is used many times per second, although optimisation has
//been done to minimise the number of refreshes done where nothing changes.
//If the nocanvas option is specified, we are using this object without a DOM object, so don't redraw.
Glass.prototype.refreshCanvas = function() {
if(this.options.mode !== "nocanvas") {
this.ctx.clearRect(0,0,this.canvasWidth,this.canvasHeight)
this.reDraw()
}
}
//Find which point was clicked, since we can't attach event handlers to pretty pictures drawn on the
//canvas.
Glass.prototype.getPointClicked = function(e, xy) {
let [x, y] = xy || this.getCurrentCoords(e)
let locus = this.options.locus * 1.5
let pointclicked = this.points.findIndex(value => {
if(x<(value[0]+locus) && x>(value[0]-locus) && y<(value[1]+locus) && y>(value[1]-locus)) {
return true
}
})
return pointclicked
}
//Gets the bond that was clicked, for reason as above.
Glass.prototype.getBondClicked = function(e, xy) {
let [x, y] = xy || this.getCurrentCoords(e)
let points = this.points
let locus = this.options.locus * 1.8
let bondclicked = this.bonds.findIndex(value => {
let p1 = [points[value[0]][0], points[value[0]][1]]
let p2 = [points[value[1]][0], points[value[1]][1]]
//Find length of the bond, in case it's an abnormal length created by the user
let l = Math.sqrt(Math.pow(p2[0]-p1[0], 2) + Math.pow(p2[1]-p1[1], 2))
//Find distance squared from each point linked by the bond
let d1 = Math.pow(x-p1[0], 2) + Math.pow(y-p1[1], 2)
let d2 = Math.pow(p2[0]-x, 2) + Math.pow(p2[1]-y, 2)
//Find the length of the line at 90 degrees to the bond, from bond to point.
let normdist = d1 - Math.pow((l*l + d1 - d2)/(2*l), 2)
//17 is a arbitrary limit that works. Also checks that the click was less than bond
//length from both atoms.
if(normdist<locus**2 && d1<l*l && d2<l*l) {
return true
}
})
return bondclicked
}
//Takes the coordinates of the old point and its index in the state, uses new coordinates to add the new
//point to the state, then activates addbond() to draw bond between them
Glass.prototype.addpoint = function(newx, newy, point, ignorenearpoints) {
//point is the index of the pointclicked. If none, it adds a new point to state, starting a new molecule.
if(typeof newx === "number" && typeof newy === "number") {
if(point !== "none") {
//uses the x and y coordinates of the new point to be set, and checks that the state doesn't already
//have a point at those coordinates
let newpointexist = this.points.findIndex(value => {
if(newx<(value[0]+7) && newx>(value[0]-7) && newy<(value[1]+7) && newy>(value[1]-7)) {
return true
}
})
if(newpointexist === -1 || ignorenearpoints === true) {
this.points.push([newx, newy, 67, [], 0]) //[x coordinate, y coordinate, atomtype, [bonded neighbours], charge]
this.addbond(point, this.points.length-1)
newpointexist = this.points.length-1
}
//if the new point does exist, make sure there isn't already a bond between these two points
//this should only occur when adding a ring to a bond (such
//that the bond becomes part of the new ring). This would usually
//occur when forming a fused ring system.
else if(!this.points[point][3].includes(newpointexist)) {
this.addbond(point, newpointexist)
}
return newpointexist
}
else {
this.points.push([newx, newy, 67, [], 0])
this.refreshCanvas()
return this.points.length - 1
}
}
}
//add a new bond to the state, connecting two points. Two points are referenced by their index in the
//point array.
Glass.prototype.addbond = function(point, other) {
this.bonds.push([point, other, 1, 0])
this.points[point][3].push(other)
this.points[other][3].push(point)
this.refreshCanvas()
}
Glass.prototype.changeAtom = function(e) {
let xy = this.getCurrentCoords(e)
let atom = this.nextAtom
//get reverse atom dictionary, so that we can convert the specified atom in the disconnection to the correct number, ready
//to add the new points back into the array
let atomAsNumber = -1
$.each(this.atomdict, function(index, value) {
if(value[0] === atom) {
atomAsNumber = index
return false
}
})
let myindex = this.points.findIndex(value => {
if(xy[0]<(value[0]+7) && xy[0]>(value[0]-7) && xy[1]<(value[1]+7) && xy[1]>(value[1]-7)) {
return true
}
})
if(myindex > -1) {
this.points[myindex][2] = parseInt(atomAsNumber)
}
else {
this.addpoint(xy[0], xy[1], "none", false)
this.points[this.points.length -1][2] = parseInt(atomAsNumber)
}
this.refreshCanvas()
}
//changes the type of atom. Uses curx and cury defined in global variable.
Glass.prototype.changeAtomOnKeyPress = function(keypress, curx, cury) {
let points = this.points
let atomdict = this.atomdict
let that = this
if(!isNaN(keypress) && atomdict[keypress]) {
$.each(this.points, function(index, value) {
if(curx<(value[0]+7) && curx>(value[0]-7) && cury<(value[1]+7) && cury>(value[1]-7)) {
points[index][2]=keypress
that.refreshCanvas()
return false
}
})
}
}
//Increase the charge of the atom on click
Glass.prototype.increaseCharge = function(pointClicked) {
if(this.points[pointClicked]) {
this.points[pointClicked][4]++
}
this.refreshCanvas()
}
//Decrease the charge of the atom on click
Glass.prototype.decreaseCharge = function(pointClicked) {
if(this.points[pointClicked]) {
this.points[pointClicked][4]--
}
this.refreshCanvas()
}
//Switch whether a bond should be single/double/triple, alter the state, and re-render
Glass.prototype.changeBondType = function(bondclick) {
let bondType = this.bonds[bondclick][2]
if(bondType === 1) {
this.bonds[bondclick][2] = 2
this.bonds[bondclick][3] = 0
}
if(bondType === 2) {
this.bonds[bondclick][2] = 3
this.bonds[bondclick][3] = 0
}
if(bondType === 3) {
this.bonds[bondclick][2] = 1
this.bonds[bondclick][3] = 0
}
this.refreshCanvas()
}
//Changes the stereochemistry of the bond that was clicked from flat, wedge, hash, reversewedge, reversehash, then flat again.
Glass.prototype.changeBondStereo = function(e, type) {
let that = this
let bondclick = this.getBondClicked(e)
let pointclick = this.getPointClicked(e)
let bonds = this.bonds
if(pointclick !== -1) {
let coords = that.getNewPointCoords(pointclick)
that.addpoint(coords[0], coords[1], pointclick, false)
if(type === "wedgebond") {
bonds[bonds.length-1][3] = 1
}
else if(type === "hashbond") {
bonds[bonds.length-1][3] = 2
}
}
else if(bondclick !== -1) {
if(type === "wedgebond") {
bonds[bondclick][3] = (bonds[bondclick][3] === 1) ? bonds[bondclick][3] = 3 : bonds[bondclick][3] = 1
}
else if(type === "hashbond") {
bonds[bondclick][3] = (bonds[bondclick][3] === 2) ? bonds[bondclick][3] = 4 : bonds[bondclick][3] = 2
}
//if type is not defined, then it means we are not in a mode to choose bond stereochemistry, and the event was
//initiated by a right click, meaning we should just cycle through bond types.
else {
bonds[bondclick][3] = (bonds[bondclick][3] === 4) ? 0 : ++bonds[bondclick][3]
}
}
this.refreshCanvas()
}
//Switches the direction of the bond. Invoked when the bond order of bond = 2. Has the effect (in combination
//with the reDraw() function) of switching which side of the single bond the double bond goes. e.g. an internal
//depiction of a PI bond in a ring to an external PI bond.
Glass.prototype.switchBondDirection = function(bondindex) {
let old1 = this.bonds[bondindex][0]
let old2 = this.bonds[bondindex][1]
this.bonds[bondindex][0] = old2
this.bonds[bondindex][1] = old1
}
//copies the state into a backup (oldstates), in case the user wishes to undo the action.
Glass.prototype.saveState = function() {
if(this.oldstates.length>19) {
this.oldstates.splice(0, 1)
}
this.oldstates.push(this.exportGlass())
}
//Copy the most recent old state into the current state, and refresh canvas, to undo an action
Glass.prototype.undoAction = function() {
if(this.oldstates.length>0) {
let data = this.oldstates.pop()
this.selection = []
this.shapeSelection = []
let importData = JSON.parse(data)
this.points = importData.points
this.bonds = importData.bonds
this.shapes = importData.shapes
this.refreshCanvas()
}
}
//Uses the number of bonds an atom already has to determine how to get the coordinates of the new point
//and what those coordinates should be.
Glass.prototype.getNewPointCoords = function(pointindex) {
let newCoords = []
let l = this.options.l
let points = this.points
let [x, y, bondcheck] = [this.points[pointindex][0], this.points[pointindex][1], this.points[pointindex][3]]
switch(bondcheck.length) {
//if there are no bonds, then put next atom to bottom right.
case 0:
newCoords = [x+l*Math.cos(-Math.PI/6), y-l*Math.sin(-Math.PI/6)]
break;
//if there is one bond, make sure the next one is 120 degrees from the first
case 1:
let diff = [points[bondcheck[0]][0] - points[pointindex][0], points[pointindex][1] - points[bondcheck[0]][1]]
let angle = Math.atan2(diff[1], diff[0])
if(angle<0) {
angle = Math.abs(-Math.PI-angle)+Math.PI
}
switch(true) {
case(0 <= angle && angle < Math.PI/3):
newCoords = [x+l*Math.cos(angle+2*Math.PI/3), y-l*Math.sin(angle+2*Math.PI/3)]
break;
case(Math.PI/3 <= angle && angle < 2*Math.PI/3):
newCoords = [x+l*Math.cos(angle-2*Math.PI/3), y-l*Math.sin(angle-2*Math.PI/3)]
break;
case(2*Math.PI/3 <= angle && angle < Math.PI):
newCoords = [x+l*Math.cos(angle-2*Math.PI/3), y-l*Math.sin(angle-2*Math.PI/3)]
break;
case(Math.PI <= angle && angle < 4*Math.PI/3):
newCoords = [x+l*Math.cos(angle+2*Math.PI/3), y-l*Math.sin(angle+2*Math.PI/3)]
break;
case(4*Math.PI/3 <= angle && angle < 5*Math.PI/3):
newCoords = [x+l*Math.cos(angle+2*Math.PI/3), y-l*Math.sin(angle+2*Math.PI/3)]
break;
case(5*Math.PI/3 <= angle && angle <= 2*Math.PI):
newCoords = [x+l*Math.cos(angle-2*Math.PI/3), y-l*Math.sin(angle-2*Math.PI/3)]
break;
}
break;
//if there are two bonds, the new bond should bisect the typically reflex angle between them.
case 2:
let p1 = [points[bondcheck[0]][0], points[bondcheck[0]][1]]
let p2 = [points[bondcheck[1]][0], points[bondcheck[1]][1]]
let diff1 = [p1[0] - points[pointindex][0], points[pointindex][1] - p1[1]]
let diff2 = [p2[0] - points[pointindex][0], points[pointindex][1] - p2[1]]
let angle1 = Math.atan2(diff1[1], diff1[0])
let angle2 = Math.atan2(diff2[1], diff2[0])
let newangle = (angle1+angle2)/2
let angarray = [angle1, angle2, newangle]
$.each(angarray, function(index, value) {
if(value<0) {
let newvalue = Math.abs(-Math.PI-value)+Math.PI
angarray[index] = newvalue
}
})
if((Math.abs(angarray[0] - angarray[2]) < Math.PI/2) || (Math.abs(angarray[1] - angarray[2]) < Math.PI/2)){
newangle = (angle1+angle2)/2 + Math.PI
}
newCoords = [this.points[pointindex][0] + l * Math.cos(newangle), this.points[pointindex][1] - l * Math.sin(newangle)]
break;
case 12:
//Max number of bonds will be 12
break;
//for three or more bonds: find the angles the bonds to the pointclicked make to the x-axis, convert to 360 degree regime,
//and sort them in order. for each neighbouring angle pair, find the mean between them.
default:
let possibleangles = []
let allangles = []
for(let i=0; i<bondcheck.length; i++) {
let diff1 = [points[bondcheck[i]][0] - points[pointindex][0], points[pointindex][1] - points[bondcheck[i]][1]]
let angle = Math.atan2(diff1[1], diff1[0])
//turn negative angles into a 360 regime
if(angle<0) {
angle = Math.abs(-Math.PI-angle)+Math.PI
}
allangles.push(angle)
}
allangles.sort()
for(let i = 0; i<allangles.length; i++) {
let angle1, angle2
if(i === allangles.length-1) {
angle1 = allangles[i]
angle2 = allangles[0]
}
else {
angle1 = allangles[i]
angle2 = allangles[i+1]
}
let newangle = (angle1+angle2)/2
let angarray = [angle1, angle2, newangle]
//check/correct that each generated angle bisects the acute angle between the parent bonds, as opposed to the
//reflex angle.
if(((Math.abs(angarray[0] - angarray[2]) > Math.PI/2) || (Math.abs(angarray[1] - angarray[2]) > Math.PI/2))){
newangle = (angle1+angle2)/2 + Math.PI
}
possibleangles.push(newangle)
}
//convert possible angles into possible coordinates.
let possibles = []
$.each(possibleangles, function(index, value) {
possibles.push([x + l*Math.cos(value), y - l*Math.sin(value), 0]) //the final number is a precursor for the rating the point is given by rateMyPoints
})
//Function to rate the possibilities that were generated, and return the coordinates of the the best option.
newCoords = this.rateMyPoint(possibles)
break;
}
return [newCoords[0], newCoords[1]]
}
//From a selection of hypothetical points (possibles) and the index of the point to which any of the hypothetical points would be bonded
//to, choose the one that is the close to the least number of points, and return the coords of that point.
Glass.prototype.rateMyPoint = function(possibles) {
let points = this.points
let bonds = this.bonds
let l = this.options.l
let returns = possibles.map(value => {
let returnval = 0
$.each(points, function(bindex, bvalue) {
//Get the distance of the possible new point coords with the point currently being examined.
//Rank the points: The larger the distance, the larger the score.
let ppdistance2 = Math.pow(bvalue[0]-value[0],2) + Math.pow(bvalue[1]-value[1], 2)
//Only count this point if the distance is less than twice the bond distance.
returnval += Math.E ** (-Math.sqrt(ppdistance2))
})
return returnval
})
//find which possibility has the best ranking, by first finding the maximum score,
//finding the index of that best score, then relating that index to that
//of the "possibles" array, and return an output.
let minimum = Math.min(...returns)
let returnsIndex = returns.findIndex(x => x === minimum)
let newCoords = [possibles[returnsIndex][0], possibles[returnsIndex][1]]
return newCoords
}
//builds a ring, given the event (click), and the size of the ring to build.
Glass.prototype.buildRing = function(e, size) {
let points = this.points
let bonds = this.bonds
let l = this.options.l
if(size === "benzene") {
this.buildRing(e,6)
bonds[bonds.length-1][2]=2
bonds[bonds.length-3][2]=2
bonds[bonds.length-5][2]=2
this.refreshCanvas()
}
else {
let indextracker = []
let pointclick = this.getPointClicked(e)
let bondclick = this.getBondClicked(e)
let radius = Math.sqrt((l * l)/(2 * (1 - Math.cos(2*Math.PI/size))))
let coords = []
if(pointclick === -1 && bondclick === -1) {
let [x, y] = this.getCurrentCoords(e)
pointclick = this.addpoint(x, y, "none", false)
coords = [l*Math.cos(-Math.PI/2)+points[pointclick][0], points[pointclick][1] - l*Math.sin(-Math.PI/2)]
}
else if(pointclick !== -1){
let bondcheck = points[pointclick][3]
if(bondcheck.length==1) {
let diff = [points[pointclick][0] - points[bondcheck[0]][0], points[bondcheck[0]][1] - points[pointclick][1]]
let angle = Math.atan2(diff[1], diff[0])
coords = [l*Math.cos(angle)+points[pointclick][0], points[pointclick][1] - l*Math.sin(angle)]
}
else {
coords = this.getNewPointCoords(pointclick)
}
}
else {
let p1 = points[bonds[bondclick][0]]
let p2 = points[bonds[bondclick][1]]
let diff = [p2[0] - p1[0], p1[1] - p2[1]]
let angle = Math.atan2(diff[1], diff[0])
let intangle = ((size-2)*Math.PI)/size
let possangles = [angle-intangle/2, angle+intangle/2]
let posspoints = [[p1[0]+Math.cos(possangles[0])*radius, p1[1]-Math.sin(possangles[0])*radius, 0], [p1[0]+Math.cos(possangles[1])*radius, p1[1]-Math.sin(possangles[1])*radius, 0]]
coords = this.rateMyPoint(posspoints)
//this.addpoint(posspoints[0][0], posspoints[0][1], "none", true)
//this.addpoint(posspoints[1][0], posspoints[1][1], "none", true)
pointclick = bonds[bondclick][0]
}
let diff = [coords[0] - points[pointclick][0], coords[1] - points[pointclick][1]]
let angle = Math.atan2(diff[1], diff[0])
let centre = [points[pointclick][0] + radius * Math.cos(angle), points[pointclick][1] + radius * Math.sin(angle)]
let lastpoint = pointclick
diff = [points[pointclick][0] - coords[0], coords[1] - points[pointclick][1]]
angle = Math.atan2(diff[1], diff[0])
for(i = 1; i<size+1; i++) {
if(i === size) {
this.addpoint(points[pointclick][0], points[pointclick][1], lastpoint, false)
}
else {
let nextindex = this.addpoint(centre[0]+(radius*Math.cos(angle + 2*Math.PI*i/size)), centre[1]-(radius*Math.sin(angle + 2*Math.PI*i/size)), lastpoint, false)
lastpoint = nextindex
}
}
}
}
//highlights a particular atom
Glass.prototype.highlightAtom = function(pointIndex) {
let x = this.points[pointIndex][0]
let y = this.points[pointIndex][1]
this.ctx.beginPath()
this.ctx.arc(x, y, 5, 0, 2*Math.PI);
this.ctx.lineWidth=1
this.ctx.stroke()
}
//highlights a particular bond
Glass.prototype.highlightBond = function(bondclick) {
let locus = this.options.locus
let points = this.points
let bonds = this.bonds
let ctx = this.ctx
let p1 = [points[bonds[bondclick][0]][0], points[bonds[bondclick][0]][1]]
let p2 = [points[bonds[bondclick][1]][0], points[bonds[bondclick][1]][1]]
let angle = Math.atan2((p1[1]-p2[1]), (p2[0]-p1[0]))
let start = [p1[0] + locus * Math.cos(angle+(Math.PI/2)), p1[1] - locus * Math.sin(angle+(Math.PI/2))]
let end = [p2[0] + locus * Math.cos(angle+Math.PI/2), p2[1] - locus * Math.sin(angle+Math.PI/2)]
ctx.beginPath()
ctx.lineWidth=1
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
let mid1 = [end[0] + (locus * Math.cos(angle)), end[1] - (locus * Math.sin(angle))]
let top = [p2[0] + (locus * Math.cos(angle)), p2[1] - (locus * Math.sin(angle))]
ctx.arcTo(mid1[0], mid1[1], top[0], top[1], locus)
let start2 = [p2[0] + locus * Math.cos(angle-Math.PI/2), p2[1] - locus * Math.sin(angle-Math.PI/2)]
let mid2 = [start2[0] + (locus * Math.cos(angle)), start2[1] - (locus * Math.sin(angle))]
ctx.arcTo(mid2[0], mid2[1], start2[0], start2[1], locus)
let end2 = [p1[0] + locus * Math.cos(angle-Math.PI/2), p1[1] - locus * Math.sin(angle-Math.PI/2)]
ctx.lineTo(end2[0], end2[1])
let mid3 = [end2[0] - locus * Math.cos(angle), end2[1] + locus * Math.sin(angle)]
let bottom = [p1[0] - locus * Math.cos(angle), p1[1] + locus * Math.sin(angle)]
ctx.arcTo(mid3[0], mid3[1], bottom[0], bottom[1], locus)
let mid4 = [start[0] - locus * Math.cos(angle), start[1] + locus * Math.sin(angle)]
ctx.arcTo(mid4[0], mid4[1], start[0], start[1], locus)
ctx.stroke()
}
//highlights a particular shape.
Glass.prototype.highlightShape = function(index) {
if(this.shapes[index][0] === "arrow") {
let locus = this.options.arrowHead * 2
let points = this.points
let ctx = this.ctx
let shape = this.shapes[index][1]
let p1 = [shape[0], shape[1]]
let p2 = [shape[0] + shape[3] * Math.cos(shape[2]), shape[1] - shape[3] * Math.sin(shape[2])]
let angle = shape[2]
let start = [p1[0] + locus * Math.cos(angle+(Math.PI/2)), p1[1] - locus * Math.sin(angle+(Math.PI/2))]
let end = [p2[0] + locus * Math.cos(angle+Math.PI/2), p2[1] - locus * Math.sin(angle+Math.PI/2)]
ctx.beginPath()
ctx.lineWidth=1
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
let mid1 = [end[0] + (locus * Math.cos(angle)), end[1] - (locus * Math.sin(angle))]
let top = [p2[0] + (locus * Math.cos(angle)), p2[1] - (locus * Math.sin(angle))]
ctx.arcTo(mid1[0], mid1[1], top[0], top[1], locus)
let start2 = [p2[0] + locus * Math.cos(angle-Math.PI/2), p2[1] - locus * Math.sin(angle-Math.PI/2)]
let mid2 = [start2[0] + (locus * Math.cos(angle)), start2[1] - (locus * Math.sin(angle))]
ctx.arcTo(mid2[0], mid2[1], start2[0], start2[1], locus)
let end2 = [p1[0] + locus * Math.cos(angle-Math.PI/2), p1[1] - locus * Math.sin(angle-Math.PI/2)]
ctx.lineTo(end2[0], end2[1])
let mid3 = [end2[0] - locus * Math.cos(angle), end2[1] + locus * Math.sin(angle)]
let bottom = [p1[0] - locus * Math.cos(angle), p1[1] + locus * Math.sin(angle)]
ctx.arcTo(mid3[0], mid3[1], bottom[0], bottom[1], locus)
let mid4 = [start[0] - locus * Math.cos(angle), start[1] + locus * Math.sin(angle)]
ctx.arcTo(mid4[0], mid4[1], start[0], start[1], locus)
ctx.stroke()
}
}
//redraws the state onto the canvas. May be called up to many times per second.
Glass.prototype.reDraw = function() {
let ctx = this.ctx
let points = this.points
let bonds = this.bonds
let locus = this.options.locus
let that = this
let l = this.options.l
let bondorderarr = {}
//get those double bonds which are part of a ring, as we don't want a centred double bond for those.
let noDoubleBondCentering = []
let rings = this.currentRings
let ringarray = []
$.each(rings, function(index, value) {
if(value[0] === "aromatic" || value[0] === "parsat") {
Array.prototype.push.apply(ringarray, value[2])
}
})
$.each(bonds, function(index, value) {
if(ringarray.includes(value[0]) && ringarray.includes(value[1])) {
noDoubleBondCentering.push(index)
}
})
//Draw out all of the bonds onto the canvas
$.each(bonds, function(index, value) {
//whilst we're flicking through all the bonds, set up an array order by point index, detailing
//that points bond order, ready for atom labelling below.
bondorderarr[value[0]]= bondorderarr[value[0]] + value[2] || value[2]
bondorderarr[value[1]]= bondorderarr[value[1]] + value[2] || value[2]
//end
let bondsToAtomOne = points[value[0]][3].length
let bondsToAtomTwo = points[value[1]][3].length
let p1 = [points[value[0]][0], points[value[0]][1], points[value[0]][2]]
let p2 = [points[value[1]][0], points[value[1]][1], points[value[1]][2]]
let angle = Math.atan2((p1[1]-p2[1]), (p2[0]-p1[0]))
switch (value[2]) {
case 1:
if(value[3] === 1) {
ctx.beginPath()
ctx.moveTo(p1[0], p1[1])
let mid1 = [p2[0] + locus/2 * Math.cos(angle+(Math.PI/2)), p2[1] - locus/2 * Math.sin(angle+(Math.PI/2))]
let mid2 = [p2[0] + locus/2 * Math.cos(angle-(Math.PI/2)), p2[1] - locus/2 * Math.sin(angle-(Math.PI/2))]
ctx.lineTo(mid1[0], mid1[1])
ctx.lineTo(mid2[0], mid2[1])
ctx.lineTo(p1[0], p1[1])
ctx.fill()
}
else if(value[3] === 2) {
ctx.beginPath()
for(let i = 0; i<5; i++) {
let mid1 = [p1[0] + l*i/5*Math.cos(angle) + locus*i/5 * Math.cos(angle+(Math.PI/2)), p1[1] - l*i/5*Math.sin(angle) - locus*i/5 * Math.sin(angle+(Math.PI/2))]
let mid2 = [p1[0] + l*i/5*Math.cos(angle) + locus*i/5 * Math.cos(angle-(Math.PI/2)), p1[1] - l*i/5*Math.sin(angle) - locus*i/5 * Math.sin(angle-(Math.PI/2))]
ctx.moveTo(mid1[0], mid1[1])
ctx.lineTo(mid2[0], mid2[1])
}
ctx.lineWidth=1
ctx.stroke()
}
else if(value[3] === 3) {
ctx.beginPath()
ctx.moveTo(p2[0], p2[1])
let mid1 = [p1[0] + locus/2 * Math.cos(angle+(Math.PI/2)), p1[1] - locus/2 * Math.sin(angle+(Math.PI/2))]
let mid2 = [p1[0] + locus/2 * Math.cos(angle-(Math.PI/2)), p1[1] - locus/2 * Math.sin(angle-(Math.PI/2))]
ctx.lineTo(mid1[0], mid1[1])
ctx.lineTo(mid2[0], mid2[1])
ctx.lineTo(p2[0], p2[1])
ctx.fill()
}
else if(value[3] === 4) {
ctx.beginPath()
for(let i = 0; i<5; i++) {
let mid1 = [p2[0] - l*i/5*Math.cos(angle) - locus*i/5 * Math.cos(angle+(Math.PI/2)), p2[1] + l*i/5*Math.sin(angle) + locus*i/5 * Math.sin(angle+(Math.PI/2))]
let mid2 = [p2[0] - l*i/5*Math.cos(angle) - locus*i/5 * Math.cos(angle-(Math.PI/2)), p2[1] + l*i/5*Math.sin(angle) + locus*i/5 * Math.sin(angle-(Math.PI/2))]
ctx.moveTo(mid1[0], mid1[1])
ctx.lineTo(mid2[0], mid2[1])
}
ctx.lineWidth=1
ctx.stroke()
}
else {
ctx.beginPath()
ctx.moveTo(p1[0], p1[1])
ctx.lineWidth=2
ctx.lineTo(p2[0], p2[1])
ctx.stroke()
}
break
case 2:
if(!noDoubleBondCentering.includes(index) && ((bondsToAtomOne>2 && bondsToAtomTwo === 1) || (bondsToAtomTwo>2 && bondsToAtomOne === 1) || (p1[2] !== 67 || p2[2] !== 67))) {
//docentred double bond
let exlen = 1
let start1 = [p1[0] + locus/2 * Math.cos(angle+(Math.PI/2)) - exlen*Math.cos(angle), p1[1] - locus/2 * Math.sin(angle+Math.PI/2) + exlen*Math.sin(angle)]
let end1 = [p2[0] + locus/2 * Math.cos(angle+Math.PI/2) + exlen*Math.cos(angle), p2[1] - locus/2 * Math.sin(angle+Math.PI/2) - exlen*Math.sin(angle)]
let start2 = [p1[0] + locus/2 * Math.cos(angle-(Math.PI/2)) - exlen*Math.cos(angle), p1[1] - locus/2 * Math.sin(angle-Math.PI/2) + exlen*Math.sin(angle)]
let end2 = [p2[0] + locus/2 * Math.cos(angle-Math.PI/2) + exlen*Math.cos(angle), p2[1] - locus/2 * Math.sin(angle-Math.PI/2) - exlen*Math.sin(angle)]
ctx.beginPath()
ctx.lineWidth=2
ctx.moveTo(start1[0], start1[1])
ctx.lineTo(end1[0], end1[1])
ctx.stroke()
ctx.beginPath()
ctx.lineWidth=2
ctx.moveTo(start2[0], start2[1])
ctx.lineTo(end2[0], end2[1])
ctx.stroke()
}
else {
ctx.beginPath()
ctx.moveTo(p1[0], p1[1])
ctx.lineWidth=2
ctx.lineTo(p2[0], p2[1])
ctx.stroke()
let angle = Math.atan2((p1[1]-p2[1]), (p2[0]-p1[0]))
let start = [p1[0] + locus * Math.cos(angle+(Math.PI/3)), p1[1] - locus * Math.sin(angle+(Math.PI/3))]
let end = [p2[0] - locus * Math.cos(angle-Math.PI/3), p2[1] + locus * Math.sin(angle-Math.PI/3)] // draw double bond not quite as long as that of single bond
ctx.beginPath()
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
ctx.stroke()
}
break;
case 3:
ctx.beginPath()
ctx.moveTo(p1[0], p1[1])
ctx.lineWidth=2
ctx.lineTo(p2[0], p2[1])
ctx.stroke()
let start = [p1[0] + locus * Math.cos(angle+(Math.PI/2)), p1[1] - locus * Math.sin(angle+(Math.PI/2))]
let end = [p2[0] + locus * Math.cos(angle+Math.PI/2), p2[1] - locus * Math.sin(angle+Math.PI/2)]
ctx.beginPath()
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
ctx.stroke()
start = [p1[0] + locus * Math.cos(angle-(Math.PI/2)), p1[1] - locus * Math.sin(angle-(Math.PI/2))]
end = [p2[0] + locus * Math.cos(angle-Math.PI/2), p2[1] - locus * Math.sin(angle-Math.PI/2)]
ctx.beginPath()
ctx.moveTo(start[0], start[1])
ctx.lineTo(end[0], end[1])
ctx.stroke()
break;
}
})
$.each(points, function(index, value) {
let bondcheck = bondorderarr[index] || 0
let fontsize = that.options.fontsize //define fontsize from that set in global options
let whitespace = that.options.whitespace
if(that.options.pointsAsIndexed === false) {
if(value[2] === 67) {
if(bondcheck === 0) {
ctx.beginPath()
ctx.arc(value[0], value[1],whitespace/2,0,2*Math.PI)
ctx.fillStyle = that.strokeStyle
ctx.fill()
}
}
else {
ctx.beginPath()
ctx.arc(value[0],value[1],whitespace,0,2*Math.PI);
ctx.fillStyle = that.canvasColour //Use the background colour of the canvas so this matches.
ctx.fill()
ctx.beginPath()
ctx.fillStyle = that.strokeStyle
ctx.font = fontsize + "px Calibri"
//get correct alignment of the atom label e.g. align center for H, Br, N, but align left for complex groups like B(OH)2
let alignstyle = that.atomdict[value[2]][3] || "center"
ctx.textAlign = alignstyle
ctx.fillText(that.atomdict[value[2]][0], value[0], value[1]+(fontsize/3))
let atomarr = that.atomdict[value[2]][1]
let bondorder = -1
if(bondcheck > atomarr.length) {
bondorder = atomarr.length
}
else {
bondorder = bondcheck - value[4] //The bond order is a combination of the number of bonds, and the charge on the atom
}
if(atomarr.length>0 && atomarr[bondorder-1]>0) {
let coords = that.getNewPointCoords(index)
let diff = [coords[0] - value[0], value[1]-coords[1]]
let angle = Math.atan2(diff[1], diff[0])
//convert all negative angles to their positive equivalent for easier analysis
if(angle<0) {
angle = Math.abs(-Math.PI-angle)+Math.PI
}
let xspacing = 2
let yspacing = 2
let mytext = ""
if(atomarr[bondorder-1] === 1) {
mytext = "H"
}
else if(atomarr[bondorder-1] === 2) {
mytext = "H"+String.fromCharCode(8322)
xspacing = 5
}
else if(atomarr[bondorder-1] === 3) {
mytext = "H"+String.fromCharCode(8323)
xspacing = 5
}
if(Math.PI/4 < angle && angle < 3*Math.PI/4 && value[3].length>1){ //Hydrogens should only be oriented like this if the point has more than 1 bond.
//hydrogen should be above parent heteroatom
ctx.fillText(mytext, value[0], value[1]-(1*fontsize/3)-yspacing)
}
else if(5*Math.PI/4 < angle && angle < 7*Math.PI/4 && value[3].length > 1) {
//hydrogen should be below parent heteroatom
ctx.fillText(mytext, value[0], value[1]+(3*fontsize/3)+yspacing)
}
else if(Math.PI/2 < angle && angle < 3*Math.PI/2){
//hydrogen should be to left of parent heteroatom
ctx.fillText(mytext, value[0]-(fontsize/2)-xspacing, value[1]+(fontsize/3))
}
else{
//hydrogen should be to right of parent heteroatom
ctx.fillText(mytext, value[0]+(fontsize/2)+xspacing, value[1]+(fontsize/3))
}
}
}
//Label the atom with its charge, if necessary
if(value[4] !== 0) {
ctx.beginPath()
ctx.font = fontsize/1.3 + "px Calibri"
//get correct alignment of the atom label e.g. align center for H, Br, N, but align left for complex groups like B(OH)2
let mytext = value[4]
if(mytext === -1) {
mytext = "-"
}
else if(mytext === 1) {
mytext = "+"
}
else if(mytext > 1) {
mytext = "+" + value[4]
}
let xspacing = whitespace * Math.cos(Math.PI/4) * 1.3
let yspacing = whitespace * Math.sin(Math.PI/4) * 1.3
if(value[3].length > 1) {
let coords = that.getNewPointCoords(index)
let diff = [coords[0] - value[0], value[1]-coords[1]]
let angle = Math.atan2(diff[1], diff[0])
//convert all negative angles to their positive equivalent for easier analysis
if(angle<0) {
angle = Math.abs(-Math.PI-angle)+Math.PI
}
if(Math.PI/2 < angle && angle < Math.PI) {
//charge should be placed top left of atom
ctx.fillText(mytext, value[0] - xspacing, value[1] - yspacing)
}
else if(Math.PI < angle && angle < 3*Math.PI/2) {
//charge should be placed bottom left of atom
ctx.fillText(mytext, value[0] - xspacing, value[1] + yspacing)
}
else if(3*Math.PI/2 < angle && angle < 2*Math.PI) {
//charge should be placed bottom right of atom
ctx.fillText(mytext, value[0] + xspacing, value[1] + yspacing)
}
else {
//charge should be placed top right of atom
ctx.fillText(mytext, value[0] + xspacing, value[1] - yspacing)
}
}
else {
//charge should be placed top right of atom
ctx.fillText(mytext, value[0] + xspacing, value[1] - yspacing)
}
}
}
else {
ctx.beginPath()
ctx.arc(value[0],value[1],whitespace,0,2*Math.PI);
ctx.fillStyle = that.canvasColour
ctx.fill()
ctx.beginPath()
ctx.fillStyle = that.strokeStyle
ctx.font = fontsize + "px Calibri"
ctx.textAlign = "center"
ctx.fillText(index, value[0], value[1]+(fontsize/3))
}
})
$.each(this.shapes, function(index, value) {
if(value[0] === "arrow") {
let x = value[1][0], y = value[1][1], angle = value[1][2], length = value[1][3]
let ahs = that.options.arrowHead
let x1 = x + Math.cos(angle)*(length-5)
let y1 = y - Math.sin(angle)*(length-5)
that.ctx.beginPath()
that.ctx.lineWidth=2
that.ctx.moveTo(x, y)
that.ctx.lineTo(x1, y1)
that.ctx.stroke()
let point1 = [x1 + Math.cos(angle+Math.PI/2)*ahs, y1-Math.sin(angle+Math.PI/2)*ahs]
that.ctx.lineTo(point1[0], point1[1])
that.ctx.stroke()
let point2 = [x1 + Math.cos(angle)*ahs, y1-Math.sin(angle)*ahs]
that.ctx.lineTo(point2[0], point2[1])
that.ctx.stroke()
let point3 = [x1 + Math.cos(angle-Math.PI/2)*ahs, y1-Math.sin(angle-Math.PI/2)*ahs]
that.ctx.lineTo(point3[0], point3[1])
that.ctx.stroke()
that.ctx.lineTo(x1, y1)
that.ctx.stroke()
}
})
$.each(this.selection, function(index, value) {
that.highlightAtom(value)
})
$.each(this.shapeSelection, function(index, value) {
that.highlightShape(value)
})
}
//Selects the items that appear inside the box that is drawn by the fn below, in response from mouse "click-n-drag"
//input from the user.
Glass.prototype.selectItems = function(e) {
$(' #'+this.id+'-canvas').off("mousemove")
let [startx, starty] = this.getCurrentCoords(e)
let that = this
$(' #'+this.id+'-canvas').on("mousemove", function(p) {
that.selector = true
that.refreshCanvas()
let [endx, endy] = that.getCurrentCoords(p)
that.selection = []
that.shapeSelection = []
$.each(that.points, function(index, value) {
if(((startx < value[0] && value[0] < endx) || (startx > value[0] && value[0] > endx)) && ((starty < value[1] && value[1] < endy) || (starty > value[1] && value[1] > endy))) {
that.selection.push(index)
}
})
$.each(that.shapes, function(index, value) {
if(value[0] === "arrow") {
let arrowheadXY = [value[1][0] + value[1][3]*Math.cos(value[1][2]), value[1][1] - value[1][3]*Math.sin(value[1][2])]
if(((startx < value[1][0] && value[1][0] < endx) || (startx > value[1][0] && value[1][0] > endx)) && ((starty < value[1][1] && value[1][1] < endy) || (starty > value[1][1] && value[1][1] > endy))) {
if(((startx < arrowheadXY[0] && arrowheadXY[0] < endx) || (startx > arrowheadXY[0] && arrowheadXY[0] > endx)) && ((starty < arrowheadXY[1] && arrowheadXY[1] < endy) || (starty > arrowheadXY[1] && arrowheadXY[1] > endy))) {
that.shapeSelection.push(index)
}
}
}
})
that.ctx.beginPath()
that.ctx.lineWidth=1
that.ctx.rect(startx, starty, endx-startx, endy-starty)
that.ctx.stroke();
})
}
//Select everything on the page
Glass.prototype.selectAll = function() {