Skip to content

Commit 318ef42

Browse files
committed
A small cleanup and comments
1 parent 850d5a9 commit 318ef42

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

gallifreyan.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
var canvasSize=1000.0;
2-
var canvasScale=canvasSize/800.0;
3-
var midPoint=canvasSize/2.0;
4-
var outerR=midPoint*0.9;
5-
var globalR;
1+
var canvasSize=1000.0; //the image resolution in pixels
2+
var canvasScale=canvasSize/800.0; //800=the canvas size on the screen
3+
var midPoint=canvasSize/2.0; //the (x, y) of the centerpoint
4+
var outerR=midPoint*0.9; //radius of the outermost circle
65
var lineWidth=3.0*canvasScale;
76
var PI=Math.PI;
87

98
var allCircles=[],
109
mainCircles=[],
11-
currentCircle, //points to a mainCircle which contains selectedCircle
12-
selectedCircle=-1, //points to selected circle
13-
snapMode=true; //can't be disabled for now
10+
currentCircle, //points to a mainCircle which contains selectedCircle
11+
selectedCircle=-1, //points to selected circle
12+
snapMode=true; //disabling this disables some rule checking; can't be toggled for now
1413

1514
var lines=[],
16-
selectedLine=-1,
17-
lineEnd=0;
15+
selectedLine=-1, //points to selected line
16+
lineEnd=0; //tells which end of the line is selected
1817

19-
var dirtyRender=1;
20-
21-
var word,
22-
wordL; //final number of circles around the mainCircle
18+
var dirtyRender=true; //whether GUI and red dots will be drawn
2319

