javascript - html2canvas 不在子 div 中显示图像

标签 javascript jquery html css html2canvas

我正在尝试使用 html2canvas 从 div 创建图像

简介:

  • 我有一张带有透明区域的图片 (png)
  • 我有另一张图片(可以是 jpg 或 png)将被拖动/调整大小 为了在上图的透明区域内看起来不错,使用 辅助分区
  • 我有一个 helper div,它将具有最高的 z-index 和可拖动 并且可以调整大小

用户满意后,他可以点击“完成编辑”来创建一个 Canvas ,最终结果显示所有图像

<div id="container">
  <div id="artwork">
    <img src="http://preview.ibb.co/gsvuPR/photo1.png" alt="photo1" border="0">
  </div>
  <div id="img">
    <img src="http://puckerupbuttercup.files.wordpress.com/2016/02/0092-happy-alone.jpg"/>
  </div>
  <div id = "dragger">
  </div>
</div>
  <a href="#d" id="done">
  done editing
  </a>


body {
  padding: 0;
  margin: 0
}
#container {
  background: #ccc;
  height: 400px;
  width: 600px
}
#artwork {
  width: 100%;
  z-index: 2;
  position: absolute;
}

#artwork img {
  height: 400px;
  width: 600px
}

#img {
  position: absolute;
  top: 0;
  left: 0;
  width:100%;
  height: 100%;
  z-index: 1;
}
#img img {
  position: relative
}
#dragger {
  border: dashed 3px grey;
  z-index: 3;
  cursor: all-scroll
}
#done {
  position: absolute;
  right: 0;
  top: 0;
  z-index: 444;
  padding; 5px;
  background: yellow
}


$("#img img").css("max-width",$("#artwork img").width());
$("#img img").css("max-height",$("#artwork img").height());
$("#dragger").css("max-width",$("#artwork img").width()-5);
$("#dragger").css("max-height",$("#artwork img").height()-5);
var img_h = $("#img img").height()-5;
var img_w = $("#img img").width()-5;

var right = $("#artwork img").width()-img_w-5;
var bottom = $("#artwork img").height()-img_h-5;

$("#dragger").width(img_w);
$("#dragger").height(img_h);

$("#dragger").draggable({
  axis: 'xy', 
  containment : [0,0,right,bottom],
  drag: function( event, ui ) {
        $("#img img").css('top', ui.offset.top +'px');
    $("#img img").css('left', ui.offset.left +'px');
  }
});

$("#dragger").resizable({
    containment: "parent"
});

$( "#dragger" ).on( "resize", function( event, ui ) {
    $("#img img").css('width', ui.size.width +5+'px');
  $("#img img").css('height', ui.size.height +5+'px');
  var img_h = $("#img img").height()-5;
    var img_w = $("#img img").width()-5;
  var right = $("#artwork img").width()-img_w-5;
    var bottom = $("#artwork img").height()-img_h-5;
  $("#dragger").draggable({
  axis: 'xy', 
  containment : [0,0,right,bottom],
  drag: function( event, ui ) {
        $("#img img").css('top', ui.offset.top +'px');
    $("#img img").css('left', ui.offset.left +'px');
  }
});
});

$("#done").on("click",function(){
 $("#dragger").toggle();
 html2canvas($("#container"), {
   allowTaint: true,
   logging: true,
   onrendered: function (canvas) {
    document.body.appendChild(canvas);
   }
 });
});

所有 javascript 已准备就绪 在调整大小/拖动方面一切都很好,但 html2canvas 没有做好在 Canvas 中显示图像供用户保存的工作

这是一个 fiddle https://jsfiddle.net/p3vzgbzo/5/

我已经在本地尝试过此代码,但没有成功将图像放在页面的相同路径上

我也尝试了 DataToURL,但没有返回任何图像

最终,如果可能的话,我希望渲染图像也能上传到服务器 我在想图像需要转换为基本代码吗?

谢谢

最佳答案

要么 #container 中的图像必须与页面来自同一来源,要么必须通过 base64 嵌入它们。

Working JSFiddle with base64 images

关于javascript - html2canvas 不在子 div 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48609572/

相关文章:

javascript - 如何在 ReactJS 项目中使用 ffmpeg.js

javascript - 是否可以修改Set中的值

javascript - 当鼠标进入div时停止jQuery动画

javascript - 自动登录用户到外部网站?

html - 文章底部的文字

javascript - 在 React 中,子组件如何调用在其父组件上下文中执行的函数?

javascript - 为什么切换图像的url src需要时间到 "load"?

javascript - JQuery悬停在下拉菜单上闪烁

jquery选择器无法从隐藏字段读取

php - Image Carousel 后端 PHP Mysql