forked from kovart/dashed-border-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
172 lines (168 loc) Β· 5.83 KB
/
main.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
const Chrome = VueColor.Chrome
const LineCapStates = {
Butt: 'butt',
Round: 'round',
Square: 'square'
}
let app = new Vue({
el: '#app',
components: {
'chrome-picker': Chrome,
},
data: {
colorPicker: {
isOpened: false,
borderColor: '#194d33',
target: ''
},
block: {
width: 200,
height: 200
},
currentStyle: {
backgroundColor: 'transparent',
borderColor: '#333',
width: 4,
dashArray: '6, 14',
dashOffset: 0,
borderRadius: 0,
linecap: LineCapStates.Square,
},
styles: {
style0: {
backgroundColor: 'transparent',
borderColor: 'black',
width: 4,
dashArray: '6, 14',
dashOffset: 0,
borderRadius: 0,
linecap: LineCapStates.Square,
},
style1: {
backgroundColor: 'transparent',
borderColor: '#EC3463',
width: 7,
dashArray: '50%, 13%',
dashOffset: 86,
borderRadius: '100',
linecap: LineCapStates.Butt,
},
style2: {
backgroundColor: 'transparent',
borderColor: 'black',
width: 4,
dashArray: '2, 8',
dashOffset: 0,
borderRadius: 0,
linecap: LineCapStates.Butt,
},
style3: {
backgroundColor: 'transparent',
borderColor: 'black',
width: 10,
dashArray: '15, 15, 1',
dashOffset: 0,
borderRadius: 0,
linecap: LineCapStates.Square,
},
style4: {
backgroundColor: 'lightblue',
borderColor: 'black',
width: 25,
dashArray: '2, 6',
dashOffset: 28,
borderRadius: 0,
linecap: LineCapStates.Butt,
},
},
options: {
linecap: LineCapStates
},
// used to change "isCopied" state after a few seconds
copyTimeout: null
},
computed: {
output() {
let str = ''
const css = this.generateBorderStyle(this.currentStyle)
for (let key in css) {
if (!css.hasOwnProperty(key)) continue
str += `${key}: ${css[key]};\n`
}
return str.substr(0, str.length - 1) // remove last line break
},
blockStyle() {
return Object.assign(this.generateBlockStyle(), this.generateBorderStyle(this.currentStyle))
},
isCopied() {
return !!this.copyTimeout
}
},
methods: {
setStyle(style) {
this.currentStyle = { ...style }
},
generateBorderStyle(style) {
const css = {}
if (/^#[a-fA-f0-9]{6}FF$/.test(style.backgroundColor)) {
css['background-color'] = style.backgroundColor.substr(0, 7)
} else if (style.backgroundColor !== 'transparent' && !/^#[a-fA-f0-9]{6}00$/.test(style.backgroundColor)) {
css['background-color'] = style.backgroundColor
}
let svg = `<svg width='100%' height='100%' xmlns='http://www.w3.org/2000/svg'>` +
`<rect width='100%' height='100%' fill='none' ` +
(style.borderRadius > 0 ? `rx='${style.borderRadius}' ry='${style.borderRadius}' ` : '') +
`stroke='${style.borderColor}' ` +
`stroke-width='${style.width}' ` +
`stroke-dasharray='${style.dashArray}' ` +
`stroke-dashoffset='${style.dashOffset}' ` +
`stroke-linecap='${style.linecap}'/></svg>`
css['background-image'] = `url("${svgToTinyDataUri(svg)}")`
if (style.borderRadius > 0) {
css['border-radius'] = style.borderRadius + 'px'
}
return css
},
generateBlockStyle() {
return {width: parseInt(this.block.width) + 'px', height: parseInt(this.block.height) + 'px'}
},
generateRandomDashArray() {
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min)
}
const minSide = Math.min(parseInt(this.block.width), parseInt(this.block.height))
const sequences = [...Array(getRandom(1, 5))]
// I don't why but it doesnt work with .map()...
sequences.forEach((_, i) => sequences[i] = getRandom(minSide / 34, minSide / 8))
this.currentStyle.dashArray = sequences.join(', ')
},
copyCss() {
const el = this.$refs.codeInput
el.select()
document.execCommand('copy')
clearTimeout(this.copyTimeout);
this.copyTimeout = setTimeout(() => {
this.copyTimeout = null
}, 3500)
},
/* ---------------------
* Color Picker Methods
* --------------------- */
openColorPicker(target) {
this.colorPicker.borderColor = target === 'border' ? this.currentStyle.borderColor : this.currentStyle.backgroundColor
this.colorPicker.target = target
this.colorPicker.isOpened = true
},
closeColorPicker() {
this.colorPicker.isOpened = false
this.colorPicker.target = ''
},
updateColor(color) {
if (this.colorPicker.target === 'border') {
this.currentStyle.borderColor = color.hex8
} else {
this.currentStyle.backgroundColor = color.hex8
}
}
}
})