-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
82 lines (74 loc) · 3 KB
/
script.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
if (!isSecureContext)
console.warn('This page is not running in a secute context. Serve it via HTTPS.');
if ('paintWorklet' in CSS)
init();
else
console.warn('The CSS Paint API is not supported, using the fallback image');
function init() {
let defaults = {
startAlpha: 1.00,
endAlpha: 0.01,
numRays: 10
};
let
root = document.documentElement
,gui = document.body.querySelector('#gui')
,numRaysSlider = gui.querySelector('#num-rays')
,startAlphaSlider = gui.querySelector('#start-alpha')
,endAlphaSlider = gui.querySelector('#end-alpha')
,header = document.body.querySelector('header')
;
CSS.paintWorklet.addModule('paint.js');
if ('registerProperty' in CSS) {
CSS.registerProperty({
name: '--num-rays',
syntax: '<integer>',
inherits: false,
initialValue: defaults.numRays,
});
CSS.registerProperty({
name: '--start-alpha',
syntax: '<number>',
inherits: false,
initialValue: defaults.startAlpha,
});
CSS.registerProperty({
name: '--end-alpha',
syntax: '<number>',
inherits: false,
initialValue: 0.2,
});
}
else {
if (getComputedStyle(header).getPropertyValue('--num-rays') === '')
header.style.setProperty('--num-rays', defaults.numRays);
if (getComputedStyle(header).getPropertyValue('--start-alpha') === '')
header.style.setProperty('--start-alpha', defaults.startAlpha);
if (getComputedStyle(header).getPropertyValue('--end-alpha') === '')
header.style.setProperty('--end-alpha', defaults.endAlpha);
}
numRaysSlider.addEventListener('input', function(e) {
let slider = e.target;
let newValue = parseFloat(slider.value);
slider.parentElement.querySelector('span').innerText = newValue;
header.style.setProperty('--num-rays', newValue);
});
numRaysSlider.value = parseFloat(getComputedStyle(header).getPropertyValue('--num-rays'));
numRaysSlider.dispatchEvent(new InputEvent('input'));
startAlphaSlider.addEventListener('input', function(e) {
let slider = e.target;
let newValue = parseFloat(slider.value);
slider.parentElement.querySelector('span').innerText = newValue.toFixed(2);
header.style.setProperty('--start-alpha', newValue);
});
startAlphaSlider.value = parseFloat(getComputedStyle(header).getPropertyValue('--start-alpha'));
startAlphaSlider.dispatchEvent(new InputEvent('input'));
endAlphaSlider.addEventListener('input', function(e) {
let slider = e.target;
let newValue = parseFloat(slider.value);
slider.parentElement.querySelector('span').innerText = newValue.toFixed(2);
header.style.setProperty('--end-alpha', newValue);
});
endAlphaSlider.value = parseFloat(getComputedStyle(header).getPropertyValue('--end-alpha'));
endAlphaSlider.dispatchEvent(new InputEvent('input'));
}