4.5.1_利用getImageData()与putImageData()方法来实现橡皮筋式选取框_改良版
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>利用getImageData()与putImageData()方法来实现橡皮筋式选取框——改良版</title>
<style>
body{
background: #eee;
}
#canvas{
background: #fff;
cursor: pointer;
margin-left: 20px;
margin-top: 50px;
box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
-webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
-moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
}
#controls{
position: absolute;
top: 20px;
left: 50px;
}
</style>
</head>
<body>
<div id="controls"><input type="button" id="resetBtn" value="重置" /></div>
<canvas id="canvas" width="800" height="520"></canvas>
</body>
<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var resetBtn = document.getElementById('resetBtn');
var image = new Image();
var imageData;
var mousedown ={};
var rubberbandRectangle = {};
var dragging = false;
context.strokeStyle = 'navy';
context.lineWidth = 1;
image.src = 'img/waterfall.jpg';
image.onload = function(){
context.drawImage(image,0,0,canvas.width,canvas.height);
}
resetBtn.onclick = function (){
context.clearRect(0,0,canvas.width,canvas.height);
context.drawImage(image,0,0,canvas.width,canvas.height);
}
canvas.onmousedown = function(e){
var loc = windowToCanvas(e.clientX,e.clientY);
e.preventDefault();
rubberbandStar(loc.x,loc.y);
}
canvas.onmousemove = function (e){
var loc;
if(dragging){
loc = windowToCanvas(e.clientX,e.clientY);
rubberbandStretch(loc.x,loc.y);
}
}
canvas.onmouseup = function(){
rubberbandEnd();
}
function rubberbandStar(x,y){
mousedown.x = x;
mousedown.y = y;
dragging = true;
captureRubberbandPixels();
}
function rubberbandStretch(x,y){
if(rubberbandRectangle.width>2*context.lineWidth&&rubberbandRectangle.height>2*context.lineWidth){
if(imageData!== undefined){
restoreRubberbandPixels();
}
}
setRubberbandRectanle(x,y);
if(rubberbandRectangle.width>2*context.lineWidth&&rubberbandRectangle.height>2*context.lineWidth){
updateRubberband();
}
}
function restoreRubberbandPixels(){
var deviceWidthOverCSSPixels = imageData.width/canvas.width;
var deviceHeightOverCSSPixels = imageData.height/canvas.height;
context.putImageData(imageData,0,0,
rubberbandRectangle.left*deviceWidthOverCSSPixels,
rubberbandRectangle.top*deviceHeightOverCSSPixels,
rubberbandRectangle.width*deviceWidthOverCSSPixels,
rubberbandRectangle.height*deviceHeightOverCSSPixels);
}
function updateRubberband(){
drawRubberband();
}
function captureRubberbandPixels(){
imageData = context.getImageData(0,0,canvas.width,canvas.height);
}
function drawRubberband(){
context.strokeRect(rubberbandRectangle.left +context.lineWidth,rubberbandRectangle.top+context.lineWidth,
rubberbandRectangle.width - 2*context.lineWidth,rubberbandRectangle.height-2*context.lineWidth);
}
function setRubberbandRectanle(x,y){
rubberbandRectangle.left = Math.min(x,mousedown.x);
rubberbandRectangle.top = Math.min(y,mousedown.y);
rubberbandRectangle.width = Math.abs(x - mousedown.x);
rubberbandRectangle.height = Math.abs(y - mousedown.y);
}
function rubberbandEnd(){
context.drawImage(canvas,rubberbandRectangle.left+context.lineWidth*2,
rubberbandRectangle.top+context.lineWidth*2,
rubberbandRectangle.width - 4*context.lineWidth,
rubberbandRectangle.height - 4*context.lineWidth,0,0,canvas.width,canvas.height);
dragging = false;
imageData = undefined;
}
function windowToCanvas(x,y){
var bbox = canvas.getBoundingClientRect();
return {
x:x-bbox.left*(canvas.width/bbox.width),
y:y-bbox.top*(canvas.height/bbox.height)
};
}
</script>
</html>