专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
程序员小灰  ·  部署DeepSeek ... ·  12 小时前  
待字闺中  ·  DeepSeek“七问”李国杰院士 ·  13 小时前  
程序员的那些事  ·  快!快!快!DeepSeek 满血版真是快 ·  2 天前  
程序员的那些事  ·  清华大学:DeepSeek + ... ·  4 天前  
程序员的那些事  ·  OpenAI ... ·  3 天前  
51好读  ›  专栏  ›  SegmentFault思否

撩妹技能 get,教你用 canvas 画一场流星雨

SegmentFault思否  · 公众号  · 程序员  · 2017-09-08 08:00

正文

开始

妹子都喜欢流星,如果她说不喜欢,那她一定是一个假妹子。

现在就一起来做一场流星雨,用程序员的野路子浪漫一下。

要画一场流星雨,首先,自然我们要会画一颗流星。

玩过 canvas 的同学,你画圆画方画线条这么 6,如果说叫你画下面这个玩意儿,你会不会觉得你用的是假 canvas?canvas 没有画一个带尾巴玩意儿的 api 啊。

画一颗流星

是的,的却是没这个 api,但是不代表我们画不出来。流星就是一个小石头,然后因为速度过快产生大量的热量带动周围的空气发光发热,所以经飞过的地方看起来就像是流星的尾巴,我们先研究一下流星这个图像,整个流星处于他自己的运动轨迹之中,当前的位置最亮,轮廓最清晰,而之前划过的地方离当前位置轨迹距离越远就越暗淡越模糊。

上面的分析结果很关键, canvas 上是每一帧就重绘一次,每一帧之间的时间间隔很短。流星经过的地方会越来越模糊最后消失不见,那有没有可以让画布画的图像每过一帧就变模糊一点而不是全部清除的办法?如果可以这样,就可以把每一帧用线段画一小段流星的运动轨迹,最后画出流星的效果。

骗纸!你也许会说,这那里像流星了???

别急,让我多画几段给你看看。

什么? 还是不像? 我们把它画小点,这下总该像了把?

上面几幅图我是在 ps 上模拟的,本质上 ps 也是在画布上绘画,我们马上在 canvas 上试试。

那,直接代码实现一下。

  1. // 坐标

  2. class Crood {

  3.    constructor(x=0, y=0) {

  4.        this.x = x;

  5.        this.y = y;

  6.    }

  7.    setCrood(x, y) {

  8.        this.x = x;

  9.        this.y = y;

  10.    }

  11.    copy() {

  12.        return new Crood(this.x, this.y);

  13.    }

  14. }

  15. // 流星

  16. class ShootingStar {

  17.    constructor(init=new Crood, final=new Crood, size=3, speed=200, onDistory=null) {

  18.        this.init = init; // 初始位置

  19.        this.final = final; // 最终位置

  20.        this.size = size; // 大小

  21.        this.speed = speed; // 速度:像素/s

  22.        // 飞行总时间

  23.        this.dur = Math.sqrt(Math.pow(this.final.x-this.init.x, 2) + Math.pow(this.final.y-this.init.y, 2)) * 1000 / this.speed;

  24.        this.pass = 0; // 已过去的时间

  25.        this.prev = this.init.copy(); // 上一帧位置

  26.        this.now = this.init.copy(); // 当前位置

  27.        this.onDistory = onDistory;

  28.    }

  29.    draw(ctx, delta) {

  30.        this.pass += delta;

  31.        this.pass = Math.min( this.pass, this.dur);

  32.        let percent = this.pass / this.dur;

  33.        this.now.setCrood(

  34.            this.init.x + (this.final.x - this.init.x) * percent,

  35.            this.init.y + (this.final.y - this.init.y) * percent

  36.        );

  37.        // canvas

  38.        ctx.strokeStyle = '#fff';

  39.        ctx.lineCap = 'round';

  40.        ctx.lineWidth = this.size;

  41.        ctx.beginPath();

  42.        ctx.moveTo(this.now.x, this.now.y);

  43.        ctx.lineTo( this.prev.x, this.prev.y);

  44.        ctx.stroke();

  45.        this.prev.setCrood(this.now.x, this.now.y);

  46.        if (this.pass === this.dur) {

  47.             this.distory();

  48.        }

  49.    }

  50.    distory() {

  51.        this.onDistory && this.onDistory();

  52.    }

  53. }

  54. // effet

  55. let cvs = document.querySelector('canvas');

  56. let ctx = cvs.getContext('2d');

  57. let T;

  58. let shootingStar = new ShootingStar(

  59.                        new Crood(100, 100),

  60.                        new Crood(400, 400),

  61.                        3,

  62.                        200,

  63.                        ()=>{cancelAnimationFrame(T)}

  64.                    );

  65. let tick = (function() {

  66.    let now = (new Date()).getTime();

  67.    let last = now;

  68.    let delta;

  69.    return function() {

  70.        delta = now - last;

  71.        delta = delta > 500 ? 30 : (delta < 16? 16 : delta);

  72.        last = now;

  73.        // console.log(delta);

  74.        T = requestAnimationFrame(tick);

  75.        ctx.save();

  76.        ctx.fillStyle = 'rgba(0,0,0,0.2)'; // 每一帧用 “半透明” 的背景色清除画布

  77.        ctx.fillRect(0, 0, cvs.width, cvs.height);

  78.        ctx.restore();

  79.        shootingStar.draw(ctx, delta);

  80.    }

  81. })();

  82. tick();

