制作爱心代码:

<!doctype html>
<html><head><meta charset="utf-8"><title>canvas爱心</title><style>html,body {height: 100%;padding: 0;margin: 0;background: #000;}canvas {position: absolute;width: 100%;height: 100%;}</style>
</head><body><canvas id="pinkboard"></canvas><script>/** Settings*/var settings = {particles: {length: 500, // maximum amount of particlesduration: 2, // particle duration in secvelocity: 100, // particle velocity in pixels/seceffect: -0.75, // play with this for a nice effectsize: 30, // particle size in pixels},};/** RequestAnimationFrame polyfill by Erik M?ller*/(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 poolparticles = 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 queuefirstFree++;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 particlesif (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 particleswhile (particles[firstActive].age >= duration && firstActive != firstFree) {firstActive++;if (firstActive == particles.length) firstActive = 0;}};ParticlePool.prototype.draw = function (context, image) {// draw active particlesif (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/sectime;// get point on heart with -PI <= t <= PIfunction 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 canvasvar 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 pathfunction 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 pathcontext.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 fillcontext.fillStyle = '#ea80b0';context.fill();// create the imagevar image = new Image();image.src = canvas.toDataURL();return image;})();// render that thing!function render() {// next animation framerequestAnimationFrame(render);// update timevar newTime = new Date().getTime() / 1000,deltaTime = newTime - (time || newTime);time = newTime;// clear canvascontext.clearRect(0, 0, canvas.width, canvas.height);// create new particlesvar 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 particlesparticles.update(deltaTime);particles.draw(context, image);}// handle (re-)sizing of the canvasfunction onResize() {canvas.width = canvas.clientWidth;canvas.height = canvas.clientHeight;}window.onresize = onResize;// delay rendering bootstrapsetTimeout(function () {onResize();render();}, 10);})(document.getElementById('pinkboard'));</script>
</body></html>

![在这里插入图片描述](https://img-blog.csdnimg.cn/6b91cae1c1c54361a0c68a6159c89c0c.png

分享一个使用HTML+js制作爱心代码相关推荐

  1. php微信_分享一个完整的微信开发php代码

    这篇文章主要为大家分享一个完整的微信开发php代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了微信开发php代码,供大家参考,具体内容如下 //封装成一个微信接口类 cla ...

  2. 分享一个有趣的关机或重启代码

    //分享一个有趣的关机或重启代码  //"/"后面s关机r重启  #include<windows.h> using namespace std; int main() ...

  3. HTML5七夕情人节表白网页制作【原生JS制作爱心表白代码】HTML+CSS+JavaScript

    这是程序员表白系列中的100款网站表白之一,旨在让任何人都能使用并创建自己的表白网站给心爱的人看. 此波共有100个表白网站,可以任意修改和使用,很多人会希望向心爱的男孩女孩告白,生性腼腆的人即使那个 ...

  4. 分享一个好玩的JS小游戏

    前言 一个js的忍者小游戏 话不多说 如图所示 简单好玩 打发时间 代码 HTML <!DOCTYPE html> <html lang="en"> < ...

  5. 分享一个java写的中国象棋代码以及相关视频

    注意:相关资料链接地址:http://pan.baidu.com/share/link?shareid=493847&uk=3223420628 若要详细视频请到该链接直接下载: http:/ ...

  6. 一个不错的js制作的右键菜单

    网上转的,比我自己写的方便拓展,所以转过来~<html> <head> <script language='javascript'> /*******以下内容可以修 ...

  7. 分享一个强大的数据可视化低代码开发平台

    点击关注公众号:互联网架构师,后台回复 2T获取2TB学习资源! 上一篇:Alibaba开源内网高并发编程手册.pdf 整体介绍 框架:基于 Vue3 框架编写,使用 hooks 写法抽离部分逻辑,使 ...

  8. 分享一个我大学时通过写代码,十天赚了两万块钱的经历!

    上图是昨天看到的一篇文章,讲述了我对于用技术兼职的一些思考. 恰好之前兼职的项目方也看到了这篇文章,因为我在文章中提到了"爬虫", 他们担心社会大众看到该文的时候,由于对爬虫不了解 ...

  9. 分享一个标准体重计算器 C#调用代码

    身体质量指数 (Body Mass Index, 简称BMI), 通过身高和体重来计算您的身材是否标准 1.计算BMI值 获取标准体重参考 注意,该示例代码仅适用于 www.apishop.net网站 ...

最新文章

  1. vivox50pro鸿蒙系统,vivo X50 Pro最适合用来拍风景,看看网友的作品就知道了
  2. 大学python搜题app_2021年中国大学MOOC的APP用Python玩转数据答案搜题公众号
  3. Install OpenCV-Python in Ubuntu
  4. c++byte数组和文件的相互转换_终于!word、excel、ppt文件相互转换技巧来了!
  5. Python学习笔记:利用控制器跳转不同页面
  6. 【Elasticsearch】 es ElasticSearch集群故障案例分析: 警惕通配符查询 Wildcard
  7. [转载] Python数据分析与可视化学习笔记(一)数据分析与可视化概述
  8. 广州仙村中学2021高考成绩查询,仙村中学(增城区)
  9. Html5微信小游戏怎么运行,怎么用pixi.js开发微信小游戏
  10. 将ClearCase的客户端编码设置为UTF-8
  11. PPC手机上用Skype打电话的方法
  12. react-custom-scrollbars滚动组件
  13. 微型计算机硬件系统包括什么,微型计算机硬件系统由什么组成(6个基本组成部件)...
  14. 2017-2018-2 20179209《网络攻防》第八周作业
  15. 操作系统学习 - 逻辑地址转物理地址
  16. 以昂扬的斗志,书写青春的热血
  17. 关于计算机网络简笔画,玩电脑简笔画图片
  18. linear-gradient实现纯CSS文字淡入效果
  19. android 字符串转小数点,Android实现计算器(计算表达式/计算小数点以及括号)...
  20. [CortexM--CMSIS]详细的说明

热门文章

  1. 羡慕!各大互联网大厂年终奖一览表,看完我酸了~
  2. 小米面经(2021春招)
  3. 计算机配置好坏怎么看,查看电脑配置状况好坏的技巧
  4. 广西教师招聘需要计算机考试证,2020广西教师招聘报考需要有教师资格证吗
  5. opera 浏览器头 不是opera 打头
  6. 函数对象,嵌套,空间与作用域
  7. DHCP动态分配ip地址
  8. luna lunatic
  9. java csv 引号_csv文本编辑引号问题
  10. 计算机校本培训心得,校本培训心得体会总结