canvas의 width와 height 의 setter를 wrap 처리 하는 방법

    wrapMethods(){
        let descWidth = Object.getOwnPropertyDescriptor(HTMLCanvasElement.prototype,'width')
        let newDescWidth = {
            configurable: descWidth.configurable, enumerable: descWidth.enumerable,
            get:descWidth.get,
            set:(v)=>{ descWidth.set.apply(this,[v]); this.flush(); },
        }
        Object.defineProperty(this,'width',newDescWidth)
        let descHeight = Object.getOwnPropertyDescriptor(HTMLCanvasElement.prototype,'height')
        let newDescHeight = {
            configurable: descHeight.configurable, enumerable: descHeight.enumerable,
            get:descHeight.get,
            set:(v)=>{ descHeight.set.apply(this,[v]); this.flush(); },
        }
        Object.defineProperty(this,'height',newDescHeight)
       
    }


그냥 setter, getter 에 선언해도 되긴 된다.
성능 이슈가 있을 것 같지만...

    get width(){      
        const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this.constructor).prototype,'width');
        return desc.get.apply(this);
    }
    /**
     * @param {number} v
     */
    set width(v){
        const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this.constructor).prototype,'width');
        desc.set.apply(this,[v]);
        this.flush();
    }

    get height(){      
        const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this.constructor).prototype,'height');
        return desc.get.apply(this);
    }
    /**
     * @param {number} v
     */
    set height(v){
        const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this.constructor).prototype,'height');
        desc.set.apply(this,[v]);
        this.flush();
    }



댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📅 달력형