【问题标题】:Canvas Bar Graph Animation画布条形图动画
【发布时间】:2014-03-25 05:40:50
【问题描述】:

我正在努力加快画布标签的速度,但在涉及基本动画时却遇到了困难。我四处搜索,可以找到我发现的大部分教程,但我在将其转化为我想要做的事情时遇到了一些麻烦。

我正在为我的投资组合构建一个条形图,并且我希望能够为图表上的条形设置动画。基本上,当页面加载时,条形图从图表底部开始,然后向上增长到它们需要的位置。我有一个我在 jquery 中构建的版本,在这里可以了解我所追求的:http://jokedesigns.com/portfoliov6/

有人可以为我指出如何实现我正在寻找的动画的正确方向吗?即使它是一个简单的功能,我也应该能够将它改造成我需要的东西。我发现的大多数教程主要涉及旋转,我确实找到了一个线性动画,但我仍然无法将其完全改造成我需要的。

这是我到目前为止的图表代码:

<html>
  <head>
    <script type="application/javascript">
function loadImages(sources, callback) {
    var images = {};
    var loadedImages = 0;
    var numImages = 0;
    // get num of sources
    for(var src in sources) {
      numImages++;
    }
    for(var src in sources) {
      images[src] = new Image();
      images[src].onload = function() {
        if(++loadedImages >= numImages) {
          callback(images);
        }
      };
      images[src].src = sources[src];
    }
  }
function draw() {
  var canvas = document.getElementById("canvas");
   var canvasbg = document.getElementById("canvasbg");
  if (canvas.getContext) {
    var ctx = canvas.getContext("2d");
    var ctx2 = canvasbg.getContext("2d");
    var img = new Image();
    img.onload = function(){
        ctx2.drawImage(img,0,0);
    };
    img.src = 'skills_bg.jpg';
    ctx.fillStyle = "#0a4888";
    ctx.fillRect (0, 82, 34, 50);
    ctx.fillStyle = "#ed9323";
    ctx.fillRect (60, 82, 34, 50);
    ctx.fillStyle = "#87982d";
    ctx.fillRect (120, 82, 34, 50);
    ctx.fillStyle = "#902e63";
    ctx.fillRect (180, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (360, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (420, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (480, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (540, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (600, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (660, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (720, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (780, 82, 34, 50);
    ctx.fillStyle = "#262627";
    ctx.fillRect (840, 82, 34, 50);

    var sources = {
        ps: 'icn_ps.png',
        ai: 'icn_ai.png',
        dw: 'icn_dw.png',
        id: 'icn_id.png',
        ht: 'icn_html.png',
        cs: 'icn_css.png',
        js: 'icn_js.png',
        sql: 'icn_mysql.png',
        php: 'icn_php.png',
        perl: 'icn_perl.png',
        ruby: 'icn_ruby.png',
        cplus: 'icn_cplusplus.png',
        asp: 'icn_asp.png'
    };
    loadImages(sources, function(images) {
        ctx.drawImage(images.ps, 0, 132, 36, 37);
        ctx.drawImage(images.ai, 60, 132, 36, 37);
        ctx.drawImage(images.dw, 120, 132, 36, 37);
        ctx.drawImage(images.id, 180, 132, 36, 37);
        ctx.drawImage(images.ht, 360, 132, 36, 37);
        ctx.drawImage(images.cs, 420, 132, 36, 37);
        ctx.drawImage(images.js, 480, 132, 36, 37);
        ctx.drawImage(images.sql, 540, 132, 36, 37);
        ctx.drawImage(images.php, 600, 132, 36, 37);
        ctx.drawImage(images.perl, 660, 132, 36, 37);
        ctx.drawImage(images.ruby, 720, 132, 36, 37);
        ctx.drawImage(images.cplus, 780, 132, 36, 37);
        ctx.drawImage(images.asp, 840, 132, 36, 37);
    });

  }
}

</script>
</head>
<body onload="draw();">
<canvas id="canvasbg" width="960" height="200" style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="canvas" width="960" height="200" style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</body>
</html>

【问题讨论】:

    标签: javascript html animation html5-canvas


    【解决方案1】:

    您可以使用 requestAnimationFrame 创建动画循环,让您的条形图增长

    要为高度不均匀的条形创建均匀增长,您可以应用百分比

    // this will grow all bars at an even rate
    
    bar1Height = bar1.maxHeight * percentComplete/100;
    
    bar2Height = bar2.maxHeight * percentComplete/100;  
    
    percentComplete++;
    

    这是示例代码和演示:http://jsfiddle.net/m1erickson/YR4D7/

    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" />
    <script src="http://code.jquery.com/jquery.min.js"></script>
    <style>
        body{ background-color: ivory; }
        canvas{border:1px solid red;}
    </style>
    <script>
        $(function(){
    
            var canvas=document.getElementById("canvas");
            var ctx=canvas.getContext("2d");
    
            var skillBars=[];
            skillBars.push({max:200,color:"red"});
            skillBars.push({max:75,color:"green"});
            skillBars.push({max:275,color:"blue"});
    
            var chartBottomY=325;
            var chartBarWidth=30;
            var chartBarPadding=20;
            var percent=0;
    
            animate();
    
            function animate() {
                // if not 100% done, request another frame
                if(percent++<100){
                    requestAnimationFrame(animate);
                }
    
                // Drawing code goes here
                ctx.clearRect(0,0,canvas.width,canvas.height);
                var x=chartBarPadding;
                for(var i=0;i<skillBars.length;i++){
                    var height=skillBars[i].max*percent/100;
                    ctx.fillStyle=skillBars[i].color;
                    ctx.fillRect(x,chartBottomY,chartBarWidth,-height);
                    x+=chartBarWidth+chartBarPadding;
                }
            }
    
        }); // end $(function(){});
    </script>
    </head>
    <body>
        <canvas id="canvas" width=350 height=350></canvas>
    </body>
    </html>
    

    【讨论】:

    • 这是一个巨大的帮助!非常感谢!
    【解决方案2】:

    我发现了几个问题。

    首先,您一次绘制所有内容,而您应该在加载每个图像后进行绘制,但是 yiu 会想要使用 setTimeout 或类似的东西来允许绘制画布元素。

    你还应该有一个 bar 的方法,让它记住当前的 step 值,它只会绘制下一个块。

    我会将 onload 放在图像标签 image.onload function with return 上,当它加载时绘制下一个栏并加载下一个。

    但请记住使用 setTimeout 来允许绘制。

    这里有一些可能有帮助的链接

    JavaScript animation with multiple setTimeout

    https://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/HTML-canvas-guide/AnimatingtheCanvas/AnimatingtheCanvas.html

    【讨论】:

    • 感谢您的提示和链接。我有点想我可能在绘图部分做错了什么。在看到您的一些资源后,我将不得不重新审视我的工作方式。
    猜你喜欢
    • 2019-03-03
    • 2011-08-17
    • 1970-01-01
    • 2016-12-03
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多