How to rotate this figure?

I am just starting with the canvas. I have done this:

http://www.kingoslo.com/instruments/

The arrow for the tachometer is depicted on canvas. Now I'm trying to make an animation to rotate it around the input of the tachometer (and not myself). This is what I wrote so far:

 <script type="text/javascript">
  var img = new Image(); 

  function init(){  
   img.src = 'background.png'; 
   setTimeout(draw,4000);   
  }  

  function draw() {  
   var ctx = document.getElementById('canvas').getContext('2d');   
   img.onload = function(){  
    ctx.drawImage(img,0,0); 
    ctx.beginPath();
    ctx.moveTo(247,308);
    ctx.bezierCurveTo(51, 408, 51, 410, 51, 411);
    ctx.bezierCurveTo(53, 413, 52, 412, 249, 313);
    ctx.fillStyle = "red";
    ctx.fill();
   }  
  }
 </script>

I have no idea how to proceed. Also, is there documentation anywhere where this happens?

Thank.

Regards,
Marius

+3
source share
1 answer

You saw this https://github.com/vjt/canvas-speedometer

I wrote your code:

Make sure that the center pointmeter must be in the center of your image, otherwise it will not work.

i, .

CODE:

<script type="text/javascript">
            function startup() {
                    var canvas = document.getElementById('canvas');
                    var context = canvas.getContext('2d');
                    var meter = new Image();
                    meter.src = 'Mt.png';    //meter background image
                    var pin = new Image();
                    pin.src = 'pin3.png';    //meter pin image                 

                    var x=meter.width/2;     // center point.X (must be the center of image)
                    var y=meter.height/2;    // center point.Y 
                    var width = meter.width; 
                    var height = meter.height;

                    var i=120;             // angle of rotation in degrees
                    var min =-115;         // maximum angle = 6 RPM in meter 
                    var max =100;          // minimum angle = 0 RPM in meter 

                    i=(i < min)? min:(i > max)? max:i;  //to check i must be within min & max range
                    var angleInRadians = Math.PI * i/180;  //converting degree to radians
                    meter.onload = function()
                    {
                        context.translate(x,y);    //setting center at x,y                        
                        context.drawImage(meter, -width / 2, -height / 2, width, height);  //drawing meter background 
                        context.rotate(angleInRadians); //rotating by angle
                        context.drawImage(pin, 0-pin.width/2,0-pin.height+pin.width/2,pin.width,pin.height);  //adjusting pin center at meter center

                    }
            }
    </script>

Update: /

                    var i=120;             // angle of rotation in degrees
                    var min =-115;         // maximum angle = 6 RPM in meter 
                    var max =100;          // minimum angle = 0 RPM in meter 

                    var r=1.7;               //handle here
                    var min =-114;           
                    var max =99;                                  
                    var span = (max-min)/6;  // dividing rotation angle by meter scale
                    var i=r*span+min;        //angle of rotation from value of r and span

:
 alt text

+13

Source: https://habr.com/ru/post/1780980/


All Articles