从今天开始,我们将开始HTML5游戏开发一系列的文章。在我们的第一篇文章中,我们将讲解在画布canvas上的基础工作,创建简单的对象,填充和事件处理程序。另外,要注意在这个阶段中,我们不会立即学习WebGL相关的3D部分。但我们会尽快在未来的WebGL。
在每篇文章中,我们都将学习到一些新的东西。我们第一次创建一个对象,有7个顶点,这些顶点,我们将绘制圆,我们将能够拖动这些顶点。此外,我们将顶点对象填充为半透明色。我认为这是作为入门教程已经足够了。
这里有我们的演示和下载包:
好吧,下载文件,然后让我们开始编码吧!
步骤1:HTML
这里是所有我演示的HTML。
index.html
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>html5game-html5游戏制作入门系列教程(一)</title>
<link href="main.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
<canvas id="scene" width="800" height="600"></canvas>
</div>
<footer>
<h2>html5游戏制作入门系列教程(一)</h2>
<a href="http://html5gamedev.org/?p=291" class="stuts">返回原文 <span>html5游戏制作入门系列教程(一)</span></a>
</footer>
</body>
</html>
步骤2:CSS
下面是CSS样式。
/* general styles */
*{
margin:0;
padding:0;
}
body {
background-color:#bababa;
background-image: -webkit-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: -moz-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: -o-radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
background-image: radial-gradient(600px 300px, circle, #ffffff, #bababa 60%);
color:#fff;
font:14px/1.3 Arial,sans-serif;
min-height:1000px;
}
.container {
width:100%;
}
.container > * {
display:block;
margin:50px auto;
}
footer {
background-color:#212121;
bottom:0;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
left:0;
position:fixed;
width:100%;
z-index:100;
}
footer h2{
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
footer a.stuts,a.stuts:visited{
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:0;
}
footer .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
h3 {
text-align:center;
}
/* tutorial styles */
#scene {
background-image:url(01.jpg);
position:relative;
}
步骤3:JS
我们将使用jQuery为我们的演示。这使得很容易绑定不同的事件(鼠标等)。下一步最重要的文件,只是因为包含了所有我们的工作与图形:
var canvas, ctx; var circles = []; var selectedCircle; var hoveredCircle; // ------------------------------------------------------------- // objects : function Circle(x, y, radius){ this.x = x; this.y = y; this.radius = radius; } // ------------------------------------------------------------- // draw functions : function clear() { // clear canvas function ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); } function drawCircle(ctx, x, y, radius) { // draw circle function ctx.fillStyle = 'rgba(255, 35, 55, 1.0)'; ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); } function drawScene() { // main drawScene function clear(); // clear canvas ctx.beginPath(); // custom shape begin ctx.fillStyle = 'rgba(255, 110, 110, 0.5)'; ctx.moveTo(circles[0].x, circles[0].y); for (var i=0; i<circles.length; i++) { ctx.lineTo(circles[i].x, circles[i].y); } ctx.closePath(); // custom shape end ctx.fill(); // fill custom shape ctx.lineWidth = 5; ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)'; ctx.stroke(); // draw border for (var i=0; i<circles.length; i++) { // display all our circles drawCircle(ctx, circles[i].x, circles[i].y, (hoveredCircle == i) ? 25 : 15); } } // ------------------------------------------------------------- // initialization $(function(){ canvas = document.getElementById('scene'); ctx = canvas.getContext('2d'); var circleRadius = 15; var width = canvas.width; var height = canvas.height; var circlesCount = 7; // we will draw 7 circles randomly for (var i=0; i<circlesCount; i++) { var x = Math.random()*width; var y = Math.random()*height; circles.push(new Circle(x,y,circleRadius)); } // binding mousedown event (for dragging) $('#scene').mousedown(function(e) { var canvasPosition = $(this).offset(); var mouseX = e.layerX || 0; var mouseY = e.layerY || 0; for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not var circleX = circles[i].x; var circleY = circles[i].y; var radius = circles[i].radius; if (Math.pow(mouseX-circleX,2) + Math.pow(mouseY-circleY,2) < Math.pow(radius,2)) { selectedCircle = i; break; } } }); $('#scene').mousemove(function(e) { // binding mousemove event for dragging selected circle var mouseX = e.layerX || 0; var mouseY = e.layerY || 0; if (selectedCircle != undefined) { var canvasPosition = $(this).offset(); var radius = circles[selectedCircle].radius; circles[selectedCircle] = new Circle(mouseX, mouseY,radius); // changing position of selected circle } hoveredCircle = undefined; for (var i=0; i<circles.length; i++) { // checking through all circles - are mouse down inside circle or not var circleX = circles[i].x; var circleY = circles[i].y; var radius = circles[i].radius; if (Math.pow(mouseX-circleX,2) + Math.pow(mouseY-circleY,2) < Math.pow(radius,2)) { hoveredCircle = i; break; } } }); $('#scene').mouseup(function(e) { // on mouseup - cleaning selectedCircle selectedCircle = undefined; }); setInterval(drawScene, 30); // loop drawScene });
我为所有必要的代码提供了详细的注释,所以我希望你不会感到困惑。
结论
翻译by html5game
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-1/
转载请注明:HTML5游戏开发者社区 » html5游戏制作入门系列教程(一)