@@ -2,9 +2,11 @@ import { IColorGetter, IArrColor } from 'src/typings'
22import { hslToRgb } from 'src/helpers'
33import { settings } from 'src/settings'
44import { pixelsCount , hueToColor } from '../shared'
5+ import { audioState } from '../wsAudio'
56
67export const getHeartbeatColor : IColorGetter = ( _ , time ) => {
7- const cycle = 1000
8+ const beat = settings . syncToMusic && audioState . bpm ? 60000 / audioState . bpm : 1000
9+ const cycle = beat
810 const t = ( time * settings . effectSpeed ) % cycle
911
1012 if ( t < lastHeartbeat ) {
@@ -16,33 +18,35 @@ export const getHeartbeatColor: IColorGetter = (_, time) => {
1618 const fade = Math . min ( t / 200 , 1 )
1719 const baseColor = prevHeartbeat . map ( ( c , i ) => c + ( nextHeartbeat [ i ] - c ) * fade ) as IArrColor
1820
19- const first = pulseIntensity ( t , 0 )
20- const second = pulseIntensity ( t , 250 )
21+ const first = pulseIntensity ( t , 0 , cycle )
22+ const second = pulseIntensity ( t , cycle / 4 , cycle )
2123 const intensity = Math . max ( first , second )
2224
2325 return baseColor . map ( c => Math . round ( c * intensity ) ) as IArrColor
2426}
2527
26- const pulseDuration = 200
27- function pulseIntensity ( t : number , offset : number ) {
28+ const pulseBaseDuration = 200
29+ function pulseIntensity ( t : number , offset : number , cycle : number ) {
30+ const duration = settings . syncToMusic ? cycle * ( pulseBaseDuration / 1000 ) : pulseBaseDuration
2831 const dt = t - offset
29- if ( dt < 0 || dt >= pulseDuration ) return 0
30- const ratio = dt / pulseDuration
32+ if ( dt < 0 || dt >= duration ) return 0
33+ const ratio = dt / duration
3134 return Math . sin ( ratio * Math . PI )
3235}
3336
3437export const getStrobeColor : IColorGetter = ( _ , time ) => {
35- const interval = 200
38+ const interval = settings . syncToMusic && audioState . bpm ? 60000 / audioState . bpm : 200
3639 const t = ( time * settings . effectSpeed ) % interval
3740 if ( t < lastStrobe ) {
3841 strobeColor = hslToRgb ( Math . random ( ) * 360 , 1 , 0.5 )
3942 }
4043 lastStrobe = t
41- return t < 40 ? strobeColor : [ 0 , 0 , 0 ]
44+ const flash = settings . syncToMusic ? interval * 0.2 : 40
45+ return t < flash ? strobeColor : [ 0 , 0 , 0 ]
4246}
4347
4448export const getPulseColor : IColorGetter = ( _ , time ) => {
45- const cycle = 1000
49+ const cycle = settings . syncToMusic && audioState . bpm ? 60000 / audioState . bpm : 1000
4650 const t = ( time * settings . effectSpeed ) % cycle
4751 if ( t < lastPulse ) {
4852 pulseColor = hslToRgb ( Math . random ( ) * 360 , 1 , 0.5 )
@@ -86,3 +90,17 @@ let lastMultiPulse = 0
8690let prevHeartbeat : IArrColor = hslToRgb ( Math . random ( ) * 360 , 1 , 0.5 )
8791let nextHeartbeat : IArrColor = prevHeartbeat
8892let lastHeartbeat = 0
93+
94+ export function resetExtraPatterns ( ) {
95+ strobeColor = [ 255 , 255 , 255 ]
96+ lastStrobe = 0
97+ pulseColor = [ 255 , 0 , 0 ]
98+ lastPulse = 0
99+ multiPulseColors = Array ( 4 )
100+ . fill ( null )
101+ . map ( ( ) => hslToRgb ( Math . random ( ) * 360 , 1 , 0.5 ) )
102+ lastMultiPulse = 0
103+ prevHeartbeat = hslToRgb ( Math . random ( ) * 360 , 1 , 0.5 )
104+ nextHeartbeat = prevHeartbeat
105+ lastHeartbeat = 0
106+ }
0 commit comments