导航

萌即是正义!时不时分享一些ACG活动记录与有趣代码的小站!

侧边栏
热门文章
1博文
游览世界文化遗产 《寒蝉鸣泣之时》圣地巡礼——白川乡 下篇
热度
654
2页面
程序员老黄历&求签
热度
520
3博文
游览世界文化遗产 《寒蝉鸣泣之时》圣地巡礼——白川乡 上篇
热度
143
4页面
关于
热度
130
5页面
友链
热度
78
6博文
360度带你《我们仍未知道那天所看见的花的名字》秩父圣地巡礼
热度
52
7推文
《摇曳露营△》联合日本静冈县推出了新的圣地巡礼地图。 这周要去的圣地巡礼正是《摇曳露营△》第二季中的伊豆半岛,也就是巡礼地图的右下角这块区域。 可以看到光是伊豆半岛这块区域的巡礼地点就非常多。 此次行程准备用时2天,能逛多少就算多少。 《摇曳露营△》× 静冈县官方:https://yurucamp-shizuoka.com
热度
52
8博文
面码找到你了!探寻超平和Busters的秘密基地——《我们仍未知道那天所看见的花的名字》秩父圣地巡礼
热度
52
9推文
晚饭吃了高冈车站附近特色的黑酱油拉面。 店名叫次元拉面,整个区域唯一排着长队的饮食店。 出于好奇排了40分钟后点了当地特色套餐。有黑拉面、可乐饼、海苔饭团。 黑酱油拉面的酱油确实比普通的酱油更黑,也更浓厚。就是味道有点熟悉,有点像我老家的面。 可乐饼的味道也不错。 海苔饭团就有意思了,上面裹了一层海苔丝,海苔味十足。 另外店内有好多名人的签名呀!还有大量的漫画。不知道这个次元是不是那个次元的意思呢?
热度
51
10推文
我在超市蔬菜区发现了什么!?
热度
39
最新评论
广树管理员
2025-12-02 13:01
@ZeroCounter:哈哈哈,说不定就是了呢
ZeroCounter
2025-12-02 11:12
第一眼看截断的标题以为《寒蝉鸣泣之时》是世界文化遗产(不是
老张博客
2025-12-02 09:03
照片很“日”风,
广树管理员
2025-12-01 08:17
@Chise Hachiroku:这年头用五笔的人应该没多少了吧
Chise Hachiroku
2025-12-01 07:39
“不过必经当天是休息日” 好可爱的错别字,大佬也是拼音输入呢
攻略中
魔法少女的魔女审判
暂无评分
Steam魔法少女的魔女审判
2025年11月27日 22时 ~ 攻略中
已累计游玩5天
迎接少女们的,是一场残酷的死亡游戏……在即将来临的“魔女审判”中,打破魔法和谎言的幻影,揪出混入少女之中的“魔女”吧。这部最新的魔法辩论×悬疑ADV作品,由日本原创品牌“Acacia”为您呈献。
全民高尔夫环球之旅
暂无评分
Steam全民高尔夫环球之旅
2025年9月07日 12时 ~ 攻略中
已累计游玩2个月27天
《全民高尔夫 环球之旅》除了能体验到与过去作品相同的乐趣、爽快与深奥之外, 还有着以精美图像设计而成、个性丰富的角色们与以世界各地为主题的球场, 以及能让所有人热血沸腾的全新模式等等,是本系列的全新力作!
ToHeart
暂无评分
SteamToHeart
2025年6月26日 19时 ~ 攻略中
已累计游玩5个月9天
AQUAPLUS推出的“温暖人心的校园恋爱游戏”将以高清全3D形式焕新归来!
fault - StP - LIGHTKRAVTE
暂无评分
Steamfault - StP - LIGHTKRAVTE
2025年5月29日 20时 ~ 攻略中
已累计游玩6个月7天
全球累计销量超过50万份的“fault”系列最新作!故事的舞台是一个融合了奇幻与科幻的超前世界——卢森海德王国。本作讲述了生活在此地的一个平凡又平庸的究极普通市民——名为果子的少年的故事。
PSN奖杯卡

PSN奖杯卡

归档
赞助商广告

用canvas做 精灵 | 光点 | 粒子 | 光子 飘动的效果

作者:广树时间:2018-03-08 17:01:04分类:JavaScript

前面做《四女神Online》模板的时候参照code pen上的一则飘雪效果改的。

首先引入HTML并想好canvas的ID:

<canvas id='canvasbg' class="star_effect"></canvas>

然后引入JS:

(function() {

    var Base, Particle, canvas, colors, context, drawables, i, mouseX, mouseY, mouseVX, mouseVY, rand, click, min, max, colors, particles, Cwidth, Cheight, GradientColors, CanvasEl;

    CanvasEl = "canvasbg"; //Canvas的ID
    min = 1; //最小光点直径
    max = 4; //最大光点直径
    particles = 200; //光点数量
    Cwidth = 800; //画布宽度
    Cheight = 600; //画布高度
    colors = ["255, 255, 255", "250, 150, 20", "255, 100, 255"]; //光点中间的颜色注意和晕开颜色顺序配对
    GradientColors = ["84,204,243", "250,150,20", "255, 100, 255"]; //光点晕开的颜色色注意和中间颜色顺序配对
    rand = function(a, b) {
        return Math.random() * (b - a) + a;
    };

    Particle = (function() {
        function Particle() {
            this.reset();
        }

        Particle.prototype.reset = function() {
            var colorSel = Math.random();
            this.color = colors[~~ (colorSel * colors.length)];
            this.GradientColors = GradientColors[~~ (colorSel * GradientColors.length)];
            this.radius = rand(min, max);
            this.x = rand(0, canvas.width);
            this.y = rand(60, canvas.height);
            this.vx = -5 + Math.random() * 10;
            this.vy = (Math.random() - 0.5) * this.radius;
            this.valpha = rand(0.02, 0.09);
            this.opacity = 0;
            this.life = 0;
            this.onupdate = undefined;
            this.type = "dust";
        };

        Particle.prototype.update = function() {
            this.x = (this.x + this.vx / 6);
            this.y = (this.y + this.vy / 6);

            if (this.opacity >= 1 && this.valpha > 0) this.valpha *= -1;
            this.opacity += this.valpha / 8;
            this.life += this.valpha / 8;

            if (this.type === "dust") this.opacity = Math.min(1, Math.max(0, this.opacity));
            else this.opacity = 1;

            if (this.onupdate) this.onupdate();
            if (this.life < 0 || this.radius <= 0 || this.y > canvas.height) {
                this.onupdate = undefined;
                this.reset();
            }
        };

        Particle.prototype.draw = function(c) {
            var grd = c.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius * 4);
            grd.addColorStop(0, "rgba(" + this.GradientColors + ", " + Math.min(this.opacity - 0.2, 0.65) + ")");
            grd.addColorStop(1, "rgba(" + this.GradientColors + ", " + '0' + ")");
            c.fillStyle = grd;

            c.beginPath();
            c.arc(this.x, this.y, this.radius * 4, 2 * Math.PI, false);
            c.fill();

            c.strokeStyle = "rgba(" + this.color + ", " + Math.min(this.opacity, 0.85) + ")";
            c.fillStyle = "rgba(" + this.color + ", " + Math.min(this.opacity, 0.65) + ")";
            c.beginPath();
            c.arc(this.x, this.y, this.radius, 2 * Math.PI, false);
            c.fill();
        };

        return Particle;

    })();

    mouseVX = mouseVY = mouseX = mouseY = 0;

    canvas = document.getElementById(CanvasEl);
    context = canvas.getContext("2d");
    canvas.width = Cwidth;
    canvas.height = Cheight;

    drawables = (function() {
        var _i, _results;
        _results = [];
        for (i = _i = 1; _i <= particles; i = ++_i) {
            _results.push(new Particle);
        }
        return _results;
    })();

    function draw() {
        window.requestAnimFrame(draw);
        var d, _i, _len;
        canvas.width = Cwidth;
        canvas.height = Cheight;
        context.clearRect(0, 0, canvas.width, canvas.height)

        for (_i = 0, _len = drawables.length; _i < _len; _i++) {
            d = drawables[_i];
            d.draw(context);
        }
    };

    function update() {
        window.requestAnimFrame(update);
        var d, _i, _len, _results;
        _results = [];
        for (_i = 0, _len = drawables.length; _i < _len; _i++) {
            d = drawables[_i];
            _results.push(d.update());
        }
        return _results;
    };

    document.onmousemove = function(e) {
        mouseVX = mouseX;
        mouseVY = mouseY;
        mouseX = ~~e.pageX;
        mouseY = ~~e.pageY;
        mouseVX = ~~ ((mouseVX - mouseX) / 2);
        mouseVY = ~~ ((mouseVY - mouseY) / 2);

    };

    window.addEventListener('resize', draw, false);
    window.requestAnimFrame = (function() {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
        function(callback) {
            window.setTimeout(callback, 1000 / 60);
        };
    })();
    window.requestAnimFrame(draw);
    window.requestAnimFrame(update);
}).call(this);


可设置的参数有:
CanvasEl = "canvasbg";//Canvas的ID
min = 1;//最小光点直径
max = 4;//最大光点直径
particles = 200;//光点数量
Cwidth = 800;//画布宽度
Cheight = 600;//画布高度
colors = ["255, 255, 255","250, 150, 20","255, 100, 255"];//光点中间的颜色注意和晕开颜色顺序配对
GradientColors = ["84,204,243","250,150,20","255, 100, 255"];//光点晕开的颜色色注意和中间颜色顺序配对
注意:光点和光晕的顺序是配对的,比如第一个光点会搭配第一个光晕。






donate.png


telegram banner (1).png


1210 x 50(蓝底).png

cloudcone