-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
266 lines (242 loc) · 5.66 KB
/
matrix.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//@ts-check
const symbols = [
"日",
"ハ",
"ミ",
"ヒ",
"ー",
"ウ",
"シ",
"ナ",
"モ",
"ニ",
"サ",
"ワ",
"ツ",
"オ",
"リ",
"ア",
"ホ",
"テ",
"マ",
"ケ",
"メ",
"エ",
"カ",
"キ",
"ム",
"ユ",
"ラ",
"セ",
"ネ",
"ス",
"タ",
"ヌ",
"ヘ",
"0",
"1",
"2",
"3",
"4",
"5",
"7",
"8",
"9",
"T",
"H",
"E",
"M",
"A",
"T",
"R",
"I",
"X",
":",
"・",
".",
"=",
"*",
"+",
"-",
"<",
">",
"¦",
"|",
"ク",
"ç",
"リ",
"Ɛ",
]
/**
* @typedef {Object} IColor
* @property {number} red
* @property {number} green
* @property {number} blue
* @property {number=} alpha
*/
/**
* @typedef {Object} IConfiguration
* @property {number} maxSymbolCount
* @property {number} symbolAlphaFadeRate
* @property {number} symbolFontSize
* @property {string} symbolFontFamily
* @property {IColor} symbolColorForeground
* @property {IColor} symbolColorFade
* @property {IColor} canvasBackgroundColor
* @property {number} frameRatePerSecond
*/
/**
* @typedef {Object} ISymbolCoordinate
* @property {string} symbol
* @property {number} x
* @property {number} y
*/
const getCanvasHeight = () => {
return window.innerHeight
}
const getCanvasWidth = () => {
return window.innerWidth
}
const getRandomSymbol = () =>
symbols[Math.floor(Math.random() * (symbols.length - 1))]
/**
* @param {ISymbolCoordinate} coordinate
* @param {number} symbolFontSize
* @return {ISymbolCoordinate}
*/
const calcFallSymbolCoordinate = ({ x, y }, symbolFontSize) => {
const fallDistance =
(Math.random() * symbolFontSize * 3) / 4 + (symbolFontSize * 3) / 4
const newY = y + fallDistance
return newY > getCanvasHeight()
? initializeSymbolCoordinate(symbolFontSize, 8)
: {
symbol: getRandomSymbol(),
x,
y: newY,
}
}
/**
* @param {ISymbolCoordinate[]} symbolsPool
* @param {number} symbolFontSize
*/
const calcNextFrameSymbolsFall = (symbolsPool, symbolFontSize) =>
symbolsPool.map((coordinate) =>
calcFallSymbolCoordinate(coordinate, symbolFontSize)
)
/** @param {IColor} color*/
const formatToRbgString = ({ red, green, blue, alpha }) =>
`rgba(${red}, ${green}, ${blue}, ${alpha || 1})`
/** @param {number} symbolFontSize*/
/** @param {string} symbolFontFamily*/
const formatToFontString = (symbolFontSize, symbolFontFamily) =>
`${symbolFontSize}px ${symbolFontFamily}`
/**
* @param {number} symbolFontSize
* @return {ISymbolCoordinate}
*/
const initializeSymbolCoordinate = (symbolFontSize, offset = 1) => {
return {
symbol: getRandomSymbol(),
x:
Math.floor((Math.random() * getCanvasWidth()) / symbolFontSize) *
symbolFontSize,
y: (Math.random() * getCanvasHeight()) / offset - 50,
}
}
/** @param {number} ms */
const wait = (ms) => new Promise((res) => setTimeout(res, ms))
/**
* @param {HTMLCanvasElement} canvas
*/
const watchScreenForCanvasDimensions = (canvas) => {
const resizeCanvas = () => {
canvas.width = getCanvasWidth()
canvas.height = getCanvasHeight()
}
resizeCanvas() // On init
window.addEventListener("resize", resizeCanvas) // On screen resize
}
/**
* @param {IConfiguration} config
* @returns {{
* renderToCanvas: (coordinates: ISymbolCoordinate[], symbolColor: IColor) => void
* }}
*/
const configureCanvasFnFactory = (config) => {
// Canvas config
const {
symbolFontSize,
symbolFontFamily,
canvasBackgroundColor,
symbolAlphaFadeRate,
} = config
const canvas = /** @type {HTMLCanvasElement} */ (
document.getElementById("canvas")
)
const canvasContext = canvas.getContext("2d")
watchScreenForCanvasDimensions(canvas)
// Init Background
canvasContext.fillStyle = formatToRbgString({
...canvasBackgroundColor,
})
canvasContext.fillRect(0, 0, getCanvasWidth(), getCanvasHeight())
// Returned function to render canvas
return {
renderToCanvas: (coordinates, symbolColor) => {
// Draw Background
canvasContext.fillStyle = formatToRbgString({
...canvasBackgroundColor,
alpha: symbolAlphaFadeRate,
})
canvasContext.fillRect(0, 0, getCanvasWidth(), getCanvasHeight())
// Draw Symbols
canvasContext.font = formatToFontString(symbolFontSize, symbolFontFamily)
canvasContext.fillStyle = formatToRbgString(symbolColor)
coordinates.forEach(({ symbol, x, y }) => {
canvasContext.fillText(symbol, x, y)
})
},
}
}
/** @param {IConfiguration} config */
const runMatrix = (config) => {
const {
maxSymbolCount,
symbolFontSize,
frameRatePerSecond,
symbolColorFade,
symbolColorForeground,
} = config
const { renderToCanvas } = configureCanvasFnFactory(config)
let symbolsPool = Array.from(Array(maxSymbolCount), () =>
initializeSymbolCoordinate(symbolFontSize)
)
const stepAnimation = async (frameNumber = 0) => {
if (frameNumber % 2 === 0) {
symbolsPool = calcNextFrameSymbolsFall(symbolsPool, symbolFontSize)
renderToCanvas(symbolsPool, symbolColorFade)
} else {
renderToCanvas(symbolsPool, symbolColorForeground)
}
// Render next frame
window.requestAnimationFrame(async () => {
await wait(1000 / frameRatePerSecond)
stepAnimation(frameNumber + 1)
})
}
stepAnimation() // Begin animation recursion
}
const main = () => {
const config = /** @type {IConfiguration} */ ({
canvasBackgroundColor: { red: 0, green: 0, blue: 0 },
frameRatePerSecond: 30,
maxSymbolCount: 60,
symbolAlphaFadeRate: 0.05,
symbolColorFade: { red: 255, green: 255, blue: 255 },
symbolColorForeground: { red: 0, green: 255, blue: 0 },
symbolFontSize: 16,
})
runMatrix(config)
}
main()