我正在寻找一个简单的图像加载动画。我想找到一个简短的解决方案,它只使用画布上的绘图和纯 Javascript。
请帮忙
我会使用由在线工具(如Ajaxload.info或Preloaders.net )生成的加载 GIF ,并将其放置在您可以在 canvas 元素前面切换的 div 标签中。
有关更多微调器生成器脚本,请参阅5 在线加载 AJAX 微调器生成器工具。
希望这可以帮助!
drawProgressIndicator: function(){
var can = this.imgCanvas;
var ctx = this.imgCtx;
ctx.save();
ctx.clearRect(0, 0, can.width, can.height);
ctx.translate(can.width / 2, can.height / 2);
ctx.scale(0.4, 0.4);
ctx.rotate(-Math.PI / 2);
ctx.strokeStyle = "black";
ctx.fillStyle = "white";
ctx.lineWidth = 8;
ctx.lineCap = "round";
var step = this.animationStep;
ctx.fillStyle = "black";
ctx.save();
ctx.rotate(step * Math.PI / 30);
ctx.strokeStyle = "#33ccff";
ctx.fillStyle = "#33ccff";
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(68, 0);
ctx.stroke();
ctx.fill();
ctx.restore();
ctx.beginPath();
ctx.lineWidth = 14;
ctx.strokeStyle = 'gray';
ctx.arc(0, 0, 80, 0, Math.PI * 2, true);
ctx.stroke();
ctx.restore();
this.animationStep += 1;
},
这是我的加载,其中有 2 个使用画布的圆圈
var canvas = document.createElement("canvas");
canvas.width = 200;
canvas.height = 200;
canvas.style.backgroundColor = "black";
document.body.appendChild(canvas);
var ctx = canvas.getContext("2d");
var bigCircle = {
center: {
x: 100,
y: 100
},
radius: 50,
speed: 4
}
var smallCirlce = {
center: {
x: 100,
y: 100
},
radius: 30,
speed: 2
}
var progress = 0;
function loading() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
progress += 0.01;
if (progress > 1) {
progress = 0;
}
drawCircle(bigCircle, progress);
drawCircle(smallCirlce, progress);
requestAnimationFrame(loading);
}
loading();
function drawCircle(circle, progress) {
ctx.beginPath();
var start = accelerateInterpolator(progress) * circle.speed;
var end = decelerateInterpolator(progress) * circle.speed;
ctx.arc(circle.center.x, circle.center.y, circle.radius, (start - 0.5) * Math.PI, (end - 0.5) * Math.PI);
ctx.lineWidth = 3;
ctx.strokeStyle = "white";
ctx.fill();
ctx.stroke();
}
function accelerateInterpolator(x) {
return x * x;
}
function decelerateInterpolator(x) {
return 1 - ((1 - x) * (1 - x));
}
希望有帮助
试试这个.. http://jsfiddle.net/swarnendu/Ky25d/
<html>
<head>
<style type="text/css">
#layer6,#layer7,#layer8{position: absolute;left: 0px;top: 0px;}
.anim_8{-webkit-animation-duration: 1.5s;-webkit-animation-iteration-count:infinite;-webkit-transform-origin:25px 25px;-webkit-animation-name:animation_6;-webkit-animation-timing-function: linear;
-moz-animation-timing-function: linear; -moz-animation-duration: 1.5s; -moz-animation-name: animation_6; -moz-transform-origin:25px 25px; -moz-animation-iteration-count:infinite ;
-o-animation-timing-function: linear; -o-animation-duration: 1.5s; -o-animation-name: animation_6; -o-transform-origin:25px 25px; -o-animation-iteration-count:infinite ;}
@-webkit-keyframes animation_6{
0%{ -webkit-transform: rotate(0deg) ;}
100%{ -webkit-transform: rotate(360deg) ;}
}
@-o-keyframes animation_6{
0%{ -o-transform: rotate(0deg) ;}
100%{ -o-transform: rotate(360deg) ;}
}
@-moz-keyframes animation_6{
0%{ -moz-transform: rotate(0deg) ;}
100%{ -moz-transform: rotate(360deg) ;}
}
</style>
<script type="text/javascript">
window.addEventListener("load",draw_canvas,false) ;
function draw_canvas()
{
var c6=document.getElementById("layer6");var ctx6=c6.getContext("2d"); ctx6.fillStyle="#FF000
这篇文章:https ://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Basic_animations帮助我绘制了自己的动画。
这是一个仅使用 javascript 和 canvas- animation的动画 它是使用应用程序canvimation创建的,帮助文件在这里。
但是,由于它不使用图像并且它并不简单,因此它可能不是您想要的。
我能想到的唯一另一种方法是使用context.drawImage和setTimeout来加载一系列图像
或者真的只是在画布中使用 gif 图像,如下面的代码所示
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function main() {
img=document.getElementById("anigif");
Ani=$("ani");
Ani.width=500;
Ani.height400;
Ani.ctx=Ani.getContext('2d');
Ani.ctx.drawImage(img,0,0)
}
</script>
</head>
<body onload="main()">
<img id="anigif" src="http://www.mathworks.com/matlabcentral/fx_files/21944/2/DancingPeaks.gif">
<canvas id="ani"></canvas>
</body>
</html>
如果您正在使用 html5 画布搜索简单的加载动画,那么这里就是.. http://jsfiddle.net/swarnendu/urV4p/
<html>
<head>
<style type="text/css">
#d1,#ci1,#ci2,#ci3{position:absolute;top:0px;left:0px}
.cc2{ -webkit-animation-duration: 1s; -webkit-animation-name: loading2w; -webkit-animation-iteration-count:infinite; -moz-animation-duration: 1s; -moz-animation-name: loading2m; -moz-animation-iteration-count:infinite; -o-animation-duration: 1s; -o-animation-name: loading2o; -o-animation-iteration-count:infinite;}
.cc3{ -webkit-animation-duration: 1s; -webkit-animation-name: loading3w; -webkit-animation-iteration-count:infinite; -moz-animation-duration: 1s; -moz-animation-name: loading3m; -moz-animation-iteration-count:infinite; -o-animation-duration: 1s; -o-animation-name: loading3o; -o-animation-iteration-count:infinite;}
@-webkit-keyframes loading2w{
0%{visibility:hidden;}
33%{visibility:hidden;}
33.33%{visibility:visible;}
100%{visibility:hidden;}
}
@-moz-keyframes loading2m{
0%{visibility:hidden;}
33%{visibility:hidden;}
33.33%{visibility:visible;}
100%{visibility:hidden;}
}
@-o-keyframes loading2o{
0%{visibility:hidden;}
33%{visibility:hidden;}
33.33%{visibility:visible;}
100%{visibility:hidden;}
}
@-webkit-keyframes loading3w
{
0%{visibility:hidden;}
66.33%{visibility:hidden;}
66.67%{visibility:visible;}
100%{visibility:hidden;}
}
@-moz-keyframes loading3m
{
0%{visibility:hidden;}
66.33%{visibility:hidden;}
66.67%{visibility:visible;}
100%{visibility:hidden;}
}
@-o-keyframes loading3o
{
0%{visibility:hidden;}
66.33%{visibility:hidden;}
66.67%{visibility:visible;}
100%{visibility:hidden;}
}
</style>
<script type="text/javascript">
window.addEventListener("load",win_load,false);
function win_load()
{
var c10=document.getElementById("ci1");var ctx10=c10.getContext("2d");ctx10.fillStyle="#f00";ctx10.font="15pt Calibri";ctx10.fillText("Loading .",5,15);
var c10=document.getElementById("ci2");var ctx10=c10.getContext("2d");ctx10.fillStyle="#f00";ctx10.font="15pt Calibri";ctx10.fillText(" .",5,15);
var c10=document.getElementById("ci3");var ctx10=c10.getContext("2d");ctx10.fillStyle="#f00";ctx10.font="15pt Calibri";ctx10.fillText(" .",5,15);
}
</script>
</head>
<body>
<div id="d1">
<canvas id="ci1" class="cc1" width="100" height="100"></canvas>
<canvas id="ci2" class="cc2" width="100" height="100"></canvas>
<canvas id="ci3" class="cc3" width="100" height="100"></canvas>
</div>
</body>