HTML5游戏开发(三)

HTML5游戏开发(三)

一、绘制

(一)矩形绘制

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>矩形绘制</title>
        <style>
            body {
                background: #FFCC99;
            }

            #canvas {
                background: #99CC99;
                border: thin solid #FFFFCC;
            }
        </style>
    </head>
    <body>
        <canvas id='canvas' width='600' height='400'>
    </canvas>
        <script src="js/rect.js"></script>
    </body>
</html>

js脚本:

var canvas = document.querySelector("#canvas"),
    context = canvas.getContext('2d');

//定义字体与大小
context.font="24px 隶书";
//文本内容
context.fillText("内容擦除",200,40);
//路径粗细  
context.lineWidth=10;
//路径样式miter(方) bevel(斜角) round(圆角)
context.lineJoin="round";
//绘制一个矩形
context.strokeRect(75,100,200,200);

//实心矩形
context.fillRect(90,150,50,50);

context.canvas.onmousedown=function(e){
    //清除画布
    context.clearRect(0,0,canvas.width,canvas.height);
}

(二)颜色与透明度

var canvas = document.querySelector("#canvas"),
    context = canvas.getContext('2d');

//定义字体与大小
context.font="24px 隶书";
//文本内容
context.fillText("内容擦除",200,40);
//路径粗细  
context.lineWidth=10;
//路径样式miter(方) bevel(斜角) round(圆角)
context.lineJoin="round";
//设置路径颜色
context.strokeStyle="#49868C";
//绘制一个矩形
context.strokeRect(75,100,200,200);
//设置填充颜色,R G B加透明色
context.fillStyle='rgba(168,21,43,0.1)';
//也可使用globalAlpha,设置透明度
//context.fillStyle='rgba(168,21,43)';
//context.globalAlpha=0.5;
//实心矩形
context.fillRect(90,150,50,50);

context.canvas.onmousedown=function(e){
    //清除画布
    context.clearRect(0,0,canvas.width,canvas.height);
}

显示效果:
image

(三)渐变色与图案

1、线性渐变

context.createLinearGradient(x0,y0,x1,y1);

参数描述
x0渐变开始点的 x 坐标
y0渐变开始点的 y 坐标
x1渐变结束点的 x 坐标
y1渐变结束点的 y 坐标
var canvas = document.querySelector("#canvas"),
    context = canvas.getContext('2d');

//定义字体与大小
context.font="24px 隶书";
//文本内容
context.fillText("线性渐变",230,40);
//创建线性渐变  x,y开始位置,后两个参数为结束位置
//从左上角开始渐变
// gradient = context.createLinearGradient(
//               0, 0,canvas.width, canvas.height);
//从左侧渐变
gradient = context.createLinearGradient(
                 0, canvas.height,canvas.width, canvas.height);
//设置渐变颜色  0~1之间的颜色停止点,表示渐变中开始与结束之间的位置
//在 0.0 到 1.0 之间的浮点值,表示渐变的开始点和结束点之间的一部分。
//offset 为 0 对应开始点,offset 为 1 对应结束点。
gradient.addColorStop(0,'red');
gradient.addColorStop(0.3,'green');
gradient.addColorStop(0.5,'blue');
gradient.addColorStop(0.4,'yellow');
gradient.addColorStop(0.2,'black');
//设置样式
context.fillStyle=gradient;
//绘制实心矩形
context.fillRect(20,120,300,200);

context.canvas.onmousedown=function(e){
    //清除画布
    context.clearRect(0,0,canvas.width,canvas.height);
}

显示效果:
image

2、放射渐变

context.createRadialGradient(x0,y0,r0,x1,y1,r1);

参数描述
x0渐变的开始圆的 x 坐标
y0渐变的开始圆的 y 坐标
r0开始圆的半径
x1渐变的结束圆的 x 坐标
y1渐变的结束圆的 y 坐标
r1结束圆的半径
var canvas = document.querySelector("#canvas"),
    context = canvas.getContext('2d');

//定义字体与大小
context.font="24px 隶书";
//文本内容
context.fillText("放射渐变",230,40);
//创建放射渐变 
gradient = context.createRadialGradient(
                canvas.width/2, canvas.height,10,
                 canvas.width/2, 0, 100);
//设置渐变颜色  0~1之间的颜色停止点,表示渐变中开始与结束之间的位置
//在 0.0 到 1.0 之间的浮点值,表示渐变的开始点和结束点之间的一部分。
//offset 为 0 对应开始点,offset 为 1 对应结束点。
gradient.addColorStop(0,'red');
gradient.addColorStop(0.3,'green');
gradient.addColorStop(0.5,'blue');
gradient.addColorStop(0.4,'yellow');
gradient.addColorStop(0.2,'black');
//设置样式
context.fillStyle=gradient;
//绘制实心矩形
context.fillRect(0,100,canvas.width,canvas.height-100);

