-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
179 lines (146 loc) · 3.95 KB
/
script.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
173
174
175
176
177
178
179
const canvas = d3.select('.canvas')
const colorInput = d3.select('#colorPicker')
const marker = d3.select('.marker')
const clearButton = d3.select('[data-clear]')
const saveButton = d3.select('[data-save]')
const undoButton = d3.select('[data-undo]')
const copyButton = d3.select('[data-copy]')
const textArea = d3.select('#css')
const drawArea = d3.select('.draw-area')
const range = d3.select('#columns')
const width = drawArea.node().offsetWidth
const height = drawArea.node().offsetHeight
let columns = 30
const multiplier = height / width
let rows = columns * multiplier
let cellSize = 100 / columns
let rowSize = 100 / rows
let isPressed = false
const bisect = d3.bisector((d) => d)
const dx = (posX) => {
const stepX = 1 / (columns - 1)
const dataX = d3.range(0, 100, stepX)
const indexX = bisect.center(dataX, posX / width)
return (dataX[indexX] * 100).toFixed(2)
}
const dy = (posY) => {
const stepY = 1 / (rows - 1)
const dataY = d3.range(0, 1, stepY)
const indexY = bisect.center(dataY, posY / height)
return (dataY[indexY] * 100).toFixed(2)
}
let bg = []
let bgPosition = []
const draw = () => {
drawArea
.style('background-image', bg.join(','))
.style('background-position', bgPosition.join(','))
}
const updateText = () => {
textArea.html(`
aspect-ratio: 4 / 3;
background-image: ${bg.join(',')};
background-size: calc(100% / ${columns}) calc(100% / ${rows});
background-position: ${bgPosition.join(',')};
background-repeat: no-repeat;
`)
}
const shouldDraw = (newBgValue, newBgPositionValue) => {
if (!isPressed) return false
if (!bg.length) return true
if (bg[0] !== newBgValue) return true
return bgPosition[0] !== newBgPositionValue
}
canvas.on('click', (e) => {
const color = colorInput.node().value
const [posX, posY] = d3.pointer(e)
const x = dx(posX)
const y = dy(posY)
bg = [ `linear-gradient(${color}, ${color})`, ...bg ]
bgPosition = [ `${x}% ${y}%`, ...bgPosition]
draw()
updateText()
})
canvas
.on('mouseover', () => {
marker.style('opacity', 1)
})
.on('mouseout', () => {
marker.style('opacity', 0)
})
.on('mousedown', () => {
isPressed = true
})
.on('mouseup', () => {
isPressed = false
})
canvas.on('mousemove', (e) => {
const color = colorInput.node().value
const [posX, posY] = d3.pointer(e)
const x = dx(posX)
const y = dy(posY)
marker
.style('background-position', `${x}% ${y}%`)
const newBgValue = `linear-gradient(${color}, ${color})`
const newBgPositionValue = `${x}% ${y}%`
if (!shouldDraw(newBgValue, newBgPositionValue)) return
bg = [ newBgValue, ...bg ]
bgPosition = [ newBgPositionValue, ...bgPosition]
draw()
updateText()
})
colorInput.on('input', () => {
marker.style('--bg', colorInput.node().value)
})
const clear = () => {
bg = []
bgPosition = []
textArea.html('')
drawArea
.style('background-image', '')
.style('background-position', '')
.style('background-size', '')
}
clearButton.on('click', clear)
saveButton.on('click', () => {
const art = textArea.node().value
localStorage.setItem('art', art)
})
const restoreSavedArt = () => {
const savedArt = localStorage.getItem('art')
if (!savedArt) return
drawArea.attr('style', savedArt)
textArea.html(savedArt)
}
const setColumnCount = () => {
columns = range.node().value
rows = columns * multiplier
cellSize = 100 / columns
rowSize = 100 / rows
canvas.style('--cellSizeCol', `${cellSize}%`)
canvas.style('--cellSizeRow', `${rowSize}%`)
canvas.style('--colCount', columns)
}
setColumnCount()
restoreSavedArt()
window.addEventListener('resize', () => {
if (window.innerWidth > width + 100) return
setColumnCount()
})
range.on('input', setColumnCount)
copyButton.on('click', (e) => {
const copyText = textArea.node()
copyText.select()
copyText.setSelectionRange(0, 99999)
navigator.clipboard.writeText(copyText.value)
copyButton.text('Copied!')
setTimeout(() => {
copyButton.text('Copy to clipboard')
}, 3000)
})
undoButton.on('click', () => {
bg.splice(0, 1)
bgPosition.splice(0, 1)
draw()
updateText()
})