-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
149 lines (125 loc) · 2.69 KB
/
main.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
let rng = document.querySelector('#range');
let length = 5;
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
let minimum = Math.min(innerWidth, innerHeight);
canvas.width = minimum/1.5;
canvas.height = minimum/1.5;
addEventListener('resize', () => {
minimum = Math.min(innerWidth, innerHeight);
canvas.width = minimum/1.5;
canvas.height = minimum/1.5;
});
rng.addEventListener('change', () => {
length = rng.value;
});
let s = new Shapes(ctx);
let axiom, theta, sentence;
let rules = [];
let translate;
function bush() {
translate = new Vector2D(canvas.width/2, canvas.height);
axiom = 'F';
rules[0] = {
a: 'F',
b: 'FF+[+F-F-F]-[-F+F+F]'
};
theta = 22.5;
sentence=axiom;
}
function crystal() {
translate = new Vector2D(canvas.width, canvas.height);
axiom = 'F+F+F+F';
rules[0] = {
a: 'F',
b: 'FF+F++F+F'
};
theta = 90;
sentence=axiom;
}
function QuadraticSnowflake() {
translate = new Vector2D(canvas.width, canvas.height);
axiom = 'FF+FF+FF+FF';
rules[0] = {
a: 'F',
b: 'F+F-F-F+F'
};
theta = 90;
sentence=axiom;
}
function Snowflake() {
translate = new Vector2D(canvas.width-100, canvas.height);
axiom = 'F++F++F';
rules[0] = {
a: 'F',
b: 'F-F++F-F'
};
theta = 60;
sentence=axiom;
}
function QuadraticKochIsland() {
translate = new Vector2D(canvas.width-100, canvas.height-100);
axiom = 'F+F+F+F';
rules[0] = {
a: 'F',
b: 'F+F-F-FFF+F+F-F'
};
theta = 90;
sentence=axiom;
}
function Pentaplexity() {
translate = new Vector2D(canvas.width-100, canvas.height-100);
axiom = 'F++F++F++F++F';
rules[0] = {
a: 'F',
b: 'F++F++F|F-F++F'
};
theta = 36;
sentence=axiom;
}
function turtle() {
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate(translate.x, translate.y);
for (let l = 0; l < sentence.length; l++) {
let now = sentence.charAt(l);
//console.log(now)
if (now == 'F') {
ctx.beginPath();
ctx.strokeStyle = 'white';
ctx.moveTo(0, 0);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.translate(0, -length);
}else if (now == '+') {
ctx.rotate(-theta*Math.PI/180);
}else if (now == '-') {
ctx.rotate(theta*Math.PI/180);
}else if (now == '[') {
ctx.save();
}else if (now == ']') {
ctx.restore();
}else if (now == '|') {
ctx.rotate(180*Math.PI/180);
}
}
}
function generate() {
let newSentence = '';
for (let i = 0; i < sentence.length; i++) {
for (let j = 0; j < rules.length; j++) {
if (sentence.charAt(i) == rules[j].a) {
newSentence += rules[j].b;
}else{
newSentence += sentence.charAt(i);
}
}
}
sentence = newSentence;
turtle();
}
addEventListener('keypress', (e) => {
if (e.key == 'q') {
generate();
}
});