import zhCN from './locale/zh_CN' ; import enUS from './locale/en_US' ; export type Scaler = ( from : HTMLCanvasElement , to : HTMLCanvasElement ) => Promise < any > ; export function debounce ( func , wait , immediate = false ) { let timeout ; return function debounceFunc ( ) { const context = this ; const args = arguments ; if ( args [ 0 ] && args [ 0 ] . persist ) { args [ 0 ] . persist ( ) ; } const later = ( ) => { timeout = null ; if ( ! immediate ) { func . apply ( context , args ) ; } } ; const callNow = immediate && ! timeout ; clearTimeout ( timeout ) ; timeout = setTimeout ( later , wait ) ; if ( callNow ) { func . apply ( context , args ) ; } } ; } function getTransformProperty ( node ) { const properties = [ 'transform' , 'WebkitTransform' , 'msTransform' , 'MozTransform' , 'OTransform' , ] ; let p = properties . shift ( ) ; while ( p ) { if ( typeof node . style [ p ] !== 'undefined' ) { return p ; } p = properties . shift ( ) ; } return '' ; } ; export function applyTransform ( element , transformString ) { const transformProperty = getTransformProperty ( element ) ; if ( transformProperty ) { element . style [ transformProperty ] = transformString ; } } export function downScaleImage ( img , scale , scaler = defaultDownScaler ) { const sourceCanvas = document . createElement ( 'canvas' ) ; sourceCanvas . width = img . width ; sourceCanvas . height = img . height ; const imgCtx = sourceCanvas . getContext ( '2d' ) ; if ( ! imgCtx ) { return Promise . reject ( 'canvas error' ) ; } imgCtx . drawImage ( img , 0 , 0 ) ; if ( scale >= 1 ) { return Promise . resolve ( sourceCanvas ) ; } const targetCanvas = document . createElement ( 'canvas' ) ; const tw = Math . floor ( img . width * scale ) ; const th = Math . floor ( img . height * scale ) ; targetCanvas . width = tw ; targetCanvas . height = th ; return scaler ( sourceCanvas , targetCanvas ) ; } function defaultDownScaler ( from , to ) { return new Promise ( resolve => { resolve ( downScaleCanvas ( from , to ) ) ; } ) ; } function downScaleCanvas ( cv , resCV ) { const scale = resCV . width / cv . width ; if ( ! ( scale < 1 ) || ! ( scale > 0 ) ) throw ( 'scale must be a positive number <1 ' ) ; var sqScale = scale * scale ; var sw = cv . width ; var sh = cv . height ; var tw = Math . floor ( sw * scale ) ; var th = Math . floor ( sh * scale ) ; var sx = 0 , sy = 0 , sIndex = 0 ; var tx = 0 , ty = 0 , yIndex = 0 , tIndex = 0 ; var tX = 0 , tY = 0 ; var w = 0 , nw = 0 , wx = 0 , nwx = 0 , wy = 0 , nwy = 0 ; var crossX = false ; var crossY = false ; var sBuffer = cv . getContext ( '2d' ) . getImageData ( 0 , 0 , sw , sh ) . data ; var tBuffer = new Float32Array ( 3 * tw * th ) ; var sR = 0 , sG = 0 , sB = 0 ; for ( sy = 0 ; sy < sh ; sy ++ ) { ty = sy * scale ; tY = 0 | ty ; yIndex = 3 * tY * tw ; crossY = ( tY != ( 0 | ty + scale ) ) ; if ( crossY ) { wy = ( tY + 1 - ty ) ; nwy = ( ty + scale - tY - 1 ) ; } for ( sx = 0 ; sx < sw ; sx ++ , sIndex += 4 ) { tx = sx * scale ; tX = 0 | tx ; tIndex = yIndex + tX * 3 ; crossX = ( tX != ( 0 | tx + scale ) ) ; if ( crossX ) { wx = ( tX + 1 - tx ) ; nwx = ( tx + scale - tX - 1 ) ; } sR = sBuffer [ sIndex ] ; sG = sBuffer [ sIndex + 1 ] ; sB = sBuffer [ sIndex + 2 ] ; if ( ! crossX && ! crossY ) { tBuffer [ tIndex ] += sR * sqScale ; tBuffer [ tIndex + 1 ] += sG * sqScale ; tBuffer [ tIndex + 2 ] += sB * sqScale ; } else if ( crossX && ! crossY ) { w = wx * scale ; tBuffer [ tIndex ] += sR * w ; tBuffer [ tIndex + 1 ] += sG * w ; tBuffer [ tIndex + 2 ] += sB * w ; nw = nwx * scale ; tBuffer [ tIndex + 3 ] += sR * nw ; tBuffer [ tIndex + 4 ] += sG * nw ; tBuffer [ tIndex + 5 ] += sB * nw ; } else if ( crossY && ! crossX ) { w = wy * scale ; tBuffer [ tIndex ] += sR * w ; tBuffer [ tIndex + 1 ] += sG * w ; tBuffer [ tIndex + 2 ] += sB * w ; nw = nwy * scale ; tBuffer [ tIndex + 3 * tw ] += sR * nw ; tBuffer [ tIndex + 3 * tw + 1 ] += sG * nw ; tBuffer [ tIndex + 3 * tw + 2 ] += sB * nw ; } else { w = wx * wy ; tBuffer [ tIndex ] += sR * w ; tBuffer [ tIndex + 1 ] += sG * w ; tBuffer [ tIndex + 2 ] += sB * w ; nw = nwx * wy ; tBuffer [ tIndex + 3 ] += sR * nw ; tBuffer [ tIndex + 4 ] += sG * nw ; tBuffer [ tIndex + 5 ] += sB * nw ; nw = wx * nwy ; tBuffer [ tIndex + 3 * tw ] += sR * nw ; tBuffer [ tIndex + 3 * tw + 1 ] += sG * nw ; tBuffer [ tIndex + 3 * tw + 2 ] += sB * nw ; nw = nwx * nwy ; tBuffer [ tIndex + 3 * tw + 3 ] += sR * nw ; tBuffer [ tIndex + 3 * tw + 4 ] += sG * nw ; tBuffer [ tIndex + 3 * tw + 5 ] += sB * nw ; } } } var resCtx = resCV . getContext ( '2d' ) ; var imgRes = resCtx . getImageData ( 0 , 0 , tw , th ) ; var tByteBuffer = imgRes . data ; var pxIndex = 0 ; for ( sIndex = 0 , tIndex = 0 ; pxIndex < tw * th ; sIndex += 3 , tIndex += 4 , pxIndex ++ ) { tByteBuffer [ tIndex ] = Math . ceil ( tBuffer [ sIndex ] ) ; tByteBuffer [ tIndex + 1 ] = Math . ceil ( tBuffer [ sIndex + 1 ] ) ; tByteBuffer [ tIndex + 2 ] = Math . ceil ( tBuffer [ sIndex + 2 ] ) ; tByteBuffer [ tIndex + 3 ] = 255 ; } resCtx . putImageData ( imgRes , 0 , 0 ) ; return resCV ; } export function getLocale ( text , locale ) { const dict = locale === 'en-US' ? enUS : zhCN ; if ( dict . hasOwnProperty ( text ) ) { return dict [ text ] ; } return text ; }