最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

HTML+JS實現(xiàn)愛心動畫效果的源碼分享

 更新時間:2022年11月09日 10:34:23   作者:顧城沐心  
最近在熱播影劇《點燃我,溫暖你》中有個片段是男主人公李峋通過代碼實現(xiàn)了一個愛心動畫的效果。在本文中,將利用HTML+CSS+JS實現(xiàn)同款效果,需要的可以嘗試一下

一、效果展示

二、源碼分享

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <script src="js/jquery.min.js"></script>
  </head>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    html,
    body {
      height: 100%;
      padding: 0;
      margin: 0;
      background: #000;
    }
 
    .aa {
      position: fixed;
      left: 50%;
      bottom: 10px;
      color: #ccc;
    }
 
    .container {
      width: 100%;
      height: 100%;
    }
    canvas {
      z-index: 99;
      position: absolute;
      width: 100%;
      height: 100%;
    }
  </style>
  <body>
    <!-- 櫻花 -->
    <div id="jsi-cherry-container" class="container">
      <audio autoplay="autopaly">
        <source src="renxi.mp3" type="audio/mp3" />
      </audio>
      <img class="img" src="./123.png" alt="" />
      <!-- 愛心 -->
      <canvas id="pinkboard" class="container"> </canvas>
    </div>
 
  </body>
