Can't get overlay to resize fabric.js?












0















I've been able to get the canvas to resize, and also include touch events. But for some reason, I can't get the overlay image to fill the canvas.



I'm using the recommended code from fabric.js



canvas.setOverlayImage('http://fabricjs.com/assets/jail_cell_bars.png', canvas.renderAll.bind(canvas), {
width: width,
height: height,
// Needed to position overlayImage at 0/0
originX: 'left',
originY: 'top'
});


My Fiddle has a fair bit of code, as I'm not sure if it's the resizer or touch events that might be causing this issue.










share|improve this question



























    0















    I've been able to get the canvas to resize, and also include touch events. But for some reason, I can't get the overlay image to fill the canvas.



    I'm using the recommended code from fabric.js



    canvas.setOverlayImage('http://fabricjs.com/assets/jail_cell_bars.png', canvas.renderAll.bind(canvas), {
    width: width,
    height: height,
    // Needed to position overlayImage at 0/0
    originX: 'left',
    originY: 'top'
    });


    My Fiddle has a fair bit of code, as I'm not sure if it's the resizer or touch events that might be causing this issue.










    share|improve this question

























      0












      0








      0








      I've been able to get the canvas to resize, and also include touch events. But for some reason, I can't get the overlay image to fill the canvas.



      I'm using the recommended code from fabric.js



      canvas.setOverlayImage('http://fabricjs.com/assets/jail_cell_bars.png', canvas.renderAll.bind(canvas), {
      width: width,
      height: height,
      // Needed to position overlayImage at 0/0
      originX: 'left',
      originY: 'top'
      });


      My Fiddle has a fair bit of code, as I'm not sure if it's the resizer or touch events that might be causing this issue.










      share|improve this question














      I've been able to get the canvas to resize, and also include touch events. But for some reason, I can't get the overlay image to fill the canvas.



      I'm using the recommended code from fabric.js



      canvas.setOverlayImage('http://fabricjs.com/assets/jail_cell_bars.png', canvas.renderAll.bind(canvas), {
      width: width,
      height: height,
      // Needed to position overlayImage at 0/0
      originX: 'left',
      originY: 'top'
      });


      My Fiddle has a fair bit of code, as I'm not sure if it's the resizer or touch events that might be causing this issue.







      javascript html canvas fabricjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 6:12









      LarmLarm

      817




      817
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The problem in your fiddle stems from the fact that in Fabric.js, setting width and height don't scale an image to the given values but rather set the dimensions of the fabric.Image container. What you need to do is call canvas.overlayImage.scaleToWidth(width) (or .scaleToHeight(height)) in the setOverlayImage()'s callback, so that the image would actually be scaled to cover the whole canvas:






          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>





          (I had to re-upload the jail_cell_bars.png image elsewhere because the original hosting was not cooperating)






          share|improve this answer



















          • 1





            Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

            – Larm
            Nov 28 '18 at 23:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513188%2fcant-get-overlay-to-resize-fabric-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The problem in your fiddle stems from the fact that in Fabric.js, setting width and height don't scale an image to the given values but rather set the dimensions of the fabric.Image container. What you need to do is call canvas.overlayImage.scaleToWidth(width) (or .scaleToHeight(height)) in the setOverlayImage()'s callback, so that the image would actually be scaled to cover the whole canvas:






          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>





          (I had to re-upload the jail_cell_bars.png image elsewhere because the original hosting was not cooperating)






          share|improve this answer



















          • 1





            Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

            – Larm
            Nov 28 '18 at 23:52
















          1














          The problem in your fiddle stems from the fact that in Fabric.js, setting width and height don't scale an image to the given values but rather set the dimensions of the fabric.Image container. What you need to do is call canvas.overlayImage.scaleToWidth(width) (or .scaleToHeight(height)) in the setOverlayImage()'s callback, so that the image would actually be scaled to cover the whole canvas:






          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>





          (I had to re-upload the jail_cell_bars.png image elsewhere because the original hosting was not cooperating)






          share|improve this answer



















          • 1





            Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

            – Larm
            Nov 28 '18 at 23:52














          1












          1








          1







          The problem in your fiddle stems from the fact that in Fabric.js, setting width and height don't scale an image to the given values but rather set the dimensions of the fabric.Image container. What you need to do is call canvas.overlayImage.scaleToWidth(width) (or .scaleToHeight(height)) in the setOverlayImage()'s callback, so that the image would actually be scaled to cover the whole canvas:






          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>





          (I had to re-upload the jail_cell_bars.png image elsewhere because the original hosting was not cooperating)






          share|improve this answer













          The problem in your fiddle stems from the fact that in Fabric.js, setting width and height don't scale an image to the given values but rather set the dimensions of the fabric.Image container. What you need to do is call canvas.overlayImage.scaleToWidth(width) (or .scaleToHeight(height)) in the setOverlayImage()'s callback, so that the image would actually be scaled to cover the whole canvas:






          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>





          (I had to re-upload the jail_cell_bars.png image elsewhere because the original hosting was not cooperating)






          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>





          //Image Edit
          var canvas = new fabric.Canvas('c', {
          preserveObjectStacking: true
          });

          canvas.setWidth(window.innerWidth);
          canvas.setHeight(window.innerWidth);
          fabric.Image.fromURL("https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg", function(img) {
          img.scale(0.5).set({
          left: 150,
          top: 150,
          angle: 0
          });
          canvas.add(img).setActiveObject(img);
          });

          var info = document.getElementById('info');




          function resize() {
          var canvasSizer = document.getElementById("imageEditor");
          var canvasScaleFactor = canvasSizer.offsetWidth / 525;
          var width = canvasSizer.offsetWidth;
          var height = canvasSizer.offsetHeight;
          var ratio = canvas.getWidth() / canvas.getHeight();
          if ((width / height) > ratio) {
          width = height * ratio;
          } else {
          height = width / ratio;
          }
          var scale = width / canvas.getWidth();
          var zoom = canvas.getZoom();
          zoom *= scale;
          canvas.setDimensions({
          width: width,
          height: height
          });
          canvas.setViewportTransform([zoom, 0, 0, zoom, 0, 0])
          canvas.setOverlayImage('https://i.imgur.com/1CURvP5.png', function() {
          canvas.overlayImage && canvas.overlayImage.scaleToWidth(width)
          canvas.renderAll()
          }, {
          // Needed to position overlayImage at 0/0
          originX: 'left',
          originY: 'top'
          });
          }

          window.addEventListener('load', resize, false);
          window.addEventListener('resize', resize, false);

          var textObj = new fabric.IText("Test Text", {
          fontSize: 22,
          top: 362.5,
          left: 262.5,
          hasControls: true,
          fontWeight: 'bold',
          fontFamily: 'Montserrat',
          fontStyle: 'normal',
          centeredrotation: true,
          originX: 'center',
          originY: 'center'
          });
          canvas.add(textObj);
          canvas.renderAll();

          canvas.on({
          'touch:gesture': function() {
          var text = document.createTextNode(' Gesture ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:drag': function() {
          var text = document.createTextNode(' Dragging ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:orientation': function() {
          var text = document.createTextNode(' Orientation ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:shake': function() {
          var text = document.createTextNode(' Shaking ');
          info.insertBefore(text, info.firstChild);
          },
          'touch:longpress': function() {
          var text = document.createTextNode(' Longpress ');
          info.insertBefore(text, info.firstChild);
          }
          });

          <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.4/fabric.js"></script>


          <div id="imageEditor">
          <div class="canvas-container">
          <canvas id="c"></canvas>
          </div>
          </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 22:55









          shkapershkaper

          1,3511915




          1,3511915








          • 1





            Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

            – Larm
            Nov 28 '18 at 23:52














          • 1





            Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

            – Larm
            Nov 28 '18 at 23:52








          1




          1





          Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

          – Larm
          Nov 28 '18 at 23:52





          Amazing thanks @shkaper! One step closer to my final project! Now I just need to smooth out the undo, redo feature. Will need to find some sort of layer lock.

          – Larm
          Nov 28 '18 at 23:52




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513188%2fcant-get-overlay-to-resize-fabric-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks