import type { IRange } from '@theatre/shared/utils/types' ; import { memoize } from 'lodash-es' ; const getFactorsOfNumber = memoize ( ( divisionsPerUnit ) : number [ ] => { const factors = [ ] ; for ( let i = 1 ; i <= divisionsPerUnit ; i ++ ) { if ( divisionsPerUnit % i === 0 ) { factors . push ( i ) ; } } return factors ; } ) ; remarks export default function createGrid ( { clippedSpaceRange , clippedSpaceWidth , fps , gapWidth = 120 , } , cb ) { const frameLengthInSeconeds = 1 / fps ; const clippedSpaceLengthInSeconds = clippedSpaceRange . end - clippedSpaceRange . start ; const frameWidthInScreenSpace = clippedSpaceWidth / ( fps * clippedSpaceLengthInSeconds ) ; const numberOfFramesFittingInMinimumCellWidth = Math . floor ( gapWidth / frameWidthInScreenSpace ) ; const numberOfFramesPerCell = numberOfFramesFittingInMinimumCellWidth < fps ? ( getFactorsOfNumber ( fps ) . find ( ( factor ) => factor >= numberOfFramesFittingInMinimumCellWidth ) as number ) : fps * Math . floor ( numberOfFramesFittingInMinimumCellWidth / fps ) ; const cellLengthInSeconds = numberOfFramesPerCell * frameLengthInSeconeds ; const startCell = Math . floor ( clippedSpaceRange . start / cellLengthInSeconds ) ; const endCell = Math . ceil ( clippedSpaceRange . end / cellLengthInSeconds ) ; for ( let cell = startCell ; cell <= endCell ; cell ++ ) { const posInUnitSpace = cell * cellLengthInSeconds ; const isFullSecond = posInUnitSpace % 1 === 0 ; cb ( posInUnitSpace , isFullSecond ) ; } }