html5 canvas實(shí)現(xiàn)跟隨鼠標(biāo)旋轉(zhuǎn)的箭頭

本文實(shí)例為大家分享了
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <title>canvas實(shí)現(xiàn)跟隨鼠標(biāo)旋轉(zhuǎ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; //相對(duì)于canvas坐標(biāo)的位置;
- 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了,這個(gè)效果實(shí)現(xiàn)起來(lái)并不復(fù)雜,但是麻雀隨小,五臟俱全,主要涉及到的知識(shí)點(diǎn)有:
1、canvas的基本繪圖;
2、js各個(gè)事件的監(jiān)聽(tīng);
3、js動(dòng)畫;
4、三角函數(shù)結(jié)合js在canvas中的基本應(yīng)用;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
原文:http://www.cnblogs.com/jone-chen/p/5243439.html
相關(guān)文章
html5 canvas實(shí)現(xiàn)的鼠標(biāo)響應(yīng)式樹(shù)葉光標(biāo)跟隨動(dòng)畫特效源碼
這是一款基于html5 canvas實(shí)現(xiàn)的鼠標(biāo)響應(yīng)式樹(shù)葉光標(biāo)跟隨動(dòng)畫特效源碼。畫面上由無(wú)數(shù)粒子組成的樹(shù)葉圖形呈現(xiàn)出分散開(kāi)來(lái)并跟隨鼠標(biāo)移動(dòng)的動(dòng)畫效果,當(dāng)點(diǎn)擊鼠標(biāo)時(shí),分散的樹(shù)葉2017-11-10html5 canvas實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的黑洞動(dòng)畫特效源碼
這是一款基于html5 canvas實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的黑洞動(dòng)畫特效源碼。畫面上黑色星空背景下,代表形體的白色圓點(diǎn)逐步由慢到快的向著鼠標(biāo)光標(biāo)所在處的黑色圓點(diǎn)匯聚并消失,呈現(xiàn)出2017-08-21html5 canvas實(shí)現(xiàn)的點(diǎn)擊跟隨鼠標(biāo)移動(dòng)光線動(dòng)畫特效源碼
這是一款基于html5 canvas實(shí)現(xiàn)的點(diǎn)擊跟隨鼠標(biāo)移動(dòng)光線動(dòng)畫特效源碼。鼠標(biāo)點(diǎn)擊頁(yè)面上可呈現(xiàn)出帶有圓點(diǎn)連線到光點(diǎn)的運(yùn)動(dòng)狀態(tài)動(dòng)畫效果,且光點(diǎn)的移動(dòng)圍繞著鼠標(biāo)位置運(yùn)動(dòng)。2017-06-05html5 canvas實(shí)現(xiàn)的跟隨鼠標(biāo)光標(biāo)動(dòng)畫特效源碼
這是一款基于html5 canvas實(shí)現(xiàn)的跟隨鼠標(biāo)光標(biāo)動(dòng)畫特效源碼。共有10種不同的光標(biāo)跟隨動(dòng)畫供用戶選擇使用。用戶只需點(diǎn)擊相應(yīng)的按鈕即可實(shí)現(xiàn)光標(biāo)跟隨特效的切換2017-04-17html5 canvas實(shí)現(xiàn)的跟隨鼠標(biāo)刮風(fēng)下雨動(dòng)畫特效源碼
這是一款基于html5 canvas實(shí)現(xiàn)的跟隨鼠標(biāo)刮風(fēng)下雨動(dòng)畫特效源碼。左右移動(dòng)鼠標(biāo)可見(jiàn)掉落的雨滴隨著鼠標(biāo)移動(dòng)的方向而改變掉落方向,呈現(xiàn)出鼠標(biāo)左右滑動(dòng)控制風(fēng)向的效果。雨滴落2016-06-01html5 canvas紙飛機(jī)跟隨鼠標(biāo)飛行特效源碼
紙飛機(jī)跟隨鼠標(biāo)飛行特效源碼是一款基于html5 canvas和js制作的紙飛機(jī)跟隨鼠標(biāo)飛行動(dòng)畫效果的代碼。鼠標(biāo)移動(dòng)越大,飛行速度越快2016-02-26ES6與canvas實(shí)現(xiàn)鼠標(biāo)小球跟隨效果
這篇文章主要介紹了ES6與canvas實(shí)現(xiàn)鼠標(biāo)小球跟隨效果的相關(guān)資料,需要的朋友可以參考下2018-02-11