import Random from './vendor/random' ; import { TweenLite , Linear , Ease , TweenConfig } from 'gsap' ; /**
 * The sole argument object to the getMaybeRandomNumber function.
 * probability - The percent chance for choosing a random value.
 * A value of 1 will always create a random value, a value of 0.5 will
 * create a random value 50% of the time, value of 0.25 will be 25%, etc.
 * normalValue - The value returned when a random value is NOT chosen.
 * [minValue = 0] - The minimum random value that can be generated.
 * [maxValue = 1] - The maximum random value that can be generated.
 */ export interface MaybeRandomNumberParams { probability : number ; normalValue : number ; minValue ? : number ; maxValue ? : number ; } export interface MaybeRandomTweenParams { target : object | object [ ] ; propName : string ; duration : number ; ease ? : Ease ; delay ? : number ; start : MaybeRandomNumberParams ; end : MaybeRandomNumberParams ; onUpdate ? ( randomValue ) : void ; } param args returns example example export function getMaybeRandomNumber ( { probability , normalValue , minValue = 0 , maxValue = 1 } ) { if ( probability > 0 ) { const randomNumber = Random . real ( 0 , 1 , true ) ( Random . engines . browserCrypto ) ; if ( randomNumber <= probability ) { return Random . real ( minValue , maxValue , true ) ( Random . engines . browserCrypto ) ; } } return normalValue ; } param target param propName param duration param ease param delay param start param end param onUpdate returns example export function createMaybeRandomTween ( { target , propName , duration , ease = Linear . easeNone , delay = 0 , start , end , onUpdate } ) { const proxy = { ... start } ; const tweenProps = { ease , delay , ... end } as Partial < TweenConfig > ; if ( Array . isArray ( target ) ) { tweenProps . onUpdate = ( ) => { const randomValue = getMaybeRandomNumber ( proxy ) ; target . forEach ( childTarget => { ( childTarget as any ) [ propName ] = randomValue ; } ) ; if ( onUpdate ) { onUpdate ( randomValue ) ; } } ; } else { tweenProps . onUpdate = ( ) => { const randomValue = getMaybeRandomNumber ( proxy ) ; ( target as any ) [ propName ] = randomValue ; if ( onUpdate ) { onUpdate ( randomValue ) ; } } ; } return TweenLite . to ( proxy , duration , tweenProps ) ; }