在HTML5 canvas上简单加载动画

时间:2022-12-15 17:58:30

I am looking for a simple image loading animation. I would like to find a short and simple sollution that only uses drawing on the canvas and plain Javascript.

我正在寻找一个简单的图像加载动画。我希望找到一种简单而简单的方法,只在画布和普通Javascript上使用。

Pleas help

请求帮助

6 个解决方案

#1


9  

I would use a loading GIF generated by an online tool such as Ajaxload.info or Preloaders.net and place it in a div tag that you may toggle in front of the canvas element.

我将使用由在线工具(如Ajaxload.info或Preloaders.net)生成的加载GIF,并将其放置在一个div标记中,您可以在canvas元素前面进行切换。

For more spinner generator scripts see 5 Online Loading AJAX Spinner Generator Tools.

有关更多的spinner生成器脚本,请参阅5个在线加载AJAX spinner生成器工具。

Hope this helps!

希望这可以帮助!

#2


4  

try this one .. http://jsfiddle.net/swarnendu/Ky25d/

试试这个. .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

#3


3  

 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;
},

#4


3  

This article:https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Basic_animations helped me draw my own animation.

本文:https://developer.mozilla.org/en- us/docs/guide/html/canvas_tutorial/basic_animation帮助我绘制了自己的动画。

#5


2  

Here is an animation that uses only javascript and canvas - animation It was created using the application canvimation the help files for this are here .

这是一个只使用javascript和canvas的动画——它是使用应用程序canvimation创建的,这里有帮助文件。

However as it does not use images and it is not simple it may not be what you are looking for.

然而,由于它不使用图像,也不简单,它可能不是您要寻找的。

The only other way I could think would be to use context.drawImage along with setTimeout to load a series of images on after another

我唯一能想到的另一种方法是使用上下文。drawImage和setTimeout一起加载一系列图像

OR really simply use a gif image in canvas as in the code below

或者只是像下面的代码那样在画布中使用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>

#6


0  

If you are searching for a simple loading animation with html5 canvas then here it is.. http://jsfiddle.net/swarnendu/urV4p/

如果你正在搜索一个简单的html5 canvas加载动画,那么它就在这里。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>

#1


9  

I would use a loading GIF generated by an online tool such as Ajaxload.info or Preloaders.net and place it in a div tag that you may toggle in front of the canvas element.

我将使用由在线工具(如Ajaxload.info或Preloaders.net)生成的加载GIF,并将其放置在一个div标记中,您可以在canvas元素前面进行切换。

For more spinner generator scripts see 5 Online Loading AJAX Spinner Generator Tools.

有关更多的spinner生成器脚本,请参阅5个在线加载AJAX spinner生成器工具。

Hope this helps!

希望这可以帮助!

#2


4  

try this one .. http://jsfiddle.net/swarnendu/Ky25d/

试试这个. .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

#3


3  

 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;
},

#4


3  

This article:https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Basic_animations helped me draw my own animation.

本文:https://developer.mozilla.org/en- us/docs/guide/html/canvas_tutorial/basic_animation帮助我绘制了自己的动画。

#5


2  

Here is an animation that uses only javascript and canvas - animation It was created using the application canvimation the help files for this are here .

这是一个只使用javascript和canvas的动画——它是使用应用程序canvimation创建的,这里有帮助文件。

However as it does not use images and it is not simple it may not be what you are looking for.

然而,由于它不使用图像,也不简单,它可能不是您要寻找的。

The only other way I could think would be to use context.drawImage along with setTimeout to load a series of images on after another

我唯一能想到的另一种方法是使用上下文。drawImage和setTimeout一起加载一系列图像

OR really simply use a gif image in canvas as in the code below

或者只是像下面的代码那样在画布中使用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>

#6


0  

If you are searching for a simple loading animation with html5 canvas then here it is.. http://jsfiddle.net/swarnendu/urV4p/

如果你正在搜索一个简单的html5 canvas加载动画,那么它就在这里。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>