


html5 canvas implements arrows that follow the mouse rotation_html5 tutorial skills
The examples in this article are shared with everyone
- >
- <html>
- <head>
- <meta charset="utf- 8" /> " X-ua-compatible "
- Content = " ie = edge " /// > <title>canvas implements arrows that follow the mouse rotation < / 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; //相对于canvas坐标的位置;
- 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 address:
- body>
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:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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

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

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

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

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

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