context.canvas.onmousedown=function(e){
    //清除画布
    context.clearRect(0,0,canvas.width,canvas.height);
}

显示效果:
image

3、图案

  除了颜色与渐变色,Canvas元素也允许使用图案来对图形和文本进行描边与填充。可以是以下三种之一:image元素、canvas元素和video元素。
可使用:
context.createPattern(image,”repeat|repeat-x|repeat-y|no-repeat”);

参数描述
image规定要使用的图片、画布或视频元素。
repeat默认。该模式在水平和垂直方向重复。
repeat-x该模式只在水平方向重复。
repeat-y该模式只在垂直方向重复。
no-repeat 该模式只显示一次(不重复)。
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>图案</title>
    </head>
    <body>
        <style>
         #canvas {
            background: #eeeeee;
            border: thin solid cornflowerblue;
         }
         #radios {
            padding: 10px;
         }
      </style>
   </head>

   <body>
      <div id='radios'>
         <input type='radio' id='repeatRadio' name='patternRadio' checked/>重复
         <input type='radio' id='repeatXRadio' name='patternRadio'/>横向重复
         <input type='radio' id='repeatYRadio' name='patternRadio'/>纵向重复
         <input type='radio' id='noRepeatRadio' name='patternRadio'/>不重复
      </div>
        <canvas id="canvas" width="450" height="275">
        </canvas>
        <script type="text/javascript" src="js/pattern.js" ></script>
    </body>
</html>

JS脚本:

var canvas = document.getElementById('canvas'),
    context = canvas.getContext('2d'),
    repeatRadio = document.getElementById('repeatRadio'),
    noRepeatRadio = document.getElementById('noRepeatRadio'),
    repeatXRadio = document.getElementById('repeatXRadio'),
    repeatYRadio = document.getElementById('repeatYRadio'),
    image = new Image();
    image.src="img/yin_yang_64px_1168759.png";

//图像加载事件
image.onload=function(){
    fillCanvasWithPattern("repeat");
}
//绘制图案    
function fillCanvasWithPattern(repeatString) {
   //创建图案对象
   var pattern = context.createPattern(image, repeatString);
   //清除背景
   context.clearRect(0, 0, canvas.width, canvas.height);
   context.fillStyle = pattern;
   //进行绘制一个实心矩形
   context.fillRect(0, 0, canvas.width, canvas.height);
   context.fill();
};
/**
 * 事件添加
 * @param {Object} e
 */
repeatRadio.onclick = function (e) {
  fillCanvasWithPattern('repeat'); 
};

repeatXRadio.onclick = function (e) {
  fillCanvasWithPattern('repeat-x'); 
};

repeatYRadio.onclick = function (e) {
  fillCanvasWithPattern('repeat-y'); 
};

noRepeatRadio.onclick = function (e) {
  fillCanvasWithPattern('no-repeat'); 
};

显示效果:
iamge

(四)阴影

通过以下属性可以设置阴影:

参数描述
shadowColor设置或返回用于阴影的颜色
shadowBlur设置或返回用于阴影的模糊级别
shadowOffsetX设置或返回阴影距形状的水平距离
shadowOffsetY设置或返回阴影距形状的垂直距离
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>阴影绘制</title>
        <style>
            body {
                background: #FFCC99;
            }
            #canvas {
                background: #99CC99;
                border: thin solid #FFFFCC;
            }
        </style>
    </head>
    <body>
        <canvas id='canvas' width='600' height='400'>
    </canvas>
        <script src="js/shadow.js"></script>
    </body>
</html>

JS脚本

var canvas = document.querySelector("#canvas"),
    context = canvas.getContext("2d");

//设置阴影颜色
context.shadowColor="#2B334A";
//设置阴影的模糊级别
context.shadowBlur=20;
context.shadowOffsetX=10;
context.shadowOffsetY=10;
context.fillStyle="#6495ED";
context.fillRect(100,100,100,100);

显示效果:
iamge

(五)路径、描边与填充

方法描述
fill()填充当前绘图(路径)
stroke()绘制已定义的路径
beginPath()起始一条路径,或重置当前路径
moveTo()把路径移动到画布中的指定点,不创建线条
closePath()创建从当前点回到起始点的路径
lineTo()添加一个新点,然后在画布中创建从该点到最后指定点的线条
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>路径</title>
        <style>
            body {
                background: #283c3c;
            }
            #canvas {
                background: #282828;
                border: thin solid #FFFFCC;
            }
        </style>
    </head>
    <body>
        <canvas id='canvas' width='600' height='500'>
    </canvas>
        <script src="js/path.js"></script>
    </body>
</html>

JS脚本

var context=document.querySelector("#canvas").getContext("2d");

//路径颜色
context.strokeStyle="#6495ED";
context.fillStyle="#49868C";
context.lineWidth = '5';  

//路径
context.beginPath();
context.rect(50, 50, 150, 80);
context.stroke();

