forked from swapnilsparsh/30DaysOfJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (145 loc) · 4.12 KB
/
Copy pathscript.js
File metadata and controls
174 lines (145 loc) · 4.12 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
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
const angleInput = document.querySelector('[data-input="range"]')
const thicknessInput = document.querySelector('[data-input="thickness"]')
const widthInput = document.querySelector('[data-input="width"]')
const colorInput = document.querySelector('[data-input="color1"]')
const contrastInput = document.querySelector('[data-input="contrast"]')
const outputContainer = document.querySelector('[data-output]')
const bg = document.querySelector('[data-bg]')
const inputs = [...document.querySelectorAll('[data-input]')]
const angleChange = (target) => {
const newAngle = `${target.value}deg`
setItem('--angle', newAngle, 'angle', target.value)
}
const thicknessChange = (target) => {
const newThickness = `${target.value}px`
setItem('--t', newThickness, 'thickness', target.value)
}
const setItem = (cssVar, newValue, property, unitlessValue) => {
bg.style.setProperty(cssVar, newValue)
getCSSOutput()
localStorage.setItem(property, unitlessValue)
}
const widthChange = (target) => {
const newWidth = `${target.value}px`
setItem('--w', newWidth, 'width', target.value)
}
const colorChange = (target) => {
const newColor = `${target.value}deg`
setItem('--h1', newColor, 'color', target.value)
}
const setContrast = (contrast) => {
const newL = `${contrast}%`
const newD = `${100 - contrast}%`
bg.style.setProperty('--l', newL)
bg.style.setProperty('--d', newD)
}
const contrastChange = (target) => {
setContrast(target.value)
getCSSOutput()
localStorage.setItem('contrast', target.value)
}
const getCSSOutput = () => {
const backgroundImage = getComputedStyle(bg, '::after').backgroundImage
const backgroundSize = getComputedStyle(bg, '::after').backgroundSize
const width = getComputedStyle(bg, '::after').width
const mask = getComputedStyle(bg, '::after').webkitMaskSize
const cssOutput = `
<pre>
.bg {
position:relative;
overflow:hidden;
}
.bg::before,
.bg::after {
content: '';
position: absolute;
inset:0;
width:${width};
background-image: ${backgroundImage};
background-size: ${backgroundSize};
}
.bg::after {
transform:scaleX(-1);
-webkit-mask: linear-gradient(90deg,#000 50%, #0000 0) 0/${mask};
mask: linear-gradient(90deg,#000 50%, #0000 0) 0/${mask};
}
</pre>`
outputContainer.innerHTML = cssOutput
}
const setStyle = (input, property, unitlessValue, value) => {
if (unitlessValue) {
input.value = unitlessValue
bg.style.setProperty(property, value)
}
}
const getInitialStateFromLocalStorage = () => {
const angle = localStorage.getItem('angle')
const thickness = localStorage.getItem('thickness')
const width = localStorage.getItem('width')
const color = localStorage.getItem('color')
const contrast = localStorage.getItem('contrast')
setStyle(angleInput, '--angle', angle, `${angle}deg`)
setStyle(thicknessInput, '--t', thickness, `${thickness}px`)
setStyle(widthInput, '--w', width, `${width}px`)
setStyle(colorInput, '--h1', color, `${color}deg`)
if (contrast) {
setContrast(contrast)
contrastInput.value = contrast
}
}
getInitialStateFromLocalStorage()
getCSSOutput()
let activeInput
const handleMouseDown = (e) => {
activeInput = e.target
}
const updateValues = (target) => {
const { id } = target
if (id === 'angle') {
angleChange(target)
}
if (id === 'thickness') {
thicknessChange(target)
}
if (id === 'color1') {
colorChange(target)
}
if (id === 'width') {
widthChange(target)
}
if (id === 'contrast') {
contrastChange(target)
}
}
const handleMouseMove = (e) => {
if (activeInput === e.target) {
updateValues(e.target)
}
}
const handleChange = (e) => {
updateValues(e.target)
}
inputs.forEach(el => {
el.addEventListener('change', handleChange)
el.addEventListener('mousedown', handleMouseDown)
el.addEventListener('mousemove', _.throttle(handleMouseMove, 100))
updateValues(el)
})
CSS.registerProperty({
name: '--angle',
syntax: '<angle>',
inherits: true,
initialValue: `${angleInput.value}deg`,
})
CSS.registerProperty({
name: '--t',
syntax: '<length>',
inherits: true,
initialValue: `${thicknessInput.value}px`,
})
CSS.registerProperty({
name: '--w',
syntax: '<length>',
inherits: true,
initialValue: `${widthInput.value}px`,
})