-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplant.html
179 lines (151 loc) · 6.52 KB
/
plant.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<style>
p { white-space: pre; }
body { font-family: "Courier", Courier, monospace;
font-size: 18px;
text-align: center;
}
.title {
text-align: center;
flex:1 0 auto;
}
a:link {
color:#00CC66;
background-color: transparent;
text-decoration: none;
}
a:visited {
color: #00CC66;
background-color: transparent;
text-decoration: none;
}
a.back {
color:#FFFFFF ;
position: absolute;
left:10px;
}
.audio-container {
display: flex;
justify-content: center;
align-items: center;
border-width: 3px;
border-color: red;
}
</style>
<body bgcolor="#303030" text="#ffffff">
<a class="back" href="https://mishathings.com" display="flex" color="#FFFFFF" style="float:left">← Back to home page</a>
<h1 position="relative" display="flex align-items:center"; justify-content="center">Plant walk</h1>
<!-- <input type="button" onclick="https://mishathings.com" value="home" style="float: left;"> -->
<div style="text-align:center">
New song + randomized videolip of a digital plant.<br><br>
1. Every step there is a 60% chance a random branch dies. But one branch always remains.<br>
2. Every step there is a 50% chance that a random branch sprouts a new branch.<br>
3. Branches move away from their nearest neighbour and don't like to be close to the edges.<br><br>
Scroll down to see plant history. Scroll up to catch up with the flow. To see how it works, view page source or click <a href="./planthtml.html">here</a>.
</div>
<br><br>
<div class='audio-container'>
<audio controls="controls" preload="none">
<source src="./data/plant_walk_full.mp3" type="audio/mpeg">
</audio>
</div>
<!-- <p style="text-align:center" id="plantwalk"></p> -->
<p style="color:#00CC66;text-align:center" id="plantwalk"></p>
<script>
/* --------------------------------------------------------------- */
/* functions to use later */
/* --------------------------------------------------------------- */
/* create random number */
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
/* replace character in string */
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substring(0,index) + chr + str.substring(index+1);
}
/* take out double values */
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
/* find minimum of array */
Array.min = function( array ){
return Math.min.apply( Math, array );
};
/* find average */
const average = (array) => array.reduce((a, b) => a + b) / array.length;
/* --------------------------------------------------------------- */
/* plant walk initialisation */
/* --------------------------------------------------------------- */
let start_position = Math.floor(plantwalk.offsetLeft + plantwalk.offsetWidth / 13)
let position = [Math.floor(start_position/3)]
/* --------------------------------------------------------------- */
/* plant walk */
/* --------------------------------------------------------------- */
function moveposition () {
/* create line */
let line = " ".repeat(start_position)
/* one random branche dies */
if (position.length > 1){
if (Math.random() < 0.6){
position.splice(randomIntFromInterval(0,position.length-1),1);
}
}
/* Create array of move directions for each branch */
move = Array.from({length: position.length}, () => randomIntFromInterval(-1,1))
/* Adjust move array to make branches move away from each other */
if (position.length > 1){
for (var i = 0; i < position.length; i++) {
diff = []
for (var j = 0; j < position.length; j++) {
if (!(i == j)){diff.push(Math.abs(position[i]-position[j]))}
else
{diff.push(NaN)}
}
let minimumValue = Math.min.apply(null, diff.filter(function(n) { return !isNaN(n); }))
let ind = diff.indexOf(minimumValue);
if ((position[i] - position[ind]) < 0)
{move[i] = -1}
else
{move[i] = +1}
}
}
/* Make the plant want to move away from the limits */
else
{if ((line.length - position) < (line.length/3))
{move = [-1]
/* console.log("right limit") */
}
else if (position < line.length/3)
{move = [1]
/* console.log("left limit") */
}}
/* adjust positions of branch, preventing them from overflowing the limits */
for (var i = 0; i < position.length; i++) {
position[i] = position[i] + move[i]
if (position[i] < 0){position[i] = 0}
if (position[i] > line.length-2){position[i] = line.length-2}
}
/* Create new branches */
position = position.filter(onlyUnique);
if (Math.random() < 0.5){
let index = randomIntFromInterval(0,position.length-1)
if (Math.random < 0.5)
{position.push(position[index]-1)}
else
{position.push(position[index]+1)}
}
/* Adjust the line and print it at the top of the website */
for (var i = 0; i < position.length; i++) {
line = setCharAt(line, position[i], "█")}
/* line = setCharAt(line, position[i], "+")} */
plantwalk.insertAdjacentHTML('afterbegin', line + "<br>")
}
mvinterval = setInterval(moveposition,50)
</script>
</body>
</html>