background-image resized to draw on canvas

background-image resized to draw on canvas

I am trying to download a canvas image based on sketch.js and the solution I find is as below. I previously set up and resized a background image on the canvas like this on the SketchPad based on a selector. Then I load in the image from an input.

  1. $('#file-input').change(function (e) {
  2.         var file = e.target.files[0],
  3.             imageType = /image.*/;

  4.         if (file === undefined)
  5.             return;

  6.         if (!file.type.match(imageType))
  7.             return;

  8.         img = new Image();

  9.         img.onload = function () {
  10.             nimg_width = this.width;
  11.             nimg_height = this.height;

  12.             var reader = new FileReader();
  13.             reader.onload = fileOnload;
  14.             reader.readAsDataURL(file);
  15.         };

  16.         img.src = _URL.createObjectURL(file);
  17.             
  18.     });

  1. if ($("#imageSIZE").val() == "original") {
  2.             img_width = nimg_width;
  3.             img_height = nimg_height;
  4.             myCanvas.width = img_width;
  5.             myCanvas.height = img_height;
  6.             $("#SketchPad").css({
  7.                 'background-image': bgi,
  8.                 'background-size': img_width+"px "+img_height+"px"
  9.             });
  10.         }

Now I want to download my drawing on top of the background image, the background image is loaded in from an input file and resized so I am not sure how to retrieve it. I tried to use the method from here.
  1.         Sketch.prototype.download = function (format) {
  2.             var mime;
  3.             format || (format = "png");
  4.             if (format === "jpg") {
  5.                 format = "jpeg";
  6.             }
  7.             mime = "image/" + format;
  8.             
  9.             var myCanvas = document.createElement('canvas');
  10.             var ctx = myCanvas.getContext('2d');

  11.             myCanvas.width = $('#SketchPad')[0].width;
  12.             myCanvas.height = $('#SketchPad')[0].height;

  13.             var bg = $('#SketchPad').css('background-image');
  14.             var bf = $('#SketchPad').css('background-size');

  15.             bf = bf.replace('px', '').replace('px', '');
  16.             var ret = bf.split(" ");

  17.             bg = bg.replace('url(', '').replace(')', '').replace('"', '').replace('"', '');

  18.             var im = new Image();

  19.             im.onload=function()
  20.             {
  21.                 ctx.drawImage(im, 0, 0, ret[0], ret[1]);

  22.                 ctx.drawImage($('#SketchPad')[0], 0, 0);

  23.                 return window.open(myCanvas.toDataURL(mime));
  24.             }

  25.             im.src = bg;

  26.             
  27.         };
Please let me know how I can get the resized background-image and draw it on the canvas. Thanks.

P.S. Nevermind I got it, code modified, thanks for help!