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

May 16, 2016 pm 03:51 PM
canvas html5 mouse follow

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles