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

这篇具有很好参考价值的文章主要介绍了跳动的爱心代码--李峋爱心代码(完整源码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

跳动的爱心效果展示

爱心代码,前端,javascript,开发语言
关注微信公众号【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文件
    爱心代码,前端,javascript,开发语言

获取源码直接运行

爱心代码,前端,javascript,开发语言文章来源地址https://www.toymoban.com/news/detail-571110.html

关注微信公众号「 ClassmateJie
更多惊喜等待你的发掘

到了这里,关于跳动的爱心代码--李峋爱心代码(完整源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【李峋】一分钟就能学会的爱心代码

       

    2024年02月06日
    浏览(30)
  • 李峋同款爱心Python代码版来了

    🤵‍♂️ 个人主页@老虎也淘气 个人主页 ✍🏻作者简介:Python学习者 🐋 希望大家多多支持我们一起进步!😄 如果文章对你有帮助的话, 欢迎评论 💬点赞👍🏻 收藏 📂加关注 init (self, generate_frame=20): 这是类的初始化方法,用于创建一个对象时进行一些初始设置。 ge

    2024年02月06日
    浏览(27)
  • 用C++实现李峋同款爱心代码

    好的,李峋同款爱心代码是一个经典的用 ASCII 码绘制的爱心图案,用 C 语言实现的话,需要使用 printf 函数和转义字符。 代码如下: ``` #include stdio.h int main() { printf(\\\"%c%c%c%c%c%c%cn\\\", \\\'/\\\', \\\'\\\', \\\'/\\\', \\\'\\\', \\\'/\\\', \\\'\\\', \\\'/\\\'); printf(\\\"%c%c%c%c%c%c%cn\\\", \\\'\\\', \\\'/\\\', \\\'\\\', \\\'/\\\', \\\'\\\', \\\'/\\\', \\\'\\\'); printf(\\\"%c%c%c%c%c%c%c%c

    2024年02月07日
    浏览(32)
  • 【Java】跳动爱心代码,百分百还原跳动的爱心!

    最近那个电视剧的C语言实现跳动爱心不是火了吗,相信大家也都看过了,没错我也来蹭蹭热度,话不多说直接上代码。 下图展示: 这个代码看似简单但写起来其实并不轻松, 是一个比较考验对底层原理的理解的题目, 假设没有较好的代码功底或者理解不够透彻, 得到的结

    2024年02月11日
    浏览(32)
  • Go语言实现跳动的爱心(附带源码)

    在 Go 语言中,你可以使用 github.com/fogleman/gg 包来实现动态的爱心效果。以下是一个简单的例子: 在这个例子中,我们使用 github.com/fogleman/gg 包创建一个图形上下文,并在一个无限循环中绘制一个动态的爱心。每一帧都会保存为 PNG 图像文件,以便后续制作成动画。 要运行这

    2024年01月23日
    浏览(31)
  • Python制作爱心跳动代码,你也是天才程序员

    前端时间电视剧《点燃我,温暖你》正在热播中,里面的天才程序员李峋制作的爱心跳动代码是不是震撼了你的心,今天我们用Python来尝试一下制作爱心跳动代码吧! 怎么说呢,用这个表白也可以的,万一她也看这个剧呢,万一就成了呢 哈哈 冲啊,兄弟们 okok 话不多说,现

    2024年02月09日
    浏览(45)
  • 使用Python绘制跳动的爱心,让你的代码也充满爱意!

    今天我要分享一个浪漫小技巧,使用Python中的HTML制作一个立体、动态的小爱心。通过成千上百个小爱心的组合,形成一个大爱心,从内到外呈现出立体的效果,给人带来强烈的视觉冲击。这个小技巧非常浪漫,让人感受到爱的力量。 一.粉色爱心 运行结果:  二.蓝色动态

    2024年02月08日
    浏览(34)
  • 程序员教你用代码制作3d爱心跳动特效,正好拿去送给女神给她个惊喜

      🐱‍🏍 【晚安独角兽】:hello你好我是独角兽,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行!  🎉   博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端、后台、数据库、项目案例等相关知识点总结,感谢你

    2023年04月22日
    浏览(41)
  • 《点燃我,温暖你》理工男神李峋同款C语言版本爱心

    近期很火的《 点燃我,温暖你 》很火,里面的 爱心代码 也很惊艳,但是程序员看了觉得尬的扣脚, 网上也有他其他的语言爱心源码,但都不是C语言的,用语言描述一下,就是好多爱心,然后从内到外,从小到大的显示。今天就给大家分享: 爱心代码, 边看边用! 2.实现 C语

    2024年02月21日
    浏览(30)
  • C/C++爱心代码(完整代码)

    C/C++绘制一个爱心的完整代码。 Dev-C++ :https://want595.blog.csdn.net/article/details/134649225

    2024年03月11日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包