效果:一颗流星

sogoyi 快看,一颗活泼不做作的流星!!! 是不是感觉动起来更加逼真一点?

流星雨

我们再加一个流星雨 MeteorShower 类,生成多一些随机位置的流星,做出流星雨。

  1. // 坐标

  2. class Crood {

  3.    constructor(x= 0, y=0) {

  4.        this.x = x;

  5.        this.y = y;

  6.    }

  7.    setCrood(x, y) {

  8.        this.x = x;

  9.        this.y = y;

  10.    }

  11.    copy() {

  12.        return new Crood(this.x, this.y);

  13.    }

  14. }

  15. // 流星

  16. class ShootingStar {

  17.    constructor(init= new Crood, final=new Crood, size=3, speed=200, onDistory=null) {

  18.        this.init = init; // 初始位置

  19.        this.final = final; // 最终位置

  20.         this.size = size; // 大小

  21.        this.speed = speed; // 速度:像素/s

  22.         // 飞行总时间

  23.        this.dur = Math.sqrt(Math.pow(this.final.x-this.init.x, 2) + Math.pow(this.final.y-this.init.y, 2)) * 1000 / this.speed;

  24.         this.pass = 0; // 已过去的时间

  25.        this.prev = this.init.copy(); // 上一帧位置

  26.        this.now = this.init.copy(); // 当前位置

  27.         this.onDistory = onDistory;

  28.    }

  29.    draw(ctx, delta) {

  30.         this.pass += delta;

  31.        this.pass = Math.min(this.pass, this.dur);

  32.        let percent = this.pass / this.dur;

  33.        this.now.setCrood(

  34.             this.init.x + (this.final.x - this.init.x) * percent,

  35.            this.init.y + (this.final.y - this.init.y) * percent

  36.        );

  37.        // canvas

  38.        ctx.strokeStyle = '#fff';

  39.        ctx.lineCap = 'round';

  40.        ctx.lineWidth = this.size;

  41.        ctx.beginPath();

  42.        ctx.moveTo(this.now.x, this.now.y);

  43.        ctx.lineTo( this.prev.x, this.prev.y);

  44.        ctx.stroke();

  45.         this.prev.setCrood(this.now.x, this.now.y);

  46.        if (this.pass === this.dur) {

  47.            this .distory();

  48.        }

  49.    }

  50.    distory() {

  51.        this.onDistory && this.onDistory();

  52.    }

  53. }

  54. class MeteorShower {

  55.    constructor(cvs, ctx) {

  56.        this.cvs = cvs;

  57.         this.ctx = ctx;

  58.        this.stars = [];

  59.         this.T;

  60.        this.stop = false;

  61.        this.playing = false;

  62.    }

  63.    createStar() {

  64.        let angle = Math.PI / 3;

  65.        let distance = Math.random() * 400;

  66.        let init = new Crood(Math.random() * this.cvs.width|0, Math.random() * 100|0);

  67.        let final = new Crood(init.x + distance * Math.cos(angle), init.y + distance * Math.sin(angle));

  68.        let size = Math.random() * 2;

  69.        let speed = Math.random() * 400 + 100;

  70.        let star = new ShootingStar(

  71.                        init, final, size, speed,

  72.                        ()=>{ this.remove(star)}

  73.                    );

  74.         return star;

  75.    }

  76.    remove(star) {

  77.        this.stars = this.stars.filter((s)=>{ return s !== star});

  78.    }

  79.    update(delta) {

  80.        if (!this.stop && this







请到「今天看啥」查看全文