-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathkelpforest.html
More file actions
133 lines (117 loc) · 4.47 KB
/
kelpforest.html
File metadata and controls
133 lines (117 loc) · 4.47 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
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
<html>
<head>
<title>Kelp Forest => Urchin Barren</title>
</head>
<body>
<script type="module">
import Animator from '/src/Animator.js'
import TwoDraw from '/src/TwoDraw.js'
import Model from '/models/KelpForestModel.js'
const model = new Model()
model.setup()
const view = new TwoDraw(model, {
div: 'modelDiv',
patchSize: 20,
drawOptions: {
turtlesSize: t => {
if (t.breed.name === 'urchin') return 1
if (t.breed.name === 'kelp') return 2
return 1.5 // seaStar
},
turtlesColor: t => {
if (t.breed.name === 'urchin') return 'purple'
if (t.breed.name === 'kelp') return 'green'
return 'orange' // seaStar
},
turtlesShape: t => {
if (t.breed.name === 'urchin') return 'circle'
if (t.breed.name === 'kelp') return 'dart'
return 'pentagon' // seaStar
},
},
})
const anim = new Animator(
() => {
model.step()
view.draw()
updateSteps()
},
5 * 365, // how many steps, here 5 years,
30 // at fps steps/second
)
Object.assign(window, { model, view, anim }) // debugging
// The above is views2/kelpforest.html w/ updateSteps() added
// the following allows running it in a nicely arranged web page
function updateSteps() {
// const year = 1 + Math.floor(anim.ticks / 365) // start from 1
const year = model.year(anim.ticks) // starts from 1
document.getElementById('stepCount').innerHTML = `Year: ${year}`
}
document
.getElementById('startButton')
.addEventListener('click', () => {
model.numKelp = parseInt(
document.getElementById('kelp').value
)
model.numSeastar = parseInt(
document.getElementById('seaStar').value
)
model.numUrchin = parseInt(
document.getElementById('urchin').value
)
anim.restart(model, view) // calls model.setup()
if (!anim.isRunning()) anim.start()
})
document
.getElementById('pauseButton')
.addEventListener('click', () => {
anim.isRunning() ? anim.stop() : anim.start()
})
</script>
<h1 style="margin-top: 20px; text-align: center">
In a Kelp stand off the Mendocino Coast....
</h1>
<div
id="bodyContainer"
style="display: flex; justify-content: center; align-items: center"
>
<div
id="inputContainer"
style="margin: 20px; display: flex; flex-direction: column"
>
<p id="stepCount">Year:</p>
<label for="kelp">Kelp:</label>
<input
type="number"
id="kelp"
value="500"
min="0"
style="margin: 2px; padding: 2px"
/>
<label for="urchin">Urchin:</label>
<input
type="number"
id="urchin"
value="50"
min="0"
style="margin: 2px; padding: 2px"
/>
<label for="seaStar">Seastars:</label>
<input
type="number"
id="seaStar"
value="5"
min="0"
style="margin: 2px; padding: 2px"
/>
<button id="startButton" style="margin: 5px">
Start Simulation
</button>
<button id="pauseButton" style="margin: 5px">
Pause/Play Simulation
</button>
</div>
<div id="modelDiv"></div>
</div>
</body>
</html>