我刚开始用帆布.我做的:
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); ctx.beginPath(); ctx.moveto(247,308); ctx.bezierCurveto(51,408,51,410,411); ctx.bezierCurveto(53,413,52,412,249,313); ctx.fillStyle = "red"; ctx.fill(); } } </script>
我不知道如何进一步发展.此外,是否有涵盖这些内容的任何地方的综合文档?
谢谢.
亲切的问候,
马吕斯
解决方法
你见过这个
https://github.com/vjt/canvas-speedometer吗?
我写了你的代码:
确保仪表的中心点必须位于图像的中心位置,否则它将无法工作.
你可以处理i的值来旋转你的引脚.
在这里,我拍了两张照片
> mt.png //用于背景仪表
http://i.stack.imgur.com/qbWeO.png
> pin3.png //用于仪表针
http://i.stack.imgur.com/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
输出:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。