本文摘自PHP中文网,作者不言,侵删。
本篇文章给大家带来的内容是关于HTML5 canvas实现中奖转盘的实例代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。最近在学canvas做动画,于是就写个转盘练下手。
上个简陋的成果图(中间那个是转的指针,外面的圈是图片,懒得写了哈哈哈)

代码很简单,都注释了,直接上代码吧,嘤嘤嘤
html
1 2 3 4 | <body>
<canvas id= "canvas" >您的浏览器不支持canvas</canvas>
<img src= "./zp.jpg" alt= "" id= "img" >
</body>
|
css
1 2 3 4 5 6 7 8 9 10 11 | <style>
#canvas{
position: absolute;
left: 230px;
top: 230px;
}
#img{
width: 600px;
height: 600px;
}
</style>
|
js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | var canvas = document.getElementById( 'canvas' );
var context = canvas.getContext( '2d' );
canvas.width=140;
canvas.height=140;
var then = Date .now();
var now;
var first_deg=0;
var rotate_deg=0;
var end_deg =85;
var speedUp = true;
var f;
context.translate(70,70);
function draw(){
context.clearRect(-70, -70, 70, 70);
context.beginPath();
context.arc(0, 0, 50, Math.PI / 180 * 0, Math.PI / 180 * 360, true);
context.moveTo(-50,0);
context.lineTo(50,0);
context.lineTo(0,-70);
context.rotate(rotate_deg*Math.PI/180);
context.closePath();
context.fillStyle= 'red' ;
context.fill();
}
function loop(){
f = window.requestAnimationFrame(loop);
now = Date .now();
if (rotate_deg<=20&&speedUp){
rotate_deg +=1;
};
if (now - then > 2000){
if (rotate_deg>=2){
speedUp = false;
rotate_deg -=1;
}
if (first_deg%360>=end_deg-5&&first_deg%360<=end_deg+5){
window.cancelAnimationFrame(f);
}
};
first_deg += rotate_deg;
draw();
}
loop();
|
最后
现在这个转盘比较大的问题就是,如果中奖区间比较小,那减速后转动的时间就长一些,最低速度转很久才停下。当区间大的时候一下子就停下了,减速效果不明显,视觉上看起来很奇怪。
相关文章推荐:
html5 canvas用来绘制弧形的代码实现
HTML5结合互联网+ 实现的3D隧道(附代码)
以上就是HTML5 canvas实现中奖转盘的实例代码的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
js (javascript)加密算法库 crypto-js 简介
css word-break属性怎么用
javascript有split函数吗
css怎么实现不透明度渐变
javascript中数组克隆的方法介绍(附代码)
javascript如何去除html标签
使用css实现皮卡丘
html的<style>标签
css内边距是什么
html 框架如何使用
更多相关阅读请进入《css》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » HTML5 canvas实现中奖转盘的实例代码