Canvas之绘制帧动画

13 篇文章 0 订阅
4 篇文章 0 订阅

一、 帧动画

绘制图片 -----使用drawImage()

  • 三个参数drawImage(img, x,y)
    • img 图片对象,Canvas对象,video对象
    • x,y 图片绘制的左上角
  • 五个参数 drawImage(img, x, y, w, h)
    • img 图片对象,Canvas对象,video对象
    • x,y 图片绘制的左上角
    • w,h图片绘制尺寸设置(图片缩放,不是截取)
  • 九个参数 drawImage(img, x, y, w, h,x1,y1,w1,h1)
    • img 图片对象,Canvas对象,video对象
    • x,y ,w,h图片中的区域
    • x1,y1,w1,h1画布中的一个区域

首先确定画布中心,然后根据定时器定时切换图片

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';

结果就可以看见在画布中央有一个走动的小人了,可以根据改变定时器的时间,改变走的的频率

二、 方向键控制行走的小人

如果是通过方向键控制行走的小人,则需要获得键值,通过键值判断方向,当键值切换时,改变图片在画布中心的定位而来改变图片行走

var Person = function (ctx) {
        /*绘制工具*/
        this.ctx = ctx || document.querySelector('canvas').getContext('2d');
        /*图片路径*/
        this.src = 'image/04.png';
        /*画布的大小*/
        this.canvasWidth = this.ctx.canvas.width;
        this.canvasHeight = this.ctx.canvas.height;

        /*行走相关参数*/
        this.stepSzie = 10;
        /* 0 前  1 左  2 右  3 后  和图片的行数包含的图片对应上*/
        this.direction = 0;
        /*x轴方向的偏移步数*/
        this.stepX = 0;
        /*y轴方向的偏移步数*/
        this.stepY = 0;

        /*初始化方法*/
        this.init();
    };

    Person.prototype.init = function () {
        var that = this;
        /*1.加载图片*/
        this.loadImage(function (image) {
            /*图片的大小*/
            that.imageWidth = image.width;
            that.imageHeight = image.height;
            /*人物的大小*/
            that.personWidth = that.imageWidth / 4;
            that.personHeight = that.imageHeight / 4;
            /*绘制图片的起点*/
            that.x0 = that.canvasWidth / 2 - that.personWidth / 2;
            that.y0 = that.canvasHeight / 2 - that.personHeight / 2;
            /*2.默认绘制在中心位置正面朝外*/
            that.ctx.drawImage(image,
                0,0,
                that.personWidth,that.personHeight,
                that.x0,that.y0,
                that.personWidth,that.personHeight);

            /*3.能通过方向键去控制人物行走*/
            that.index = 0;
            document.onkeydown = function (e) {
                if(e.keyCode == 40){
                    that.direction = 0;
                    that.stepY ++;
                    that.drawImage(image);
                    /*前*/
                }else if(e.keyCode == 37){
                    that.direction = 1;
                    that.stepX --;
                    that.drawImage(image);
                    /*左*/
                }else if(e.keyCode == 39){
                    that.direction = 2;
                    that.stepX ++;
                    that.drawImage(image);
                    /*右*/
                }else if(e.keyCode == 38){
                    that.direction = 3;
                    that.stepY --;
                    that.drawImage(image);
                    /*后*/
                }
            }
        });
    }
    /*加载图片*/
    Person.prototype.loadImage = function (callback) {
        var image = new Image();
        image.onload = function () {
            callback && callback(image);
        };
        image.src = this.src;
    };
    /*绘制图片*/
    Person.prototype.drawImage = function (image) {
        this.index ++;
        /*清除画布*/
        this.ctx.clearRect(0,0,this.canvasWidth,this.canvasHeight);
        /*绘图*/
        /*在精灵图上的定位 x  索引*/
        /*在精灵图上的定位 y  方向*/
        this.ctx.drawImage(image,
            this.index * this.personWidth,this.direction * this.personHeight,
            this.personWidth,this.personHeight,
            this.x0 + this.stepX * this.stepSzie ,this.y0 + this.stepY * this.stepSzie,
            this.personWidth,this.personHeight);
        /*如果索引超出了 变成0*/
        if(this.index >= 3){
            this.index = 0;
        }
    };


    new Person();

三、坐标变换

  • 平移,移动画布的原点
    • translate(x, y) 参数代表移动目标点的坐标
  • 缩放
    • scale(x, y) 参数表示宽高的缩放比
  • 旋转
    • rotate(angle) 参数表示旋转角度

示例:

var startAngle = 0;
    ctx.translate(150,150);
    setInterval(function () {
        startAngle += Math.PI/180;
        ctx.rotate(startAngle);
        ctx.strokeRect(-50,-50,100,100);
    },500);

结果生成一个旋转的矩形

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值