跳动的爱心代码--李峋爱心代码(完整源码)

2023-10-29

本文章分为两部分:
第一部分为实现效果展示,第二部分是实现跳动爱心源码。
关注微信公众号: ClassmateJie

跳动的爱心效果展示

在这里插入图片描述
关注微信公众号【ClassmateJie】获取完整源码,回复爱心代码

实现步骤

1.建一个html文件,代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>爱心跳动,3D拖拽搬</title>
  <link rel="stylesheet" href="./css/style.css">

</head>

<body>

  <script src='./js/three.min.js'></script>
  <!-- <script src='./js/MeshSurfaceSampler.js'></script> -->
  <script src='./js/TrackballControls.js'></script>
  <script src='./js/simplex-noise.js'></script>
  <script src='./js/OBJLoader.js'></script>
  <script src='./js/gsap.min.js'></script>
  <script src="./js/script.js"></script>


  <script>


    (function () {
      const _face = new THREE.Triangle();

      const _color = new THREE.Vector3();

      class MeshSurfaceSampler {

        constructor(mesh) {

          let geometry = mesh.geometry;

          if (!geometry.isBufferGeometry || geometry.attributes.position.itemSize !== 3) {

            throw new Error('THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.');

          }

          if (geometry.index) {

            console.warn('THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.');
            geometry = geometry.toNonIndexed();

          }

          this.geometry = geometry;
          this.randomFunction = Math.random;
          this.positionAttribute = this.geometry.getAttribute('position');
          this.colorAttribute = this.geometry.getAttribute('color');
          this.weightAttribute = null;
          this.distribution = null;

        }

        setWeightAttribute(name) {

          this.weightAttribute = name ? this.geometry.getAttribute(name) : null;
          return this;

        }

        build() {

          const positionAttribute = this.positionAttribute;
          const weightAttribute = this.weightAttribute;
          const faceWeights = new Float32Array(positionAttribute.count / 3); // Accumulate weights for each mesh face.

          for (let i = 0; i < positionAttribute.count; i += 3) {

            let faceWeight = 1;

            if (weightAttribute) {

              faceWeight = weightAttribute.getX(i) + weightAttribute.getX(i + 1) + weightAttribute.getX(i + 2);

            }

            _face.a.fromBufferAttribute(positionAttribute, i);

            _face.b.fromBufferAttribute(positionAttribute, i + 1);

            _face.c.fromBufferAttribute(positionAttribute, i + 2);

            faceWeight *= _face.getArea();
            faceWeights[i / 3] = faceWeight;

          } // Store cumulative total face weights in an array, where weight index
          // corresponds to face index.


          this.distribution = new Float32Array(positionAttribute.count / 3);
          let cumulativeTotal = 0;

          for (let i = 0; i < faceWeights.length; i++) {

            cumulativeTotal += faceWeights[i];
            this.distribution[i] = cumulativeTotal;

          }

          return this;

        }

        setRandomGenerator(randomFunction) {

          this.randomFunction = randomFunction;
          return this;

        }

        sample(targetPosition, targetNormal, targetColor) {

          const cumulativeTotal = this.distribution[this.distribution.length - 1];
          const faceIndex = this.binarySearch(this.randomFunction() * cumulativeTotal);
          return this.sampleFace(faceIndex, targetPosition, targetNormal, targetColor);

        }

        binarySearch(x) {

          const dist = this.distribution;
          let start = 0;
          let end = dist.length - 1;
          let index = - 1;

          while (start <= end) {

            const mid = Math.ceil((start + end) / 2);

            if (mid === 0 || dist[mid - 1] <= x && dist[mid] > x) {

              index = mid;
              break;

            } else if (x < dist[mid]) {

              end = mid - 1;

            } else {

              start = mid + 1;

            }

          }

          return index;

        }

        sampleFace(faceIndex, targetPosition, targetNormal, targetColor) {

          let u = this.randomFunction();
          let v = this.randomFunction();

          if (u + v > 1) {

            u = 1 - u;
            v = 1 - v;

          }

          _face.a.fromBufferAttribute(this.positionAttribute, faceIndex * 3);

          _face.b.fromBufferAttribute(this.positionAttribute, faceIndex * 3 + 1);

          _face.c.fromBufferAttribute(this.positionAttribute, faceIndex * 3 + 2);

          targetPosition.set(0, 0, 0).addScaledVector(_face.a, u).addScaledVector(_face.b, v).addScaledVector(_face.c, 1 - (u + v));

          if (targetNormal !== undefined) {

            _face.getNormal(targetNormal);

          }

          if (targetColor !== undefined && this.colorAttribute !== undefined) {

            _face.a.fromBufferAttribute(this.colorAttribute, faceIndex * 3);

            _face.b.fromBufferAttribute(this.colorAttribute, faceIndex * 3 + 1);

            _face.c.fromBufferAttribute(this.colorAttribute, faceIndex * 3 + 2);

            _color.set(0, 0, 0).addScaledVector(_face.a, u).addScaledVector(_face.b, v).addScaledVector(_face.c, 1 - (u + v));

            targetColor.r = _color.x;
            targetColor.g = _color.y;
            targetColor.b = _color.z;
          }
          return this;

        }

      }

      THREE.MeshSurfaceSampler = MeshSurfaceSampler;

    })();

  </script>
  <script>
    (function () {

      const _object_pattern = /^[og]\s*(.+)?/; // mtllib file_reference

      const _material_library_pattern = /^mtllib /; // usemtl material_name

      const _material_use_pattern = /^usemtl /; // usemap map_name

      const _map_use_pattern = /^usemap /;

      const _vA = new THREE.Vector3();

      const _vB = new THREE.Vector3();

      const _vC = new THREE.Vector3();

      const _ab = new THREE.Vector3();

      const _cb = new THREE.Vector3();

      function ParserState() {

        const state = {
          objects: [],
          object: {},
          vertices: [],
          normals: [],
          colors: [],
          uvs: [],
          materials: {},
          materialLibraries: [],
          startObject: function (name, fromDeclaration) {

            // If the current object (initial from reset) is not from a g/o declaration in the parsed
            // file. We need to use it for the first parsed g/o to keep things in sync.
            if (this.object && this.object.fromDeclaration === false) {

              this.object.name = name;
              this.object.fromDeclaration = fromDeclaration !== false;
              return;

            }

            const previousMaterial = this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined;

            if (this.object && typeof this.object._finalize === 'function') {

              this.object._finalize(true);

            }

            this.object = {
              name: name || '',
              fromDeclaration: fromDeclaration !== false,
              geometry: {
                vertices: [],
                normals: [],
                colors: [],
                uvs: [],
                hasUVIndices: false
              },
              materials: [],
              smooth: true,
              startMaterial: function (name, libraries) {

                const previous = this._finalize(false); // New usemtl declaration overwrites an inherited material, except if faces were declared
                // after the material, then it must be preserved for proper MultiMaterial continuation.


                if (previous && (previous.inherited || previous.groupCount <= 0)) {

                  this.materials.splice(previous.index, 1);

                }

                const material = {
                  index: this.materials.length,
                  name: name || '',
                  mtllib: Array.isArray(libraries) && libraries.length > 0 ? libraries[libraries.length - 1] : '',
                  smooth: previous !== undefined ? previous.smooth : this.smooth,
                  groupStart: previous !== undefined ? previous.groupEnd : 0,
                  groupEnd: - 1,
                  groupCount: - 1,
                  inherited: false,
                  clone: function (index) {

                    const cloned = {
                      index: typeof index === 'number' ? index : this.index,
                      name: this.name,
                      mtllib: this.mtllib,
                      smooth: this.smooth,
                      groupStart: 0,
                      groupEnd: - 1,
                      groupCount: - 1,
                      inherited: false
                    };
                    cloned.clone = this.clone.bind(cloned);
                    return cloned;

                  }
                };
                this.materials.push(material);
                return material;

              },
              currentMaterial: function () {

                if (this.materials.length > 0) {

                  return this.materials[this.materials.length - 1];

                }

                return undefined;

              },
              _finalize: function (end) {

                const lastMultiMaterial = this.currentMaterial();

                if (lastMultiMaterial && lastMultiMaterial.groupEnd === - 1) {

                  lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
                  lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
                  lastMultiMaterial.inherited = false;

                } // Ignore objects tail materials if no face declarations followed them before a new o/g started.


                if (end && this.materials.length > 1) {

                  for (let mi = this.materials.length - 1; mi >= 0; mi--) {

                    if (this.materials[mi].groupCount <= 0) {

                      this.materials.splice(mi, 1);

                    }

                  }

                } // Guarantee at least one empty material, this makes the creation later more straight forward.


                if (end && this.materials.length === 0) {

                  this.materials.push({
                    name: '',
                    smooth: this.smooth
                  });

                }
.....
</body>

</html>


  1. 建立一个css文件
body {
  background: rgb(0, 0, 0);
  overflow: hidden;
  margin: 0;
  /* background-color: #000 !important; */
}

  1. 运行html文件
    在这里插入图片描述

获取源码直接运行

在这里插入图片描述

关注微信公众号「 ClassmateJie
更多惊喜等待你的发掘
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

跳动的爱心代码--李峋爱心代码(完整源码) 的相关文章

  • Google Analytics API - 跟踪子域

    我有一个网站 每个用户都注册为子域 username domain com 我正在使用以下代码跟踪所有子域 var gaq gaq gaq push setAccount UA XXXXXX X gaq push setDomainName
  • 调整大小后获取实际图像大小

    我有一个充满缩略图的页面 用 css 调整大小150x150 当我单击缩略图时 页面变暗 并且图像以其真实尺寸显示 目前 我必须手动创建一个包含所有图像的实际高度的数组 为了解决设计问题 减少画廊的手动操作 我需要在调整图像大小 CSS 后
  • 如何使用 jQuery 将各种元素包装在 div 标签中?

    我有一个 html 结构 如下所示 h5 Title h5 p Content p ul li Item li li Item li ul p Content p h5 Title h5 p Content p ul li Item li
  • 如何使单词中的每个字母在悬停时发生变化

    假设我的网站上某个段落中有一个单词 IamGreat 我希望它在悬停时更改为 Good4you 但是 我不想更改整个单词 而是希望每个字母单独更改 因此 如果我将鼠标悬停在字母 I 上 它将变成字母 G 字母 r 将变成数字 4 等 这两个
  • 等待动态加载脚本

    在我的页面正文中 我需要插入以下代码作为 AJAX 调用的结果 p Loading jQuery p p Using jQuery p 我不能使用 load 由于文档已经加载 因此该事件不会触发 这安全吗 如果没有 我如何确保在执行自定义生
  • jQuery 或 JavaScript 中的“$”符号是什么意思? [复制]

    这个问题在这里已经有答案了 可能的重复 JavaScript 中 符号的含义是什么 https stackoverflow com questions 1150381 what is the meaning of sign in javas
  • AngularJS Youtube 播放器嵌入非常大的播放列表

    我目前正在构建一个 AngularJS 应用程序 我知道它有点过时 但我对它很有信心 我的应用程序需要嵌入一个 YouTube 播放器 其中包含一个非常大的播放列表 大约 1500 个项目 但我无法对其进行编码 以便它实际上可以嵌入超过 2
  • javascript 代码只能在函数之外工作 - 为什么?

    为什么这段代码不能像下面写的那样工作 但如果我注释掉function testBgChange 并将代码保留在该函数内 它可以正常工作 如果我将代码保留在函数中然后调用该函数 会有什么区别
  • 谷歌地图API v3如何获取所有形状的坐标

    我有这个谷歌脚本 可以创建形状和删除形状 但没有太多关于保存形状的信息 我查了一下互联网 知道我可以通过 overlaycomplete 中的 getpaths 访问路径坐标 而且我还可以将坐标推入一个收集所有形状的数组中 但是 如果用户删
  • 变量值的 swap() 函数[重复]

    这个问题在这里已经有答案了 我无法达到下面这个交换函数的预期结果 我希望将值打印为 3 2 function swap x y var t x x y y t console log swap 2 3 任何线索将不胜感激 您的函数正在内部交
  • QUnit 与固定装置的奇怪行为,测试交替失败和通过

    我在 QUnit 中进行了以下设置 Dozen or so previous tests here test Test some markup generation function qunit fixture plugin jQuery
  • Angular 8 webpack-bundle-analyzer 寻找错误的polyfill 文件

    无论我做什么 构建项目后我都会收到以下错误 Error parsing bundle asset
  • 使 div 的大小与其内部图像的大小相同

    我有一个带有以下代码的div HTML div img src img logo png div CSS div imgContainer width 250px height 250px padding 13px 问题是用户可以编辑图像大
  • Electron Auth0Lock“原始文件://不允许”

    尝试让 auth0 与我的电子应用程序一起使用 当我按照默认教程并尝试使用用户名 密码 身份验证进行身份验证时 锁定失败并显示 403 错误 并响应 不允许使用 Origin file 我还在 auth0 仪表板中客户端设置的允许来源 CO
  • IE7 问题 - 当禁用文件下载自动提示时无法下载流式文件

    我的应用程序是基于 J2EE JSP Servlet 的 当我尝试从 JSP 打开新窗口 弹出窗口 并调用 Servlet 操作 例如 Streamer do 以在该弹出窗口内传输 PDF 文件时 我遇到了问题 问题 当 IE 7 gt 工
  • Chrome 上的 contenteditable 中未显示编辑光标

    当您打开此页面时 请参阅现场演示 http jsfiddle net gs3p1a6r 3 show 与 Chrome span span CSS myspan border 0 outline 0 JS myspan focus the
  • jQuery可排序发布数据,但没有数据

    谁能告诉我我在这里缺少什么 我的数据似乎总是空的 我做错了什么 document ready function nav sortable connectWith nav axis y update function event ui var
  • 查看元素的所有 dom 事件

    我有一个 jQuery UI 日期选择器 当您单击日期时 它会清除我的 URL 哈希值 并且不会更改文本框中的日期 我假设某个地方还有其他一些 JavaScript 实用程序 它也正在调用某种委托事件 抛出错误并终止 jquery 处理程序
  • 如何在jsp页面中包含javascript

    我是 J2EE 和 Web 开发的新手 这是我的问题 我想在网页中包含 angular js 这是有效的版本 但我也想要一些本地的 javascript 文件 并且希望我想在本地目录中导入 angularjs
  • 在 Javascript 中动态创建 []array

    也许这将是一个真正愚蠢的问题 但我是 JavaScript 新手 并且坚持动态创建数组 如下格式 items Date 2012 01 21T23 45 10 280Z Value 7 Date 2012 01 26T23 45 10 28

随机推荐

  • C#的线性规划代码

    using System using System Collections Generic using System Linq using System Text using System Threading Tasks namespace
  • C语言实现RAND函数的方法

    C语言使用rand 一个值就可以实现生成一个伪随机数供我们使用 那么rand函数是如何实现的呢 我们自己可不可以编辑出来 其实是可以的 rand作为伪随机数发生器产生的是一个伪随机数 一般的用途能够满足 要想实现这个函数 需要用一个公式 x
  • Springboot使用Thymeleaf模板引擎无法加载css样式等静态资源

    我的项目文件目录如下 在使用thymeleaf模板引擎时访问不到css样式文件如下 其原因是Thymeleaf模板引擎引用文件需要按照规定的写法 如下 需使用th href属性 路径需使用 文件路径 包裹 加上后就可以看见样式了
  • 报表的发布

    各种不同的报表工具 其发布方式是不一样的 现在说说一些报表工具的发布方式 笔者对报表了解不多 可能有些认识上的错误 望大家指明 DevExpress公司出品的报表工具 XtraReport 它的报表设计器就是集成在VS NET集成开发环境中
  • 简单了解JVM 组成以及各部分

    简单介绍JVM各组成部分 图1 JVM组成部分 JVM包含两个子系统和两个组件 两个子系统 类加载子系统 Class loader 和执行引擎 Execution engine 两个组件 运行时数据区 Runtime data area 本
  • leetcode 1233. 删除子文件夹

    1233 删除子文件夹 难度中等142 你是一位系统管理员 手里有一份文件夹列表 folder 你的任务是要删除该列表中的所有 子文件夹 并以 任意顺序 返回剩下的文件夹 如果文件夹 folder i 位于另一个文件夹 folder j 下
  • Java集合框架

    文章目录 一 简介 1 集合框架介绍 2 相关容器介绍 2 1 Set相关 2 2 List相关 2 3 Queue相关 2 4 Map相关 3 集合重点 二 ArrayList分析 1 ArrayList使用 2 ArrayList介绍
  • MMCV/MMDet/MMDet3D 的版本对应

    MMDetection3D version MMDetection version MMSegmentation version MMCV version master mmdet gt 2 24 0 lt 3 0 0 mmseg gt 0
  • [Pytorch系列-65]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - 无监督图像生成CycleGan的基本原理

    作者主页 文火冰糖的硅基工坊 文火冰糖 王文兵 的博客 文火冰糖的硅基工坊 CSDN博客 本文网址 https blog csdn net HiWangWenBing article details 121962962 目录 第1章 什么是
  • 图片像素点统计

    最近这几天闲来无事 想起来18年12月末帮别人做了一个编程题 题目就是如何统计一张图片中的气泡数目 以及每个气泡的面积 上面这张图就是案例 里面白色的都是不规则形态的气泡 当拿到这个题目时看一眼就大致有些思路 因为怎么说也是学了数据结构的人
  • PHp 密码验证

    MD5加密 string md5 string str bool raw output false 参数 str 原始字符串 raw output 如果可选的 raw output 被设置为 TRUE 那么 MD5 报文摘要将以16字节长度
  • 纯前端实现地址分词,模糊匹配

    关于地址分词的一点思路 一些主要代码的简要说明 本人的思路是 解析的结果存储在一个类似树状的结构中 就和DOM节点类似 用parent字段指向父级 用children字段指向子级 准备工作 CityModel 类 先构建出一个 CityMo
  • QT程序发布方法

    方法一 首先打开想要发布的程序所在的项目 然后将右下角的Debug换成Release Debug版本的程序非常大 因为有很多调试的信息 接着 按Ctrl R运行一遍 确保自己的程序没有问题 然后到程序的输出文件夹中 一般在项目目录的上一层目
  • 二分法查找之应用

    待定 1 二分法查找的前提 有序 1 二分法查找元素 例题1 287 寻找重复数 给定一个包含 n 1 个整数的数组 nums 其数字都在 1 到 n 之间 包括 1 和 n 可知至少存在一个重复的整数 假设只有一个重复的整数 找出这个重复
  • python opencv去图片水印

    背景 最近有一个需要为图片去水印的需求 于是各种折腾开始 背景了解图片标准 图片使用RGB编码 RGB色彩模式是工业界的一种颜色标准 是通过对红 R 绿 G 蓝 B 三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色 RGB即是代
  • java sort方法详解

    JAVA ARRAYS SORT 方法 static void sort ElementType a 默认为升序排序 static void sort ElementType a int FromIndex int ToIndex 在指定范
  • bash shell 计时运行耗时

    starttime date Y m d H M S ls usr lib endtime date Y m d H M S start seconds date date starttime s end seconds date date
  • WEEX框架(一)框架简介和快速上手体验

    框架简介 Weex 是能够完美兼顾性能与动态性 让移动开发者通过简捷的前端语法写出Native级别的性能体验的框架 并支持iOS 安卓 Web等多端部署 由阿里巴巴研发和维护 对于移动开发者来说 Weex主要解决了频繁发版和多端研发两大痛点
  • Google App Engine技术架构资料大盘点

    一 Google的核心技术 在切入Google App Engine之前 首先会对Google的核心技术和其整体架构进行分析 以帮助大家之后更好地理解Google App Engine的实现 本篇将主要介绍Google的十个核心技术 而且可
  • 跳动的爱心代码--李峋爱心代码(完整源码)

    本文章分为两部分 第一部分为实现效果展示 第二部分是实现跳动爱心源码 关注微信公众号 ClassmateJie 跳动的爱心效果展示 关注微信公众号 ClassmateJie 获取完整源码 回复爱心代码 实现步骤 1 建一个html文件 代码