來自秋娃兒送上的前端水貨

給各位大佬送上一個無聊打發(fā)時間的東西滋早,絕對魔性(有很多隱藏彩蛋B凇)

先不說了直接上HTML代碼:

<canvas></canvas>
<!-- 
                  ,--.    ,--.
                 ((O ))--((O ))
               ,'_`--'____`--'_`.
              _:  ____________  :_
             | | ||::::::::::|| | |
             | | ||::::::::::|| | |
             | | ||::::::::::|| | |
             |_| |/__________\| |_|
               |________________|
            __..-'            `-..__
         .-| : .----------------. : |-.
       ,\ || | |\______________/| | || /.
      /`.\:| | ||  __  __  __  || | |;/,'\
     :`-._\;.| || '--''--''--' || |,:/_.-':
     |    :  | || .----------. || |  :    |
     |    |  | || '----------' || |  |    |
     |    |  | ||   _   _   _  || |  |    |
     :,--.;  | ||  (_) (_) (_) || |  :,--.;
     (`-'|)  | ||______________|| |  (|`-')
      `--'   | |/______________\| |   `--'
             |____________________|
              `.________________,'
               (_______)(_______)
               (_______)(_______)
               (_______)(_______)
               (_______)(_______)
              |        ||        |
              '--------''--------'
-->

是不是覺得很簡單很容易理解的HTML代碼

我們需要設(shè)置一下CSS樣式代碼,處理下默認(rèn)樣式雜七雜八的了:

    body, html {
        position: absolute;
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
            
    canvas {
        position: absolute;
        width: 100%;
        height: 100%;
        background:#000;
        cursor: pointer;
    }

overflow: hidden;屬性主要是讓頁面不要出現(xiàn)滾動條喲馆衔,不然到時候我們玩的時候的體驗
咱們來看看JS代碼锰蓬,這個好像才是重點闰靴。

         {
            class Robot {
                constructor(color, light, size, x, y, struct) {
                    this.x = x;
                    this.points = [];
                    this.links = [];
                    this.frame = 0;
                    this.dir = 1;
                    this.size = size;
                    this.color = Math.round(color);
                    this.light = light;
                    // ---- 創(chuàng)建點 ----
                    for (const p of struct.points) {
                        this.points.push(new Robot.Point(size * p.x + x, size * p.y + y, p.f));
                    }
                    // ---- 創(chuàng)建鏈接 ----
                    for (const link of struct.links) {
                        const p0 = this.points[link.p0];
                        const p1 = this.points[link.p1];
                        const dx = p0.x - p1.x;
                        const dy = p0.y - p1.y;
                        this.links.push(
                            new Robot.Link(
                                this,
                                p0,
                                p1,
                                Math.sqrt(dx * dx + dy * dy),
                                link.size * size / 3,
                                link.lum,
                                link.force,
                                link.disk
                            )
                        );
                    }
                }
                update() {
                    if (++this.frame % 20 === 0) this.dir = -this.dir;
                    if (
                        dancerDrag &&
                        this === dancerDrag &&
                        this.size < 16 &&
                        this.frame > 600
                    ) {
                        dancerDrag = null;
                        dancers.push(
                            new Robot(
                                this.color,
                                this.light * 1.25,
                                this.size * 2,
                                pointer.x,
                                pointer.y - 100 * this.size * 2,
                                struct
                            )
                        );
                        dancers.sort(function(d0, d1) {
                            return d0.size - d1.size;
                        });
                    }
                    // ---- 更新鏈接 ----
                    for (const link of this.links) {
                        const p0 = link.p0;
                        const p1 = link.p1;
                        const dx = p0.x - p1.x;
                        const dy = p0.y - p1.y;
                        const dist = Math.sqrt(dx * dx + dy * dy);
                        if (dist) {
                            const tw = p0.w + p1.w;
                            const r1 = p1.w / tw;
                            const r0 = p0.w / tw;
                            const dz = (link.distance - dist) * link.force;
                            const sx = dx / dist * dz;
                            const sy = dy / dist * dz;
                            p1.x -= sx * r0;
                            p1.y -= sy * r0;
                            p0.x += sx * r1;
                            p0.y += sy * r1;
                        }
                    }
                    // ---- 更新點 ----
                    for (const point of this.points) {
                        // ---- 拖曳 ----
                        if (this === dancerDrag && point === pointDrag) {
                            point.x += (pointer.x - point.x) * 0.1;
                            point.y += (pointer.y - point.y) * 0.1;
                        }
                        // ---- 跳舞 ----
                        if (this !== dancerDrag) {
                            point.fn && point.fn(16 * Math.sqrt(this.size), this.dir);
                        }
                        // ---- 虛擬集成 ----
                        point.vx = point.x - point.px;
                        point.vy = point.y - point.py;
                        point.px = point.x;
                        point.py = point.y;
                        point.vx *= 0.995;
                        point.vy *= 0.995;
                        point.x += point.vx;
                        point.y += point.vy + 0.01;
                    }
                    // ---- 地面 ----
                    for (const link of this.links) {
                        const p1 = link.p1;
                        if (p1.y > canvas.height * ground - link.size * 0.5) {
                            p1.y = canvas.height * ground - link.size * 0.5;
                            p1.x -= p1.vx;
                            p1.vx = 0;
                            p1.vy = 0;
                        }
                    }
                    // ---- 中心位置 ----
                    this.points[3].x += (this.x - this.points[3].x) * 0.001;
                }
                draw() {
                    for (const link of this.links) {
                        if (link.size) {
                            const dx = link.p1.x - link.p0.x;
                            const dy = link.p1.y - link.p0.y;
                            const a = Math.atan2(dy, dx);
                            const d = Math.sqrt(dx * dx + dy * dy);
                            // ---- 陰影 ----
                            ctx.save();
                            ctx.translate(link.p0.x + link.size * 0.25, link.p0.y + link.size * 0.25);
                            ctx.rotate(a);
                            ctx.drawImage(
                                link.shadow,
                                -link.size * 0.5,
                                -link.size * 0.5,
                                d + link.size,
                                link.size
                            );
                            ctx.restore();
                            // ---- 劃 ----
                            ctx.save();
                            ctx.translate(link.p0.x, link.p0.y);
                            ctx.rotate(a);
                            ctx.drawImage(
                                link.image,
                                -link.size * 0.5,
                                -link.size * 0.5,
                                d + link.size,
                                link.size
                            );
                            ctx.restore();
                        }
                    }
                }
            }
            Robot.Link = class Link {
                constructor(parent, p0, p1, dist, size, light, force, disk) {
                    // ---- 緩存筆劃 ----
                    function stroke(color, axis) {
                        const image = document.createElement("canvas");
                        image.width = dist + size;
                        image.height = size;
                        const ict = image.getContext("2d");
                        ict.beginPath();
                        ict.lineCap = "round";
                        ict.lineWidth = size;
                        ict.strokeStyle = color;
                        if (disk) {
                            ict.arc(size * 0.5 + dist, size * 0.5, size * 0.5, 0, 2 * Math.PI);
                            ict.fillStyle = color;
                            ict.fill();
                        } else {
                            ict.moveTo(size * 0.5, size * 0.5);
                            ict.lineTo(size * 0.5 + dist, size * 0.5);
                            ict.stroke();
                        }
                        if (axis) {
                            const s = size / 10;
                            ict.fillStyle = "#000";
                            ict.fillRect(size * 0.5 - s, size * 0.5 - s, s * 2, s * 2);
                            ict.fillRect(size * 0.5 - s + dist, size * 0.5 - s, s * 2, s * 2);
                        }
                        return image;
                    }
                    this.p0 = p0;
                    this.p1 = p1;
                    this.distance = dist;
                    this.size = size;
                    this.light = light || 1.0;
                    this.force = force || 0.5;
                    this.image = stroke(
                        "hsl(" + parent.color + " ,30%, " + parent.light * this.light + "%)",
                        true
                    );
                    this.shadow = stroke("rgba(0,0,0,0.5)");
                }
            };
            Robot.Point = class Point {
                constructor(x, y, fn, w) {
                    this.x = x;
                    this.y = y;
                    this.w = w || 0.5;
                    this.fn = fn || null;
                    this.px = x;
                    this.py = y;
                    this.vx = 0.0;
                    this.vy = 0.0;
                }
            };
            // ---- 設(shè)置畫布 ----
            const canvas = {
                init() {
                    this.elem = document.querySelector("canvas");
                    this.resize();
                    window.addEventListener("resize", () => this.resize(), false);
                    return this.elem.getContext("2d");
                },
                resize() {
                    this.width = this.elem.width = this.elem.offsetWidth;
                    this.height = this.elem.height = this.elem.offsetHeight;
                    ground = this.height > 500 ? 0.85 : 1.0;
                    for (let i = 0; i < dancers.length; i++) {
                        dancers[i].x = (i + 2) * canvas.width / 9;
                    }
                }
            };
            // ---- 設(shè)置指針 ----
            const pointer = {
                init(canvas) {
                    this.x = 0;
                    this.y = 0;
                    window.addEventListener("mousemove", e => this.move(e), false);
                    canvas.elem.addEventListener("touchmove", e => this.move(e), false);
                    window.addEventListener("mousedown", e => this.down(e), false);
                    window.addEventListener("touchstart", e => this.down(e), false);
                    window.addEventListener("mouseup", e => this.up(e), false);
                    window.addEventListener("touchend", e => this.up(e), false);
                },
                down(e) {
                    this.move(e);
                    for (const dancer of dancers) {
                        for (const point of dancer.points) {
                            const dx = pointer.x - point.x;
                            const dy = pointer.y - point.y;
                            const d = Math.sqrt(dx * dx + dy * dy);
                            if (d < 60) {
                                dancerDrag = dancer;
                                pointDrag = point;
                                dancer.frame = 0;
                            }
                        }
                    }
                },
                up(e) {
                    dancerDrag = null;
                },
                move(e) {
                    let touchMode = e.targetTouches,
                        pointer;
                    if (touchMode) {
                        e.preventDefault();
                        pointer = touchMode[0];
                    } else pointer = e;
                    this.x = pointer.clientX;
                    this.y = pointer.clientY;
                }
            };
            // ---- 初始化 ----
            const dancers = [];
            let ground = 1.0;
            const ctx = canvas.init();
            pointer.init(canvas);
            let dancerDrag = null;
            let pointDrag = null;
            // ---- 主回路 ----
            const run = () => {
                requestAnimationFrame(run);
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.fillStyle = "#222";
                ctx.fillRect(0, 0, canvas.width, canvas.height * 0.15);
                ctx.fillRect(0, canvas.height * 0.85, canvas.width, canvas.height * 0.15);
                for (const dancer of dancers) {
                    dancer.update();
                    dancer.draw();
                }
            };
            // ---- 機器人結(jié)構(gòu) ----
            const struct = {
                points: [
                    {
                        x: 0,
                        y: -4,
                        f(s, d) {
                            this.y -= 0.01 * s;
                        }
                    },
                    {
                        x: 0,
                        y: -16,
                        f(s, d) {
                            this.y -= 0.02 * s * d;
                        }
                    },
                    {
                        x: 0,
                        y: 12,
                        f(s, d) {
                            this.y += 0.02 * s * d;
                        }
                    },
                    { x: -12, y: 0 },
                    { x: 12, y: 0 },
                    {
                        x: -3,
                        y: 34,
                        f(s, d) {
                            if (d > 0) {
                                this.x += 0.01 * s;
                                this.y -= 0.015 * s;
                            } else {
                                this.y += 0.02 * s;
                            }
                        }
                    },
                    {
                        x: 3,
                        y: 34,
                        f(s, d) {
                            if (d > 0) {
                                this.y += 0.02 * s;
                            } else {
                                this.x -= 0.01 * s;
                                this.y -= 0.015 * s;
                            }
                        }
                    },
                    {
                        x: -28,
                        y: 0,
                        f(s, d) {
                            this.x += this.vx * 0.035;
                            this.y -= 0.001 * s;
                        }
                    },
                    {
                        x: 28,
                        y: 0,
                        f(s, d) {
                            this.x += this.vx * 0.035;
                            this.y -= 0.001 * s;
                        }
                    },
                    {
                        x: -3,
                        y: 64,
                        f(s, d) {
                            this.y += 0.015 * s;
                            if (d > 0) {
                                this.y -= 0.01 * s;
                            } else {
                                this.y += 0.05 * s;
                            }
                        }
                    },
                    {
                        x: 3,
                        y: 64,
                        f(s, d) {
                            this.y += 0.015 * s;
                            if (d > 0) {
                                this.y += 0.05 * s;
                            } else {
                                this.y -= 0.01 * s;
                            }
                        }
                    }
                ],
                links: [
                    { p0: 3, p1: 7, size: 12, lum: 0.5 },
                    { p0: 1, p1: 3, size: 24, lum: 0.5 },
                    { p0: 1, p1: 0, size: 60, lum: 0.5, disk: 1 },
                    { p0: 5, p1: 9, size: 16, lum: 0.5 },
                    { p0: 2, p1: 5, size: 32, lum: 0.5 },
                    { p0: 1, p1: 2, size: 50, lum: 1 },
                    { p0: 6, p1: 10, size: 16, lum: 1.5 },
                    { p0: 2, p1: 6, size: 32, lum: 1.5 },
                    { p0: 4, p1: 8, size: 12, lum: 1.5 },
                    { p0: 1, p1: 4, size: 24, lum: 1.5 }
                ]
            };
            // ---- 虛擬機器人 ----
            for (let i = 0; i < 6; i++) {
                dancers.push(
                    new Robot(
                        i * 360 / 7,
                        80,
                        (window.location.href.indexOf("fullcpgrid") > -1) ? 3 : 4,
                        (i + 2) * canvas.width / 9,
                        canvas.height * ground - 300,
                        struct
                    )
                );
            }
            run();
        }

大佬們把代碼copy下來就可以看到效果啦(最好是用谷歌瀏覽器打開喲)~


5Z4OSE7}~BZ_)HCWL03S`3T.png

有什么疑難雜癥留言我回復(fù)赋秀,么么噠~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末喳钟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子减细,更是在濱河造成了極大的恐慌匆瓜,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件未蝌,死亡現(xiàn)場離奇詭異驮吱,居然都是意外死亡,警方通過查閱死者的電腦和手機萧吠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門左冬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人纸型,你說我怎么就攤上這事拇砰∶芳桑” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵除破,是天一觀的道長牧氮。 經(jīng)常有香客問我,道長瑰枫,這世上最難降的妖魔是什么踱葛? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮光坝,結(jié)果婚禮上剖毯,老公的妹妹穿的比我還像新娘。我一直安慰自己教馆,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布擂达。 她就那樣靜靜地躺著土铺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪板鬓。 梳的紋絲不亂的頭發(fā)上悲敷,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機與錄音俭令,去河邊找鬼后德。 笑死,一個胖子當(dāng)著我的面吹牛抄腔,可吹牛的內(nèi)容都是我干的瓢湃。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赫蛇,長吁一口氣:“原來是場噩夢啊……” “哼绵患!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起悟耘,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤落蝙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后暂幼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體筏勒,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年旺嬉,在試婚紗的時候發(fā)現(xiàn)自己被綠了管行。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡邪媳,死狀恐怖病瞳,靈堂內(nèi)的尸體忽然破棺而出揽咕,到底是詐尸還是另有隱情,我是刑警寧澤套菜,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布亲善,位于F島的核電站,受9級特大地震影響逗柴,放射性物質(zhì)發(fā)生泄漏蛹头。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一戏溺、第九天 我趴在偏房一處隱蔽的房頂上張望渣蜗。 院中可真熱鬧,春花似錦旷祸、人聲如沸耕拷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽骚烧。三九已至,卻和暖如春闰围,著一層夾襖步出監(jiān)牢的瞬間赃绊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工羡榴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留碧查,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓校仑,卻偏偏與公主長得像忠售,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子迄沫,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容