javascript - How to make html div with text over image downloadable/savable for users? -
i have div takes user image , places user text on it. goal users to, after seeing preview , customizing image/text like, able download or save image click of button. possible? here's code: (i'm new html/css please forgive ugly formatting/methods)
html:
<script `src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>` <p>dom-rendered</p> <p> </p> <div id="imagewrap" class="wrap" style="border-style: solid;"> <img src="../images/environment.gif" id="img_prev" width="640" height="640" /> <h3 class="desc">something inspirational</h3> </div> <div id="canvaswrapper" class="outer"> <p>canvas-rendered (try right-click, save image as!)</p> <p>or, <a id="downloadlink" download="cat.png">click here download!</a> </div>
css:
.desc { text-align: center; } .outer, .wrap, .html2canvas, .image_text { display: inline-block; vertical-align: top; } .wrap { text-align: center; } #imagewrap { background-color: white; }
javascript:
window.onload = function() { html2canvas(document.getelementbyid("imagewrap"), { onrendered: function(canvas) { canvas.classname = "html2canvas"; document.getelementbyid("canvaswrapper").appendchild(canvas); var image = canvas.todataurl("image/png"); document.getelementbyid("downloadlink").href = image; }, usecors: true }); } function changepicture(image) { var @ = $(image).attr('at'); var newpath = '../images/' + at; $("#img_prev").attr('src', newpath); } function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#img_prev') .attr('src', e.target.result) .width(640) .height(640); }; reader.readasdataurl(input.files[0]); } }; $(document).on("click", '.font-names li a', function() { $("#imagewrap h3").css("font-family", $(this).parent().css("font-family")); $("#new_tile_font_style").val($(this).parent().css("font-family")); }); $(document).on("click", '.font-sizes li a', function() { $("#imagewrap h3").css("font-size", $(this).parent().css("font-size")); $("#new_tile_font_size").val($(this).parent().css("font-size") + "px"); }); $(document).on("click", '.font-colors li a', function() { $("#imagewrap h3").css("color", $(this).parent().css("color")); $("#new_tile_font_color").val($(this).parent().css("color")); }); $("#new_tile_quote").on('keyup', function() { var enteredtext = $("#new_tile_quote").val().replace(/\n/g, "<br>"); $("#imagewrap h3").html(enteredtext); });
what you're trying accomplish entirely possible using html, js, , css, no server-side code. here simplified demo uses html2canvas library render entire dom element canvas, user can download it.
be sure click "full page" on demo can see whole thing!
window.onload = function() { html2canvas(document.getelementbyid("imagewrap"), { onrendered: function(canvas) { canvas.classname = "html2canvas"; document.getelementbyid("canvaswrapper").appendchild(canvas); var image = canvas.todataurl("image/png"); document.getelementbyid("downloadlink").href = image; }, usecors: true }); }
.desc { text-align: center; } .outer, .wrap, .html2canvas, .image_text { display: inline-block; vertical-align: top; } .wrap { text-align: center; } #imagewrap { background-color: white; } #wow { color: red; display: block; transform: translate(0px, -12px) rotate(-10deg); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> <div class="outer"> <p>dom-rendered</p> <p> </p> <div id="imagewrap" class="wrap" style="border-style: solid;"> <img src="https://i.imgur.com/efm76qe.jpg?1" id="img_prev" width="170" /> <h3 class="desc">something <br /><span style="color: blue;">inspirational</span></h3> <span id="wow">wow!</span> </div> </div> <div id="canvaswrapper" class="outer"> <p>canvas-rendered (try right-click, save image as!)</p> <p>or, <a id="downloadlink" download="cat.png">click here download!</a> </div>
Comments
Post a Comment