在画布上保存图像的效果符合预期,但图像像素化程度很高

问题描述 投票:0回答:1

我一直在修补后面的内容,在搜寻网络之后,我想尽办法使捕获的图像更清晰;简而言之,我的问题是下载的图像非常不稳定。

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/[email protected]/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>

我很幸运地尝试更新我的各种库。如何提高最终效果(下载的图像)?还是我应该尝试使用的另一个库?

javascript fabricjs html2canvas
1个回答
0
投票

这里是实现它的方法之一。它是带有示例文本,圆形,矩形和图像的简单画布,您可以将其导出为.png文件,可以随时将格式更改为.jpeg,也可以使用。

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>

您也可以在此代码沙箱https://codesandbox.io/s/stackoverflow-60879984-fabricjs-362-export-canvas-as-image-k69er中检查它>

© www.soinside.com 2019 - 2024. All rights reserved.