Home > Web Front-end > H5 Tutorial > html5 canvas implements arrows that follow the mouse rotation_html5 tutorial skills

html5 canvas implements arrows that follow the mouse rotation_html5 tutorial skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:51:42
Original
2838 people have browsed it

The examples in this article are shared with everyone

XML/HTML CodeCopy content to clipboard
  1. >
  2. <html>
  3. <head>
  4. <meta charset="utf- 8" />
  5. " X-ua-compatible "
  6. Content = " ie = edge " /// > <title>canvas implements arrows that follow the mouse rotation < /
  7. title
  8. >  <style>
  9. *{padding: 0;margin: 0}
  10.  style>
  11.  
  12. head>
  13. <body>
  14. <canvas width=
  15. "500"
  16. height="500" style="border: 1px solid #555; display: block;margin: 0 auto;">canvas >  <script>
  17. var
  18. arrow=function () {
  19.  
  20. this.x=0
  21. this.y=0;
  22.  
  23. this.color="#f90"
  24.  
  25. this.rolation=0
  26. }    
  27.         var canvas=document.querySelector('canvas')   
  28.         var ctx=canvas.getContext('2d');   
  29.         arrow.prototype.draw=function (ctx) {   
  30.             ctx.save();   
  31.             ctx.translate(this.x,this.y);   
  32.             ctx.rotate(this.rolation)   
  33.             ctx.fillStyle=this.color;   
  34.             ctx.beginPath();   
  35.             ctx.moveTo(0, 15);   
  36.             ctx.lineTo(-50, 15);   
  37.             ctx.lineTo(-50, -15);   
  38.             ctx.lineTo(0,-15);   
  39.             ctx.lineTo(0,-35);   
  40.             ctx.lineTo(50,0);   
  41.             ctx.lineTo(0,35);   
  42.             ctx.closePath()   
  43.             ctx.fill();   
  44.             ctx.restore();   
  45.         }   
  46.         var Arrow=new arrow();   
  47.         Arrow.x=canvas.width/2;   
  48.         Arrow.y=canvas.height/2;   
  49.            
  50.         var c_x,c_y; //相对于canvas坐标的位置;   
  51.         var isMouseDown=false;   
  52.         Arrow.draw(ctx)   
  53.         canvas.addEventListener('mousedown',function(e) {   
  54.             isMouseDown=true;   
  55.         },false)   
  56.         canvas.addEventListener('mousemove',function(e) {   
  57.             if(isMouseDown==true){   
  58.                 c_x=getPos(e).x-canvas.offsetLeft;   
  59.                 c_y=getPos(e).y-canvas.offsetTop;   
  60.                 //setInterval(drawFram,1000/60)   
  61.                 requestAnimationFrame(drawFram)   
  62.             }   
  63.         },false)   
  64.         canvas.addEventListener('mouseup',function(e) {   
  65.             isMouseDown=false;   
  66.         },false)   
  67.         function drawFram(){   
  68.             var dx=c_x-Arrow.x;   
  69.             var dy=c_y-Arrow.y;   
  70.             Arrow.rolation=Math.atan2(dy,dx);   
  71.             ctx.clearRect(0,0,canvas.width,canvas.height);   
  72.             Arrow.draw(ctx)   
  73.         }   
  74.         function getPos(e) {   
  75.             var mouse={x:0,y:0}   
  76.             var ee=e||event;   
  77.         
  78.             if(e.pageX||e.pageY){   
  79.                 mouse.x=e.pageX;   
  80.                 mouse.y=e.pageY;   
  81.             }else{
  82. mouse.x=e.pageX document.body.scrollLeft document.document.documentElement.scrollLeft;
  83. mouse.y=e.pageY document.body.scrollTop document.document.documentElement.scrollTop;
  84.                                                            
  85. return mouse;
  86.                                                               
  87.  
  88. script>  body> html> DEMO address:
http://codepen.io/jonechen/pen/eZpgWd

No nonsense, let’s go straight to the DEMO. This effect is not complicated to achieve, but it is as small as a sparrow and has all the internal organs. The main knowledge points involved are: 1. Basic drawing of canvas;

2. Monitoring of various js events;

3. js animation;

4. Basic application of trigonometric functions combined with js in canvas;


The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Original text:

http://www.cnblogs.com/jone-chen/p/5243439.html

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
html5 validation for symfony 2.1
From 1970-01-01 08:00:00
0
0
0
The difference between HTML and HTML5
From 1970-01-01 08:00:00
0
0
0
html5 show hide
From 1970-01-01 08:00:00
0
0
0
Can PDF files run HTML5 and Javascript?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template