canvas教程

HTML5制作Loading加载效果

字号+ 作者:H5之家 来源:H5之家 2015-11-11 11:09 我要评论( )

!doctype html html head meta http-equiv=content-type content=UTF-8/ titleloading/title script type=text/javascript function loading(canvas,options){ t

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="UTF-8"/>
<title>loading</title>
<script type="text/javascript">
function loading(canvas,options){
this.canvas = canvas;
if(options){
this.radius = options.radius||12;
this.circleLineWidth = options.circleLineWidth||4;
this.circleColor = options.circleColor||'lightgray';
this.dotColor = options.dotColor||'gray';
}else{
this.radius = 12;
this.circelLineWidth = 4;
this.circleColor = 'lightgray';
this.dotColor = 'gray';
}
}
loading.prototype = {
show:function (){
var canvas = this.canvas;
if(!canvas.getContext)return;
if(canvas.__loading)return;
canvas.__loading = this;
var ctx = canvas.getContext('2d');
var radius = this.radius;
var rotators = [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];
var me = this;
canvas.loadingInterval = setInterval(function(){
ctx.clearRect(0,0,canvas.width,canvas.height);
var lineWidth = me.circleLineWidth;
var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = me.circleColor;
ctx.arc(center.x,center.y,radius,0,Math.PI*2);
ctx.closePath();
ctx.stroke();
for(var i=0;i<rotators.length;i++){
var rotatorAngle = rotators[i].currentAngle||rotators[i].angle;
var rotatorCenter = {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};
var rotatorRadius = rotators[i].radius;
ctx.beginPath();
ctx.fillStyle = me.dotColor;
ctx.arc(rotatorCenter.x,rotatorCenter.y,rotatorRadius,0,Math.PI*2);
ctx.closePath();
ctx.fill();
rotators[i].currentAngle = rotatorAngle+4/radius;
}
},50);
},
hide:function(){
var canvas = this.canvas;
canvas.__loading = false;
if(canvas.loadingInterval){
window.clearInterval(canvas.loadingInterval);
}
var ctx = canvas.getContext('2d');
if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
}
};
</script>
</head>
<body>
<canvas id="canvas" width="300" height="100" style="border:1px solid #69c"></canvas>
<p>
<input type="button" onclick="loadingObj.hide()" value="HideLoading"/>
<input type="button" onclick="loadingObj.show()" value="showLoading"/>
</p>
<script>
var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
loadingObj.show();
</script>
</body>
</html>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  •  canvas 魔兽技巧冷却效果

    canvas 魔兽技巧冷却效果

    2017-04-28 12:00

  • android自定义组件(手机加速球+水面波动效果)

    android自定义组件(手机加速球+水面波动效果)

    2017-04-26 12:03

  • 使用canvas实现擦除效果

    使用canvas实现擦除效果

    2017-04-23 18:00

  • JavaScript Canvas绘制圆形时钟效果

    JavaScript Canvas绘制圆形时钟效果

    2017-04-21 09:04

网友点评