zoukankan      html  css  js  c++  java
  • canvas实现帧动画

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            canvas {
                border: 1px solid #ccc;
            }
        </style>
    </head>
    <body>
    <!--<img src="image/01.jpg" alt="">-->
    <canvas width="600" height="400"></canvas>
    <script>
        var myCanvas = document.querySelector('canvas');
        var ctx = myCanvas.getContext('2d');
        var image = new Image();
        image.onload = function () {
            /*图片加载完成*/
            /*动态的去获取当前图片的尺寸*/
            var imageWidth = image.width;
            var imageHeight = image.height;
            /*计算出每一个小人物的尺寸*/
            var personWidth = imageWidth/4;
            var personHeight = imageHeight/4;
            /*位截取图片*/
            /*帧动画  在固定的时间间隔更换显示的图片  根据图片的索引*/
            var index = 0;
            /*绘制在画布的中心*/
            /*图片绘制的起始点*/
            var x0 = ctx.canvas.width /2 - personWidth / 2;
            var y0 = ctx.canvas.height /2 - personHeight / 2;
            ctx.drawImage(image,0,0,personWidth,personHeight,x0,y0,personWidth,personHeight);
            setInterval(function () {
                index ++;
                ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
                ctx.drawImage(image,index * personWidth,0,personWidth,personHeight,x0,y0,personWidth,personHeight);
                if(index >= 3){
                    index = 0;
                }
            },1000);
        };
        image.src = 'image/04.png';
    </script>
    </body>
    </html>

    运行结果如下:

    别废话,拿你代码给我看。
  • 相关阅读:
    js 数据图表
    yii query builder
    mysql if
    这又是起点
    [cookie篇]从cookie-parser中间件说起
    How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system
    AngularJS打印问题
    笔记本上班时间自动静音下班自动打开
    SCP命令
    Installing Ruby 1.9.3 on Ubuntu 12.04 Precise Pengolin (without RVM)
  • 原文地址:https://www.cnblogs.com/lvxueyang/p/13707442.html
Copyright © 2011-2022 走看看