context.beginPath();
context.rect(220, 50, 150, 80);
//填充
context.fill();

context.beginPath();
context.rect(400, 50, 150, 80);
//描边
context.stroke();
context.fill();

//绘制圆弧,默认为逆时针
context.beginPath();
context.arc(100, 250, 60, 0, Math.PI*3/2,false);
context.stroke();
//顺时针绘制
context.beginPath();
context.arc(250, 250, 60, 0, Math.PI*3/2,true);
context.fill();

context.beginPath();
context.arc(400, 250, 60, 0, Math.PI*3/2);
context.stroke();
context.fill();

//绘制一个三角形
context.beginPath();
//起始点
context.moveTo(60,360);
context.lineTo(60,450);
context.lineTo(100,380);
context.lineTo(60,360);
//绘制路径
context.stroke();
context.closePath();


//绘制一个多边形
context.beginPath();
//起始点
context.moveTo(160,360);
context.lineTo(160,450);
context.lineTo(260,380);
context.lineTo(220,300);
context.lineTo(160,360);
//绘制路径
context.stroke();
context.closePath();

显示效果:
iamge

http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616 html5 挺火,写个html5游戏玩玩吧,想起cocos2d 貌似各个平台都有,网上找了找,下载了个Cocos2d-html5引擎包。 貌似另一个开源引擎lufylegend.js也很好,下次用用lufylegend.js试试。 开发环境 chrome Cocos2d-html5 游戏地址:http://hehe108.sinaapp.com/cocos2d/ (adsw回车) 实现方法如下 1.创建好 LayerGradient的子类 (里面放坦克子弹) 2.重写 onEnter 方法添加一些基本按钮 跟一些初始坦克,子弹 3.通过schedule方法 控制 坦克 子弹的重画 4.根据键盘按键(ASWD)确定出坦克的方向,根据坦克的方向修改坦克的X,Y轴坐标,来实现坦克的移动 5.通过cc.rectIntersectsRect函数来进行碰撞检测,实现子弹打击坦克 具体代码 1.在项目里面添加方向 var Direction = { L:0, U:1, D:2, R:3, STOP:4 }; 2.添加子弹类的相关属性 SPEED:10, WIDTH:15, HEIGHT:15, x:null, y:null, dir:null, live:true, tankClient:null, //LayerGradient子类TankClient 的引用 good:null, 3.子弹初始化,重画 ctor:function (x,y,good,dir,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.dir=dir; this.tankClient=tankClient; this.good=good; this.initWithFile(s_missile); this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if(!this.live){ this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); this.Move(); }, 4.添加子弹打击坦克的方法 HitTank:function(t){ if (cc.rectIntersectsRect(this.GetRectangle(), t.GetRectangle()) && t.live && this.live && this.good != t.good){ t.live = false; this.live = false; return true; } return false; }, 5.添加坦克类相关属性 SPEED:5, WIDTH:58, HEIGHT:58, x:0, y:0, l:false, u:false, r:false, d:false, dir:Direction["STOP"], ptDir:Direction["D"], tankClient:null, good:null, step:0, live:true, 6.在tank类中 坦克初始化,重画 ctor:function (x,y,good,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.tankClient=tankClient; this.good=good; if(good){ this.initWithFile(s_tank); }else{ this.initWithFile(s_enemy); } this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if (!this.live){ if (!this.good){ this.tankClient.removeChild(this, true); } this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); switch (this.ptDir) { case Direction["D"]: this.setRotation(0); //旋转精灵控制 炮筒方向 break; case Direction["U"]: this.setRotation(180); break; case Direction["L"]: this.setRotation(270); break; case Direction["R"]: this.setRotation(90); break; } this.Move(); }, 7.tank发子弹的方法 Fire:function() { if(!this.live) return null; for(var i=0;i<this.tankClient.missiles.length;i++){ var m = this.tankClient.missiles[i]; if (m.live == false){ m.x=this.x; m.y=this.y; m.live=true; m.dir=this.ptDir; m.good=this.good; this.tankClient.addChild(m); return m; } } var missile=new Missile(this.x,this.y,this.good,this.ptDir,this.tankClient); this.tankClient.missiles.push(missile); return missile; }, 8.LayerGradient加入坦克 this.tanks = []; this.myTank = new Tank(60,20, true, this); for (var i = 0; i < 10; i++){ this.tanks.push(new Tank(50 + 70 * (i + 1), 420, false, this)); } 9.LayerGradient中调用子弹打击坦克的方法 for(var i=0;i<this.missiles.length;i++){ var m = this.missiles[i]; m.HitTank(this.myTank); m.HitTanks(this.tanks); m.Draw(); } 10.控制坦克移动射击的部分代码 onKeyUp:function(key) { this.myTank.KeyReleased(key); }, onKeyDown:function(key) { this.myTank.KeyPressed(key); } 11.用Ant和compiler合并压缩js后发布到sae 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值