用html画一次函数爱心代码

时间: 2023-11-10 14:03:51 浏览: 52
``` <!DOCTYPE html> <html> <head> <title></title> </head> <style> * { padding: 0; margin: 0; } html, body { height: 100%; padding: 0; margin: 0; background: #000; } canvas { position: absolute; width: 100%; height: 100%; } .aa { position: fixed; left: 50%; bottom: 10px; color: #ccc; } </style> <body> <canvas id="pinkboard"></canvas> <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 } }; /* * 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 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> </body> </html> ``` ![示例图片](https://devbit-static.oss-cn-beijing.aliyuncs.com/devbit-static/img/heart.png)

相关推荐

最新推荐

recommend-type

在textarea文本域中显示HTML代码的方法

该函数创建了一个`div`元素,并设置了其`innerHTML`为包含HTML代码的字符串。然后,这个`div`元素被添加到`textarea`之后,而不是直接插入到`textarea`的文本中。这种方法虽然不能直接在`textarea`中显示HTML代码,...
recommend-type

[小白系列]sigmoid和tanh激活函数的绘制(分开画,合起来画,总有一款适合你),逐行代码详解

第一种:分开来画 import matplotlib.pyplot as plt import numpy as np plt.rcParams['axes.unicode_minus'] = False def sigmoid(x): return 1.0/(1.0+np.exp(-x)) def tanh(x): return (np.exp(x) - np.exp(-x)...
recommend-type

Oracle自定义脱敏函数的代码详解

Oracle自定义脱敏函数是数据库管理中用于保护敏感数据的一种常见方法,尤其是在处理用户个人信息时。本文将深入解析Oracle自定义脱敏函数的实现细节,以确保在数据共享、备份或分析时不泄露关键信息。 首先,我们来...
recommend-type

Opencv中imwrite函数源代码

在源代码中还定义了一个imwrite函数,它是imwrite_函数的封装,用于简化调用过程。 ``` bool imwrite( const String& filename, InputArray _img, const std::vector&lt;int&gt;& params ){ Mat img = _img.getMat(); ...
recommend-type

C语言 用指针作为函数返回值详解

例如,以下代码中,`func()`返回了一个指向局部变量`n`的指针: ```c #include int *func() { int n = 100; return &n; } int main() { int *p = func(), n; n = *p; printf("value = %d\n", n); return 0...
recommend-type

电力电子系统建模与控制入门

"该资源是关于电力电子系统建模及控制的课程介绍,包含了课程的基本信息、教材与参考书目,以及课程的主要内容和学习要求。" 电力电子系统建模及控制是电力工程领域的一个重要分支,涉及到多学科的交叉应用,如功率变换技术、电工电子技术和自动控制理论。这门课程主要讲解电力电子系统的动态模型建立方法和控制系统设计,旨在培养学生的建模和控制能力。 课程安排在每周二的第1、2节课,上课地点位于东12教401室。教材采用了徐德鸿编著的《电力电子系统建模及控制》,同时推荐了几本参考书,包括朱桂萍的《电力电子电路的计算机仿真》、Jai P. Agrawal的《Powerelectronicsystems theory and design》以及Robert W. Erickson的《Fundamentals of Power Electronics》。 课程内容涵盖了从绪论到具体电力电子变换器的建模与控制,如DC/DC变换器的动态建模、电流断续模式下的建模、电流峰值控制,以及反馈控制设计。还包括三相功率变换器的动态模型、空间矢量调制技术、逆变器的建模与控制,以及DC/DC和逆变器并联系统的动态模型和均流控制。学习这门课程的学生被要求事先预习,并尝试对书本内容进行仿真模拟,以加深理解。 电力电子技术在20世纪的众多科技成果中扮演了关键角色,广泛应用于各个领域,如电气化、汽车、通信、国防等。课程通过列举各种电力电子装置的应用实例,如直流开关电源、逆变电源、静止无功补偿装置等,强调了其在有功电源、无功电源和传动装置中的重要地位,进一步凸显了电力电子系统建模与控制技术的实用性。 学习这门课程,学生将深入理解电力电子系统的内部工作机制,掌握动态模型建立的方法,以及如何设计有效的控制系统,为实际工程应用打下坚实基础。通过仿真练习,学生可以增强解决实际问题的能力,从而在未来的工程实践中更好地应用电力电子技术。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

图像写入的陷阱:imwrite函数的潜在风险和规避策略,规避图像写入风险,保障数据安全

![图像写入的陷阱:imwrite函数的潜在风险和规避策略,规避图像写入风险,保障数据安全](https://static-aliyun-doc.oss-accelerate.aliyuncs.com/assets/img/zh-CN/2275688951/p86862.png) # 1. 图像写入的基本原理与陷阱 图像写入是计算机视觉和图像处理中一项基本操作,它将图像数据从内存保存到文件中。图像写入过程涉及将图像数据转换为特定文件格式,并将其写入磁盘。 在图像写入过程中,存在一些潜在陷阱,可能会导致写入失败或图像质量下降。这些陷阱包括: - **数据类型不匹配:**图像数据可能与目标文
recommend-type

protobuf-5.27.2 交叉编译

protobuf(Protocol Buffers)是一个由Google开发的轻量级、高效的序列化数据格式,用于在各种语言之间传输结构化的数据。版本5.27.2是一个较新的稳定版本,支持跨平台编译,使得可以在不同的架构和操作系统上构建和使用protobuf库。 交叉编译是指在一个平台上(通常为开发机)编译生成目标平台的可执行文件或库。对于protobuf的交叉编译,通常需要按照以下步骤操作: 1. 安装必要的工具:在源码目录下,你需要安装适合你的目标平台的C++编译器和相关工具链。 2. 配置Makefile或CMakeLists.txt:在protobuf的源码目录中,通常有一个CMa
recommend-type

SQL数据库基础入门:发展历程与关键概念

本文档深入介绍了SQL数据库的基础知识,首先从数据库的定义出发,强调其作为数据管理工具的重要性,减轻了开发人员的数据处理负担。数据库的核心概念是"万物皆关系",即使在面向对象编程中也有明显区分。文档讲述了数据库的发展历程,从早期的层次化和网状数据库到关系型数据库的兴起,如Oracle的里程碑式论文和拉里·埃里森推动的关系数据库商业化。Oracle的成功带动了全球范围内的数据库竞争,最终催生了SQL这一通用的数据库操作语言,统一了标准,使得关系型数据库成为主流。 接着,文档详细解释了数据库系统的构成,包括数据库本身(存储相关数据的集合)、数据库管理系统(DBMS,负责数据管理和操作的软件),以及数据库管理员(DBA,负责维护和管理整个系统)和用户应用程序(如Microsoft的SSMS)。这些组成部分协同工作,确保数据的有效管理和高效处理。 数据库系统的基本要求包括数据的独立性,即数据和程序的解耦,有助于快速开发和降低成本;减少冗余数据,提高数据共享性,以提高效率;以及系统的稳定性和安全性。学习SQL时,要注意不同数据库软件可能存在的差异,但核心语言SQL的学习是通用的,后续再根据具体产品学习特异性。 本文档提供了一个全面的框架,涵盖了SQL数据库从基础概念、发展历程、系统架构到基本要求的方方面面,对于初学者和数据库管理员来说是一份宝贵的参考资料。