简体   繁体   中英

allow html2canvas to display images and download with images

i have been trying to get an answer for this question for the past 3 days and cant seem to find an answer for my question. my code is very long please forgive me. the reason why my code is long is because i have to take all the variables in consideration i asked the same question and left out some code and when i tried integrating it into my page it did not work.what i want is for my dog image to show in my shirt image when i download it but right now i get a blank download. here is a live code 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> 

You have not done anything on your download click.

Just try this.

$('#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();
       }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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