code: function imageToCanvas(img) {
      var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
      var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  
      if (!img.complete) {
        throw new Error('Can not create canvas from Image. Image is not loaded yet.');
      }
      var canvas = document.createElement("canvas");
      if (width === null && height === null) {
        canvas.width = img.naturalWidth;
        canvas.height = img.naturalHeight;
        canvas.getContext("2d").drawImage(img, 0, 0);
      } else {
        canvas.width = width;
        canvas.height = height;
        canvas.getContext("2d").drawImage(img, 0, 0, width, height);
      }
      return canvas;
    }
docs: SECTION:: create different sources from Image object