【问题标题】:Background image not rendered on downloaded HTML5 canvas PNG背景图像未在下载的 HTML5 画布 PNG 上呈现
【发布时间】:2018-07-30 15:45:10
【问题描述】:

我有 3 个输入背景图像,它们会在点击时更改并填充画布区域。我通过文件输入上传其他图像,将其放置在 bg 图像的顶部。当我将画布保存为 PNG 时,所有图像(背景图像除外)都会被渲染。我希望画布上的所有图像都渲染并保存为 PNG。

对此的任何见解将不胜感激:-)

jsfiddle

javascript:

// Buttons onclick change background image of canvas
$(document).ready(function() {
  $("#canvascolor > input").click(function() {
    var img = $(this).attr('src');
    $('#myCanvas').css("background-image", "url(" + img + ")");
  });
});
// END Buttons onclick background image of canvas

var canvas = new fabric.Canvas('myCanvas', {
  backgroundColor: 'transparent'
});

function renderImage(e) {
  var imgUrl = URL.createObjectURL(e.target.files[0]);
  fabric.Image.fromURL(imgUrl, function(img) {
    // set default props
    img.set({
      width: 150,
      height: 150,
      top: 75,
      left: 75,
      transparentCorners: false
    });
    canvas.add(img);
    canvas.renderAll();
  });
}

function saveOnPC() {
  var link = document.createElement('a');
  link.href = canvas.toDataURL();
  link.download = 'myImage.png';
  link.click();
}

function click(el) {
  // Simulate click on the element.
  var evt = document.createEvent('Event');
  evt.initEvent('click', true, true);
  el.dispatchEvent(evt);
}

document.querySelector('#fileSelect').addEventListener('click', function(e) {
  var fileInput = document.querySelector('#fileElem');
  //click(fileInput); // Simulate the click with a custom event.
  fileInput.click(); // Or, use the native click() of the file input.
}, false);

$('#remove').click(function() {
  var object = canvas.getActiveObject();
  if (!object) {
    alert('Please select the element to remove');
    return '';
  }
  canvas.remove(object);
});

HTML:

<canvas id="myCanvas" width="400" height="800"></canvas>
<section id="canvascolor">
   <input id="bostonblue" class="canvasborder" type="image" src="https://s26.postimg.org/i3ibods55/blue.jpg">
   <input id="bostonblack" class="canvasborder" type="image" src="https://s26.postimg.org/vlp803yzd/black.jpg">
   <input id="bostonamber" class="canvasborder" type="image" src="https://s26.postimg.org/o5pyeao4p/amber.jpg">
 </section>

【问题讨论】:

    标签: jquery canvas fabricjs


    【解决方案1】:

    不是通过css设置背景图片,而是使用canvas.setBackgroundImage()设置canvas的背景图片

    $("#canvascolor > input").click(function () {
        var img = $(this).attr('src');
        canvas.setBackgroundImage(img,canvas.renderAll.bind(canvas));
    });
    

    【讨论】:

    • 是的,该功能在不同版本中运行良好,但无论我将代码放在哪里,我都无法将画布另存为图像。我这个画布的另一个版本使用文件上传(用户输入)来添加背景图像,我想将自托管的背景图像更改为画布 onclick。您知道如何使此代码适用于图像输入(其中 3 个)而不是文件输入吗?我有代码和小提琴。 jsfiddle.net/carolynrue/q7Z9k/1340/
    • 嗨,小提琴奏效了——它做了什么要求它:选择一个静态背景图像,背景图像出现在我的画布上点击图像输入,但只有用户上传——我有我希望用户从中选择的服务器上的背景图像。那是我的问题
    • @crue 检查您是否遇到了 cors 问题,同时执行canvas.toDataURL()
    • 感谢您的建议。我正在使用 PostImage 来托管我的图像。如何检查 CORS?
    • 我检查了 cors:XHR 状态:200 XHR 状态文本:OK
    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多