import storage from 'common/storage' ; export function getQuery ( ) { let search = window . location . search ; let query = Object . create ( null ) ; if ( ! search ) { return query ; } let reg = /[\?|&](\w+)=(\w+)/g ; let matches = null ; while ( ( matches = reg . exec ( search ) ) ) { query [ matches [ 1 ] ] = matches [ 2 ] ; } return query ; } export function isSingleModule ( m ) { let query = getQuery ( ) ; return query && query . module === m ; } export function windowToCanvas ( canvas , x , y ) { let box = canvas . getBoundingClientRect ( ) ; return { x : x - box . left * ( canvas . width / box . width ) , y : y - box . top * ( canvas . height / box . height ) } ; } export function random ( min , max ) { return Math . random ( ) * ( max - min ) + min ; } export function distance ( x1 , y1 , x2 , y2 ) { let x = Math . abs ( x1 - x2 ) ; let y = Math . abs ( y1 - y2 ) ; return Math . sqrt ( Math . pow ( x , 2 ) + Math . pow ( y , 2 ) ) ; } export function throttle ( fn , delay ) { let lastTime = Date . now ( ) ; let timer = null ; return function ( ... args ) { let currentTime = Date . now ( ) ; if ( currentTime - lastTime > delay ) { window . clearTimeout ( timer ) ; fn . apply ( null , args ) ; lastTime = currentTime ; } window . clearTimeout ( timer ) ; timer = setTimeout ( ( ) => { fn . apply ( null , args ) ; lastTime = currentTime ; } , delay ) ; } ; } export const isSupportPassive = ( function ( ) { const IS_SUPPORT_PASSIVE = 'IS_SUPPORT_PASSIVE' ; let isSupportPassive = storage . get ( IS_SUPPORT_PASSIVE ) ; if ( isSupportPassive != null ) { return isSupportPassive ; } try { let opts = Object . defineProperty ( { } , 'passive' , { get : function ( ) { isSupportPassive = true ; } , } ) ; window . addEventListener ( 'testPassive' , null , opts ) ; window . removeEventListener ( 'testPassive' , null , opts ) ; } catch ( e ) { } storage . set ( IS_SUPPORT_PASSIVE , isSupportPassive ) ; return isSupportPassive ; } ) ( ) ; export function RoundDecimal ( value , digital = 2 ) { digital = Math . max ( 0 , digital ) ; return Number ( value . toFixed ( digital ) ) ; } export function transToAngle ( radian ) { return ( 180 * radian ) / Math . PI ; } export function transToRadian ( angle ) { return ( Math . PI * angle ) / 180 ; }