import { createOffscreenContext } from '../utils/context.utils' ; import { DRAWING_FUNCTIONS } from '../globals/drawing-functions.global' ; import { PROTECTED_KEYS } from '../globals/protected-keys.global' ; import { applyFilter } from '../utils/filter.utils' ; export function applyMethodPatches ( context ) { Object . keys ( context . prototype ) . filter ( member => PROTECTED_KEYS . indexOf ( member ) < 0 ) . map ( member => ( { member , descriptor : Object . getOwnPropertyDescriptor ( context . prototype , member ) } ) ) . filter ( ( { descriptor } ) => descriptor . value && typeof descriptor . value === 'function' ) . forEach ( ( { member , descriptor } ) => { const original = descriptor . value ; Object . defineProperty ( context . prototype , member , { value : function ( ... args ) { if ( this . canvas . __skipFilterPatch ) { return original . call ( this , ... args ) ; } if ( ! this . canvas . __currentPathMirror ) { this . canvas . __currentPathMirror = createOffscreenContext ( this ) ; } const result = this . canvas . __currentPathMirror [ member ] ( ... args ) ; if ( DRAWING_FUNCTIONS . indexOf ( member ) > - 1 ) { applyFilter ( this . canvas . __currentPathMirror , this . filter ) ; this . canvas . __skipFilterPatch = true ; let originalTransform ; if ( 'getTransform' in this ) { originalTransform = this . getTransform ( ) ; this . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ; } this . drawImage ( this . canvas . __currentPathMirror . canvas , 0 , 0 ) ; if ( originalTransform ) { this . setTransform ( originalTransform ) ; } this . canvas . __skipFilterPatch = false ; this . canvas . __currentPathMirror = createOffscreenContext ( this ) ; } return result ; } } ) ; } ) ; }