Contents
  1. 1. 一、 帧动画
  2. 2. 二、 方向键控制行走的小人
  3. 3. 三、坐标变换

一、 帧动画

绘制图片 —–使用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画布中的一个区域

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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';

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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) 参数表示旋转角度

示例:

1
2
3
4
5
6
7
var startAngle = 0;
ctx.translate(150,150);
setInterval(function () {
startAngle += Math.PI/180;
ctx.rotate(startAngle);
ctx.strokeRect(-50,-50,100,100);
},500);

结果生成一个旋转的矩形

Contents
  1. 1. 一、 帧动画
  2. 2. 二、 方向键控制行走的小人
  3. 3. 三、坐标变换