Code Monkey home page Code Monkey logo

canvas-mock's Introduction

canvas-mock

I created this to be used when running Phaser.js in a Node.js environment. This can be used instead of node-canvas to simulate the existance of canvas.

Installation

Install it as any other node module:
npm install canvas-mock

Usage

The module appends the mock canvas functions to an element. This is a usage example with jsdom

var jsdom = require("jsdom").jsdom;
var window = jsdom().defaultView;
var canvasMockify = require('canvas-mock');

var Canvas = window.document.createElement('canvas');
canvasMockify(Canvas); // mock canvas functions required by Phaser.js are added

canvas-mock's People

Contributors

cristy94 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

koshilki

canvas-mock's Issues

Add new method to canvas context list

Please add the following method:

      measureText: function(text) {
        return {
          width:  12*text.length,
          height: 14
        };
      },

For this one, it's useful to return some kind of token value, in order to ensure the caller does not choke on the results.

More general way of adding getContext() to a mock canvas object.

The issue with the current way of doing it, is that the change is not global; i.e. the mock canvas object is reset during regular usage by any component. This makes it kind of useless for unit tests.

The following works for me, I hope you can adapt something similar:

var myHackedInCanvas;    // Use one global canvas instance for all calls to createElement('canvas');

function replaceCanvasContext (el) {
  // The previous export
}

/**
 * Overrides document.createElement(), in order to supply a custom canvas element.
 *
 * In the canvas element, getContext() is overridden in order to supply a simple 
 * mock object for the 2D context. For all other elements, the call functions unchanged.
 *
 * The override is only done if there is no 2D context already present.
 * This allows for normal running in a browser, and for node.js the usage of 'canvas'.
 *
 * @param {object} the current global window object. This can possibly come from module 'jsdom',
 *                 when running under node.js.
 */
function mockify(window) {
  var d = window.document;
  var f = window.document.createElement;

  // Check if 2D context already present. That happens either when running in a browser,
  // or this is node.js with 'canvas' installed. 
  var ctx = d.createElement('canvas').getContext('2d');
  if (ctx !== null && ctx !== undefined) {
    //console.log('2D context is present, no need to override');
    return;
  }

  window.document.createElement = function(param) {
    if (param === 'canvas') {
      if (myHackedInCanvas === undefined) {
        myHackedInCanvas = f.call(d, 'canvas');
        replaceCanvasContext(myHackedInCanvas);
      }
      return myHackedInCanvas;
    } else {
      return f.call(d, param);
    }
  };
}

module.exports = mockify;

This does change the object to pass in, so in a sense breaking. I personally do not care, but you may think different. Perhaps you know a better way.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.