Html5 canvas implements arrows that follow the mouse rotation, html5canvas

Source: Internet
Author: User

Html5 canvas implements arrows that follow the mouse rotation, html5canvas

Examples of this article are as follows:

Copy XML/HTML Code to clipboard
  1. <! DOCTYPE html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8"/>
  5. <Meta http-equiv = "X-UA-Compatible" content = "IE = edge"/>
  6. <Title> canvas: arrows that rotate with the mouse </title>
  7. <Style>
  8. * {Padding: 0; margin: 0}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Canvas width = "500" height = "500" style = "border: 1px solid #555; display: block; margin: 0 auto;"> </canvas>
  13. <Script>
  14. Var arrow = function (){
  15. This. x = 0;
  16. This. y = 0;
  17. This. color = "# f90"
  18. This. rolation = 0;
  19. }
  20. Var canvas = document. querySelector ('canvas ')
  21. Var ctx = canvas. getContext ('2d ');
  22. Arrow. prototype. draw = function (ctx ){
  23. Ctx. save ();
  24. Ctx. translate (this. x, this. y );
  25. Ctx. rotate (this. rolation)
  26. Ctx. fillStyle = this. color;
  27. Ctx. beginPath ();
  28. Ctx. moveTo (0, 15 );
  29. Ctx. lineTo (-50, 15 );
  30. Ctx. lineTo (-50,-15 );
  31. Ctx. lineTo (0,-15 );
  32. Ctx. lineTo (0,-35 );
  33. Ctx. lineTo (50, 0 );
  34. Ctx. lineTo (0, 35 );
  35. Ctx. closePath ()
  36. Ctx. fill ();
  37. Ctx. restore ();
  38. }
  39. Var Arrow = new arrow ();
  40. Arrow. x = canvas. width/2;
  41. Arrow. y = canvas. height/2;
  42. Var c_x, c_y; // The position relative to the canvas coordinate;
  43. Var isMouseDown = false;
  44. Arrow. draw (ctx)
  45. Canvas. addEventListener ('mousedown ', function (e ){
  46. IsMouseDown = true;
  47. }, False)
  48. Canvas. addEventListener ('mousemove ', function (e ){
  49. If (isMouseDown = true ){
  50. C_x = getPos (e). x-canvas.offsetLeft;
  51. C_y = getPos (e). y-canvas.offsetTop;
  52. // SetInterval (FIG, 1000/60)
  53. RequestAnimationFrame (drawFram)
  54. }
  55. }, False)
  56. Canvas. addEventListener ('mouseup', function (e ){
  57. IsMouseDown = false;
  58. }, False)
  59. Function drawFram (){
  60. Var dx= c_x-Arrow.x;
  61. Var dy = c_y-Arrow.y;
  62. Arrow. rolation = Math. atan2 (dy, dx );
  63. Ctx. clearRect (0, 0, canvas. width, canvas. height );
  64. Arrow. draw (ctx)
  65. }
  66. Function getPos (e ){
  67. Var mouse = {x: 0, y: 0}
  68. Var ee = e | event;
  69. If (e. pageX | e. pageY ){
  70. Mouse. x = e. pageX;
  71. Mouse. y = e. pageY;
  72. } Else {
  73. Mouse.xe.e.pagex?document.body.scrollleft?document.document.doc umentElement. scrollLeft;
  74. Mouse.yunce.pagey?document.body.scrolltop=document.document.doc umentElement. scrollTop;
  75. }
  76. Return mouse;
  77. }
  78. </Script>
  79. </Body>
  80. </Html>

DEMO address: http://codepen.io/jonechen/pen/eZpgWd

It's not complicated to implement the DEMO directly, but the sparrows are small and dirty, mainly involving the following knowledge points:

1. Basic Drawing of canvas;
2. monitors various js events;
3. js animation;
4. trigonometric functions combined with the basic application of js in canvas;

The above is all the content of this article, hoping to help you learn.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.