/
HomeBgTile.vue
253 lines (232 loc) · 6.26 KB
/
HomeBgTile.vue
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
<template>
<div id="tiles-container">
<div
v-for="tileState in tileStates"
:key="tileState.key"
:class="'tile ' + tileState.className"
/>
</div>
</template>
<script>
export default {
data() {
return {
key: 0,
tileStates: undefined,
visibleTilesNum: 0,
interval: 0,
resizeEvent: null,
bannerWidth: null,
bannerHeight: null
}
},
mounted() {
let bannerRect = document
.querySelector('#eodiro-banner')
.getBoundingClientRect()
this.bannerHeight = bannerRect.height
this.bannerWidth = bannerRect.width
this.createTiles()
window.addEventListener(
'resize',
(this.resizeEvent = () => {
let bannerHeightChanged = false
let bannerWidthChanged = false
bannerRect = document
.querySelector('#eodiro-banner')
.getBoundingClientRect()
if (this.bannerHeight !== bannerRect.height) {
this.bannerHeight = bannerRect.height
bannerHeightChanged = true
}
if (this.bannerWidth !== bannerRect.width) {
this.bannerWidth = bannerRect.width
bannerWidthChanged = true
}
if (bannerHeightChanged || bannerWidthChanged) {
window.clearInterval(this.interval)
this.updateTilesNumber()
this.activateAnimation()
}
})
)
},
beforeDestroy() {
clearInterval(this.interval)
window.removeEventListener('resize', this.resizeEvent)
},
methods: {
calculateVisibleTilesNumber() {
// notice - there must be no scrollbar
const heightParentComponent = 37 / 100
const remRatioMoreThan1400 = 1.1
const remRatioMoreThan700 = 1
const remRatioUnder700 = 0.85
const widthDevice = window.innerWidth
const heightDevice = window.innerHeight
let tileNumHeight, tileNumWidth
// calc one rem
let rem = parseInt(
window.getComputedStyle(document.body).getPropertyValue('font-size'),
10
)
let widthTile
// rem dependency
if (widthDevice >= 1400) {
rem *= remRatioMoreThan1400
} else if (widthDevice >= 700) {
rem *= remRatioMoreThan700
} else {
rem *= remRatioUnder700
}
const widthContainerTiles = widthDevice * 1.05
const heightContainerTiles = heightDevice * heightParentComponent
if (widthDevice >= 700) {
// minimize widthTile
widthTile = 3.5 * rem
// calc tileNumWidth in minimized widthTile and round it
tileNumWidth = Math.floor(
(widthContainerTiles + 1.5 * rem) / (widthTile + 1.5 * rem)
)
// calc real widthTile
widthTile = (widthContainerTiles + 1.5 * rem) / tileNumWidth - 1.5 * rem
// calc tileNumHeight
tileNumHeight =
Math.floor(
(heightContainerTiles + 1.5 * rem) / (widthTile + 1.5 * rem)
) + 1
} else {
// minimize widthTile
widthTile = 3 * rem
// calc tileNumWidth in minimized widthTile and round it
tileNumWidth = Math.floor(
(widthContainerTiles + 1 * rem) / (widthTile + 1 * rem)
)
// calc real widthTile
widthTile = (widthContainerTiles + 1 * rem) / tileNumWidth - 1 * rem
// calc tileNumHeight
tileNumHeight =
Math.floor((heightContainerTiles + 1 * rem) / (widthTile + 1 * rem)) +
1
}
return tileNumHeight * tileNumWidth
},
createTiles() {
const ts = []
const tileNum = this.calculateVisibleTilesNumber()
for (let i = 0; i < tileNum; i++) {
ts.push({
key: i,
className: 'color-1'
})
}
this.tileStates = ts
// Use nextTick to run animation after
// the elements are rendered with the data input
this.$nextTick(() => {
this.activateAnimation()
})
},
updateTilesNumber() {
const currentTileNum = this.calculateVisibleTilesNumber()
let previousTileNum
if (this.tileStates != null) {
previousTileNum = this.tileStates.length
} else {
previousTileNum = 0
}
const diff = currentTileNum - previousTileNum
// update the num of tile only difference previous and current
if (diff > 0) {
for (let i = 0; i < diff; i++) {
this.tileStates.push({
key: previousTileNum + i,
className: 'color-1'
})
}
} else if (diff < 0) {
for (let i = 0; i > diff; i--) {
this.tileStates.pop()
}
}
},
activateAnimation() {
let i, c
const tileNum = this.getTileNum()
this.interval = setInterval(() => {
for (let a = 0; a < Math.ceil(tileNum / 20); a++) {
i = Math.floor(Math.random() * tileNum)
c = Math.floor(Math.random() * 9)
this.tileStates[i].className = 'color-' + c
}
}, 100)
},
getTileNum() {
if (this.tileStates != null) {
return this.tileStates.length
}
return 0
}
}
}
</script>
<style lang="scss">
@import '~/assets/styles/scss/main.scss';
#tiles-container {
position: absolute;
width: 105%;
height: auto;
overflow: hidden;
display: grid;
grid-gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(3.5rem, 1fr));
grid-template-rows: repeat(auto-fit, minmax(3.5rem, 1fr));
@include smaller-than(700px) {
grid-gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(3rem, 1fr));
grid-template-rows: repeat(auto-fit, minmax(3rem, 1fr));
}
.tile {
border-radius: 1rem;
background-color: #fff0f3;
opacity: 0;
transition: opacity 500ms linear,
background-color $color-scheme-transition-time ease;
@include smaller-than(700px) {
border-radius: 0.85rem;
}
@include dark-mode {
background-color: #63000d;
}
&::before {
content: '';
display: block;
padding-top: 100%;
}
&.color-1 {
opacity: 0;
}
&.color-2 {
opacity: 0;
}
&.color-3 {
opacity: 0.01;
}
&.color-4 {
opacity: 0.02;
}
&.color-5 {
opacity: 0.04;
}
&.color-6 {
opacity: 0.08;
}
&.color-7 {
opacity: 0.16;
}
&.color-8 {
opacity: 0.32;
}
}
}
</style>