允许html2canvas显示图像并下载图像

时间:2017-01-04 04:29:41

标签: javascript jquery css html2canvas

过去3天我一直试图找到这个问题的答案,似乎无法找到我的问题的答案。我的代码很长,请原谅我。我的代码很长的原因是因为我必须考虑所有的变量我问同样的问题并省略了一些代码,当我尝试将它集成到我的页面时它没有用。我想要的是我的狗图像当我下载它时显示在我的衬衫图像,但现在我得到一个空白下载。这是一个实时代码http://torcdesign.com/evol/

function fctCheck(gender) {
  var elems = document.getElementsByName("subselector");
  for (var i = 0; i < elems.length; i++) {
    elems.item(i).style.display = "none";
  }
  document.getElementById(gender).style.display = "block";
}
$('#men').on('change', function() {
  $("#mtsm").css('display', (this.value == 'tsm') ? 'block' : 'none');
});



$(document).ready(function() {
  $('.colore.active').each(function() {
    $('.container ').css('background-image', 'url(' + $(this).data("image") + ')');
  });
  $('.colore').on('click', function() {
    $('.container').css('src', '');
    $('.container ').css('background-image', 'url(' + $(this).data("image") + ')');
    $('.container7').css('background-image', 'url(' + $(this).data("image-back") + ')');
  });
  $('#toggler').click(function() {
    var tmp = $('.container').css('background-image');
    $('.container').css('background-image', $('.container7').css('background-image'));

    $('.container7').css('background-image', tmp);
  });
});localStorage.option1 = "wow cool option text here"
$(document).ready(function() {
  $('#imajes').change(function() {
    $('.subselector').hide();
    $('.smallimages').hide();
    $('#' + $(this).val()).show();
	
  });
  
    $('.smallimages').hide();
    var id = $(this).attr('id');
    var val = $(this).val();
	
	
$('#dog').on('change', function() {
  
  $("#bulldogimges").css('display', (this.value == 'bulldog') ? 'block' : 'none');



});

$('img').on('click', function() {
    $('#fotos').append('<div class="imgdrag"><img class="modal-content" src="' + $(this).attr('src')+ '"/></div>'); $('.imgdrag').draggable();
$('#fotos').droppable();
	           $('.modal-content').resizable();


$(".download").attr("href",$(this).attr('src'));
  $(".download").show();
});

  
  
});





var download = document.getElementById("download"),
		result = document.getElementById("previewImage");

function renderContent() {
    html2canvas(document.getElementById("firstshirt"), {
        allowTaint: true
    }).then(function(canvas) {
    		result.appendChild(canvas);
        download.style.display = "inline"; download.href = result.children[0].toDataURL();
    });
}

function downloadImage() {
		
}

document.getElementById("btn-Preview-Image").onclick = renderContent;
download.onclick = downloadImage
.container {background-color: transparent;
    width: 490px;
    height: 500px;
    border: 2px solid;
    position: relative;
overflow: hidden;

/* Will stretch to specified width/height */
  background-size: 490px 500px;
  background-repeat: no-repeat;

}.colore {
  float: left;
  width: 20px;
  height: 20px;
  margin: 5px;
  border: 1px solid rgba(0, 0, 0, .2);
}
.white {
  background: #FFFFFF;
}.black {
  background: #000000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" 
	src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>

<script src="https://rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.min.js"></script>
<script src="https://hongru.github.io/proj/canvas2image/canvas2image.js"></script>

<script  
 src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>
 <link rel="stylesheet"
href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
  <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
  <link rel="stylesheet"
 href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
 <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.min.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<link href='https://fonts.googleapis.com/css?family=Philosopher' rel='stylesheet' type='text/css'>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.js"></script>
<script src="https://files.codepedia.info/uploads/iScripts/html2canvas.js"></script>


<div id="firstshirt" class="container" style="float:left;">
<center><div id="wrapper"><div id="boxes" class="container5" style="float:center;">
 <div data-bind="foreach:items" class="fix_backround">
   <div class="item" data-bind="draggable:true,droppable:true">
        
      
      
</div></div>

<a href="" download><span id="image" class="preview-area"></span></a><div id="fotos" ><img class="modal-content" id="imgdisplay" /></div></div></div></center><br><br><br><br></div>
<input id="btn-Preview-Image" type="button" value="Preview"/><a id="download" download="my_image.png" href="#">Download image</a>

<select id="Gender" onchange="fctCheck(this.value);">
        <option value="">Choose Gender</option>
        <option value="men">Men</option>
   
    </select>  
   
    <select id="men" name="subselector" style="display:none">
      <option value="">Choose an item</option> 
      <option value="tsm">T-Shirt</option>
    
    
    </select>  
  
     
            
           <div style='display:none;' id="mtsm">
    <div class="colore white" data-image="https://torcdesign.com/shirts/white.jpg">
    </div>
    <div class="colore black active" data-image="https://torcdesign.com/shirts/black.jpg">
    </div>
   </div><p></p>
<select id="imajes">
        <option value="">Choose Image</option>
        <option value="new-upload">Upload Image</option>
        <option value="dog">Dog</option>
      
    </select> 

<select id="dog" name="subselector" class="subselector" style="display:none">
  <option value="">Choose an item</option>
  <option value="bulldog">Bulldog</option>
 
</select>






<div style='display:none;' id="bulldogimges" class="smallimages">
<div class="imgcontainerss" data-image="https://torcdesign.com/clipart/pT78gE6pc.gif">
    <img src="https://torcdesign.com/clipart/pT78gE6pc.gif" alt="Smiley face" width="55" height="55">
  </div>
<div class="imgcontainerss" data-image="https://torcdesign.com/clipart/LiKkRqkeT.gif">
    <img src="https://torcdesign.com/clipart/LiKkRqkeT.gif" alt="Smiley face" width="55" height="55">
  </div>
<div class="imgcontainerss" data-image="https://torcdesign.com/clipart/free-bulldog-clipart-031350.gif">
    <img src="https://torcdesign.com/clipart/free-bulldog-clipart-031350.gif" alt="Smiley face" width="55" height="55">
  </div></div>
<h3>result:</h3>
<div> <div id="previewImage">
    </div><a id="download" download="my_image.png" href="#">Download image</a></div>

1 个答案:

答案 0 :(得分:2)

您的下载点击没有做任何事情。

试试吧。

$('#download').click(function(){
   html2canvas($('#firstshirt'),{
    onrendered: function (canvas) {
         var a = document.createElement('a');
         a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
         a.download = 'test.jpg';
         a.click();
       }
    });
});