When you move the cursor outside the image, the mousedown event is lost.
How to create the right handler ?
How to prevent crop out of the image?
How to add the ability to change the size of the crop , with crop_image 256х160 ?
input = document.querySelector('.box input'); input.onchange = function() { image = new Image(); image.src = window.URL.createObjectURL(this.files[0]); this.parentNode.style = 'display: none'; document.querySelector('.box').innerHTML += "<div class='original_image'>" + image.outerHTML + "<div class='crop' style='margin: 0px 0px'></div></div>" + "<div class='crop_image'>" + image.outerHTML + "</div>"; crop = document.querySelector('.crop'); crop_image = document.querySelector('.crop_image img'); crop.onmousedown = function() { this.onmousemove = function(e) { this.style.marginTop = parseInt(this.style.marginTop) + e.movementY + 'px'; this.style.marginLeft = parseInt(this.style.marginLeft) + e.movementX + 'px'; crop_image.style.marginTop = '-' + this.style.marginTop; crop_image.style.marginLeft = '-' + this.style.marginLeft; return false; } this.onmouseup = function() { this.onmousemove = null; } } } .box { position: relative; width: 768px; height: 256px; background: #FFF; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .3); padding: 2px; } .select { position: absolute; width: 100%; cursor: pointer; line-height: 256px; text-align: center; font-family: Segoe UI; font-size: 20px; font-weight: 300; color: #C7254E; } .original_image img { max-height: 256px; } .original_image { position: relative; float: left; overflow: hidden; max-height: 256px; margin-right: 2px; } .crop { position: absolute; top: 0; width: 256px; height: 160px; box-shadow: 0 0 0 512px rgba(0, 0, 0, .4); cursor: move; } .crop_image img { max-height: 256px; } .crop_image { width: 256px; height: 160px; overflow: hidden; } <div class='box'> <label class='select'><a>выбрать изображение</a> <input type='file' accept='image/*' style='display: none'> </label> </div> 