</html>
<script>
    /*
     * Settings
     */
    var settings = {
      particles: {
        length: 500, // maximum amount of particles
        duration: 2, // particle duration in sec
        velocity: 100, // particle velocity in pixels/sec
        effect: -0.75, // play with this for a nice effect
        size: 30, // particle size in pixels
      },
    };
 
    (function () {
      var b = 0;
      var c = ["ms", "moz", "webkit", "o"];
      for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) {
        window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"];
        window.cancelAnimationFrame =
          window[c[a] + "CancelAnimationFrame"] ||
          window[c[a] + "CancelRequestAnimationFrame"];
      }
      if (!window.requestAnimationFrame) {
        window.requestAnimationFrame = function (h, e) {
          var d = new Date().getTime();
          var f = Math.max(0, 16 - (d - b));
          var g = window.setTimeout(function () {
            h(d + f);
          }, f);
          b = d + f;
          return g;
        };
      }
      if (!window.cancelAnimationFrame) {
        window.cancelAnimationFrame = function (d) {
          clearTimeout(d);
        };
      }
    })();
 
    /*
     * Point class
     */
    var Point = (function () {
      function Point(x, y) {
        this.x = typeof x !== "undefined" ? x : 0;
        this.y = typeof y !== "undefined" ? y : 0;
      }
      Point.prototype.clone = function () {
        return new Point(this.x, this.y);
      };
      Point.prototype.length = function (length) {
        if (typeof length == "undefined")
          return Math.sqrt(this.x * this.x + this.y * this.y);
        this.normalize();
        this.x *= length;
        this.y *= length;
        return this;
      };
      Point.prototype.normalize = function () {
        var length = this.length();
        this.x /= length;
        this.y /= length;
        return this;
      };
      return Point;
    })();
 
    /*
     * Particle class
     */
    var Particle = (function () {
      function Particle() {
        this.position = new Point();
        this.velocity = new Point();
        this.acceleration = new Point();
        this.age = 0;
      }
      Particle.prototype.initialize = function (x, y, dx, dy) {
        this.position.x = x;
        this.position.y = y;
        this.velocity.x = dx;
        this.velocity.y = dy;
        this.acceleration.x = dx * settings.particles.effect;
        this.acceleration.y = dy * settings.particles.effect;
        this.age = 0;
      };
      Particle.prototype.update = function (deltaTime) {
        this.position.x += this.velocity.x * deltaTime;
        this.position.y += this.velocity.y * deltaTime;
        this.velocity.x += this.acceleration.x * deltaTime;
        this.velocity.y += this.acceleration.y * deltaTime;
        this.age += deltaTime;
      };
      Particle.prototype.draw = function (context, image) {
        function ease(t) {
          return --t * t * t + 1;
        }
        var size = image.width * ease(this.age / settings.particles.duration);
        context.globalAlpha = 1 - this.age / settings.particles.duration;
        context.drawImage(
          image,
          this.position.x - size / 2,
          this.position.y - size / 2,
          size,
          size
        );
      };
      return Particle;
    })();
 
    /*
     * ParticlePool class
     */
    var ParticlePool = (function () {
      var particles,
        firstActive = 0,
        firstFree = 0,
        duration = settings.particles.duration;
 
      function ParticlePool(length) {
        // create and populate particle pool
        particles = new Array(length);
        for (var i = 0; i < particles.length; i++)
          particles[i] = new Particle();
      }
      ParticlePool.prototype.add = function (x, y, dx, dy) {
        particles[firstFree].initialize(x, y, dx, dy);
 
        // handle circular queue
        firstFree++;
        if (firstFree == particles.length) firstFree = 0;
        if (firstActive == firstFree) firstActive++;
        if (firstActive == particles.length) firstActive = 0;
      };
      ParticlePool.prototype.update = function (deltaTime) {
        var i;
 
        // update active particles
        if (firstActive < firstFree) {
          for (i = firstActive; i < firstFree; i++)
            particles[i].update(deltaTime);
        }
        if (firstFree < firstActive) {
          for (i = firstActive; i < particles.length; i++)
            particles[i].update(deltaTime);
          for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
        }
 
        // remove inactive particles
        while (
          particles[firstActive].age >= duration &&
          firstActive != firstFree
        ) {
          firstActive++;
          if (firstActive == particles.length) firstActive = 0;
        }
      };
      ParticlePool.prototype.draw = function (context, image) {
        // draw active particles
        if (firstActive < firstFree) {
          for (i = firstActive; i < firstFree; i++)
            particles[i].draw(context, image);
        }
        if (firstFree < firstActive) {
          for (i = firstActive; i < particles.length; i++)
            particles[i].draw(context, image);
          for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
        }
      };
      return ParticlePool;
    })();
 
    /*
     * Putting it all together
     */
    (function (canvas) {
      var context = canvas.getContext("2d"),
        particles = new ParticlePool(settings.particles.length),
        particleRate =
          settings.particles.length / settings.particles.duration, // particles/sec
        time;
 
      // get point on heart with -PI <= t <= PI
      function pointOnHeart(t) {
        return new Point(
          160 * Math.pow(Math.sin(t), 3),
          130 * Math.cos(t) -
            50 * Math.cos(2 * t) -
            20 * Math.cos(3 * t) -
            10 * Math.cos(4 * t) +
            25
        );
      }
 
      // creating the particle image using a dummy canvas
      var image = (function () {
        var canvas = document.createElement("canvas"),
          context = canvas.getContext("2d");
        canvas.width = settings.particles.size;
        canvas.height = settings.particles.size;
        // helper function to create the path
        function to(t) {
          var point = pointOnHeart(t);
          point.x =
            settings.particles.size / 2 +
            (point.x * settings.particles.size) / 350;
          point.y =
            settings.particles.size / 2 -
            (point.y * settings.particles.size) / 350;
          return point;
        }
        // create the path
        context.beginPath();
        var t = -Math.PI;
        var point = to(t);
        context.moveTo(point.x, point.y);
        while (t < Math.PI) {
          t += 0.01; // baby steps!
          point = to(t);
          context.lineTo(point.x, point.y);
        }
        context.closePath();
        // create the fill
        context.fillStyle = "#ea80b0";
        context.fill();
        // create the image
        var image = new Image();
        image.src = canvas.toDataURL();
        return image;
      })();
 
      // render that thing!
      function render() {
        // next animation frame
        requestAnimationFrame(render);
 
        // update time
        var newTime = new Date().getTime() / 1000,
          deltaTime = newTime - (time || newTime);
        time = newTime;
 
        // clear canvas
        context.clearRect(0, 0, canvas.width, canvas.height);
 
        // create new particles
        var amount = particleRate * deltaTime;
        for (var i = 0; i < amount; i++) {
          var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
          var dir = pos.clone().length(settings.particles.velocity);
          particles.add(
            canvas.width / 2 + pos.x,
            canvas.height / 2 - pos.y,
            dir.x,
            -dir.y
          );
        }
 
        // update and draw particles
        particles.update(deltaTime);
        particles.draw(context, image);
      }
 
      // handle (re-)sizing of the canvas
      function onResize() {
        canvas.width = canvas.clientWidth;
        canvas.height = canvas.clientHeight;
      }
      window.onresize = onResize;
 
      // delay rendering bootstrap
      setTimeout(function () {
        onResize();
        render();
      }, 10);
    })(document.getElementById("pinkboard"));
  </script>
 
  <script>
    var RENDERER = {
      INIT_CHERRY_BLOSSOM_COUNT: 30,
      MAX_ADDING_INTERVAL: 10,
 
      init: function () {
        this.setParameters();
        this.reconstructMethods();
        this.createCherries();
        this.render();
        if (
          navigator.userAgent.match(
            /(phone|pod|iPhone|iPod|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
          )
        ) {
          // var box = document.querySelectorAll(".box")[0];
          // console.log(box, "移動端");
          // box.style.marginTop = "65%";
        }
      },
      setParameters: function () {
        this.$container = $("#jsi-cherry-container");
        this.width = this.$container.width();
        this.height = this.$container.height();
        this.context = $("<canvas />")
          .attr({ width: this.width, height: this.height })
          .appendTo(this.$container)
          .get(0)
        var rate = this.FOCUS_POSITION / (this.z + this.FOCUS_POSITION),
          x = this.renderer.width / 2 + this.x * rate,
          y = this.renderer.height / 2 - this.y * rate;
        return { rate: rate, x: x, y: y };
      },
      re
          }
        } else {
          this.phi += Math.PI / (axis.y == this.thresholdY ? 200 : 500);
          this.phi %= Math.PI;
        }
        if (this.y <= -this.renderer.height * this.SURFACE_RATE) {
          this.x += 2;
          this.y = -this.renderer.height * this.SURFACE_RATE;
        } else {
          this.x += this.vx;
          this.y += this.vy;
        }
        return (
          this.z > -this.FOCUS_POSITION &&
          this.z < this.FAR_LIMIT &&
          this.x < this.renderer.width * 1.5
        );
      },
    };
    $(function () {
      RENDERER.init();
    });
  </script>

