Search code examples
javascriptfabricjshtml2canvas

Saving a Canvas as Image Works as Expected but Image is Very Pixelated


I have been tinkering with what follows, and after scouring the web I'm turning up short for how to make the captured image clearer; in brief, the problem I have is that the downloaded image is very choppy.

var canvas = [],
  image;
var mainCanvas;
mainCanvas = new fabric.Canvas('c0');
for (i = 1; i <= 3; i++) {
  canvas[i] = new fabric.StaticCanvas('sc' + i);
}

function addText() {
  var text = new fabric.IText('Type here...', {
    fontSize: 27,
    top: 10,
    left: 10,
  });
  mainCanvas.add(text);
}

var rect = new fabric.Rect({
  fill: '#ff0000',
  width: 100,
  height: 100,
  id: 1
});
var circle = new fabric.Circle({
  fill: '#ffff00',
  radius: 50,
  left: 150,
  top: 150,
  originX: 'center',
  originY: 'center',
  id: 2
});

mainCanvas.on('object:added', onModified);
mainCanvas.on('object:modified', onModified);
mainCanvas.on('object:scaling', onModified);
mainCanvas.on('object:moving', onModified);
mainCanvas.add(rect, circle);

function onModified(option) {
  var ob = option.target;
  var index = mainCanvas.getObjects().indexOf(ob);
  ob.clone(function(obj) {
    for (i = 1; i <= 3; i++) {
      canvas[i].insertAt(obj, index, true);
    }
  });
};

$('#update').click(function() {
  updateCanvas();
});

function updateCanvas() {
  var json = JSON.stringify(mainCanvas);
  for (i = 1; i <= 3; i++) {
    canvas[i].loadFromJSON(json);
  }
}
// Toggling Images
function replaceImage(imgUrl) {
  if (!isImageLoaded) return; //return if initial image not loaded
  image.setSrc(imgUrl, function() {
    mainCanvas.renderAll();
    updateCanvas();
  },{ crossOrigin: 'anonymous' } );
}

// Default (Blank)
fabric.Image.fromURL('https://i.imgur.com/SamdNdX.png', function(img) {
  isImageLoaded = true;
  image = img.set({
    selectable: false,
    evented: false,
  });
  mainCanvas.add(image);
  mainCanvas.sendToBack(image);
  updateCanvas();
},{ crossOrigin: 'anonymous' });

$('#save').click(function() {
  html2canvas($('#imagesave'), {
    onrendered: function(canvas) {
      var a = document.createElement('a');
      // toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
      a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
      a.download = 'myfile.jpg';
      a.click();
    }
  })
});
html * {
  margin: 0px;
  padding: 0px;
}

body {
  margin: 0px;
  padding: 0px;
}

canvas {
  margin: 0px;
  display: block;
  padding: 0;
}

td,
tr {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  vertical-align: baseline;
}

#imagesave {
  background-color: white;
  height: 637.5px;
  width: 825px;
  padding-left: 75px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.0.0-rc.5/dist/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.js"></script>

<button onclick="addText();" class="dropdown-item">Add Text</button><button id="save">Save</button>
<button onclick="replaceImage('https://i.imgur.com/SamdNdX.png')">Blank</button>
<button onclick="replaceImage('https://i.imgur.com/TIINd6E.png')">Hands Pic</button>

<div id="imagesave">

  <table>
    <tr>
      <td>
        <canvas id="c0" width="187.5" height="636"></canvas>
      </td>
      <td>
        <canvas id="sc1" width="187.5" height="636"></canvas>
      </td>
      <td>
        <canvas id="sc2" width="187.5" height="636"></canvas>
      </td>
      <td>
        <canvas id="sc3" width="187.5" height="636"></canvas>
      </td>
    </tr>

  </table>

</div>

I have tried updating my various libraries without much luck. What can I do to sharpen the end result (downloaded image)? Or maybe another library that I should try using?


Solution

  • Here is one of the ways to achieve it. It is a simple canvas with a sample text, circle, rectangle, and an image and which you can export to a .png file, feel free to change the format to .jpeg, it will work as well.

    var mainCanvas = new fabric.Canvas('canvas');
    
    // Add a rectange
    var rect = new fabric.Rect({
      fill: '#ff0000',
      width: 100,
      height: 100,
      id: 1
    });
    
    mainCanvas.add(rect);
    
    // Add a circle
    var circle = new fabric.Circle({
      fill: '#ffff00',
      radius: 50,
      left: 150,
      top: 150,
      originX: 'center',
      originY: 'center',
      id: 2
    });
    
    mainCanvas.add(circle);
    
    // Add a text
    var text = new fabric.IText('Type here...', {
      fontSize: 27,
      top: 10,
      left: 10,
    });
    
    mainCanvas.add(text);
    
    // Add an image
    fabric.Image.fromURL('https://placekitten.com/200/303', function(img) {
      var image = img.set({
        selectable: false,
        evented: false,
        left: 230
      });
    
      mainCanvas.add(image);
      mainCanvas.sendToBack(image);
    
      mainCanvas.renderAll()
    }, { crossOrigin: 'anonymous' });
    
    // Handle click of the save button
        var saveButton = document.getElementById('save')
        saveButton.addEventListener('click', function (e) {
    
          this.href = mainCanvas.toDataURL({
              format: 'png'
          });
    
          this.download = 'canvas.png'
        }, false)
        body {
          font-family: sans-serif;
        }
        canvas {
          border: 2px solid #333;
          }
        #save {
          border: 1px solid #333;
          text-decoration: none;
          color: #fff;
          font-weight: bold;
          background-color: #333;
          padding: 10px;
          margin-top: 15px;
          display: inline-block;
        }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"></script>
    
    <div id="app">
      <canvas id="canvas" width="500" height="350"></canvas>
    
      <a href="#" id="save">Save canvas as image</a>
    </div>

    You can also check it in this Code Sandbox https://codesandbox.io/s/stackoverflow-60879984-fabricjs-362-export-canvas-as-image-k69er