-
Notifications
You must be signed in to change notification settings - Fork 0
/
org.js
171 lines (130 loc) · 3.36 KB
/
org.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
const bodyEl = document.body
function getBoundaries() {
const els = Array.from(document.querySelectorAll(`
body,
.meeting--entities
`))
}
function randomPoint(rect, padding=0) {
const x = Math.floor(Math.random() * (rect.width - rect.x) - padding)
, y = Math.floor(Math.random() * (rect.height - rect.y) - padding)
return { x, y }
}
function setup() {
const canvasEl = document.createElement('canvas')
, rect = bodyEl.getBoundingClientRect()
canvasEl.setAttribute('height', rect.height)
canvasEl.setAttribute('width', rect.width)
Object.assign(canvasEl.style, {
position: 'absolute',
left: 0,
top: 0,
})
bodyEl.insertBefore(canvasEl, bodyEl.firstChild)
return [ rect, canvasEl ]
}
function radians(degrees) {
return degrees * Math.PI / 180
}
function randomBetween(start, end) {
const diff = end - start
return start + Math.floor(Math.random() * diff)
}
class Letter {
constructor(letter, rect, d) {
this.letter = letter
this.rect = rect;
this.d = d;
this.tailOdds = 0
this.i = 0
this.init()
}
init() {
this.pos = randomPoint(this.rect, 30)
this.tails = []
this.angle = randomBetween(0, 360)
this.refreshDelta()
}
refreshDelta() {
this.dx = Math.floor(this.d * Math.sin(radians(this.angle)))
this.dy = Math.floor(this.d * Math.cos(radians(this.angle)))
}
tick() {
const pos = {
x: this.pos.x + this.dx,
y: this.pos.y + this.dy,
}
// Right limit
if (pos.x >= this.rect.width) {
this.angle = randomBetween(180, 360)
this.refreshDelta()
}
// Left limit
if (pos.x <= 0) {
this.angle = randomBetween(0, 180)
this.refreshDelta()
}
// Top limit
if (pos.y >= this.rect.height) {
this.angle = randomBetween(90, 270)
this.refreshDelta()
}
// Bottom limit
if (pos.y <= 0) {
this.angle = randomBetween(-90, 90)
this.refreshDelta()
}
this.i += 1
if (this.i === 3) {
this.i = 0
if (this.tailOdds < 100) {
const tailsLength = this.tails.unshift(this.pos)
this.tailOdds += tailsLength * Math.random()
} else {
this.tails.unshift(this.pos)
this.tails.pop()
this.tails.pop()
if (this.tails.length === 0) {
this.tailOdds = 0;
}
// this.tailOdds -= Math.random()
}
}
this.pos = pos
}
}
function main(letter) {
const [ rect, canvasEl ] = setup()
, ctx = canvasEl.getContext('2d')
ctx.font = '36px sans-serif'
ctx.fillStyle = '#333'
let pos = randomPoint(rect, 30)
, angle = randomBetween(0, 360)
const d = 3
const numLetters = 12
, letters = []
for (let i = 0; i < numLetters; i++) {
const letter = ['O', 'R', 'G'][i % 3]
letters.push(new Letter(letter, rect, randomBetween(2, 5)))
}
// let i = 0
function step() {
ctx.clearRect(0, 0, rect.width, rect.height)
letters.forEach(letter => {
letter.tick()
letter.tails.forEach((pos, i) => {
const shade = 255 - 255 / (i + 8)
ctx.fillStyle = `rgb(${shade}, ${shade}, ${shade})`
ctx.fillText(letter.letter, pos.x, pos.y)
})
})
letters.forEach(letter => {
ctx.fillStyle = '#333'
ctx.fillText(letter.letter, letter.pos.x, letter.pos.y)
})
requestAnimationFrame(step)
// i += 1
}
step()
}
main()