JS 第三方工具封裝經典案例(canvas元素拖動)

2b勿擾發表於2020-01-21
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>圖片繪製並拖拽</title>
    <style>
        body,div{
            margin:0;
            padding:0;
        }
    </style>
</head>
<body>
    <canvas id="myCanvas" width="500" height="400" style="border:1px solid red;">
        <p>您的系統不支援此程式!</p>
    </canvas>

    <a href="" id="downlink">下載圖片</a>

    <img src='' id="modelImg">
    <script>
        var canvas = document.getElementById('myCanvas');
        var cansText =  canvas.getContext('2d');

        var img = new Image();
        img.src="./images/finger.png";
        img.onload = function(){ 
            cansText.drawImage(img,50,50,150,100);
        }
    cansText.save();  
    cansText.fillStyle = "#000000";
    cansText.font='15px Arial';
    cansText.translate(300,350);
 
    cansText.fillText("canvas生成圖片測試",0,0);
    cansText.fillText("長按儲存到手機",0,30);
    cansText.restore();

        function dragImg(){
            var moving = false; //標記是否在拖動
            var movingLine = false; //塗鴉
            var imgCurrent =  {
                x:50,
                y:50
            };
            var dis = {};
            canvas.onmousedown = function(e){
              
                //計算滑鼠落下點與被拖動的左邊和上邊的距離
                dis={
                    x:e.clientX-imgCurrent.x,
                    y:e.clientY-imgCurrent.y
                }
                //判斷是不是在圖片上
                if(dis.x>150 || dis.y>100){
                    return;
                }
                moving = true;
            }
            canvas.onmousemove = function(e){
                if(!moving) {
                    return;
                }  
                var px = e.clientX - dis.x;
                var py = e.clientY - dis.y;

                //不要超出邊界 如何處理
                // 橫座標  0 - (畫布的寬度-圖片的寬度)
                // 縱座標  0 - (畫布的高度-圖片的高度)

                var movescope = {
                    mx:canvas.width - 150,
                    my:canvas.height-100
                };
                if(px>movescope.mx || px<0){
                    return;
                }
                if(py>movescope.my || py<0){
                    return;
                }

                cansText.clearRect(0,0,canvas.width,canvas.height);   
                cansText.drawImage(img,px,py,150,100);
                imgCurrent= {
                    x:px,
                    y:py
                }
            }
            canvas.onmouseup = function(){
                moving = false;
                movingLine = false;
            }

            canvas.addEventListener('mousedown',function(ev){
                movingLine = true;
            })
            canvas.addEventListener('mousemove',function(ev){
                if(!movingLine){
                    return;
                }
                // drawLine()
                cansText.fillStyle = 'red';
                cansText.beginPath(); //開始一條路徑
                cansText.arc(ev.clientX,ev.clientY,5,0,2 *Math.PI,true); //路徑
                cansText.fill();

            })

        }
        dragImg();

        //圖片的下載   base64
        document.getElementById('downlink').addEventListener('click',function(ev){          
             ev.preventDefault();
            //  this.download = 'hello.png';

            this.href = canvas.toDataURL('image/png');
             modelImg.src= this.href;

        })
    </script>
</body>
</html>

相關文章