Skip to content

Instantly share code, notes, and snippets.

@cgbystrom
Created February 16, 2015 19:28
Show Gist options
  • Select an option

  • Save cgbystrom/7e734d988664f1b89ff5 to your computer and use it in GitHub Desktop.

Select an option

Save cgbystrom/7e734d988664f1b89ff5 to your computer and use it in GitHub Desktop.

Revisions

  1. cgbystrom created this gist Feb 16, 2015.
    86 changes: 86 additions & 0 deletions canvas-logger.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    function hookCanvasGetContext () {
    var ctxFns = [
    'fillRect',
    'save',
    'restore',
    'scale',
    'rotate',
    'translate',
    'transform',
    'setTransform',
    'resetTransform',
    'createLinearGradient',
    'createRadialGradient',
    'createPattern',
    'clearRect',
    'strokeRect',
    'beginPath',
    'fill',
    'stroke',
    'drawFocusIfNeeded',
    'clip',
    'isPointInPath',
    'isPointInStroke',
    'fillText',
    'strokeText',
    'measureText',
    'drawImage',
    'createImageData',
    'getImageData',
    'putImageData',
    'getContextAttributes',
    'setLineDash',
    'getLineDash',
    'setAlpha',
    'setCompositeOperation',
    'setLineWidth',
    'setLineCap',
    'setLineJoin',
    'setMiterLimit',
    'clearShadow',
    'setStrokeColor',
    'setFillColor',
    'drawImageFromRect',
    'setShadow',
    'closePath',
    'moveTo',
    'lineTo',
    'quadraticCurveTo',
    'bezierCurveTo',
    'arcTo',
    'rect',
    'arc',
    'ellipse',
    'scrollPathIntoView',
    'addHitRegion',
    'removeHitRegion',
    'clearHitRegions',
    'isContextLost'
    ];

    var origGetContext = HTMLCanvasElement.prototype.getContext;
    var callsMade = [];

    function hookFunc (ctx, name) {
    var origFn = ctx[name];
    ctx[name] = function () {
    callsMade.push([name, arguments]);
    return origFn.apply(this, arguments);
    };
    }

    window.printCanvasCalls = function () {
    callsMade.forEach(function (c) {
    console.log(c[0], c[1]);
    })
    };

    HTMLCanvasElement.prototype.getContext = function () {
    var ctx = origGetContext.apply(this, arguments);
    ctxFns.forEach(function (fnName) {
    hookFunc(ctx, fnName);
    });

    return ctx;
    }
    }