从今天开始,我们将开始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游戏制作入门系列教程(一)
相关推荐
NULL 博文链接:https://justcoding.iteye.com/blog/2166578
NULL 博文链接:https://justcoding.iteye.com/blog/2166906
NULL 博文链接:https://justcoding.iteye.com/blog/2166907
NULL 博文链接:https://justcoding.iteye.com/blog/2166903
NULL 博文链接:https://justcoding.iteye.com/blog/2166904
NULL 博文链接:https://justcoding.iteye.com/blog/2166905
NULL 博文链接:https://justcoding.iteye.com/blog/2166577
Dreamweaver8 基础入门教程,TeliuTe系列教程之Dreamweaver8基础入门,一步一步教你学会Dreamweaver网页设计。 使用说明:解压后,请使用IE打开index.html即可。 1. 学习平台为Dreamweaver 8.0 中文版; 2. 本软件...
如果说以前 HTML5 只是一个很潮的概念,现在,HTML5 已经成为大势所趋。对于越来越多的、基于移动端 web 开发的人员而言,学习 HTML5 更是迫在眉睫。移动端 web 开发,最大的特点是不需要考虑 IE6、7、8 这些已经被...
系列文章 https://www.cnblogs.com/densen2014/p/16027851.html Blazor入门100天 演示地址:https://blazor.app1.es 1.使用JS隔离封装viewerjs库 2.使用JS隔离制作手写签名组件 3.使用JS隔离封装ZXing扫码 4.使用JS...
网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R,修复! Windows XP(包括 Windows 2000)的控制台命令是在系统出现一些意外情况下的一种非常有效的...
PHPLIB就是最常用的可以提供一般事务需要的一系列基库。 - 可扩展性 就像前面说的那样,PHP已经进入了一个高速发展的时期。对于一个非程序员来说为PHP扩展附加功能可能会比较难,但是对于一个PHP程序员来说并不...
HTML 不是一种编程语言,而是一种标记语言 (markup language),是网页制作所必备的。 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML或XML等文件样式的计算机语言。 CSS不仅可以静态地修饰网页,...
本书是一本Access 2003数据库入门教材,也是一本合格的Access 2003高级用户参考书。本书分为12章,涵盖了使用Access 2003来设计数据库系统的相关概念与技巧,通过实例让读者轻松学会表、查询、窗体、数据访问页的...
本书是一本Access 2003数据库入门教材,也是一本合格的Access 2003高级用户参考书。本书分为12章,涵盖了使用Access 2003来设计数据库系统的相关概念与技巧,通过实例让读者轻松学会表、查询、窗体、数据访问页的...
在写作的同时我参考了网上jQuery的系列教程文章, 在博客园和Google上并没有找到让我满意的系列教程. 我喜欢将知识系统的,深入浅出的讲解.不喜欢写那种"学习笔记"式的文章. 同时本系列将很快全部写完(有工作压力就是...
制作一个简单的多页Tab功能 一完美的关于请求的目录不存在而需要url重写的解决方案! 在C#中实现MSN消息框的功能 XmlHttp实现无刷新三联动ListBox 鼠标放在一个连接上,会显示图片(类似tooltip) 使用microsoft.web.ui...