-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
46 lines (37 loc) · 1.44 KB
/
Copy pathutils.js
File metadata and controls
46 lines (37 loc) · 1.44 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
import { empty } from './constants.js'
const containerWidth = 15 // width (in pixels) of each container
const numContainers = Math.floor(window.innerWidth / containerWidth) // number of vertical containers of Matrix code
// Fisher-Yates shuffle
export const shuffle = (array) => {
let currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Create a vertical container that holds Matrix characters
export const createContainer = (containerIndex) => {
const container = document.createElement('div')
container.className = 'container'
container.setAttribute('id', `container-${containerIndex}`)
document.body.appendChild(container)
}
// Create containers across the width of the screen
// Returns an object where each key is a container ID and each value is a container status
export const createContainers = () => {
const containers = []
const containerStates = {}
for (let i = 0; i < numContainers; i++) {
createContainer(i)
containers.push(i)
}
containers.forEach(container => containerStates[container] = empty)
return containerStates
}