JavaScript实现外溢动态爱心的效果的示例代码


Posted in Javascript onMarch 21, 2022

还在为节日送女朋友什么礼物而烦恼吗?最近用JavaScript制作了一个外溢动态爱心的效果,还可以在爱心上填写你想要的文字!快学习一下给自己女朋友也diy一个吧

效果演示

JavaScript实现外溢动态爱心的效果的示例代码

源码介绍

(
    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'));

以上就是JavaScript实现外溢动态爱心的效果的示例代码的详细内容,更多关于JavaScript动态爱心的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
js判断浏览器的比较全的代码
Feb 13 Javascript
学习JavaScript设计模式之享元模式
Jan 18 Javascript
JS实现的自定义水平滚动字体插件完整实例
Jun 17 Javascript
原生js实现键盘控制div移动且解决停顿问题
Dec 05 Javascript
两种简单的跨域方法(jsonp、php)
Jan 02 Javascript
JavaScript中为事件指定处理程序的五种方式分析
Jul 27 Javascript
vue左侧菜单,树形图递归实现代码
Aug 24 Javascript
详解mpvue scroll-view自动回弹bug解决方案
Oct 01 Javascript
angularjs通过过滤器返回超链接的方法
Oct 26 Javascript
使用Node.js实现一个多人游戏服务器引擎
Mar 13 Javascript
vue 微信扫码登录(自定义样式)
Jan 06 Javascript
Vue的全局过滤器和私有过滤器的实现
Apr 20 Javascript
实现AJAX异步调用和局部刷新的基本步骤
Mar 17 #Javascript
一篇文章弄清楚Ajax请求的五个步骤
VUE中的v-if与v-show区别介绍
Mar 13 #Vue.js
Vue.js中v-bind指令的用法介绍
Mar 13 #Vue.js
Vue2.0搭建脚手架
Vue.js中v-for指令的用法介绍
Mar 13 #Vue.js
面试中canvas绘制图片模糊图片问题处理
You might like
php中session_unset与session_destroy的区别分析
2011/06/16 PHP
完美解决:Apache启动问题―(OS 10022)提供了一个无效的参数
2013/06/08 PHP
php生成过去100年下拉列表的方法
2015/07/20 PHP
PHP CURL与java http使用方法详解
2018/01/26 PHP
javascript 打印内容方法小结
2009/11/04 Javascript
JavaScript中的Array对象使用说明
2011/01/17 Javascript
javascript使用中为什么10..toString()正常而10.toString()出错呢
2013/01/11 Javascript
JavaScript数据类型判定的总结笔记
2015/07/31 Javascript
Vue打包后出现一些map文件的解决方法
2018/02/13 Javascript
详解ES6通过WeakMap解决内存泄漏问题
2018/03/09 Javascript
JavaScript创建对象的常用方式总结
2018/08/10 Javascript
Python3中常用的处理时间和实现定时任务的方法的介绍
2015/04/07 Python
python中使用正则表达式的连接符示例代码
2017/10/10 Python
python使用jieba实现中文分词去停用词方法示例
2018/03/11 Python
对python PLT中的image和skimage处理图片方法详解
2019/01/10 Python
Python 窗体(tkinter)按钮 位置实例
2019/06/13 Python
python matplotlib库绘制条形图练习题
2019/08/10 Python
Python爬虫实例——scrapy框架爬取拉勾网招聘信息
2020/07/14 Python
python 实时调取摄像头的示例代码
2020/11/25 Python
用HTML5.0制作网页的教程
2010/05/30 HTML / CSS
html5 学习简单的拾色器
2010/09/03 HTML / CSS
英国床和浴室商场:Bed & Bath Emporium
2018/05/20 全球购物
李维斯牛仔裤英国官方网站:Levi’s英国
2019/10/10 全球购物
介绍一下Ruby的多线程处理
2013/02/01 面试题
英文版餐饮业求职信
2013/10/18 职场文书
国际贸易专业个人职业生涯规划
2014/02/15 职场文书
2014年三八妇女节活动方案
2014/02/28 职场文书
材料成型及控制工程专业求职信
2014/06/19 职场文书
课前一分钟演讲稿
2014/08/26 职场文书
群众路线对照检查材料
2014/09/22 职场文书
庆祝三八妇女节标语
2014/10/09 职场文书
家庭贫困证明
2015/06/16 职场文书
商务宴会祝酒词
2015/08/11 职场文书
2016父亲节感恩话语
2015/12/09 职场文书
SQL语法CONSTRAINT约束操作详情
2022/01/18 MySQL
springboot为异步任务规划自定义线程池的实现
2022/06/14 Java/Android