javascript - Crop content from document and scale cropped area to another div -
my objective on 1 able crop part of html document (the image, white background) , able scale same cropped area red square in bottom of fiddle. it's final step build snipping tool (this merely experiment web tools) , in need of final push now, can't seem solve without use of existent repositories. wouldn't point of trying after all. help, idea? feel free edit fiddle ofc
html
<div id="selection_box"></div> <img src="http://popcultureblog.dallasnews.com/files/2012/10/norah1bynoahabrams_27933966.jpeg" id="image_box"> <div id="captured_box"></div>
javasctipt
function getcursorposition(e) { e = e || window.event; if (e) { if (e.pagex || e.pagex == 0) return [e.pagex, e.pagey]; var de = document.documentelement || {}; var db = document.body || {}; if ((e.clientx || e.clientx == 0) && ((db.scrollleft || db.scrollleft == 0) || (de.clientleft || de.clientleft == 0))) return [e.clientx + (de.scrollleft || db.scrollleft || 0) - (de.clientleft || 0), e.clienty + (de.scrolltop || db.scrolltop || 0) - (de.clienttop || 0)]; } return null; } function mousedown(e) { var mxy = getcursorposition(e); var box = document.getelementbyid("selection_box"); box.orig_x = mxy[0]; box.orig_y = mxy[1]; box.style.left = mxy[0] + "px"; box.style.top = mxy[1] + "px"; box.style.display = "block"; document.onmousemove = mousemove; document.onmouseup = mouseup; } function mousemove(e) { var mxy = getcursorposition(e); var box = document.getelementbyid("selection_box"); box.style.width = (mxy[0] - box.orig_x) + "px"; box.style.height = (mxy[1] - box.orig_y) + "px"; } function mouseup(e) { var mxy = getcursorposition(e), box = document.getelementbyid("selection_box"), image_box = document.getelementbyid("image_box"), selection = getselection; box.style.display = "none"; box.style.width = "0"; box.style.height = "0"; document.onmousemove = function () {}; document.onmouseup = function () {}; } document.onmousedown = mousedown;
css
* { -moz-user-select: none; -webkit-user-select: none; } #selection_box { -webkit-animation: blackwhite 3s infinite; display: none; position: absolute; border: 1px dashed #000000; background: transparent; width: 0; height: 0; z-index: 1; } @-webkit-keyframes blackwhite { 40% { border-color:white; } 100% { border-color:black; } } #image_box { position: absolute; top: 75px; width: 700px; height: 370px; } #captured_box { position: absolute; top: 500px; left: 100px; width: 100px; height: 100px; background-color: red; }
this should fix problems of realisation(here mouse can go left , click point.)
function mousemove(e) { var mxy = getcursorposition(e); var box = document.getelementbyid("selection_box"); var right=mxy[0]>box.orig_x; var left=!right; var up=mxy[1]<box.orig_y; var down=!up; if(up && right){ box.style.top = mxy[1] + "px"; } else if(up && left){ box.style.left = mxy[0] + "px"; box.style.top = mxy[1] + "px"; } else if(down && left){ box.style.left = mxy[0] + "px"; box.style.top = mxy[1] - "px"; } box.style.width = (math.abs(mxy[0] - box.orig_x)) + "px"; box.style.height = (math.abs(mxy[1] - box.orig_y)) + "px"; }
Comments
Post a Comment