import { clamp , wrap } from '../utils' ; import { namespace } from './constants' ; import { Data } from './models' ; import { getState } from './state' ; public param data export function getPlaybackState ( data ) { return getState ( data , 'playback' ) ; } public export interface PlaybackState { frameTime : number ; lastFrame : number ; lastLane : number ; handler : number ; } function updateLane ( data , lane ) { data . lane = data . wrapLane ? wrap ( lane , 0 , data . lanes - 1 , data . lanes ) : clamp ( lane , 0 , data . lanes - 1 ) ; } function updateAnimationFrame ( data ) { data . frame += ( data . reverse ? - 1 : 1 ) ; data . frame = wrap ( data . frame , 0 , data . frames - 1 , data . frames ) ; if ( ! data . loop && ( data . frame === data . stopFrame ) ) { stopAnimation ( data ) ; } } function updateInputFrame ( data , frame ) { data . frame = Number ( frame ) ; data . frame = data . wrap ? wrap ( data . frame , 0 , data . frames - 1 , data . frames ) : clamp ( data . frame , 0 , data . frames - 1 ) ; } function updateAnimation ( data ) { const state = getPlaybackState ( data ) ; if ( state . handler ) { updateBefore ( data ) ; updateAnimationFrame ( data ) ; updateAfter ( data ) ; } } function updateBefore ( data ) { const state = getPlaybackState ( data ) ; state . lastFrame = data . frame ; state . lastLane = data . lane ; } function updateAfter ( data ) { const state = getPlaybackState ( data ) ; if ( state . lastFrame !== data . frame || state . lastLane !== data . lane ) { data . target . trigger ( 'onFrameChanged.' + namespace , data ) ; } data . target . trigger ( 'onFrame.' + namespace , data ) ; data . target . trigger ( 'onDraw.' + namespace , data ) ; } public param data param frame param lane export function updateFrame ( data , frame ? , lane ? ) { updateBefore ( data ) ; if ( frame != null ) { updateInputFrame ( data , frame ) ; } if ( lane != null ) { updateLane ( data , lane ) ; } updateAfter ( data ) ; } public param data export function stopAnimation ( data ) { data . animate = false ; const state = getPlaybackState ( data ) ; if ( state . handler != null ) { window . clearInterval ( state . handler ) ; state . handler = null ; } } remarks public param data export function applyAnimation ( data ) { const state = getPlaybackState ( data ) ; if ( state . handler && ( ! data . animate || state . frameTime !== data . frameTime ) ) { stopAnimation ( data ) ; } if ( data . animate && ! state . handler ) { state . frameTime = data . frameTime ; state . handler = window . setInterval ( ( ) => updateAnimation ( data ) , state . frameTime ) ; } } remarks public param data export function startAnimation ( data ) { data . animate = true ; applyAnimation ( data ) ; }