温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

html5中canvas如何实现跟随鼠标旋转的箭头

发布时间:2021-05-12 14:11:15 来源:亿速云 阅读:194 作者:小新 栏目:web开发

这篇文章给大家分享的是有关html5中canvas如何实现跟随鼠标旋转的箭头的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

XML/HTML Code复制内容到剪贴板

<!DOCTYPE html>  
<html>  
 <head>    
  <meta charset="utf-8" />    
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />    
  <title>canvas实现跟随鼠标旋转的箭头</title>    
  <style>  
    *{padding: 0;margin: 0}   
    </style>    
 </head>    
 <body>    
  <canvas width="500" height="500" style="border: 1px solid #555; display: block;margin: 0 auto;"></canvas>    
  <script>  
        var arrow=function () {   
            this.x=0;    
            this.y=0;   
            this.color="#f90"  
            this.rolation=0;   
        }    
        var canvas=document.querySelector('canvas')   
        var ctx=canvas.getContext('2d');   
        arrow.prototype.draw=function (ctx) {   
            ctx.save();   
            ctx.translate(this.x,this.y);   
            ctx.rotate(this.rolation)   
            ctx.fillStyle=this.color;   
            ctx.beginPath();   
            ctx.moveTo(0, 15);   
            ctx.lineTo(-50, 15);   
            ctx.lineTo(-50, -15);   
            ctx.lineTo(0,-15);   
            ctx.lineTo(0,-35);   
            ctx.lineTo(50,0);   
            ctx.lineTo(0,35);   
            ctx.closePath()   
            ctx.fill();   
            ctx.restore();   
        }   
        var Arrow=new arrow();   
        Arrow.x=canvas.width/2;   
        Arrow.y=canvas.height/2;   
           
        var c_x,c_y; //相对于canvas坐标的位置;   
        var isMouseDown=false;   
        Arrow.draw(ctx)   
        canvas.addEventListener('mousedown',function(e) {   
            isMouseDown=true;   
        },false)   
        canvas.addEventListener('mousemove',function(e) {   
            if(isMouseDown==true){   
                c_x=getPos(e).x-canvas.offsetLeft;   
                c_y=getPos(e).y-canvas.offsetTop;   
                //setInterval(drawFram,1000/60)   
                requestAnimationFrame(drawFram)   
            }   
        },false)   
        canvas.addEventListener('mouseup',function(e) {   
            isMouseDown=false;   
        },false)   
        function drawFram(){   
            var dx=c_x-Arrow.x;   
            var dy=c_y-Arrow.y;   
            Arrow.rolation=Math.atan2(dy,dx);   
            ctx.clearRect(0,0,canvas.width,canvas.height);   
            Arrow.draw(ctx)   
        }   
        function getPos(e) {   
            var mouse={x:0,y:0}   
            var ee=e||event;   
        
            if(e.pageX||e.pageY){   
                mouse.x=e.pageX;   
                mouse.y=e.pageY;   
            }else{   
                mouse.x=e.pageX+document.body.scrollLeft+document.document.documentElement.scrollLeft;   
                mouse.y=e.pageY+document.body.scrollTop+document.document.documentElement.scrollTop;   
            }   
            return mouse;   
        }   
    </script>     
 </body>  
</html>

DEMO地址:http://codepen.io/jonechen/pen/eZpgWd

不废话,直接上DEMO了,这个效果实现起来并不复杂,但是麻雀随小,五脏俱全,主要涉及到的知识点有:

1、canvas的基本绘图;
2、js各个事件的监听;
3、js动画;
4、三角函数结合js在canvas中的基本应用;

感谢各位的阅读!关于“html5中canvas如何实现跟随鼠标旋转的箭头”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI