import { SKLayer , SKFrame } from '../types' ; const calculateMaskIntersection = ( parentMask , childMask ) => { const maskFrame = { ... childMask } ; const defaultFrame = { _class : 'rect' , x : 0 , y : 0 , width : 0 , height : 0 } ; if ( maskFrame . x + maskFrame . width <= parentMask . x ) { return defaultFrame ; } if ( maskFrame . y + maskFrame . height <= parentMask . y ) { return defaultFrame ; } if ( maskFrame . x >= parentMask . x + parentMask . width ) { return defaultFrame ; } if ( maskFrame . y >= parentMask . y + parentMask . height ) { return defaultFrame ; } if ( maskFrame . x < parentMask . x ) { maskFrame . width = maskFrame . x + maskFrame . width - parentMask . x ; maskFrame . x = parentMask . x ; } if ( maskFrame . y < parentMask . y ) { maskFrame . height = maskFrame . y + maskFrame . height - parentMask . y ; maskFrame . y = parentMask . y ; } if ( maskFrame . x + maskFrame . width > parentMask . x + parentMask . width ) { maskFrame . width = parentMask . x + parentMask . width - maskFrame . x ; } if ( maskFrame . y + maskFrame . height > parentMask . y + parentMask . height ) { maskFrame . height = parentMask . y + parentMask . height - maskFrame . y ; } return maskFrame ; } ; type MaskOptions = { flag : boolean ; frame : SKFrame ; baseFrame : SKFrame ; } ; param layers param param1 const _mask = ( layers , { flag = false , frame , baseFrame } ) : SKLayer [ ] => { let currFrame ; let maskFlag = false ; const defaultFrame = { ... frame } ; for ( let i = 0 ; i < layers . length ; i ++ ) { if ( layers [ i ] . shouldBreakMaskChain ) { maskFlag = false ; frame = { ... defaultFrame } ; } if ( flag || maskFlag ) { if ( flag && maskFlag ) { } else if ( maskFlag ) { frame = { ... currFrame } ; } flag = true ; if ( layers [ i ] . _class === 'slice' ) { layers [ i ] . frame = calculateMaskIntersection ( baseFrame , layers [ i ] . frame ) ; } else { layers [ i ] . frame = calculateMaskIntersection ( frame , layers [ i ] . frame ) ; } } if ( layers [ i ] . hasClippingMask ) { maskFlag = true ; flag = true ; currFrame = { ... layers [ i ] . frame } ; frame = calculateMaskIntersection ( defaultFrame , layers [ i ] . frame ) ; } if ( Array . isArray ( layers [ i ] . layers ) ) { layers [ i ] . layers = _mask ( layers [ i ] . layers , { flag , frame , baseFrame } ) ; } } layers = layers . filter ( ( layer ) : boolean => { if ( layer . frame . width === 0 || layer . frame . height === 0 ) { return false ; } return true ; } ) ; return layers ; } ; export default ( layers ) : SKLayer [ ] => { return _mask ( layers , { flag : true , frame : layers [ 0 ] . frame , baseFrame : layers [ 0 ] . frame } ) ; } ;