-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
102 lines (93 loc) · 2.38 KB
/
index.html
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
<!doctype html>
<html>
<head>
<style>
body {
background-color: #ccc;
font-family: monospace;
word-wrap: break-word;
}
div { width: 100%; }
canvas { margin: 0 auto; display: block;}
</style>
</head>
<body>
<div>
<canvas id="primaryCanvas" width="800" height="600"></canvas>
</div>
<div>Generation <span id="generation"></span></div>
<script>
var App = {};
var AXIOM = "0";
var currentGeneration = AXIOM;
var rules = {
'0': { // This rule will apply to '0' character
becomes: '1[0]0', // The '0' character will become '1[0]0' in the next generation
draws: function(ctx) { // Here we define what a '0' character is drawn as
ctx.translate(0, -10); // Move our cursor 10 units up
ctx.lineTo(0, 0); // Draw a line to the cursor
}
},
'1': {
becomes: '11', // Note that this can also be a function that takes in this character
draws: function(ctx) {
ctx.translate(0, -10);
ctx.lineTo(0, 0);
}
},
'[': {
draws: function(ctx) {
ctx.save(); // This saves the state of the drawing context, letting us remember where we were.
ctx.moveTo(0, 0);
ctx.rotate(-Math.PI / 4); // Rotate the space 45 degrees to the left, so that a line pointing up before now is diagonal to the left
}
},
']': {
draws: function(ctx) {
ctx.restore(); // This recalls the state of the last saved drawing context.
ctx.moveTo(0, 0);
ctx.rotate(Math.PI / 4);
}
}
}
// This function is called once at the start of the app.
// Use it to set up everything that you only want to draw once!
App.start = function(ctx) {
};
// This function is called every frame. Use it it you want to
// draw something every frame!
App.update = function(ctx, time) {
ctx.translate(400, 600);
ctx.moveTo(0, 0);
App.drawGeneration(ctx);
};
App.drawGeneration = function(ctx) {
for(var i = 0; i < currentGeneration.length; ++i) {
var c = currentGeneration.charAt(i);
if(rules[c] && rules[c].draws) {
rules[c].draws(ctx);
}
}
};
App.generate = function(n) {
var newGeneration = "";
for(var i = 0; i < currentGeneration.length; ++i) {
var c = currentGeneration.charAt(i);
if(rules[c] && rules[c].becomes) {
if(typeof(rules[c].becomes) == 'function') {
newGeneration += rules[c].becomes(c);
}
else {
newGeneration += rules[c].becomes;
}
} else {
newGeneration += c;
}
}
currentGeneration = newGeneration;
return currentGeneration;
};
</script>
<script src="lib.js"></script>
</body>
</html>