-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
149 lines (125 loc) · 3.72 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
const canvas = document.querySelector('#main-canvas')
const ctx = canvas.getContext('2d')
// Set canvas size
canvas.width = 200
canvas.height = 200
// Fill the canvas and create some shapes
ctx.fillStyle = '#c5c5c5'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = '#A81BF4'
ctx.scale(2, 2)
const randomColor = () => {
let str = Math.round(Math.random() * 0x1000000).toString(16)
while (str.length < 6) {
str = '0' + str
}
return '#' + str
}
console.log(randomColor())
for (let i = 0; i < 25; i++) {
ctx.fillStyle = randomColor()
ctx.fillRect(
Math.round((Math.random() * canvas.width) / 2),
Math.round((Math.random() * canvas.height) / 3),
Math.round(Math.random() * 50),
Math.round(Math.random() * 50)
)
}
function getColorAtPixel(imageData, x, y) {
const { width, data } = imageData
return {
r: data[4 * (width * y + x) + 0],
g: data[4 * (width * y + x) + 1],
b: data[4 * (width * y + x) + 2],
a: data[4 * (width * y + x) + 3],
}
}
function setColorAtPixel(imageData, color, x, y) {
const { width, data } = imageData
data[4 * (width * y + x) + 0] = color.r & 0xff
data[4 * (width * y + x) + 1] = color.g & 0xff
data[4 * (width * y + x) + 2] = color.b & 0xff
data[4 * (width * y + x) + 3] = color.a & 0xff
}
function colorMatch(a, b) {
return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a
}
function floodFill(imageData, newColor, x, y) {
const { width, height, data } = imageData
const stack = []
const baseColor = getColorAtPixel(imageData, x, y)
let operator = { x, y }
// Check if base color and new color are the same
if (colorMatch(baseColor, newColor)) {
return
}
// Add the clicked location to stack
stack.push({ x: operator.x, y: operator.y })
while (stack.length) {
operator = stack.pop()
let contiguousDown = true // Vertical is assumed to be true
let contiguousUp = true // Vertical is assumed to be true
let contiguousLeft = false
let contiguousRight = false
// Move to top most contiguousDown pixel
while (contiguousUp && operator.y >= 0) {
operator.y--
contiguousUp = colorMatch(
getColorAtPixel(imageData, operator.x, operator.y),
baseColor
)
}
// Move downward
while (contiguousDown && operator.y < height) {
setColorAtPixel(imageData, newColor, operator.x, operator.y)
// Check left
if (
operator.x - 1 >= 0 &&
colorMatch(
getColorAtPixel(imageData, operator.x - 1, operator.y),
baseColor
)
) {
if (!contiguousLeft) {
contiguousLeft = true
stack.push({ x: operator.x - 1, y: operator.y })
}
} else {
contiguousLeft = false
}
// Check right
if (
operator.x + 1 < width &&
colorMatch(
getColorAtPixel(imageData, operator.x + 1, operator.y),
baseColor
)
) {
if (!contiguousRight) {
stack.push({ x: operator.x + 1, y: operator.y })
contiguousRight = true
}
} else {
contiguousRight = false
}
operator.y++
contiguousDown = colorMatch(
getColorAtPixel(imageData, operator.x, operator.y),
baseColor
)
}
}
}
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
const col = { r: 0xff, g: 0xff, b: 0x0, a: 0xff }
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect()
const x = Math.round(event.clientX - rect.left)
const y = Math.round(event.clientY - rect.top)
floodFill(imageData, col, x, y)
ctx.putImageData(imageData, 0, 0)
})
var btn = document.querySelector('#refresh')
btn.addEventListener('click', function () {
location.reload()
})