javascript - Setting image size percentage in canvas and jquery -
i'm trying create one-pager @ moment canvas can click add/place images @ random. i've gotten of want work, being extremely new jquery , canvas, still can't figure out how set max-size images. far i've understood, can quite hard make images on canvas work when haven't set canvas size manually in css, have resizing canvas, i'm not sure how around that.
i want images place have max-height/max-width of 80% of viewport. tried setting max size in css, didn't anything.
here's jsfiddle
$(document).ready(function () { var images = []; $("#siteload").fadein(1500); $('.put').each(function() { images.push($(this).attr('src')); }); (function() { var htmlcanvas = document.getelementbyid('c'), context = htmlcanvas.getcontext('2d'); initialize(); img = new image(); count = 0; htmlcanvas.onclick= function(evt) { img.src = images[count]; var x = evt.offsetx - img.width/2, y = evt.offsety - img.height/2; context.drawimage(img, x, y); count++; if (count == images.length) { count = 0; } } function initialize() { window.addeventlistener('resize', resizecanvas, false); resizecanvas(); } function resizecanvas() { htmlcanvas.width = window.innerwidth; htmlcanvas.height = window.innerheight; } })(); }); $(".hvr_cnt").mouseenter(function () { title = $("<div class='hvr_ttl'>" + $(this).children()[0].title + "</div>"); $(this).children()[0].title = ""; $(this).append(title); }); $(document).mousemove(function(e){ $('.hvr_ttl').css({ top: e.pagey - $(".hvr_ttl").height()/2, left: e.pagex - $(".hvr_ttl").width()/2 }); }); $(".hvr_cnt").mouseleave(function (e) { $(this).children()[0].title = $($(this).children()[1]).html(); $(this).children()[1].remove(); }); body { margin: 0; padding: 0; background: rgb(240, 240, 240); width: 100%; height: 100%; border: 0; color: rgb(40, 40, 40); overflow: hidden; display: block; } .slides { display: none } #c { display: block; position: absolute; left: 0px; top: 0px; } .hvr_ttl { display: block; position: absolute; pointer-events: none; cursor: none; } .hvr_cnt { cursor: none; padding: none; margin: none; } <body ontouchstart=""> <div class="hvr_cnt"> <canvas id="c" title="click"></canvas> </div> <ul class="slides"> <li><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/smiley.svg/2000px-smiley.svg.png" class="put" /></li> <li><img src="https://emojipedia-us.s3.amazonaws.com/thumbs/160/google/56/thumbs-up-sign_1f44d.png" class="put" /></li> </ul> </body> any appreciated :)
context.drawimage(img, 0, 0,img.width,img.height,0,0,htmlcanvas.width,htmlcanvas.height); you can use background-size:cover;
Comments
Post a Comment