HTML5 mobile 旋轉餅圖

繼Android、IOS之後我有不知屎活的接手了‘mobile web’的東西,折騰得仙仙欲屎,還好這次旁邊有個WEB開發的同事可以給我請教,總算是能開發點東西了。

需求是要在手機上弄一個可以旋轉的圓環圖,滑動到指針所在位置,就給出對應圓環的信息,例如百分比,費用。

實現方法如下:

1、畫出餅圖Canvas(http://www.w3school.com.cn/html5/html_5_canvas.asp)

<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="100" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
//畫紅
cxt.fillStyle="#F00";
cxt.beginPath();
cxt.moveTo(50,50);
cxt.arc(50,50,50,0,Math.PI,true);
cxt.closePath();
cxt.fill();
//畫黑
cxt.fillStyle="#000";
cxt.beginPath();
cxt.moveTo(50,50);
cxt.arc(50,50,50,Math.PI,Math.PI*2,true);
cxt.closePath();
cxt.fill();
//畫小圓
cxt.fillStyle="#FFF";
cxt.beginPath();
cxt.moveTo(50,50);
cxt.arc(50,50,25,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();

</script>

</body>
</html>


Your browser does not support the canvas element.

2、旋轉

我用的是百度的Touch.js

http://touch.code.baidu.com/

https://github.com/Clouda-team/touch.code.baidu.com

結果就是這樣:

<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="100" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>

<script src="http://touch.code.baidu.com/touch-0.2.14.min.js"></script>
<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
//畫紅
cxt.fillStyle="#F00";
cxt.beginPath();
cxt.moveTo(50,50);
cxt.arc(50,50,50,0,Math.PI,true);
cxt.closePath();
cxt.fill();
//畫黑
cxt.fillStyle="#000";
cxt.beginPath();
cxt.moveTo(50,50);
cxt.arc(50,50,50,Math.PI,Math.PI*2,true);
cxt.closePath();
cxt.fill();
//畫小圓
cxt.fillStyle="#FFF";
cxt.beginPath();
cxt.moveTo(50,50);
cxt.arc(50,50,25,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();

var angle = 0;
touch.on('#myCanvas', 'touchstart', function(ev){
    ev.startRotate();
    ev.preventDefault();
});

touch.on('#myCanvas', 'rotate', function(ev){
    var totalAngle = angle + ev.rotation;
    if(ev.fingerStatus === 'end'){
        angle = angle + ev.rotation;
    }
    this.style.webkitTransform = 'rotate(' + totalAngle + 'deg)';
});
</script>

</body>
</html>

完。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值