1- // if (!isMobile || !window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
21const shiftLayer1 = document . getElementById ( "shift-layer1" ) ;
32const shiftLayer2 = document . getElementById ( "shift-layer2" ) ;
43const bg = document . getElementById ( 'bg' ) ;
5- let shiftLayer1State = createLayerState ( shiftLayer1 ) ;
6- let shiftLayer2State = createLayerState ( shiftLayer2 ) ;
7- let bgState = createBgLayerState ( bg ) ;
4+
5+ const allShiftLayers = Array . from ( document . querySelectorAll ( '[id^="shift-layer"]' ) ) ;
6+ const totalShiftLayers = allShiftLayers . length ;
7+
88function getShiftLayerInfo ( element ) {
9- const allLayers = document . querySelectorAll ( '[id^="shift-layer"]' ) ;
10- const totalLayers = allLayers . length ;
119 const match = element . id . match ( / s h i f t - l a y e r ( \d + ) / ) ;
1210 const currentLayer = match ? parseInt ( match [ 1 ] ) : null ;
13- return { currentLayer, totalLayers } ;
11+ return { currentLayer, totalLayers : totalShiftLayers } ;
1412}
13+
14+ let shiftLayer1State = createLayerState ( shiftLayer1 ) ;
15+ let shiftLayer2State = createLayerState ( shiftLayer2 ) ;
16+ let bgState = createBgLayerState ( bg ) ;
17+
1518function randomlyModifyValue ( value , minFactor = 0.85 , maxFactor = 1.15 ) {
1619 if ( Math . random ( ) < 0.333 ) {
1720 const factor = Math . random ( ) * ( maxFactor - minFactor ) + minFactor ;
1821 return Math . round ( value * factor ) ;
1922 }
2023 return Math . round ( value ) ;
2124}
25+
2226function getRandomInRange ( min , max , modifier = 1 , element ) {
2327 if ( ! element . id . startsWith ( 'shift-layer' ) ) {
2428 return ( Math . random ( ) * ( max - min ) + min ) * modifier ;
2529 }
26- const { currentLayer, totalLayers } = getShiftLayerInfo ( element ) ;
27- for ( let i = 1 ; i <= totalLayers ; i ++ ) {
28- if ( currentLayer === i ) {
29- return randomlyModifyValue ( Math . random ( ) * ( max - min ) + min ) * modifier ;
30- }
30+ const { currentLayer } = getShiftLayerInfo ( element ) ;
31+ if ( currentLayer != null ) {
32+ return randomlyModifyValue ( Math . random ( ) * ( max - min ) + min ) * modifier ;
3133 }
34+ return ( Math . random ( ) * ( max - min ) + min ) * modifier ;
35+ }
36+
37+ function roundTo ( value , decimals ) {
38+ const factor = 10 ** decimals ;
39+ return Math . round ( value * factor ) / factor ;
3240}
41+
3342function getRandomTransitionDuration ( element ) {
34- const randomValue = getRandomInRange ( 7000 , 21000 , 1 , element ) ;
35- return Math . round ( randomValue ) ;
43+ return Math . round ( getRandomInRange ( 7000 , 21000 , 1 , element ) ) ;
3644}
45+
3746function getRandomHueShift ( element ) {
38- const randomValue = getRandomInRange ( - 180 , 180 , 1 , element ) ;
39- return Math . round ( randomValue * 10 ) / 10 ;
47+ return roundTo ( getRandomInRange ( - 180 , 180 , 1 , element ) , 1 ) ;
4048}
49+
4150function getRandomFilterValue ( element ) {
42- const randomValue = getRandomInRange ( 87 , 185 , 1 , element ) ;
43- return Math . round ( randomValue * 10 ) / 10 ;
51+ return roundTo ( getRandomInRange ( 87 , 185 , 1 , element ) , 1 ) ;
4452}
53+
4554function getRandomOpacityValue ( element ) {
46- const randomValue = getRandomInRange ( 65 , 95 , 1 , element ) ;
47- return Math . round ( randomValue * 10 ) / 10 ;
55+ return roundTo ( getRandomInRange ( 65 , 95 , 1 , element ) , 1 ) ;
4856}
57+
4958function getRandomOpacityValue2 ( element ) {
50- const randomValue = getRandomInRange ( .29 , .37 , 1 , element ) ;
51- return Math . round ( randomValue * 1000 ) / 1000 ;
59+ return roundTo ( getRandomInRange ( 0.29 , 0.37 , 1 , element ) , 3 ) ;
5260}
61+
5362function getRandomGradientSteps ( element ) {
5463 const { currentLayer } = getShiftLayerInfo ( element ) ;
55- return currentLayer === 1 ? 7 * Math . ceil ( Math . random ( ) * 7 ) : randomlyModifyValue ( Math . ceil ( Math . random ( ) * 12 ) + 2 ) ;
64+ if ( currentLayer === 1 ) {
65+ return 7 * Math . ceil ( Math . random ( ) * 7 ) ;
66+ }
67+ return randomlyModifyValue ( Math . ceil ( Math . random ( ) * 12 ) + 2 ) ;
5668}
69+
5770function createLayerState ( element ) {
5871 const { currentLayer } = getShiftLayerInfo ( element ) ;
5972 return {
@@ -74,13 +87,9 @@ function createLayerState(element) {
7487 targetHueShift : getRandomHueShift ( element ) ,
7588 } ;
7689}
90+
7791function createBgLayerState ( element ) {
78- let initialOpacity ;
79- if ( element . id === 'bg' ) {
80- initialOpacity = 0.33 ;
81- } else {
82- initialOpacity = 1 ;
83- }
92+ const initialOpacity = element . id === 'bg' ? 0.33 : 1 ;
8493 return {
8594 transitionCurrentTime : 0 ,
8695 transitionProgress : 0 ,
@@ -89,45 +98,60 @@ function createBgLayerState(element) {
8998 targetOpacity : getRandomOpacityValue2 ( element ) ,
9099 } ;
91100}
101+
92102function setGradient ( element , state ) {
93103 const { currentLayer } = getShiftLayerInfo ( element ) ;
94- function getAdjustedHue ( currentGradientStep , gradientAngle ) {
95- return ( currentGradientStep * gradientAngle ) ;
96- }
97- const width = element . offsetWidth || element . clientWidth ;
104+ const width = element . offsetWidth || element . clientWidth ;
98105 const height = element . offsetHeight || element . clientHeight ;
99- const diagonal = Math . sqrt ( width * width + height * height ) ;
106+ const diagonal = Math . sqrt ( width ** 2 + height ** 2 ) ;
100107 const percentage = ( diagonal / ( Math . max ( width , height ) * Math . sqrt ( 2 ) ) ) * 100 ;
108+
101109 let gradients = [ ] ;
102110 const gradientSteps = state . currentGradientSteps ;
103111 const gradientAngle = 360 / gradientSteps ;
112+
104113 for ( let i = 0 ; i < gradientSteps ; i ++ ) {
105114 const angle = i * gradientAngle ;
106115 const x = 50 + 50 * Math . cos ( angle * Math . PI / 180 ) ;
107116 const y = 50 + 50 * Math . sin ( angle * Math . PI / 180 ) ;
108- const currentGradientStep = i + state . currentGradientSteps ;
109- gradients . push ( `radial-gradient(ellipse farthest-corner at ${ Math . round ( x * 10 ) / 10 } % ${ Math . round ( y * 10 ) / 10 } %, hsl(${ getAdjustedHue ( currentGradientStep , gradientAngle ) } , 100%, 50%), transparent ${ Math . round ( percentage * 10 ) / 10 } %)` ) ;
117+ const currentGradientStep = i + gradientSteps ;
118+ gradients . push (
119+ `radial-gradient(ellipse farthest-corner at ${ roundTo ( x , 1 ) } % ${ roundTo ( y , 1 ) } %, hsl(${ currentGradientStep * gradientAngle } , 100%, 50%), transparent ${ roundTo ( percentage , 1 ) } %)`
120+ ) ;
110121 }
122+
111123 element . style . backgroundImage = gradients . join ( ', ' ) ;
112- element . style . filter = `opacity(${ state . currentOpacity } %) contrast(${ state . currentContrast } %) brightness(${ state . currentBrightness } %) saturate(${ state . currentSaturation } %) hue-rotate(${ state . currentHueShift } deg)` ;
124+ element . style . filter = `
125+ opacity(${ state . currentOpacity } %)
126+ contrast(${ state . currentContrast } %)
127+ brightness(${ state . currentBrightness } %)
128+ saturate(${ state . currentSaturation } %)
129+ hue-rotate(${ state . currentHueShift } deg)
130+ ` . trim ( ) ;
113131}
132+
114133function updateLayerState ( state , element , deltaTime ) {
115134 state . transitionCurrentTime += deltaTime ;
116- state . transitionProgress += ( deltaTime / state . transitionDuration ) ;
117- if ( state . transitionProgress > 1 ) state . transitionProgress = 1 ;
135+ state . transitionProgress = Math . min ( 1 , state . transitionProgress + deltaTime / state . transitionDuration ) ;
136+
118137 const t = ( ) => metaRecursiveEaseNoise ( state . transitionProgress ) ;
119138 const { currentLayer } = getShiftLayerInfo ( element ) ;
139+
140+ const randomFactor = ( max ) => Math . random ( ) * max ;
141+
120142 if ( currentLayer === 1 ) {
121- state . currentGradientSteps += Math . round ( ( state . targetGradientSteps - state . currentGradientSteps ) * ( t ( ) * 0.00005 ) * Math . random ( ) * 10 ) / 10 ;
122- state . currentHueShift += Math . round ( ( state . targetHueShift - state . currentHueShift ) * ( t ( ) * 0.05 ) * Math . random ( ) * 10 ) / 10 ;
143+ state . currentGradientSteps += Math . round ( ( state . targetGradientSteps - state . currentGradientSteps ) * t ( ) * 0.00005 * randomFactor ( 10 ) ) / 10 ;
144+ state . currentHueShift += Math . round ( ( state . targetHueShift - state . currentHueShift ) * t ( ) * 0.05 * randomFactor ( 10 ) ) / 10 ;
123145 } else {
124- state . currentGradientSteps += Math . round ( ( state . targetGradientSteps - state . currentGradientSteps ) * ( t ( ) * 0.05 ) * Math . random ( ) * 10 ) / 10 ;
125- state . currentHueShift += Math . round ( ( state . targetHueShift - state . currentHueShift ) * ( t ( ) * 0.01 ) * Math . random ( ) * 10 ) / 10 ;
146+ state . currentGradientSteps += Math . round ( ( state . targetGradientSteps - state . currentGradientSteps ) * t ( ) * 0.05 * randomFactor ( 10 ) ) / 10 ;
147+ state . currentHueShift += Math . round ( ( state . targetHueShift - state . currentHueShift ) * t ( ) * 0.01 * randomFactor ( 10 ) ) / 10 ;
126148 }
127- state . currentContrast += Math . round ( ( state . targetContrast - state . currentContrast ) * ( t ( ) * Math . random ( ) ) * 10 ) / 10 ;
128- state . currentBrightness += Math . round ( ( state . targetBrightness - state . currentBrightness ) * ( t ( ) * Math . random ( ) ) * 10 ) / 10 ;
129- state . currentSaturation += Math . round ( ( state . targetSaturation - state . currentSaturation ) * ( t ( ) * Math . random ( ) ) * 10 ) / 10 ;
130- state . currentOpacity += Math . round ( ( state . targetOpacity - state . currentOpacity ) * ( t ( ) * Math . random ( ) ) * 10 ) / 10 ;
149+
150+ state . currentContrast += Math . round ( ( state . targetContrast - state . currentContrast ) * t ( ) * randomFactor ( 1 ) * 10 ) / 10 ;
151+ state . currentBrightness += Math . round ( ( state . targetBrightness - state . currentBrightness ) * t ( ) * randomFactor ( 1 ) * 10 ) / 10 ;
152+ state . currentSaturation += Math . round ( ( state . targetSaturation - state . currentSaturation ) * t ( ) * randomFactor ( 1 ) * 10 ) / 10 ;
153+ state . currentOpacity += Math . round ( ( state . targetOpacity - state . currentOpacity ) * t ( ) * randomFactor ( 1 ) * 10 ) / 10 ;
154+
131155 if ( state . transitionCurrentTime >= state . transitionDuration ) {
132156 state . transitionCurrentTime = 0 ;
133157 state . transitionProgress = 0 ;
@@ -140,19 +164,23 @@ function updateLayerState(state, element, deltaTime) {
140164 state . targetOpacity = getRandomOpacityValue ( element ) ;
141165 }
142166}
167+
143168function updateBgLayerState ( state , element , deltaTime ) {
144169 state . transitionCurrentTime += deltaTime ;
145- state . transitionProgress += ( deltaTime / state . transitionDuration ) * Math . random ( ) ;
146- if ( state . transitionProgress > 1 ) state . transitionProgress = 1 ;
170+ state . transitionProgress = Math . min ( 1 , state . transitionProgress + ( deltaTime / state . transitionDuration ) * Math . random ( ) ) ;
171+
147172 const t = ( ) => metaRecursiveEaseNoise ( state . transitionProgress ) ;
148- state . currentOpacity += Math . round ( ( state . targetOpacity - state . currentOpacity ) * t ( ) * ( Math . random ( ) * 1 ) * 1000 ) / 1000 ;
173+
174+ state . currentOpacity += Math . round ( ( state . targetOpacity - state . currentOpacity ) * t ( ) * Math . random ( ) * 1000 ) / 1000 ;
175+
149176 if ( state . transitionCurrentTime >= state . transitionDuration ) {
150177 state . transitionCurrentTime = 0 ;
151178 state . transitionProgress = 0 ;
152179 state . transitionDuration = getRandomTransitionDuration ( element ) ;
153180 state . targetOpacity = element . id === 'bg' ? getRandomOpacityValue2 ( element ) : getRandomOpacityValue ( element ) ;
154181 }
155182}
183+
156184function updateColors ( updateTime ) {
157185 if ( ! isWindowActive ) {
158186 requestAnimationFrame ( updateColors ) ;
@@ -169,4 +197,3 @@ function updateColors(updateTime) {
169197 requestAnimationFrame ( updateColors ) ;
170198}
171199requestAnimationFrame ( updateColors ) ;
172- // }
0 commit comments