AlloyRenderingEngine燃烧的进度条

写在前面

Github: https://github.com/AlloyTeam/AlloyGameEngine

HTML 5新增了progress标签,那么再去使用AlloyRenderingEngine去模拟进度条是否多余?
不多余。有四大好处:

  • 样式更加灵活(想怎么绘制就怎么绘制)
  • 跨平台跨浏览器样式更加统一(使用便签,各个浏览器默认样式是五花八门)
  • 效果更加酷炫,(比如燃烧的进度条= =!)
  • 像素能够统一管理

统一像素管理的好处:

  • 更容易的全尺寸缩小和放大(最常见的:全屏游戏)
  • 缩小和放大不用操心内部元素错位(只是交给浏览器去进行插值运算)
  • 更好的滤镜控制(如游戏中死亡画面,全屏黑白化)
  • 更好的移植性和跨平台性(opengl<->canvas2d<->webgl<->opengl等等各种mapping)

上面所列的,对AlloyRenderingEngine所有组件都适用。

演示

 
开始演示(ps:可以直接点击进度条黑色部分设置进度)

组件使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
; (function () {
    //注意:当要渲染文字(Text)和图形(Graphics)时,请使用Cavnas渲染
    //Progress组件内部使用了Graphics
    //第二个参数true代表关闭webgl,使用Canvas2d渲染
    //如果要使用webgl渲染,请使用Lable渲染文字,Shape渲染矢量图。
    var stage = new ARE.Stage("#ourCanvas", true);
    var progress = new ARE.Progress({
        width: 200,
        height: 20,
        borderColor: "#3d3d3d",
        fillColor: "#black"
    })
    progress.x = 50;
    progress.y = 50;
    stage.add(progress);
    var current = 0, pause = true;
    stage.onTick(function () {
        if (!pause) {
            current += 0.005;
            progress.value = current;
        }
    });
    //进度条的over时,鼠标的形状
    progress.cursor = "pointer";
    progress.onClick(function (evt) {
        //注意这里可以使用evt.stageX来得到相对于舞台(Canvas)的偏移
        current = progress.value = (evt.stageX - progress.x) / progress.width;
    })
    var btn = document.querySelector("#progressBegin");
    //点击按钮,开始进度条开始运行
    btn.addEventListener("click", function () {
        pause = false;
    }, false);
})();

组件原理(看注释)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
; (function () {
    //先把要使用类的赋给临时变量,以后就不用打点了:)
    var Stage = ARE.Stage, Container = ARE.Container, Graphics = ARE.Graphics;
    //进度条继承自容器
    ARE.Progress = Container.extend({
        //构造函数
        ctor: function (option) {
            //把容器的属性和方法搞给自己
            this._super();
            this.width = option.width;
            this.height = option.height;
            this.fillColor = option.fillColor;
            this.value = option.value||0;
            //外层边框
            this.box = new Graphics()       
            //直接根据传进的宽和高画个矩形
            this.box.lineWidth(2).strokeStyle(option.borderColor || "black").strokeRect(0, 0, option.width, option.height);
            //把边框添加到自身(因为自身就是容器,继承自Container,所以有了add方法)
            this.add(this.box);
            var barWidth = this.value * option.width - 4;          
            this.bar = new Graphics();
            //把bar添加到自身(因为自身就是容器,继承自Container,所以有了add方法)
            this.add(this.bar);
            this.bar.fillStyle(option.fillColor || "green").fillRect(2, 2, barWidth < 0 ? 0 : barWidth, option.height - 4);
            //引导的火焰,使用粒子系统去模拟
            this.pilot = new ARE.ParticleSystem({
                emitX: 0,
                emitY:0,
                speed:10,
                angle: 180,
                angleRange: 90,
                emitArea: [1, option.height],        
                texture: "data:image/png;base64\,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJkSURBVHjaxJeJbusgEEW94S1L//83X18M2MSuLd2pbqc4wZGqRLrKBsyZhQHny7Jk73xVL8xpVhWrcmiB5lX+6GJ5YgQ2owbAm8oIwH1VgKZUmGcRqKGGPgtEQQAzGR8hQ59fAmhJHSAagigJ4E7GPWRXOYC6owAd1JM6wDQPADyMWUqZRMqmAojHp1Vn6EQQEgUNMJLnUjMyJsM49wygBkAPw9dVFwXRkncCIIW3GRgoTQUZn6HxCMAFEFd8TwEQ78X4rHbILoAUmeT+RFG4UhQ6MiIAE4W/UsYFjuVjAIa2nIY4q1R0GFtQWG3E84lqw2GO2QOoCKBVu0BAPgDSU0eUDjjQenNkV/AW/pWChhpMTelo1a64AOKM30vk18GzTHXCNtI/Knz3DFBgsUqBGIjTInXRY1yA9xkVoqW5tVq3pDR9A0hfF5BSARmVnh7RMDCaIdcNgbPBkgzn1Bu+SfIEFSpSBmkxyrMicb0fAEuCZrWnN89veA/4XcakrPcjBWzkTuLjlbfTQPOlBhz+HwkqqPXmPQDdrQItxE1moGof1S74j/8txk8EHhTQrAE8qlwfqS5yukm1x/rAJ9Jiaa6nyATqD78aUVBhFo8b1V4DdTXdCW+IxA1zB4JhiOhZMEWO1HqnvdoHZ4FAMIhV9REF8FiUm0jsYPEJx/Fm/N8OhH90HI9YRHesWbXXZwAShU8qThe7H8YAuJmw5yOd989uRINKRTJAhoF8jbqrHKfeCYdIISZfSq26bk/K+yO3YvfKrVgiwQBHnwt8ynPB25+M8hceTt/ybPhnryJ78+tLgAEAuCFyiQgQB30AAAAASUVORK5CYII=",
                filter: [0.63, 0.35, 0.18, 1],
                emitCount: 1,
                maxCount: 50
            });
            this.pilot.y = option.height / 2;
            //设置火焰色缩放,因为ParticleSystem本身也是继承自容器,所以具备scale属性
            //等同于this.pilot.scaleX = this.pilot.scaleY = 0.4;
            this.pilot.scale = 0.4;
            //把引导的火焰添加到自身(因为自身就是容器,继承自Container,所以有了add方法)
            this.add(this.pilot);           
            //监听value的变化,赋值胜于call method
            ARE.Observe(this, "value", function (name, value) {            
                if (value >= 1) {
                    //通过maxCount限制粒子的个数,达到关闭火焰的效果
                    this.pilot.maxCount = 0;
                    this.value = 1;
                } else {
                    this.pilot.maxCount = 50;
                    this.value = value;
                }
                //设置火焰的位置
                this.pilot.x = this.value * this.width;
                var barWidth = this.value * this.width - 4;
                this.bar.clear().fillStyle(this.fillColor || "green").fillRect(2, 2, barWidth < 0 ? 0 : barWidth, this.height - 4);             
            })
        }
    });
})();

