forked from jameswilce/DevMinDArT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
108 lines (95 loc) · 3.55 KB
/
functions.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
/**
* Enables/disables app links based on the current week number since program start.
* Used to progressively unlock content over time
*/
export function showOnlyCurrentLinks() {
// set programme start date (mm/dd/yyyy)
const startDate = new Date("02/19/2024");
const dayInMilliseconds = 86400000;
const today = new Date();
const elapsedDays = Math.floor((today - startDate) / dayInMilliseconds);
const currentWeekNumber = Math.ceil(elapsedDays / 7);
const homeGrid = document.querySelector('[data-element="home-grid"]');
if (!homeGrid) {
console.warn(
'Parent element with attribute data-element="home-grid" not found'
);
}
const drawingAppLinks = homeGrid.querySelectorAll("[data-week]");
// Enable/disable links based on their week number
// Links for future weeks are made unclickable using the 'disabled' attribute
drawingAppLinks.forEach((link) => {
link.dataset.week <= currentWeekNumber
? link.removeAttribute("disabled")
: link.setAttribute("disabled", true);
});
}
// Interface utility functions for managing button states and interactions
// Remove 'active' class from all buttons
export function clearActiveButtonState() {
const activeButtons = document.querySelectorAll(".btn.active");
activeButtons.forEach((button) => button.classList.remove("active"));
}
export function hasActiveClass(el) {
return Boolean(el && el.classList.contains("active"));
}
/**
* Adds both click and touch handlers to an element
* @param {HTMLElement} element - The element to add handlers to
* @param {Function} handler - The event handler function
*/
export function addInteractionHandlers(element, handler) {
element.addEventListener("click", handler);
element.addEventListener("touchend", (e) => {
e.preventDefault(); // Prevent mouse event from firing
handler(e);
});
}
// Color utility functions
// Convert hex color to RGB color object
export function hexToRgb(p5, hex) {
hex = hex.replace("#", "");
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return p5.color(r, g, b);
}
// Apply alpha channel to a color
export function colorAlpha(p5, aColor, alpha) {
const c = p5.color(aColor);
return p5.color(p5.red(c), p5.green(c), p5.blue(c), alpha * 255);
}
/**
* Sets up canvas event listeners for touch and mouse interactions
*/
export function setupCanvasEventListeners() {
const canvasContainer = document.querySelector(
'[data-element="canvas-container"]'
);
const canvas = canvasContainer.querySelector("canvas");
canvas.addEventListener("touchmove", handleMove, { passive: false });
canvas.addEventListener("mousemove", handleMove);
canvas.addEventListener("touchstart", touchdown);
canvas.addEventListener("mousedown", touchdown);
canvas.addEventListener("touchend", touchstop);
canvas.addEventListener("touchleave", touchstop);
canvas.addEventListener("touchcancel", touchstop);
canvas.addEventListener("mouseup", touchstop);
canvas.addEventListener("mouseleave", touchstop);
}
/**
* Checks if a click/touch event occurred on a button (so we can prevent propogation of that click to the canvas)
* @param {Event} event - The event to check
* @returns {boolean} True if click was on a button/interface element
*/
export function isClickOnButton(event) {
// is event is a valid DOM event with target property?
if (!event || !event.target || typeof event.target.closest !== "function") {
return false;
}
return (
event.target.closest(".btn") !== null ||
event.target.closest(".interface") !== null
);
}