<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
}
.main {
position: relative;
height: 500px;
500px;
border: 1px solid gray;
overflow: hidden;
}
#btnfile {
}
.image {
position: absolute;
height: 500px;
500px;
z-index: 1;
}
.cut_container {
position: absolute;
border: 1px solid orange;
height: 150px;
150px;
left: 175px;
top: 175px;
z-index: 8888;
}
.btn_container {
position: absolute;
top: 500px;
}
</style>
</head>
<body>
<canvas id="mycan" width="200" height="200" style="display:none"></canvas>
<div class="main" id="img_container">
<div class="cut_container" id="cut_container" draggable="false"></div>
<img id="myImg" class="image" src="images/6.jpg" draggable="false" />
</div>
<div class="btn_container">
<input type="file" id="btnfile" />
<button id="btn_cut" onclick="cut_picture()">确定</button>
</div>
</body>
</html>
<script type="text/javascript">
var img = document.getElementById("myImg");
var width = parseInt(img.width);
var height = parseInt(img.height);
var x = 0; var y = 0;
var cut = $(document.getElementById("cut_container"))
$("#btnfile").change(function () {
var fr = new FileReader();
var file = this.files[0];
fr.readAsDataURL(file);
fr.onload = function () {
console.log(fr.result);
img.src = fr.result;
width = img.width;
height = img.height;
}
})
var x = 0, y = 0;
var canvas = document.getElementById("mycan");
var ctx = canvas.getContext("2d");
img.onload = function () {
ctx.drawImage(img, 0, 0, width, height);
}
document.getElementById("img_container").onmousewheel = function (e) {
e = event || e;
var v = e.wheelDelta;
if (v > 0) {
width += 10;
height += 10;
$(img).css({ height: height, width })
}
else {
width -= 10;
height -= 10;
if (width < 150 || height < 150) {
width = 150;
height = 150;
}
$(img).css({ height: height, width })
}
}
$("#myImg").mousedown(function (e) {
e = event || e;
var W = e.offsetX * 1;
var H = e.offsetY * 1;
$("#img_container").bind("mousemove", function (ex) {
ex = event || ex;
var left = ex.clientX * 1;
var top = ex.clientY * 1;
var l = left - W;
var t = top - H +$(document).scrollTop()*1;
$("#myImg").css({ left: l, top: t })
})
})
$("#img_container").mouseup(function () {
$("#img_container").unbind("mousemove")
})
function cut_picture() {
canvas.height = 150;
canvas.width = 150;
x = parseInt(img.style.left) - parseInt(cut.css("left"));
y = parseInt(img.style.top) - parseInt(cut.css("top"));
ctx.drawImage(img, x, y, width, height);
console.log(canvas.toDataURL())
window.open(canvas.toDataURL())
}
</script>