|
| 1 | +const DEFAULT_COLOR_1 = '#00ff00'; // GREEN |
| 2 | +const DEFAULT_COLOR_2 = '#ff0000'; // RED |
| 3 | + |
| 4 | +const directionSelector = /** @type {HTMLSelectElement} */ (document.getElementById('direction')); |
| 5 | +const inputColor1 = /** @type {HTMLInputElement} */ (document.getElementById('color1')); |
| 6 | +const inputColor2 = /** @type {HTMLInputElement} */ (document.getElementById('color2')); |
| 7 | + |
| 8 | +const displayCSSText = /** @type {HTMLElement} */ (document.getElementById('display-css-text')); |
| 9 | + |
| 10 | +/** |
| 11 | + * @type {{[directionValue: string] : string}} |
| 12 | + */ |
| 13 | +const directionDict = { |
| 14 | + left: 'to left', |
| 15 | + right: 'to right', |
| 16 | + top: 'to top', |
| 17 | + bottom: 'to bottom', |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {string} color1 |
| 22 | + * @param {string} color2 |
| 23 | + * @param {string} directionValue |
| 24 | + */ |
| 25 | +function generateGradientText(directionValue, color1, color2) { |
| 26 | + const direction = directionDict[directionValue]; |
| 27 | + if (direction) return `linear-gradient(${direction}, ${color1}, ${color2})`; |
| 28 | + return `linear-gradient(to right, ${color1}, ${color2})`; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {HTMLElement} body |
| 33 | + * @param {HTMLElement} display |
| 34 | + * @param {HTMLInputElement} input1 |
| 35 | + * @param {HTMLInputElement} input2 |
| 36 | + * @param {string} direction |
| 37 | + */ |
| 38 | +function update(body, display, direction, input1, input2) { |
| 39 | + const cssText = generateGradientText(direction, input1.value, input2.value); |
| 40 | + |
| 41 | + body.style.background = cssText; |
| 42 | + display.textContent = `${body.style.background};`; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @param {HTMLElement} body |
| 47 | + * @param {HTMLElement} display |
| 48 | + * @param {HTMLInputElement} input1 |
| 49 | + * @param {HTMLInputElement} input2 |
| 50 | + * @param {string} direction |
| 51 | + */ |
| 52 | +function init(body, display, direction, input1, input2) { |
| 53 | + input1.value = DEFAULT_COLOR_1; |
| 54 | + input2.value = DEFAULT_COLOR_2; |
| 55 | + |
| 56 | + update(body, display, direction, input1, input2); |
| 57 | +} |
| 58 | + |
| 59 | +addEventListener('DOMContentLoaded', () => { |
| 60 | + init(document.body, displayCSSText, directionSelector.value, inputColor1, inputColor2); |
| 61 | + |
| 62 | + inputColor1.addEventListener('input', () => |
| 63 | + update(document.body, displayCSSText, directionSelector.value, inputColor1, inputColor2), |
| 64 | + ); |
| 65 | + inputColor2.addEventListener('input', () => |
| 66 | + update(document.body, displayCSSText, directionSelector.value, inputColor1, inputColor2), |
| 67 | + ); |
| 68 | + |
| 69 | + directionSelector.addEventListener('change', () => |
| 70 | + update(document.body, displayCSSText, directionSelector.value, inputColor1, inputColor2), |
| 71 | + ); |
| 72 | +}); |
0 commit comments