到此這篇關(guān)于HTML+JS實現(xiàn)愛心動畫效果的源碼分享的文章就介紹到這了,更多相關(guān)HTML JS愛心動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談JS繼承_借用構(gòu)造函數(shù) & 組合式繼承

    淺談JS繼承_借用構(gòu)造函數(shù) & 組合式繼承

    下面小編就為大家?guī)硪黄獪\談JS繼承_借用構(gòu)造函數(shù) & 組合式繼承。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • JavaScript動態(tài)修改網(wǎng)頁元素內(nèi)容的方法

    JavaScript動態(tài)修改網(wǎng)頁元素內(nèi)容的方法

    這篇文章主要介紹了JavaScript動態(tài)修改網(wǎng)頁元素內(nèi)容的方法,實例分析了javascript操作html元素的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • JavaScript監(jiān)聽鍵盤事件代碼實現(xiàn)

    JavaScript監(jiān)聽鍵盤事件代碼實現(xiàn)

    這篇文章主要介紹了JavaScript監(jiān)聽鍵盤事件代碼實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • 微信小程序自定義狀態(tài)欄

    微信小程序自定義狀態(tài)欄

    這篇文章主要為大家詳細介紹了微信小程序自定義狀態(tài)欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • javascript封裝簡單實現(xiàn)方法

    javascript封裝簡單實現(xiàn)方法

    這篇文章主要介紹了javascript封裝簡單實現(xiàn)方法,涉及javascript中方法與屬性的相關(guān)設(shè)置與使用技巧,非常簡單實用,需要的朋友可以參考下
    2015-08-08
  • JavaScript學習小結(jié)(7)之JS RegExp

    JavaScript學習小結(jié)(7)之JS RegExp

    在js中,正則表達式是由一個RegExp對象表示的,RegExp 是正則表達式的縮寫。RegExp簡單的模式可以是一個單獨的字符。更復(fù)雜的模式包括了更多的字符,并可用于解析、格式檢查、替換等等。可以使用一個RegExp()構(gòu)造函數(shù)來創(chuàng)建RegExp對象,也可以使用直接量語法
    2015-11-11
  • JavaScript中變量的相互引用

    JavaScript中變量的相互引用

    變量相互引用的原因是:由于某些類型的變量是按地址存儲的而導致的兩個或多的參數(shù)的值互相影響.
    2010-05-05
  • 使用JavaScript操作Visual Viewport的方法示例

    使用JavaScript操作Visual Viewport的方法示例

    在現(xiàn)代前端開發(fā)中,視口(viewport)是一個非常重要的概念,它決定了用戶在瀏覽網(wǎng)頁時所看到的內(nèi)容,JavaScript 提供了一個強大的接口 —— Visual Viewport API,讓開發(fā)者可以更靈活地控制和獲取視口的信息,本文將詳細介紹如何使用 Visual Viewport API
    2024-09-09
  • 利用JS判斷鼠標移入元素的方向

    利用JS判斷鼠標移入元素的方向

    本文對JS判斷鼠標移入元素的方向的實現(xiàn)方法進行介紹,并分享了完整的示例代碼,有需要的朋友可以看下
    2016-12-12
  • javascript實現(xiàn)詳細時間提醒信息效果的方法

    javascript實現(xiàn)詳細時間提醒信息效果的方法

    這篇文章主要介紹了javascript實現(xiàn)詳細時間提醒信息效果的方法,涉及javascript操作時間的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03

最新評論

嘉峪关市| 开江县| 长岭县| 合川市| 滦南县| 马龙县| 松江区| 喀什市| 尼勒克县| 利辛县| 新化县| 青田县| 始兴县| 晋州市| 信丰县| 台州市| 龙山县| 海宁市| 德清县| 肥乡县| 城固县| 会同县| 广州市| 弥勒县| 鱼台县| 平利县| 浪卡子县| 乌海市| 得荣县| 黄骅市| 炎陵县| 宜黄县| 镶黄旗| 惠安县| 江陵县| 永仁县| 永和县| 满城县| 余干县| 桐城市| 遂平县|