-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoPattern.vue
More file actions
58 lines (54 loc) · 1.31 KB
/
Copy pathGeoPattern.vue
File metadata and controls
58 lines (54 loc) · 1.31 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
47
48
49
50
51
52
53
54
55
56
57
58
<template>
<div class="geo-pattern" :style="style">
<slot />
</div>
</template>
<script>
import gp from 'geopattern'
const PLUGIN_NAME = 'vuepress-plugin-geopattern'
export default {
name: 'GeoPattern',
props: {
// Will be used as the seed for generation
seed: { type: String, default: () => PLUGIN_NAME + new Date().toString() },
// Given array elements is hexadecimal color value [0-F]
colorHexs: { type: Array, default: () => ['3', '9', 'C'] },
// Controls the relative background color of the generated image.
baseColor: { type: String, default: '#933c3c' }
},
computed: {
style () {
return {
'background-image': this.getPattern()
}
}
},
methods: {
getPattern () {
return gp.generate(this.seed, {
color: this.getRandomColor(this.colorHexs),
baseColor: this.baseColor
}).toDataUrl()
},
getRandomColor (hexs) {
const shuffle = ([...arr]) => {
let m = arr.length
while (m) {
let temp = null
const i = Math.floor(Math.random() * m--)
temp = arr[m]
arr[m] = arr[i]
arr[i] = temp
}
return arr
}
return shuffle(hexs).join('')
}
}
}
</script>
<style scoped>
.geo-pattern {
transition: all 0.5s ease-in-out;
}
</style>