4.5.1_利用getImageData()与putImageData()方法来实现橡皮筋式选取框_改良版

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值