本文實例為大家分享了
XML/HTML Code複製內容到剪貼簿
-
html>
-
html>
-
head>
-
meta charset charset />
-
meta http-equiv -UA-Compatible" content="IE=edge" "IE=edge"
-
title>canvastitle>
-
style>
-
*{padding: 0;margin: 0}
-
style>
-
head>
-
身體
>-
canvas width width 🎜> height="500" style >"border: 1px solid #555; display: block;margin: 0 auto;">>
- >can| >
- script>
var
- arrow=function=function=
-
this.x=0=
this.y=0=0-
=0 this.color=
"#f90"- ="#f90"= this.rolation=0=0=
}
-
var 營業=文件=
文件-
=文檔
var ctx=
canvas-
=canvas=canvas=
canvas- =
canvas- =
canvas- =
canvas-
=canvas=canvas
- arrow.prototype.draw
=-
ctx.save(); -
ctx.translate(this.x,this.y); -
ctx.rotate(this.rolation) -
- ctx.fillStyle
=- 這
=-
這這樣
-
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.x=
- 透明度=
- 透明度=透明度.=透明度.=
- 透明度.=透明度=透明度.
Arrow.y=身高.height/2;
var c_x,c_y; //相對於canvas座標的位置;
var isMouseDown=false=false;
Arrow.draw(ctx)
canvas.addEventListener('mousedown',function(e) {
isMouseDown=true;
},假)
-
canvas.addEventListener('mousemove',function(e) {
-
if(isMouseDown==true){
- ==true){ ==true){
c_x
= -
c_y=c_y
=
//setInterval(drawFram,1000/60) -
requestAnimationFrame(drawFram) -
} -
},假) -
canvas.addEventListener('mouseup',function(e) { -
isMouseDown=false
=- false
=- false
=-
false=false=點
-
},假)
函數drawFram(){
var
dx-
=c_x-Arrow.x;
var
dy- =
Arrow.rolation- =
數學-
.atan2(dy,dx);
-
ctx.clearRect(0,0,canvas.width,canvas.height);
Arrow.draw(ctx)
-
}
函數 getPos(e) {
var - 滑鼠
={x:0,y:0} - ={x:0,y:0}
var -
eee
e-
if(e.pageX||e.pageY){ -
mouse.x=e.pageX;
mouse.y=e.pageY;
}else{
-
mouse.x=c. >
-
mouse.y=c. >
}
-
return mouse;
-
}
-
-
script> >
- body>
-
html>
DEMO位址:
http://codepen.io/jonechen/pen/eZpgWd
不廢話,直接上DEMO了,這個效果實現起來並不復雜,但是麻雀隨小,五臟俱全,主要涉及到的知識點有:
1、canvas的基本繪圖;
2、js各事件的監聽;
3、js動畫;
4、三角函數結合js在canvas中的基本應用;
以上就是本文的全部內容,希望對大家的學習有所幫助。
原文:
http://www.cnblogs.com/jone-chen/p/5243439.html