简体   繁体   English

画布内容作为高分辨率图像。

[英]Canvas Content as High Resolution Image.

I am using KonvaJS in my project. 我在项目中使用KonvaJS I need to save the stage/canvas as an image( it would be great if exported image is high resolution image ). 我需要将stage/canvas另存为图像( 如果导出的图像是高分辨率图像,那会很棒 )。 KonvaJS has a function called stage.toDataURL() which returns canvas content as a Base64 string. KonvaJS具有一个名为stage.toDataURL()的函数,该函数以Base64字符串的形式返回画布内容。 The challenge with this function is that it only returns visible area of the canvas. 此功能的挑战在于它仅返回画布的可见区域。 I need to export entire canvas as image not only it's visible area. 我需要将整个画布导出为图像,不仅是可见区域。 I tried with Canvas2Image but it is also saving only visible area not full canvas. 我尝试使用Canvas2Image,但是它也只保存可见区域而不是整个画布。 Is there any way I can achieve it? 有什么办法可以实现? Here's a plunkr to play with some poc(proof of concept) code. 这是玩一些poc(概念验证)代码的傻瓜

var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
  container: 'container',
  width: width,
  height: height
});

var layer = new Konva.Layer();

var rectX = stage.getWidth() / 2 - 50;
var rectY = stage.getHeight() / 2 - 25;

(function() {
  for (var i = 0; i < 50; i++) {
    layer.add(new Konva.Rect({
      x: i * 50,
      y: 0,
      width: 50,
      height: 10000,
      fill: Konva.Util.getRandomColor()
    }));
  }
})();

var box = new Konva.Rect({
  x: rectX,
  y: rectY,
  width: 100,
  height: 50,
  fill: '#00D2FF',
  stroke: 'black',
  strokeWidth: 4,
  draggable: true
});

box.on('mouseover', function() {
  document.body.style.cursor = 'pointer';
});

box.on('mouseout', function() {
  document.body.style.cursor = 'default';
});

layer.add(box);
layer.draw();
stage.add(layer);

function exportCanvas() {
  var dataURL = stage.toDataURL();
  window.open(dataURL);
}

document.getElementById('save').addEventListener('click', exportCanvas, false);

The best way to achieve this is to resize stage before export, then resize it back: 实现此目的的最佳方法是在导出之前调整舞台大小,然后重新调整大小:

var oldSize = stage.size();

var exportSize = {
    width: 3000,
    height: 3000
};
stage.size(exportSize);
stage.draw();
var dataURL = stage.toDataURL(exportSize);
stage.size(oldSize);
stage.draw();

Demo: http://plnkr.co/edit/DiqsE7rZdDx3JIAyJZQK?p=preview 演示: http//plnkr.co/edit/DiqsE7rZdDx3JIAyJZQK?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM