-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeroPattern.vue
99 lines (94 loc) · 2.44 KB
/
HeroPattern.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
<template>
<div class="hero-pattern" :style="style">
<slot />
</div>
</template>
<script>
import { SVG_DATAURI } from '@dynamic/svg2datauri'
export default {
name: 'HeroPattern',
props: {
// Pattern Name
pattern: { type: String, default: 'line-in-motion' },
// Given color is hexadecimal color value
fillColor: {
type: String,
default: () => '#9c92ac',
validator (val) {
const regex = /#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/g
return regex.test(val)
}
},
// Given color is hexadecimal color value
backgroundColor: {
type: String,
default: () => '#dfdbe5',
validator (val) {
const regex = /#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/g
return regex.test(val)
}
},
fillOpacity: {
type: String,
default: () => '0.4'
},
repeat: { type: String, default: 'repeat' }
},
data: () => ({
svgDataUri: SVG_DATAURI
}),
computed: {
style () {
let svg = {}
const idx = this.svgDataUri.findIndex(element => element.name.split('.').shift() === this.pattern)
if (idx !== -1) {
svg = this.svgDataUri[idx]
const drop = (arr, n = 1) => arr.slice(n)
const color = drop(Array.from(this.fillColor)).join('')
const fillReg = /fill='(\w+|\S+)'/g
const fillOpacityReg = /fill-opacity='(\w+|\S+)'/
const datauri = svg.datauri
.replace(fillReg, `fill='%23${color}'`)
.replace(fillOpacityReg, `fill-opacity='${this.fillOpacity}'`)
return {
'background-color': this.backgroundColor,
'background-image': `url("${datauri}")`,
'background-repeat': this.repeat
}
}
return {}
}
},
mounted () {
// this.getPattern()
// console.log(SVG_DATAURI)
},
methods: {
// async getPattern () {
// const svg = await fs.readFile(path.join(__dirname, './assets/icons/line-in-motion'))
// console.log(svg)
// const uri = svgToMiniDataURI(svg)
// console.log(uri)
// },
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>
.hero-pattern {
transition: all 0.5s ease-in-out;
}
</style>