2420
Array.prototype.contains = function(k){
2521
for(var p in this)
@@ -45,6 +41,7 @@ $(document).ready(function(){
4541
redraw();
4642
});
4743

44+
//resets everything and parses the text
4845
function updateText(){
4946
resetZoom();
5047

@@ -65,6 +62,8 @@ function updateText(){
6562
generateWords(w);
6663
}
6764

65+
//a line is always defined be the circles it is connected to and angles in relation to these circles.
66+
//thus, it will always be connected to the circles' borders.
6867
function Line(circle1, a1, circle2, a2){
6968
this.draw=function(){
7069
if(selectedLine==this) ctx.strokeStyle="grey";
@@ -97,6 +96,13 @@ function Line(circle1, a1, circle2, a2){
9796
this.update();
9897
}
9998

99+
//every circle or arc you can see is of this class.
100+
//every circles has:
101+
//an owner - the location is always calculated in relation to its owner's position and angle
102+
//a type - which corresponds to the row of the alphabet
103+
//a subtype - which corresponds to the column of the alphabet
104+
//if the letter is a vowel, then type=5 (when it's a standalone letter) or 6 (when it's connected to a consonant)
105+
//a list of other circles and lines connected to it, so they can easily updated in a cascading style
100106
function Circle(owner,type,subtype, d, r, a){
101107
this.draw = function(){
102108
if(selectedCircle==this) ctx.strokeStyle="grey";
@@ -137,7 +143,7 @@ function Circle(owner,type,subtype, d, r, a){
137143
ctx.strokeStyle="black";
138144
if(dirtyRender && this.selectable){ctx.beginPath(); ctx.arc(this.x,this.y,lineWidth,0,PI*2);ctx.fillStyle="red"; ctx.fill();}
139145
}
140-
this.update=function(d, a){
146+
this.update=function(d, a){ //recalculates the position, forces other circles/lines connected to it to update too
141147
var dx, dy;
142148
var oldA=this.a;
143149
dx=Math.cos(a)*(d), dy=Math.sin(a)*(d);
@@ -155,12 +161,14 @@ function Circle(owner,type,subtype, d, r, a){
155161
this.owner=owner;
156162
this.children=[];
157163
this.type=type; this.subtype=subtype;
158-
this.nLines=0; this.lines=[];
164+
this.nLines=0; //expected number of lines, according to rules
165+
this.lines=[];
159166
this.selectable=true;
160167
this.r = r;
161168
this.update(d, a);
162169
}
163170

171+
//selects the circle/line. Checks whether any buttons are pressed.
164172
function doClick(e){
165173
var mouse=getMouse(e);
166174
if(selectedCircle != -1) {selectedCircle=-1; redraw(); return;}
@@ -196,6 +204,7 @@ function doClick(e){
196204
if(selectedLine!=-1){selectedCircle=-1;}
197205
};
198206

207+
//makes sure that the correct distance from the base circle is kept according to language rules
199208
function updateLocation(selected, d, a){
200209
if(!snapMode) {selected.update(d, a); return;}
201210
switch(selected.type){
@@ -245,6 +254,7 @@ function updateLocation(selected, d, a){
245254
for(var i=0;i<selected.children.length;i++) updateLocation(selected.children[i], selected.children[i].d, selected.children[i].a);
246255
}
247256

257+
//manages the movement of circles and lines. In case of circles, updateLocation() is called to enforce language rules
248258
$('canvas').mousemove(function(e){
249259
var mouse=getMouse(e);
250260
if(selectedCircle != -1){
@@ -283,6 +293,7 @@ $('canvas').mousemove(function(e){
283293
}
284294
});
285295

296+
//changes the circle's radius
286297
$('canvas').mousewheel(function(event, delta, deltaX, deltaY){
287298
if(selectedCircle != -1){
288299

@@ -305,6 +316,7 @@ $('canvas').mousewheel(function(event, delta, deltaX, deltaY){
305316
return false;
306317
});
307318

319+
//draws red lines to signify the min/max angles that the circle can move within
308320
function drawAngles(){
309321
if(currentCircle.children.length<3) return;
310322
var len=selectedCircle.owner.r*1.3;
@@ -319,6 +331,7 @@ function drawAngles(){
319331
ctx.strokeStyle="black";
320332
}
321333

334+
//generates the sentence
322335
function generateWords(words){
323336
allCircles.push(new Circle({x:midPoint, y:midPoint, a:0}, 4,0,0, outerR, 0));
324337
allCircles[0].selectable=false;
@@ -331,30 +344,31 @@ function generateWords(words){
331344
for(var i=0;i<words.length;i++){
332345
if(i>0)angle-=delta;if(angle<-PI) angle+=2*PI;
333346

334-
word=words[i];
335-
wordL=0;
347+
var word=words[i];
348+
var wordL=0; //approximates the number of letters, taking into account that some will be merged
336349
for(var j=0;j<word.length;j++){
337350
if(j>0 && word[j].match("(a|e|i|o|u)") && !(word[j-1].match("(a|e|i|o|u)"))) continue;
338351
wordL++;
339352
}
340-
generateWord(word, r, d, angle)
353+
generateWord(word, wordL, r, d, angle)
341354
}
342-
redraw();
343355
createLines();
344356

345357
redraw();
346358
}
347359

360+
//assigns the subtype
348361
var map={"b":1,"ch":2,"d":3,"f":4,"g":5,"h":6,
349362
"j":1,"k":2,"l":3,"m":4,"n":5,"p":6,
350363
"t":1,"sh":2,"r":3,"s":4,"v":5,"w":6,
351364
"th":1,"y":2,"z":3,"nq":4,"qu":5,"x":6,
352365
"a":1,"e":2,"i":3,"o":4,"u":5};
353366

354-
function generateWord(word, mcR, dist, mainAngle){
367+
//generates a single word
368+
function generateWord(word, wordL, mcR, dist, mainAngle){
355369
var delta=2*PI/wordL;
356370
var angle=PI/2;
357-
globalR=1.8*mcR/(wordL+2);
371+
var globalR=1.8*mcR/(wordL+2);
358372

359373
var i;
360374
var owner, newCircle;
@@ -422,6 +436,7 @@ function generateWord(word, mcR, dist, mainAngle){
422436
}
423437
}
424438

439+
//generates the lines after all the circles are created
425440
function createLines(){
426441
var i, j, k, circle, circle2, intersection, angle;
427442
var bestAngle, inter, minInter;
@@ -499,6 +514,7 @@ function createLines(){
499514
}
500515
}
501516

517+
//checks whether all the circles have a correct amount of lines connected
502518
function checkLines(){
503519
for(var i=1;i<allCircles.length;++i){ //we don't check the first circle
504520
if(mainCircles.indexOf(allCircles[i])!=-1) continue; //also skip mainCircles
@@ -507,10 +523,11 @@ function checkLines(){
507523
return 1;
508524
}
509525

526+
//the core drawing routine
510527
function redraw(){
511528
ctx.setTransform(1,0,0,1, 0, 0);
512529
ctx.clearRect(0,0,canvasSize,canvasSize);
513-
530+
514531
var data=scrollerObj.getValues();
515532
ctx.setTransform(data.zoom,0,0,data.zoom,-data.left*canvasScale,-data.top*canvasScale);
516533

0 commit comments

Comments
 (0)