粉丝破千了,喊几个机器人跳个舞庆祝下

2023-11-11

先看效果:

机器人跳舞

再看代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>机器人跳舞</title>
    <style>
        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;
        }
    </style>
</head>
<body>
<canvas></canvas>

</body>
<script>
    "use strict";
    /工作线程代码///
    const theLastExperience = noWorkers => {
        "use strict";
        // ---- 机器人结构 ----
        const struct = {
            points: [
                {
                    x: 0,
                    y: -4,
                    f(s, d) {
                        this.y -= 0.01 * s * ts;
                    }
                },
                {
                    x: 0,
                    y: -16,
                    f(s, d) {
                        this.y -= 0.02 * s * d * ts;
                    }
                },
                {
                    x: 0,
                    y: 12,
                    f(s, d) {
                        this.y += 0.02 * s * d * ts;
                    }
                },
                { x: -12, y: 0 },
                { x: 12, y: 0 },
                {
                    x: -3,
                    y: 34,
                    f(s, d) {
                        if (d > 0) {
                            this.x += 0.01 * s * ts;
                            this.y -= 0.015 * s * ts;
                        } else {
                            this.y += 0.02 * s * ts;
                        }
                    }
                },
                {
                    x: 3,
                    y: 34,
                    f(s, d) {
                        if (d > 0) {
                            this.y += 0.02 * s * ts;
                        } else {
                            this.x -= 0.01 * s * ts;
                            this.y -= 0.015 * s * ts;
                        }
                    }
                },
                {
                    x: -28,
                    y: 0,
                    f(s, d) {
                        this.x += this.vx * 0.025 * ts;
                        this.y -= 0.001 * s * ts;
                    }
                },
                {
                    x: 28,
                    y: 0,
                    f(s, d) {
                        this.x += this.vx * 0.025 * ts;
                        this.y -= 0.001 * s * ts;
                    }
                },
                {
                    x: -3,
                    y: 64,
                    f(s, d) {
                        this.y += 0.015 * s * ts;
                        if (d > 0) {
                            this.y -= 0.01 * s * ts;
                        } else {
                            this.y += 0.05 * s * ts;
                        }
                    }
                },
                {
                    x: 3,
                    y: 64,
                    f(s, d) {
                        this.y += 0.015 * s * ts;
                        if (d > 0) {
                            this.y += 0.05 * s * ts;
                        } else {
                            this.y -= 0.01 * s * ts;
                        }
                    }
                }
            ],
            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 }
            ]
        };
        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;
                // ---- 创建点 ----
                for (const p of struct.points) {
                    this.points.push(new Robot.Point(size * p.x + x, size * p.y + y, p.f));
                }
                // ---- 创建链接 ----
                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 % Math.round(20 / ts) === 0) this.dir = -this.dir;
                if (this === pointer.dancerDrag && this.size < 16 && this.frame > 600) {
                    pointer.dancerDrag = null;
                    dancers.push(
                        new Robot(
                            this.color + 90,
                            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) link.update();
                // ---- 更新点----
                for (const point of this.points) point.update(this);
                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);
                        // ---- 阴影 ----
                        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
                        );
                        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
                        );
                        ctx.restore();
                    }
                }
            }
        }
        Robot.Link = class Link {
            constructor(parent, p0, p1, dist, size, light, force, disk) {
                this.p0 = p0;
                this.p1 = p1;
                this.distance = dist;
                this.size = size;
                this.light = light || 1.0;
                this.force = force || 0.5;
                this.image = this.stroke(
                    "hsl(" + parent.color + " ,30%, " + parent.light * this.light + "%)",
                    true, disk, dist, size
                );
                this.shadow = this.stroke("rgba(0,0,0,0.5)", false, disk, dist, size);
            }
            update() {
                const p0 = this.p0;
                const p1 = this.p1;
                const dx = p1.x - p0.x;
                const dy = p1.y - p0.y;
                const dist = Math.sqrt(dx * dx + dy * dy);
                if (dist > 0.0) {
                    const tw = p0.w + p1.w;
                    const r1 = p1.w / tw;
                    const r0 = p0.w / tw;
                    const dz = (this.distance - dist) * this.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;
                }
            }
            stroke(color, axis, disk, dist, size) {
                let image;
                if (noWorkers) {
                    image = document.createElement("canvas");
                    image.width = dist + size;
                    image.height = size;
                } else {
                    image = new OffscreenCanvas(dist + size, 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;
            }
        };
        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;
            }
            update(robot) {
                // ---- 拖拉 ----
                if (robot === pointer.dancerDrag && this === pointer.pointDrag) {
                    this.x += (pointer.x - this.x) * 0.1;
                    this.y += (pointer.y - this.y) * 0.1;
                }
                // ---- 跳舞 ----
                if (robot !== pointer.dancerDrag) {
                    this.fn && this.fn(16 * Math.sqrt(robot.size), robot.dir);
                }
                this.vx = this.x - this.px;
                this.vy = this.y - this.py;
                this.px = this.x;
                this.py = this.y;
                this.vx *= 0.995;
                this.vy *= 0.995;
                this.x += this.vx;
                this.y += this.vy + 0.01 * ts;
            }
        };
        // ---- 初始化 ----
        const dancers = [];
        let ground = 1.0;
        let canvas = { width: 0, height: 0, resize: true };
        let ctx = null;
        let pointer = { x: 0, y: 0, dancerDrag: null, pointDrag: null };
        let ts = 1;
        let lastTime = 0;
        // ---- 来自主线程的消息 ----
        const message = e => {
            switch (e.data.msg) {
                case "start":
                    canvas.elem = e.data.elem;
                    canvas.width = canvas.elem.width;
                    canvas.height = canvas.elem.height;
                    ctx = canvas.elem.getContext("2d");
                    initRobots();
                    requestAnimationFrame(run);
                    break;
                case "resize":
                    canvas.width = e.data.width;
                    canvas.height = e.data.height;
                    canvas.resize = true;
                    break;
                case "pointerMove":
                    pointer.x = e.data.x;
                    pointer.y = e.data.y;
                    break;
                case "pointerDown":
                    pointer.x = e.data.x;
                    pointer.y = e.data.y;
                    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) {
                                pointer.dancerDrag = dancer;
                                pointer.pointDrag = point;
                                dancer.frame = 0;
                            }
                        }
                    }
                    break;
                case "pointerUp":
                    pointer.dancerDrag = null;
                    break;
            }
        };
        // ----调整屏幕大小----
        const resize = () => {
            canvas.elem.width = canvas.width;
            canvas.elem.height = canvas.height;
            canvas.resize = false;
            ground = canvas.height > 500 ? 0.85 : 1.0;
            for (let i = 0; i < dancers.length; i++) {
                dancers[i].x = (i + 2) * canvas.width / 9;
            }
        }
        // ---- 主回路 ----
        const run = (time) => {
            requestAnimationFrame(run);
            if (canvas.resize === true) resize();
            // ----根据屏幕频率调整速度 ----
            if (lastTime !== 0) {
                const t = (time - lastTime) / 16;
                ts += (t - ts) * 0.1;
                if (ts > 1) ts = 1;
            }
            lastTime = time;
            // ---- 清除屏幕 ----
            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();
            }
        };
        const initRobots = () => {
            // ---- 启动机器人 ----
            ground = canvas.height > 500 ? 0.85 : 1.0;
            for (let i = 0; i < 6; i++) {
                dancers.push(
                    new Robot(
                        i * 360 / 7,
                        80,
                        Math.sqrt(Math.min(canvas.width, canvas.height)) / 6,
                        (i + 2) * canvas.width / 9,
                        canvas.height * 0.5 - 100,
                        struct
                    )
                );
            }
        };
        // ---- 主线程与工作线程
        if (noWorkers) {
            // ---- 模拟postMessage接口 ----
            return {
                postMessage(data) {
                    message({ data: data });
                }
            };
        } else {
            onmessage = message;
        }
    };
    / 主线程代码 ///
    let worker = null;
    const createWorker = fn => {
        const URL = window.URL || window.webkitURL;
        return new Worker(URL.createObjectURL(new Blob(["(" + fn + ")()"])));
    };
    // ---- 初始化画布 ----
    const canvas = document.querySelector("canvas");
    canvas.width = canvas.offsetWidth;
    canvas.height = canvas.offsetHeight;
    // ---- 实例化工作者 ----
    if (window.Worker && window.OffscreenCanvas) {
        // 从函数中实例化后台工作程序
        worker = createWorker(theLastExperience);
        const offscreen = canvas.transferControlToOffscreen();
        // 发送数据
        worker.postMessage({ msg: "start", elem: offscreen }, [offscreen]);
    } else {
        // 回退执行到主线程
        worker = theLastExperience(true);
        worker.postMessage({ msg: "start", elem: canvas });
    }
    // ---- 调整大小事件 ----
    window.addEventListener(
        "resize",
        () => {
            worker.postMessage({
                msg: "resize",
                width: canvas.offsetWidth,
                height: canvas.offsetHeight
            });
        },
        false
    );
    // ----指针事件 ----
    const pointer = {
        x: 0,
        y: 0,
        down(e) {
            this.move(e);
            worker.postMessage({
                msg: "pointerDown",
                x: this.x,
                y: this.y
            });
        },
        up(e) {
            worker.postMessage({
                msg: "pointerUp"
            });
        },
        move(e) {
            if (e.targetTouches) {
                e.preventDefault();
                this.x = e.targetTouches[0].clientX;
                this.y = e.targetTouches[0].clientY;
            } else {
                this.x = e.clientX;
                this.y = e.clientY;
            }
            worker.postMessage({
                msg: "pointerMove",
                x: this.x,
                y: this.y
            });
        }
    };
    window.addEventListener("mousemove", e => pointer.move(e), false);
    canvas.addEventListener("touchmove", e => pointer.move(e), false);
    window.addEventListener("mousedown", e => pointer.down(e), false);
    window.addEventListener("touchstart", e => pointer.down(e), false);
    window.addEventListener("mouseup", e => pointer.up(e), false);
    window.addEventListener("touchend", e => pointer.up(e), false);

