-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (61 loc) · 1.96 KB
/
Copy pathscript.js
File metadata and controls
68 lines (61 loc) · 1.96 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
const $ = (e) => document.querySelector(e)
const img = $("#poster")
const gradient = $(".gradient")
const btnPrev = $("#previous")
const btnShare = $("#share")
const originalShareText = btnShare.innerHTML
const canvas = $("#canvas")
const ctx = canvas.getContext("2d")
img.onload = () => {
canvas.width = img.width
canvas.height = img.height
ctx.drawImage(img, 0, 0, img.width, img.height)
const imageData = ctx.getImageData(0, 0, img.width, img.height)
const pixels = imageData.data
const color = getDominantColor(pixels, img.width, img.height)
gradient.style.background = `rgba(${color.r}, ${color.g}, ${color.b}, 0.5)`
}
function getDominantColor(data, width, height) {
const colorMap = {}
const step = 30
for (let y = 0; y < height; y += step) {
for (let x = 0; x < width; x += step) {
const index = (y * width + x) * 4
const r = data[index]
const g = data[index + 1]
const b = data[index + 2]
const key = `${r},${g},${b}`
colorMap[key] = (colorMap[key] || 0) + 1
}
}
let dominantColor = { r: 0, g: 0, b: 0 }
let maxCount = 0
for (let key in colorMap) {
if (colorMap[key] > maxCount) {
maxCount = colorMap[key]
const [r, g, b] = key.split(",").map(Number)
dominantColor = { r, g, b }
}
}
return dominantColor
}
if (btnPrev) {
btnPrev.addEventListener("click", () => {
history.back()
})
}
btnShare.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(window.location.href)
btnShare.textContent = "Link Copied!"
setTimeout(() => {
btnShare.innerHTML = originalShareText
}, 2000)
} catch (error) {
console.error("Failed to copy: ", error)
btnShare.textContent = "Error Copying"
setTimeout(() => {
btnShare.innerHTML = originalShareText
}, 2000)
}
})