html - Jquery crop using canvas not working -
hi given below code. 1 html file. trying crop image box. when draw canvas resolutions coming wrong. please help.
i need cut portion of image box lies.
<html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <style> body, html { height: 100%; margin: 0; padding: 0; width: 100%; } #container { width: 100%; height: 100%; position: fixed; } .cover-container { display: table; height: 315px; margin: 5% auto 0; width: 90%; position: relative; } #camera { height: 100%; position: fixed; width: 100%; } #camera2 { height: 100%; position: fixed; width: 100%; } #cover { display: block; height: 315px; left: 0; /* position: absolute; */ top: 0; width: 100%; } #capturebtn { z-index: 9999; width: 100%; height: auto; } #capturebtn input { width: 100px; height: 50px; margin: 0 auto; right: 0; bottom: 15px; left: 0; position: fixed; } @media(max-width:960px) , (min-width: 768px) { #cover { display: block; height: 241px !important; left: 0; position: absolute; top: 0; width: 650px !important; } .cover-container { display: table; height: 241px; margin: 5% auto 0; width: 650px; position: relative; } } #capture { left: -100px !important; } #reset { left: 100px !important; } #crop { left: 300px !important; } @media(max-width:767px) , (min-width: 360px) { #cover { display: block; height: 185px !important; left: 0; position: absolute; top: 0; width: 500px !important; } .cover-container { display: table; height: 185px; margin: 5% auto 0; width: 500px; position: relative; } } </style> <script> var croppicture = function () { var cover = document.getelementbyid("cover"); var ctx = cover.getcontext("2d"); var img = new image(); img.onload = function () { var position = $("#cover").offset(); //alert(json.stringify($("#cover").offset())); //alert("cover width " + cover.width); //alert("cover height" + cover.height); //alert(position.left); var coverleft = position.left; var covertop = position.top; var coverwidth = 851; var coverheight = 315; // var width = $('#cover').width(); // var parentwidth = $('#container').offsetparent().width(); // var percent = 100 * width / parentwidth; //alert("percent " + percent); alert("innerwidth " + window.innerwidth); var destwidth = $('#cover').width(); var destheight = $('#cover').height(); alert("destwidth " + destwidth); ctx.drawimage(img, coverleft, covertop, coverwidth, coverheight, 0, 0, destwidth, destheight); //$("#source").css("display", "none"); } img.src = document.getelementbyid("source").src; } </script> <body> <div id="container"> <div class="cover-container"> <canvas id="cover" height="315" width="851" style="border:1px solid #000"></canvas> </div> <img id="source" width="100%" height="100%" src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg"> </div> <div id="capturebtn"> <input id="crop" type="button" value="crop" onclick="croppicture();" /> </div> </body> </html>
Comments
Post a Comment