溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

html5中canvas如何實現(xiàn)跟隨鼠標旋轉的箭頭

發(fā)布時間:2021-05-12 14:11:15 來源:億速云 閱讀:206 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關html5中canvas如何實現(xiàn)跟隨鼠標旋轉的箭頭的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

XML/HTML Code復制內容到剪貼板

<!DOCTYPE html>  
<html>  
 <head>    
  <meta charset="utf-8" />    
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />    
  <title>canvas實現(xiàn)跟隨鼠標旋轉的箭頭</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了,這個效果實現(xiàn)起來并不復雜,但是麻雀隨小,五臟俱全,主要涉及到的知識點有:

1、canvas的基本繪圖;
2、js各個事件的監(jiān)聽;
3、js動畫;
4、三角函數(shù)結合js在canvas中的基本應用;

感謝各位的閱讀!關于“html5中canvas如何實現(xiàn)跟隨鼠標旋轉的箭頭”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI