canvas学习第一章

canvas学习第一章

基本知识

  • ctx.fillStyle = ‘#000000’ 设置文本颜色
  • ctx.font = ‘20px _sans’ 设置字体大小与字号
  • ctx.textBaseline = ‘top’ 设置字体垂直对齐方式
  • ctx.fillText(‘Hello World’,195,80) 将测试文本输出到屏幕上
  • ctx.strokeStyle = ‘#000000’ 用于方块边框的颜色
  • ctx.strokeRect(5,5,490,290) 绘制矩形边框
  • ctx.fillRect(0,0,500,300) 填充矩形
  • canvas.toDataURL() 这个方法返回的数据是代表当前canvas对象产生的位图字符串,就像屏幕的一个快照,通过提供一个不同MIME类型作为参数,可以返回不同的数据格式。基本的格式是image/png,但也可以获取image/jpeg和其它格式。
  • 绘制图片
    1
    2
    3
    4
    5
    var image = new Image();
    image.src = 'hello.gif';
    image.onload = function(){ //回调函数在image加载成功时触发
    ctx.drawImage(image,160,130); //图像对象,与坐标
    }

我们利用以上知识来制作一个猜字母小游戏

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CH1Ex4: Guesss The Letter Game </title>
<script src="modernizr-1.6.min.js"></script>
<script type="text/javascript">
//当页面加载完成时触发eventWindowLoaded方法
window.addEventListener('load', eventWindowLoaded, false);
var Debugger = function () { };
Debugger.log = function (message) {
try {
console.log(message);
} catch (exception) {
return;
}
}
function eventWindowLoaded() {
canvasApp();
}
function canvasSupport () {
return Modernizr.canvas;
}
function eventWindowLoaded() {
canvasApp();
}
function canvasApp() {
var guesses = 0; //猜的次数
var message = "Guess The Letter From a (lower) to z (higher)";
var letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var today = new Date();
var letterToGuess = ""; //要猜的字母
var higherOrLower = ""; //键盘按下的字母是大还是小
var lettersGuessed; //键盘按下的字母
var gameOver = false;
if (!canvasSupport()) {
return;
}
var theCanvas = document.getElementById("canvasOne");
var context = theCanvas.getContext("2d"); //获得canvas的上下文
initGame(); //初始化游戏
function initGame() {
var letterIndex = Math.floor(Math.random() * letters.length);
letterToGuess = letters[letterIndex]; //随机生成一个字母
guesses = 0;
lettersGuessed = []; //存储已经猜的字母
gameOver = false; //游戏成功与否标注位
window.addEventListener("keyup",eventKeyPressed,true); //监听键盘事件
var formElement = document.getElementById("createImageData"); //生成快照
formElement.addEventListener('click', createImageDataPressed, false);
drawScreen();
}
function eventKeyPressed(e) {
if (!gameOver) {
var letterPressed = String.fromCharCode(e.keyCode); //获得键入的字母
letterPressed = letterPressed.toLowerCase(); //转换成小写
guesses++; //猜测次数加一
lettersGuessed.push(letterPressed); //存储到数组中
if (letterPressed == letterToGuess) { //如果猜对了
gameOver = true; //标志位置true
} else {
letterIndex = letters.indexOf(letterToGuess); //被猜的字母在数组中的位置
guessIndex = letters.indexOf(letterPressed); //键入的字母在数组中的位置
Debugger.log(guessIndex);//控制台调试
if (guessIndex < 0) { //如果输入的不是字母,返回-1
higherOrLower = "That is not a letter";
} else if (guessIndex > letterIndex) { //输入偏大
higherOrLower = "Lower";
} else {
higherOrLower = "Higher"; //输入偏小
}
}
drawScreen();
}
}
function drawScreen() {
//绘制背景
context.fillStyle = "#ffffaa";
context.fillRect(0, 0, 500, 300);
//绘制边框
context.strokeStyle = "#000000";
context.strokeRect(5, 5, 490, 290);
context.textBaseline = "top";
//绘制日期
context.fillStyle = "#000000";
context.font = "10px _san";
context.fillText (today, 150 ,10);
//绘制消息
context.fillStyle = "#FF0000";
context.font = "14px _sans";
context.fillText (message, 125, 30);
//绘制猜测次数
context.fillStyle = "#109910";
context.font = "16px _sans";
context.fillText ('Guesses: ' + guesses, 215, 50);
//输入是大还是小
context.fillStyle = "#000000";
context.font = "16px _sans";
context.fillText ("Higher Or Lower: " + higherOrLower, 150,125);
//Letters Guessed
context.fillStyle = "#FF0000";
context.font = "16px _sans";
context.fillText ("Letters Guessed: " + lettersGuessed.toString(), 10, 260);
if (gameOver) {
context.fillStyle = "#FF0000";
context.font = "40px _sans";
context.fillText ( "You Got it!", 150, 180);
}
}
function createImageDataPressed(e) {
window.open(theCanvas.toDataURL(),"canavsImage","left=0,top=0,width=" + theCanvas.width + ",height=" + theCanvas.height +",toolbar=0,resizable=0");
}
}
</script>
</head>
<body>
<div style="position: absolute; top: 50px; left: 50px;">
<canvas id="canvasOne" width="500" height="300">
Your browser does not support HTML 5 Canvas.
</canvas>
<form>
<input type="button" id="createImageData" value="Export Canvas Image">
</form>
</div>
</body>
</html>