用HTML5把Canvas緩沖區(qū)內(nèi)容輸出到屏幕
我們使用使用HTML編寫網(wǎng)頁游戲,現(xiàn)在卻在使用HTML5。那么兩者有區(qū)別嗎?元素用來創(chuàng)建客戶端游戲時非常方便。針對客戶端游戲編程,Canvas和JavaScript使用起來非常簡單。
典型的游戲類
在開始我們的游戲?qū)崿F(xiàn)和畫布元素之前,我想很快向您展示典型的游戲類結(jié)構(gòu):
function Game() {
this.Initialize = function () {
// initialize all game variables
}
this.LoadContent = function () {
// load content – graphics, sound etc.
// since all content is loaded run main game loop
// Calls RunGameLoop method every ‘draw interval’
this.GameLoop = setInterval(this.RunGameLoop, this.DrawInterval);
}
this.RunGameLoop = function (game) {
this.Update();
this.Draw();
}
this.Update = function () {
// update game variables, handle user input, perform calculations etc.
}
this.Draw = function () {
// draw game frame
}
}
這是一個典型的JavaScript游戲骨架。有趣的是“setInterval”方法。當完成所有資源的加載后,我們可以開始主要的游戲循環(huán),收集用戶輸入,執(zhí)行計算,并每隔X毫秒渲染我們的游戲。這種方式特別適用于哪些后臺有計算程序,人工智能運動、動畫等相關(guān)的游戲。對于哪些只基于用戶輸入而重畫游戲內(nèi)容的靜態(tài)游戲,可以修改這個框架類和去除游戲循環(huán)。
function Game() {
this.Initialize = function () {
// initialize all game variables
}
this.LoadContent = function () {
// load content – graphics, sound etc.
var ourGame = this;
$(document).bind('keyup', function (event) {
ourGame.Update(event);
ourGame.Draw();
});
}
this.Update = function (event) {
// update game variables, handle user input, perform calculations etc.
}
this.Draw = function () {
// draw game frame
}
}
這兩個游戲更新和重畫操作均只發(fā)生于用戶輸入。這種方法有效減少了CPU負載,只適用于簡單的游戲。
我要給你示例一個大家熟知的非常簡單的經(jīng)典游戲Sokoban。濟南網(wǎng)頁制作(www.mwinds.net)幾乎每個游戲平臺都有不同版本的sokoban,但是我還沒有看到任何使用canvas元素的應(yīng)用。
canvas入門
讓我們僅使用單個canvas元素,開始創(chuàng)建我們的HTML5頁面。
<html>
<head>
<title>Sokoban</title>
</head>
<body>
<canvas id="canvas" width="800" height="500">
Canvas not supported.
</canvas>
</body>
</html>
就是這樣!現(xiàn)在我們可以看到一個支持Canvas元素的空白頁面,出現(xiàn)在支持的瀏覽器:Chrome、Firefox和Safari。IE 8以及相應(yīng)的舊版本不支持Canvas。
在我們開始利用畫布繪畫前,我們需要得到繪圖上下文。Canvas公開一個或多個繪圖上下文,但是我們將專注于最受歡迎的一個——“2 d”上下文。
讓我們添加一個引用JavaScript文件后直接我們關(guān)閉canvas標記:
<script type="text/javascript" src="../Scripts/test01.js"></script>
這就是部分的JavaScript文件。
var _canvas = document.getElementById('canvas');
var _canvasContext = null;
//check whether browser supports getting canvas context
if (_canvas && _canvas.getContext) {
_canvasContext = _canvas.getContext('2d');
// ... drawing here ...
}
在本教程中,我將快速講解canvas上下文相關(guān)的繪畫方法,他們是:
rawImage(img,x,y);
? fillRect(x,y,width、height);
? strokeRect(x,y, width、height);
? fillText(“text”,x,y);
重要提示:Canvas的開始坐標 (0,0)位于左上角。
這些方法非常簡單。drawImage要么繪制指定Image對象,要么根據(jù)x,y坐標,繪畫
<Img>
元素。fillRect和strokeRect都用于繪圖的矩形。唯一的區(qū)別是,fillRect是畫一個填充色彩的矩形,而strokeRect是畫一個空矩形,邊框為彩色。fillText用于在畫布上放置文本。
點擊以下鏈接,瀏覽演示內(nèi)容:
http://demo2.felinesoft.com/Sokoban/Home/Test01
關(guān)于渲染2個矩形,文本和圖像的JavaScript源文件,可以在這里找到:
http://demo2.felinesoft.com/Sokoban/Scripts/Test01.js
這就是測試頁面:
雙緩沖
因為我們現(xiàn)在有一個游戲骨架,我們也知道如何利用Canvas繪圖。在實現(xiàn)真正的游戲之前,唯一剩下的是雙緩沖技術(shù)。不過我們不會活靈活現(xiàn)地演示這種技術(shù),因為我們沒有閃爍的動畫效果。但是,既然這篇文章是你學(xué)習(xí)Canvas游戲的起點,我尋思著應(yīng)該向您展示如何利用雙緩沖技術(shù)在Canvas快速繪畫。
雙緩沖技術(shù)背后的想法是減少閃爍:首先基于內(nèi)存緩沖區(qū)繪畫,然后將內(nèi)存中渲染完成的圖像,刷入到屏幕上。
我們只需要稍微修改我們的Canvas的JavaScript:
_canvas = document.getElementById('canvas');
if (_canvas && _canvas.getContext) {
_canvasContext = _canvas.getContext('2d');
_canvasBuffer = document.createElement('canvas');
_canvasBuffer.width = _canvas.width;
_canvasBuffer.height = _canvas.height;
_canvasBufferContext = _canvasBuffer.getContext('2d');
}
現(xiàn)在,我們就調(diào)用_canvasBufferContext 而非_canvasContext對象繪圖了。如下:
_canvasContext.drawImage(_canvasBuffer, 0, 0);
這樣就把Canvas緩沖區(qū)的內(nèi)容輸出到屏幕。就這么簡單!
標簽: 濟南網(wǎng)站建設(shè) 網(wǎng)站建設(shè) 濟南網(wǎng)站制作 網(wǎng)址: www.fanwen1688.com
- 打印本文
- 關(guān)閉本頁
- 建站服務(wù)熱線:0531-68808868 售后服務(wù)專線:0531-88961515