-
Notifications
You must be signed in to change notification settings - Fork 0
/
ramps.js
148 lines (128 loc) · 5.11 KB
/
ramps.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
function initialize() {
const stops = document.getElementById("stops").value;
createSVGStops(stops, true);
createColorPickers(stops, true);
attachColorChangeHandlers(stops);
['cx', 'cy', 'r', 'fx', 'fy'].forEach(id => {
const slider = document.getElementById(id);
const valueElement = document.getElementById(`${id}-value`);
valueElement.textContent = slider.value;
});
}
function createSVGStops(stops, isInitialLoad) {
const gradient = document.getElementById("radial-gradient");
while (gradient.firstChild) {
gradient.firstChild.remove();
}
for (let i = 0; i < stops; i++) {
const stop = document.createElementNS('http://www.w3.org/2000/svg', 'stop');
stop.setAttribute('offset', `${(i / (stops - 1)) * 100}%`);
const color = localStorage.getItem(`color-${i}`) || '#000000';
stop.setAttribute('stop-color', color);
gradient.appendChild(stop);
}
}
function createColorPickers(stops, isInitialLoad) {
let stopElements = "";
for (let i = 0; i < stops; i++) {
let color;
if (isInitialLoad) {
color = getRandomColor();
localStorage.setItem(`color-${i}`, color);
} else {
color = localStorage.getItem(`color-${i}`) || '#000000';
}
stopElements += `<input type="color" id="color-${i}" value="${color}">`;
}
const colorPickerContainer = document.getElementById("color-picker");
colorPickerContainer.innerHTML = stopElements;
}
function attachColorChangeHandlers(stops) {
for (let i = 0; i < stops; i++) {
handleColorChange(i);
}
}
function handleAttributeSliderChange(id, attribute) {
const element = document.getElementById(id);
const valueElement = document.getElementById(`${id}-value`);
element.addEventListener("input", function () {
document.getElementById("radial-gradient").setAttribute(attribute, `${this.value}%`);
valueElement.textContent = this.value;
});
}
function handleColorChange(index) {
const element = document.getElementById(`color-${index}`);
const stopElement = document.querySelector(`#radial-gradient stop:nth-child(${index + 1})`);
element.addEventListener("input", function () {
stopElement.setAttribute('stop-color', this.value);
localStorage.setItem(`color-${index}`, this.value);
});
}
function handleStopsChange() {
const stopElement = document.getElementById("stops");
stopElement.addEventListener("input", function () {
const stops = this.value;
createSVGStops(stops, false);
createColorPickers(stops, false);
attachColorChangeHandlers(stops);
});
}
function handleSpreadChange() {
document.querySelectorAll("input[name=spread]").forEach((item) => {
item.addEventListener("change", function () {
document.getElementById("radial-gradient").setAttribute("spreadMethod", this.value);
});
});
// set initial value based on checked radio button
const initialValue = document.querySelector('input[name="spread"]:checked').value;
document.getElementById("radial-gradient").setAttribute("spreadMethod", initialValue);
}
function getRandomColor() {
return '#' + Math.random().toString(16).slice(2, 8).toUpperCase();
}
function handleRandomise() {
const randomiseButton = document.getElementById('randomise');
randomiseButton.addEventListener('click', function () {
const stops = document.getElementById("stops").value;
for (let i = 0; i < stops; i++) {
const randomColor = getRandomColor();
const colorElement = document.getElementById(`color-${i}`);
colorElement.value = randomColor;
const stopElement = document.querySelector(`#radial-gradient stop:nth-child(${i + 1})`);
stopElement.setAttribute('stop-color', randomColor);
localStorage.setItem(`color-${i}`, randomColor);
}
});
}
function handleGradientSkewChange() {
const skewX = document.getElementById('skewX');
const skewY = document.getElementById('skewY');
const skewXValue = document.getElementById('skewX-value');
const skewYValue = document.getElementById('skewY-value');
skewXValue.textContent = skewX.value;
skewYValue.textContent = skewY.value;
skewX.addEventListener('input', function () {
skewXValue.textContent = this.value;
setGradientSkew(this.value, skewY.value);
});
skewY.addEventListener('input', function () {
skewYValue.textContent = this.value;
setGradientSkew(skewX.value, this.value);
});
}
function setGradientSkew(x, y) {
const gradient = document.getElementById('radial-gradient');
gradient.setAttribute('gradientTransform', `skewX(${x}) skewY(${y})`);
}
window.onload = function () {
initialize();
handleRandomise();
handleAttributeSliderChange("cx", "cx");
handleAttributeSliderChange("cy", "cy");
handleAttributeSliderChange("fx", "fx");
handleAttributeSliderChange("fy", "fy");
handleAttributeSliderChange("r", "r");
handleSpreadChange();
handleStopsChange();
handleGradientSkewChange();
};