欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

html5游戲制作入門系列教程(二)

系統 1832 0

今天,我們繼續html5游戲制作入門系列的系列文章。今天,我們將繼續基礎知識(也許甚至是高級技巧的基礎)。我要告訴你如何具有漸變顏色填充對象,繪制文本,使用自定義的字體繪制文本,基本的動畫,以及最重要的UI元素 – 按鈕。

?

我們以前的文章中,你可以在這里閱讀: html5游戲制作入門系列教程(一) 。我們會用到以前的腳本,并將其功能加強。我要繪制文本,使用自定義字體,動畫對象(方形)與漸變色填充,將利用“播放/暫停”按鈕暫停動畫。

?

這里有我們的演示和下載包:

在線演示 源碼下載

?

好吧,下載所需文件,讓我們開始編碼!

?

步驟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 class="stuts" >返回原文 <span>html5游戲制作入門系列教程(二)</span></a>
</footer>
</body>
</html>
      

?

步驟2:CSS

下面是CSS樣式。

        /* general styles */
*{
    margin:0;
    padding:0;
}
@font-face {
    font-family: "DS-Digital";
    src: url("Ds-digib.ttf");
}
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;
}
#scene {
    background-image:url(01.jpg);
    position:relative;
}
      

?

注意@ font-face的這種使用方式,自定義字體文件(TTF)連接到我們的教程中(在畫布上繪制)。

?

步驟3: JS

        var canvas, ctx;
var circles = [];
var selectedCircle;
var hoveredCircle;
var button;
var moving = false;
var speed = 2.0;

// -------------------------------------------------------------

// objects :

function Circle(x, y, radius){
    this.x = x;
    this.y = y;
    this.radius = radius;
}

function Button(x, y, w, h, state, image) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.state = state;
    this.imageShift = 0;
    this.image = image;
}
// -------------------------------------------------------------

// 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();

    ctx.lineWidth = 1;
    ctx.strokeStyle = 'rgba(0, 0, 0, 1.0)';
    ctx.stroke(); // draw border
}

function drawScene() { // main drawScene function
    clear(); // clear canvas

    // draw the title text
    ctx.font = '42px DS-Digital';
    ctx.textAlign = 'center';
    ctx.fillStyle = '#ffffff';
    ctx.fillText('Welcome to lesson #2', ctx.canvas.width/2, 50);

    var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400);
    bg_gradient.addColorStop(0.0, 'rgba(255, 0, 0, 0.8)');
    bg_gradient.addColorStop(0.5, 'rgba(0, 255, 0, 0.8)');
    bg_gradient.addColorStop(1.0, 'rgba(0, 0, 255, 0.8)');

    ctx.beginPath(); // custom shape begin
    ctx.fillStyle = bg_gradient;
    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 = 2;
    ctx.strokeStyle = 'rgba(0, 0, 255, 0.5)';
    ctx.stroke(); // draw border

    // reverting direction
    if (circles[0].x <= 300 || circles[0].x >= 385) {
        speed = -speed;
    }

    // central object behavior
    if (moving) {
        circles[0].x -= speed;
        circles[0].y -= speed;
        circles[1].x += speed;
        circles[1].y -= speed;
        circles[2].x += speed;
        circles[2].y += speed;
        circles[3].x -= speed;
        circles[3].y += speed;
    }

    drawCircle(ctx, circles[0].x, circles[0].y, (hoveredCircle == 0) ? 25 : 15);
    drawCircle(ctx, circles[1].x, circles[1].y, (hoveredCircle == 1) ? 25 : 15);
    drawCircle(ctx, circles[2].x, circles[2].y, (hoveredCircle == 2) ? 25 : 15);
    drawCircle(ctx, circles[3].x, circles[3].y, (hoveredCircle == 3) ? 25 : 15);

    // draw button
    ctx.drawImage(button.image, 0, button.imageShift, button.w, button.h, button.x, button.y, button.w, button.h);

    // draw text
    ctx.font = '30px DS-Digital';
    ctx.fillStyle = '#ffffff';
    ctx.fillText('Play/Pause', 135, 480);
    ctx.fillText(button.state, 135, 515);
}

// -------------------------------------------------------------

// initialization

