-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (108 loc) · 3.54 KB
/
Copy pathscript.js
File metadata and controls
120 lines (108 loc) · 3.54 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
function copyEmailToClipboard(event) {
event.preventDefault();
const href = event.currentTarget.getAttribute("href");
const email = href.replace("mailto:", "");
// Use the Clipboard API to write the text
navigator.clipboard
.writeText(email)
.then(() => {
alert("Email address copied: " + email);
})
.catch((err) => {
console.error("Could not copy text: ", err);
});
}
const projects = document.querySelectorAll(".proj-container");
const projBtns = document.querySelectorAll("#proj-list-container button");
const projNumBtns = document.querySelectorAll(".proj-num-btn");
let currentActiveTarget = "colourmania";
projBtns.forEach((btn) => {
btn.addEventListener("click", () => {
const target = btn.dataset.target;
currentActiveTarget = target;
projects.forEach((project) => {
project.classList.toggle("active", project.dataset.view === target);
console.log(target);
});
projNumBtns.forEach((btn) => {
btn.classList.toggle("active", btn.dataset.target === target);
});
});
// btn.addEventListener("mouseenter", () => {
// const target = btn.dataset.target;
// projNumBtns.forEach((btn) => {
// btn.classList.toggle("active", btn.dataset.target === target);
// });
// });
// btn.addEventListener("mouseleave", () => {
// projNumBtns.forEach((numBtn) => {
// numBtn.classList.toggle(
// "active",
// numBtn.dataset.target === currentActiveTarget
// );
// });
// });
});
createCanvas(4, 50);
function createCanvas(rows, cols) {
const container = document.querySelector(".sketch-container");
// if (!initialise) {
// container.replaceChildren();
// }
for (let i = 0; i < rows; i++) {
const row = document.createElement("div");
row.style.display = "flex";
for (let j = 0; j < cols; j++) {
const square = document.createElement("div");
square.classList.add("pixel");
square.setAttribute(
"style",
"border-left: 3px solid black; border-top: 3px solid black; flex: 1; aspect-ratio: 1/1;"
);
if (j === cols - 1) {
square.style.borderRight = "3px solid black";
}
if (i === rows - 1) {
square.style.borderBottom = "3px solid black";
}
row.appendChild(square);
}
container.appendChild(row);
}
container.addEventListener("mouseover", (event) => {
if (
event.target.classList.contains("pixel") &&
!event.target.classList.contains("coloured")
) {
event.target.style.backgroundColor = generateRandomColor();
event.target.classList.add("coloured");
}
});
}
const generateRandomColor = () => {
// 1. Pick a random Hue (0-360)
const h = Math.floor(Math.random() * 360);
// 2. Fix Saturation at 80% and Lightness at 50% for vibrancy
const s = 80;
const l = 50;
// 3. Convert HSL to Hex
const l_alt = l / 100;
const a = (s * Math.min(l_alt, 1 - l_alt)) / 100;
const f = (n) => {
const k = (n + h / 30) % 12;
const color = l_alt - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color)
.toString(16)
.padStart(2, "0");
};
return `#${f(0)}${f(8)}${f(4)}`;
};
// function getSize() {
// let size = Number(window.prompt("Choose the size of the square canvas", ""));
// if (size < 0 || size > 99) {
// size = Number(window.prompt("Choose the size of the square canvas", ""));
// }
// createCanvas(size, false);
// }
// const changeSize = document.querySelector("button");
// changeSize.addEventListener("click", getSize);