10、鼠标拖拽对象

    通过不断更新物体的坐标位置使其追随鼠标指针的位置,就可以实现在canvas元素上拖拽物体。

    1、捕捉 mousedown 事件,判断鼠标坐标是否在物体所在的坐标范围内;

    2、如果是,则表示鼠标点击在物体上,此时为 canvas 添加 mousemove 事件的处理程序,并在该处理程序内将物体的 x、y 坐标更新为鼠标指针当前的坐标位置;

    3、最后,在 mouseup 事件触发时,将 mousemove 事件的处理程序移除。

 

代码实现如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,width=device-width">
    <title></title>
    <style>
        *{margin: 0;padding: 0}
        body {
            background-color: #eee
        }
        canvas {
            background-color: #fff
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="600" height="400">
        Your browser does not support HTML5 Canvas.
    </canvas>
 
    <script>
        (function(){
            window.addEventListener("load", eventWindowLoaded, false);
            function eventWindowLoaded(){
                canvasApp();
            }
            function canvasSupport(e){
                return !!e.getContext;
            }
            function canvasApp(){
                let canvas = document.getElementById("canvas");
                if(!canvasSupport(canvas)){
                    return ;
                }
                // start
                let context = canvas.getContext("2d");
                drawScreen(canvas, context);
            }
 
            // write your codes
            function drawScreen(canvas, context){
                let mouse = captureMouse(canvas);
                let ball = new Ball();
                
                ball.x = canvas.width / 2;
                ball.y = canvas.height / 2;

                canvas.addEventListener("mousedown", function() {
                    if (containsPoint(ball.getBounds(), mouse.x, mouse.y)) {
                        canvas.addEventListener("mouseup", onMouseUp, false);
                        canvas.addEventListener("mousemove", onMouseMove, false);
                    }
                }, false);

                function onMouseUp() {
                    canvas.removeEventListener("mouseup", onMouseUp, false);
                    canvas.removeEventListener("mousemove", onMouseMove, false);
                }

                function onMouseMove() {
                    ball.x = mouse.x;
                    ball.y = mouse.y;
                }

                (function drawFrame() {
                    window.requestAnimationFrame(drawFrame, canvas);
                    context.clearRect(0, 0, canvas.width, canvas.height);
                    
                    ball.draw(context);
                } ())
            }
        })();
    </script>
    <script>
        function containsPoint(rect, x, y) {
            return !(x < rect.x || x > rect.x + rect.width || y < rect.y || y > rect.y + rect.height);
        }

        function captureMouse(element) {
            var mouse = {x: 0, y: 0, event: null},
                body_scrollLeft = document.body.scrollLeft,
                body_scrollTop = document.body.scrollTop,
                element_scrollLeft = document.documentElement.scrollLeft,
                element_scrollTop = document.documentElement.scrollTop,
                offsetLeft = element.offsetLeft,
                offsetTop = element.offsetTop;
        
            element.addEventListener("mousemove", function(){
                var x, y;
        
                if(event.pageX || event.pageY){
                    x = event.pageX;
                    y = event.pageY;
                }else{
                    x = event.clientX + body_scrollLeft + element_scrollLeft;
                    y = event.clientY + body_scrollTop + element_scrollTop;
                }
                x -= offsetLeft;
                y -= offsetTop;
        
                mouse.x = x;
                mouse.y = y;
                mouse.event = event;
            }, false);
        
            return mouse;
        }
    </script>
    <script>
        function Ball(radius, color) {
            if (radius === undefined) {
                radius = 40;
            }

            if (color === undefined) {
                color = "#ff0000";
            }

            this.x = 0;
            this.y = 0;
            this.color = color;
            this.radius = radius;
        }

        Ball.prototype.draw = function(context) {
            context.save();

            context.translate(this.x, this.y);
            context.fillStyle = this.color;
            context.beginPath();
            context.arc(0, 0, this.radius, 0, 360 * Math.PI / 180, false);
            context.closePath();
            context.fill();

            context.restore();
        }

        Ball.prototype.getBounds = function() {
            return {
                x: this.x - this.radius,
                y: this.y - this.radius,
                width: this.radius * 2,
                height: this.radius * 2
            };
        }
    </script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值