</script>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

粉丝破千了,喊几个机器人跳个舞庆祝下 的相关文章

随机推荐

  • 射频功率放大器PA芯片选型

    一 功率放大器选型 下图示例一个PA的核心参数 从频率失真和非线性失真两个方面基本可以上述参数的含义及其作用 如频率范围 功率平坦度 S21等主要和频率失真有关 即不同频率信号所表现的增益和相位差值 以及该PA的适用频段 而输出功率 输出功
  • Outlier Detection for Improved Data Quality and Diversity in Dialog Systems-学习笔记

    Outlier Detection for Improved Data Quality and Diversity in Dialog Systems 论文按如下方式检测数据集中的异常值 1 生成每个实例的矢量表示 2 平均向量以获得均值表
  • Android下实现字符串或文件的MD5加密

    MD5 信息摘要算法简单介绍 MD5 Message Digest Algorithm 一种被广泛使用的密码散列函数 可以产生出一个128位 16字节 的散列值 hash value 用于确保信息传输完整一致 计算出来的MD5值是有可能重复
  • CVE-2023-33246 Apache RocketMQ 命令注入漏洞复现及分析

    CVE 2023 33246 Apache RocketMQ 命令注入漏洞复现及分析 0x0 威胁情报 漏洞编号 CVE编号 CVE 2023 33246 漏洞评估 危害评级 高危 漏洞类型 RCE 公开程度 PoC已公开 利用条件 1 在
  • 【数据结构与算法】3.(单向、无向、带权)图,广度、深度优先搜索,贪心算法

    文章目录 1 图简介 2 图的存储方式 2 1 邻接矩阵存储方法 2 2 邻接表存储方法 3 有向 无向图和查询算法 3 1 数据结构 3 2 广度优先算法BFS 3 3 深度优先算法DFS 3 3 1 DFS查询单条路径 3 3 2 DF
  • PhotoShop 之盖印图层

    Ctrl Shift Alt E 生成盖印图层 盖印图层实现的结果和合并图层差不多 也就是把图层合并在一起生成一个新的图层 和合并图层所不同的是 盖印图层是生成新的图层 而被合并的图层依然存在 保持其它图层完好无损
  • reverse ez_xor writeup

    拿到ez xor exe附件直接丢进PE 可以看到是64位exe文件 丢进ida64 Shift F12查看字符串 如果是笔记本电脑的话 F12自带热键 先按Fn 即Fn Shift F12 一般在这里找有没有和flag相关的字符串 可以看
  • SecureCRT软件安装

    首先从官网下载SecureCRT官网地址 https www vandyke com cgi bin releases php product securecrt 也可以从百度网盘下载 下载完毕后正常安装SecureCRT 注意选择安装路径
  • 17-链表

    链表 一系列结构连在一起 每一个结构体变量里面都有一个指针pNext pNext指向下一个结构体变量 尾节点的pNext指向NULL 静态链表 struct students stu1 1 a NULL struct students st
  • Pytorch搭建神经网络完成监督学习-分类任务

    一 创建训练集 为了保证后续过程中产生的随机数都是一致的 方便测试 我们首先种下一颗随机种子 import torch import matplotlib pyplot as plt import torch nn functional a
  • Air780E模块硬件资料

    模块硬件资料 资料简介 相关链接 规格书 Air780E 模块产品规格书 V1 0 0 pdf 硬件设计手册 Air780E 硬件设计手册 V1 0 5 pdf 原理图及PCB Air780E 封装 zip 参考设计原理图 AD PADS9
  • 我的csdn排名和浏览量半个月没有变化

    我的csdn排名和浏览量半个月没有变化 希望csdn的管理员看见了 可以查一下 这样让用户很不放心咱们网站
  • AAAI 2021论文:门控记忆神经网络

    多维时间序列由多个随时间演化的相关变量共同构成 这种数据结构广泛存在于科学研究和现实应用场景中 比如在电商场景中 多类产品的销售额随时间变化 共同构成一组多维时间序列 在金融股票市场中 多支股票的价格构成一组多维时间序列 提取这类数据结构中
  • 正态性检验ks和sw区别_非参数检验思路总结,清晰理解就靠它了!

    1 何时使用非参数检验 或许你还没有理解什么是参数检验 非参数检验 但一定曾在无意之中使用过它们 如我们常用的方差分析 T检验 都属于参数检验 参数检验 就是假定数据服从某种分布 通过样本信息对总体参数进行检验 因而在分析前 先要检验数据是
  • libghttp的使用

    libghttp的使用 前言 一 libghttp是什么 二 使用步骤 1 引入库 前言 需要使用get请求来获得点数据 但是由于需要用户名和密码 所以失败了 但是编译的过程还有其他还是有参考价值的 一 libghttp是什么 官方网站ht
  • 华为OD机试真题 Java 实现【最长子字符串的长度】【2022Q4 100分】,附详细解题思路

    目录 专栏导读 一 题目描述 二 输入描述 三 输出描述 四 解题思路 解题思路如下 解题思路分析 五 Java算法源码 六 效果展示 1 输入 2 输出 3 说明 华为OD机试 2023B卷题库疯狂收录中 刷题点这里 专栏导读 一 题目描
  • 代码质量检测(三)—— SonarLint和SonarQube的本地使用

    根据 代码质量检测 一 常用代码质量管理工具 的介绍和 代码质量检测 二 如何选择代码检查工具 的分析 我们大概得出结论 在当前开源的代码质量检测工具中 阿里系列除外 pmd 基于源代码分析 主要面向安全编码规则 如 避免声明同名变量 包括
  • ​​PMP项目管理—第6章 项目进度管理。

    PMBOK项目管理知识体系指南 PMP项目管理学习笔记 总 第1章 引论 第2章 项目运行环境 第3章 项目经理的角色 第4章 项目整合管理 第5章 项目范围管理 第6章 项目进度管理 第7章 项目成本管理 第8章 项目质量管理 第9章 项
  • Ubuntu搭建Git仓库

    Ubuntu中搭建Git仓库 简介 这里使用的是阿里的Ubuntu服务器进行Git仓库搭建 Git在个人服务器搭建不适合新手 需要一定基础 安装Git 首先登录服务器 使用 以下命令安装Git sudo apt get install gi
  • 粉丝破千了,喊几个机器人跳个舞庆祝下

    先看效果 机器人跳舞 再看代码