最新动态请关注Github: https://github.com/AlloyTeam/AlloyGameEngine

作者:张磊 

AlloyRenderingEngine的更多相关文章

  1. AlloyRenderingEngine燃烧的进度条

    写在前面 Github: https://github.com/AlloyTeam/AlloyGameEngine HTML 5新增了progress标签,那么再去使用AlloyRenderingEn ...

  2. AlloyRenderingEngine文本框组件

    写在前面 Github: https://github.com/AlloyTeam/AlloyGameEngine 在dom元素里,自带了input标签,设置其type为text,它就是一个文本框. ...

  3. AlloyRenderingEngine继承

    写在前面 不读文章,只对代码感兴趣可以直接跳转到这里 https://github.com/AlloyTeam/AlloyGameEngine然后star一下,多谢支持:). 前几天发了篇向ES6靠齐 ...

  4. AlloyRenderingEngine之Shape

    写在前面 不读文章,只对代码感兴趣可以直接跳转到这里 https://github.com/AlloyTeam/AlloyGameEngine 然后star一下,多谢支持:). 游戏或者应用中,不是所 ...

  5. AlloyRenderingEngine入门

    写在前面 AlloyRenderingEngine是一款非常快速的渲染引擎,目前该项目已经合并至 https://github.com/AlloyTeam/AlloyGameEngine/ , 属于A ...

  6. AlloyRenderingEngine开门大吉

    快速入口 不读文章可以直接拐向这里: github:https://github.com/AlloyTeam/AlloyRenderingEngine website:http://alloyteam ...

  7. spritecow改造

    快速入口 不读文章可以直接拐向这里: spritecow二代:https://kmdjs.github.io/arejs-tool-sprite/ 写在前面 工欲善其事必先利其器,最近fork了一份s ...

随机推荐

  1. oracle存储过程的例子

    oracle存储过程的例子 分类: 数据(仓)库及处理 2010-05-03 17:15 1055人阅读 评论(2)收藏 举报 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的 ...

  2. Error Code: 1318. Incorrect number of arguments for PROCEDURE company.new_procedure; expected 2, got

    1.错误叙述性说明 20:27:34 call new_procedure(20150112) Error Code: 1318. Incorrect number of arguments for ...

  3. Unity3D 表对象分类中的实现(C#)

    // Sort by distance in descending order private void SortTargetsByDistance () { targets.Sort(delegat ...

  4. MVC5+EF6 入门完整教程 总目录

    本系列文章会从一个主干开始,逐渐深入,初步规划30篇.初级10篇,中级10篇,综合项目实战10篇 初级10篇 MVC5+EF6 入门完整教程10:多对多关联表更新&使用原生SQL@201505 ...

  5. HDU 4067 Random Maze

    意甲冠军: 一个"随机图"它被定义为具有以下性质如: 一个入口和一个出口 有向图 对于入口  出度比入度大1 对于出口  入度比出度大1 对于其它点  入度等于出度 现给出一幅有向 ...

  6. c# 用正则表达式在指定的字符串中每隔指定个数的文字插入指定字符串

    public static string AddNewLine(string inString,int num,string addString="\r\n") { return ...

  7. Chapter06-Phylogenetic Trees Inherited(POJ 2414)(减少国家DP)

    Phylogenetic Trees Inherited Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 480 Accepted ...

  8. ehCache浅谈(转)

    ehcache FAQ中提到 Remember that a value in a cache element is globally accessible from multiple threads ...

  9. Android中的应用——谷歌官方Json分析工具Gson使用

    一个.Gson基本介绍 Gson(又称Google Gson)是Google公司公布的一个开放源码的Java库.主要用途为串行化Java对象为JSON字符串,或反串行化JSON字符串成Java对象. ...

  10. GPU 编程入门到精通(五)之 GPU 程序优化进阶

    博主因为工作其中的须要,開始学习 GPU 上面的编程,主要涉及到的是基于 GPU 的深度学习方面的知识.鉴于之前没有接触过 GPU 编程.因此在这里特地学习一下 GPU 上面的编程. 有志同道合的小伙 ...