$(function(){
    canvas = document.getElementById('scene');
    ctx = canvas.getContext('2d');

    var circleRadius = 15;
    var width = canvas.width;
    var height = canvas.height;

    // lets add 4 circles manually
    circles.push(new Circle(width / 2 - 20, height / 2 - 20, circleRadius));
    circles.push(new Circle(width / 2 + 20, height / 2 - 20, circleRadius));
    circles.push(new Circle(width / 2 + 20, height / 2 + 20, circleRadius));
    circles.push(new Circle(width / 2 - 20, height / 2 + 20, circleRadius));

    // load the guide sprite image
    buttonImage = new Image();
    buttonImage.src = 'button.png';
    buttonImage.onload = function() {
    }
    button = new Button(50, 450, 180, 120, 'normal', buttonImage);

    // binding mousedown event (for dragging)
    $('#scene').mousedown(function(e) {

        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;
            }
        }

        // button behavior
        if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) {
            button.state = 'pressed';
            button.imageShift = 262;
        }
    });

    $('#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;
                circles[hoveredCircle] = new Circle(circleX, circleY, 25);
                break;
            }
        }

        // button behavior
        if (button.state != 'pressed') {
            button.state = 'normal';
            button.imageShift = 0;
            if (mouseX > button.x && mouseX < button.x+button.w && mouseY > button.y && mouseY < button.y+button.h) {
                button.state = 'hover';
                button.imageShift = 131;
            }
        }
    });

    $('#scene').mouseup(function(e) { // on mouseup - cleaning selectedCircle
        selectedCircle = undefined;

        // button behavior
        if (button.state == 'pressed') {
            moving = !moving;
        }
        button.state = 'normal';
        button.imageShift = 0;
    });

    setInterval(drawScene, 30); // loop drawScene
});
      

?

下面是關于一些新功能代碼的解釋:
1)我們可以繪制文本(自定義字體)使用下面的代碼,如下:

        ctx.font = '42px DS-Digital';
ctx.textAlign = 'center';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to lesson #2', ctx.canvas.width/2, 50);
      

?

2)運用漸變色:

        var bg_gradient = ctx.createLinearGradient(0, 200, 0, 400);
bg_gradient.addColorStop(0.0, 'rgba(255, 0, 0, 0.8)');
bg_gradient.addColorStop(0.5, 'rgba(0, 255, 0, 0.8)');
bg_gradient.addColorStop(1.0, 'rgba(0, 0, 255, 0.8)');
ctx.fillStyle = bg_gradient;
      

?

3)按鈕 – 我用一個精靈圖像,繪制按鈕的三個狀態,并添加懸停/事件的事件處理程序。下面是加載和繪制圖像畫布的方式:

        buttonImage = new Image();
buttonImage.src = 'button.png';
.......
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, width, height);
      

?

結論

超級酷,不是嗎?我會很高興看到您的評論和意見。祝你好運!

在線演示 源碼下載

?

?

轉載請注明: HTML5游戲開發者社區 ? html5游戲制作入門系列教程(二)

html5游戲制作入門系列教程(二)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品一 | 成年免费视频 | 日韩欧美色图 | 国产区视频在线观看 | 奇米影视在线 | 杏导航aⅴ福利网站 | 日本欧美一区二区三区视频麻豆 | 91精品福利 | 成人欧美视频在线观看 | 成人九色 | 欧美日韩在线国产 | 小明永久免费 | 日本午夜高清视频 | 久久穴| 欧美一区二区三区久久精品 | 亚洲免费精品视频 | 国产日韩欧美在线 | 日本高清视频在线播放 | 久久精品一区二区三区四区 | 久久亚洲综合 | 6月婷婷| 亚洲精品在线播放视频 | 日本黄视色视频在线观看 | 欧美激情日韩 | 中文字幕在线一区二区三区 | 视频一区二区三区免费观看 | 亚洲午夜大片 | 草草草在线视频 | 日韩一二三区 | 午夜电影合集 | 国产牛仔裤系列在线观看 | 欧美6一10sex性hd | 国产高清免费视频 | 91看片片| 特黄特色大片免费视频观看 | 国产成人久久婷婷精品流白浆 | 香蕉久| 亚洲欧美综合精品久久成人 | 久久久国产精品免费视频 | 欧美乱视频 | 国产综合亚洲精品一区二 |