-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
90 lines (73 loc) · 2.06 KB
/
Copy pathparser.js
File metadata and controls
90 lines (73 loc) · 2.06 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
class Parser {
constructor(code, colour) {
this.code = code;
this.colour = colour || 255;
/**
* if true then the pen is down. else its up
*/
this.drawing = true;
push();
stroke(this.colour);
strokeWeight(2);
translate(height / 2, width / 2);
this.parse(this.code);
pop();
}
parse(code) {
if(!code){
return false;
}
code = code.trim();
let c = [];
let tmp = code.split(/(\s)|(\[.+\])/gm);
tmp.forEach(function(el){
if(el && el.trim()){
c.push(el);
}
});
while (c.length > 0) {
// get the first element of array
switch (c.shift()) {
case "fd":
this.move(parseInt(c.shift()));
break;
case "bd":
this.move(-parseInt(c.shift()));
break;
case "lt":
this.turn(-parseInt(c.shift()));
break;
case "rt":
this.turn(parseInt(c.shift()));
break;
case "pu":
this.drawing = false;
break;
case "pd":
this.drawing = true;
break;
case "repeat":
let amt = parseInt(c.shift());
let command = c.shift().replace(/^\[|\]$/g, "");
// console.log(command);
if(amt && command){
for(let i = 0; i < amt; i++){
try{
this.parse(command);
}catch(err){}
}
}
break;
}
}
}
move(amt) {
if (this.drawing) {
line(0, 0, amt, 0);
}
translate(amt, 0);
}
turn(angle) {
rotate(angle);
}
}