-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
206 lines (166 loc) · 4.79 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
const canvas = createCanvas(window.innerWidth, window.innerHeight)
const characterPool = createCharacterPool()
/** @type {CanvasRenderingContext2D} */
const context = canvas.getContext('2d')
// https://dev.to/pahund/how-to-fix-blurry-text-on-html-canvases-on-mobile-phones-3iep
function createCanvas(width, height, set2dTransform = true) {
const ratio = Math.ceil(window.devicePixelRatio);
const canvas = document.getElementById('canvas')
canvas.width = width * ratio;
canvas.height = height * ratio;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
if (set2dTransform) {
canvas.getContext('2d').setTransform(ratio, 0, 0, ratio, 0, 0);
}
return canvas;
}
const FONT_SIZE = 32
let frameCount = 0
context.shadowOffsetY = -3
context.shadowOffsetX = 0
context.shadowBlur = 20
context.shadowColor = `rgba(50, 202, 104, .7), rgba(150, 0, 1, .8)`
context.font = `bold ${FONT_SIZE}px Times New Roman`
context.font = `bold ${FONT_SIZE}px Courier`
context.fillStyle = 'rgba(50, 202, 104, 1)'
const baseHighlight = '#9af0b8'
const matrix = createMatrix()
function updateMatrix() {
clearContext()
matrix.update()
matrix.render()
// renderGrid()
frameCount++
requestAnimationFrame(updateMatrix)
}
function renderGrid() {
// linhas horizontais
context.fillStyle = 'rgba(255, 0, 0, 0.5)'
for (let i = 0; i < matrix.numberOfColumns; i++) {
context.fillRect(0, FONT_SIZE * i, window.innerWidth, 2)
}
// linhas verticais
context.fillStyle = 'rgba(0, 0, 255, 0.5)'
for (let i = 0; i < matrix.numberOfColumns; i++) {
context.fillRect(FONT_SIZE * i, 0, 2, window.innerWidth)
}
}
updateMatrix()
function createMatrix() {
const matrix = {
columns: [],
numberOfColumns: window.innerWidth / FONT_SIZE,
update,
render
}
let columnY = 0, column
for (let i = 0; i < matrix.numberOfColumns; i++) {
columnY = 400 * getRandomInt(5, 400)
column = createColumn(i * FONT_SIZE, -columnY)
column.y = -column.symbolHolders.length * FONT_SIZE
matrix.columns.push(column)
}
function render() {
for (let i = 0; i < matrix.columns.length; i++) {
matrix.columns[i].render();
}
}
function update() {
for (let i = 0; i < matrix.columns.length; i++) {
matrix.columns[i].update();
}
}
return matrix
}
/**
* @param {number} x
* @param {number} initialY
*/
function createColumn(x, initialY) {
const height = getRandomInt(25, 10)
const pixelsPerStep = Math.ceil(canvas.height / FONT_SIZE)
// const mult = getRandomInt(8, 8)
const mult = getRandomInt(1, 1)
// const mult = getRandomInt(1, 0)
const column = {
height,
x,
y: initialY - height * FONT_SIZE,
acc: -pixelsPerStep,
// verticalDisplacement: FONT_SIZE/100,
verticalDisplacement: FONT_SIZE / getRandomInt(10, 20),
symbolHolders: [],
update,
render
}
for (let i = 0; i < column.height; ++i) {
column.symbolHolders.push(createSymbolHolder())
}
column.symbolHolders.at(-1).style = baseHighlight
function update() {
column.y += column.verticalDisplacement
// column.y = pixelsPerStep*Math.ceil(column.acc)
// column.y = FONT_SIZE * Math.ceil(column.acc)
// column.acc += 4 * 1 / 60
// console.log(column.y);
if (column.y > window.innerHeight) {
column.y = -column.height * FONT_SIZE
column.acc = -column.symbolHolders.length
}
for (let i = 0; i < column.symbolHolders.length; i++) {
column.symbolHolders[i].update();
}
}
function render() {
for (let i = 0; i < column.symbolHolders.length; i++) {
column.symbolHolders[i].render(column.x, column.y + i * FONT_SIZE)
// context.fillStyle = 'rgba(0, 255, 0, 0.5)'
// context.shadowColor = ''
// context.fillRect(column.x, column.y, FONT_SIZE, FONT_SIZE)
}
}
return column
}
function createSymbolHolder() {
const symbolHolder = {
style: context.fillStyle,
symbol: createSymbol(),
switchInterval: getRandomInt(20, 100),
update,
render,
}
function update() {
if (frameCount % symbolHolder.switchInterval == 0) {
symbolHolder.symbol = createSymbol()
}
}
function render(x, y) {
context.fillStyle = symbolHolder.style
context.fillText(symbolHolder.symbol, x, y)
}
return symbolHolder
}
function createSymbol() {
return characterPool[getRandomInt(0, 95)]
}
function createCharacterPool() {
let pool = ''
let i = 0
const baseKatakanaCharacterCode = 0x030A0
for (; i < 95; i++) {
pool += String.fromCharCode(baseKatakanaCharacterCode + i)
}
return pool
}
function clearContext() {
context.clearRect(0, 0, canvas.width, canvas.height)
}
/**
* @param {number} base
* @param {number} amplitude
* @returns {number} random number
*/
function getRandomInt(base, amplitude) {
return base + Math.floor(Math.random() * amplitude)
}