如何旋转这个形状?

发布于 2024-10-08 16:39:13 字数 827 浏览 2 评论 0原文

我刚刚开始使用画布。我做了这个:

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

转速计的箭头是使用画布绘制的。现在我正在尝试制作一个动画以使其围绕转速计的输入(而不是其本身)旋转。这就是我到目前为止所写的:

 <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 am just starting out with canvas. I made this:

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

The arrow for the tachometer is a drawn using canvas. Now I am trying to make an animation to rotate it around the enter of the tachometer (not itself). This is what I have written 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 get further. Also, is there comprehensive documentation anywhere that covers these things?

Thanks.

Kind regards,
Marius

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-10-15 16:39:13

你见过这个 https://github.com/vjt/canvas-speedometer

我已经编写了您的代码:

确保仪表的中心点必须位于图像的中心,否则它将无法工作。

您可以处理 i 的值来以米为单位旋转图钉。

在这里,我拍摄了两张图像

  1. mt.png //用于背景仪表
    https://i.sstatic.net/qbWeO.png
  2. pin3.png //用于仪表别针
    https://i.sstatic.net/SQEv6.png

代码:

<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>

<强>更新:
如果您想处理值为 rpm 的仪表
替换

                    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

输出:
替代文本

Have you seen this https://github.com/vjt/canvas-speedometer

I have written your code:

Make sure that the center point of the meter must be at the center of your image else it will not work.

You can handle value of i to rotate your pin in meter.

Here, I have taken two images

  1. mt.png //for background meter
    https://i.sstatic.net/qbWeO.png
  2. pin3.png //for meter pin
    https://i.sstatic.net/SQEv6.png

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:
If you want to handle meter with value of rpm
Replace

                    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 

with

                    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

output:
alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文