导航

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

侧边栏
最新评论
广树管理员
2025-07-05 14:19
@saber酱:耶
saber酱
2025-07-05 14:03
萝莉塞高!
广树管理员
2025-07-05 11:52
@Jeffer.Z:360照片 + 一些图片合成技巧
Jeffer.Z
2025-07-05 11:28
广子你那张360度照片怎么做的,我看到了里面的纸片人,正常应该就是一个360照片啊。
广树管理员
2025-07-04 21:59
@HelloGakki:毕竟是当年国宾级别的酒店
攻略中
ToHeart
暂无评分
SteamToHeart
2025年6月26日 19时 ~ 攻略中
已累计游玩1周2天
AQUAPLUS推出的“温暖人心的校园恋爱游戏”将以高清全3D形式焕新归来!
爱上火车-Last Run!!-
暂无评分
Steam爱上火车-Last Run!!-
2025年6月24日 19时 ~ 攻略中
已累计游玩1周4天
游戏设定于架空日本。为了辅助驾驶员行驶,每列火车都配备了专用的铁路人偶。然而,由于新技术“空凝机”以及人工智能的面世,现有的铁路系统几乎已被废弃,铁路人偶和对应列车也被出售。主人公右田双铁所生活的“御一夜市”正在衰退,有人提出让空凝机工厂落户此地以振兴经济。空凝机工厂会带来水污染,而主人公为了阻止其落户而回到御一夜市。
符文工房龙之天地
暂无评分
Steam符文工房龙之天地
2025年6月10日 20时 ~ 攻略中
已累计游玩3周4天
体验奇幻冒险与日常生活的 RPG《符文工房》系列最新作。 玩家可以在日常生活中种田、钓鱼和复兴村庄,也可以在冒险的过程中邂逅各种各样的角色。打造属于自己的“村庄”,享受全新的冒险与生活吧!
fault - StP - LIGHTKRAVTE
暂无评分
Steamfault - StP - LIGHTKRAVTE
2025年5月29日 20时 ~ 攻略中
已累计游玩1个月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

1210 x 50(蓝底).png

cloudcone