'Overriding prototype method and calling the original method

I want to overrid the drawImage function of Html5 Canvas.Here is code.

var p = CanvasRenderingContext2D.prototype.drawImage;
CanvasRenderingContext2D.prototype.drawImage = function() {
    var len = arguments.length;
    var ig, sx, sy, swidth, sheight, x, y, width, height
    if (len === 3) {
        p(arguments[0], arguments[1] * 2, arguments[2] * 2, this);
    } else if (len === 5) {
        //Uncaught TypeError: Illegal invocation.
        p(arguments[0], arguments[1] * 2, arguments[2] * 2, arguments[3] * 2, arguments[4] * 2, this); 
    } else if (len === 9) {
        p(arguments[0], arguments[1] * 2, arguments[2] * 2, arguments[3] * 2, arguments[4] * 2, arguments[5] * 2, arguments[6] * 2, arguments[7] * 2, arguments[8] * 2, this);
    }
}

And call the function as the following.

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);

But It throw an error in the annotations line. After overrided prototype method,how to call the original method.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source