哈喽,大家好,我是木易巷!

爱情,是每个人都在追求的东西。身为一个IT行业人士,我也会追求爱情,我也会像心爱的人表达感情,只是我所表达的方式与他人有所不同。我的主要战场在计算机上,所以我就想到了用计算机来表达感情,在网页上做爱心,还是会动的爱心给她~

这次给大家带来炫酷的跳动爱心,有三种效果,大家自行领取~

目录

第一种效果图

代码部分

第二种效果图

代码部分

第三种效果图

代码部分


上一篇的三个爱心:https://blog.csdn.net/qq_44794321/article/details/127719490https://blog.csdn.net/qq_44794321/article/details/127719490


第一种效果图:

代码部分

代码如下仅供参考,可以直接拿去复制粘贴!

import random
from math import sin, cos, pi, log
from tkinter import *CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
CANVAS_CENTER_X = CANVAS_WIDTH / 2
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2
IMAGE_ENLARGE = 11
HEART_COLOR = "#FF99CC"
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):x = 16 * (sin(t) ** 3)y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))# 放大x *= shrink_ratioy *= shrink_ratio# 移到画布中央x += CANVAS_CENTER_Xy += CANVAS_CENTER_Yreturn int(x), int(y)
def scatter_inside(x, y, beta=0.15):ratio_x = - beta * log(random.random())ratio_y = - beta * log(random.random())dx = ratio_x * (x - CANVAS_CENTER_X)dy = ratio_y * (y - CANVAS_CENTER_Y)return x - dx, y - dy
def shrink(x, y, ratio):force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)dx = ratio * force * (x - CANVAS_CENTER_X)dy = ratio * force * (y - CANVAS_CENTER_Y)return x - dx, y - dy
def curve(p):return 2 * (2 * sin(4 * p)) / (2 * pi)
class Heart:def __init__(self, generate_frame=20):self._points = set()  # 原始爱心坐标集合self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合self._center_diffusion_points = set()  # 中心扩散效果点坐标集合self.all_points = {}  # 每帧动态点坐标self.build(2000)self.random_halo = 1000self.generate_frame = generate_framefor frame in range(generate_frame):self.calc(frame)def build(self, number):for _ in range(number):t = random.uniform(0, 2 * pi)x, y = heart_function(t)self._points.add((x, y))# 爱心内扩散for _x, _y in list(self._points):for _ in range(3):x, y = scatter_inside(_x, _y, 0.05)self._edge_diffusion_points.add((x, y))# 爱心内再次扩散point_list = list(self._points)for _ in range(4000):x, y = random.choice(point_list)x, y = scatter_inside(x, y, 0.17)self._center_diffusion_points.add((x, y))@staticmethoddef calc_position(x, y, ratio):force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)return x - dx, y - dydef calc(self, generate_frame):ratio = 10 * curve(generate_frame / 10 * pi)halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))all_points = []# 光环heart_halo_point = set()for _ in range(halo_number):t = random.uniform(0, 2 * pi)x, y = heart_function(t, shrink_ratio=11.6)x, y = shrink(x, y, halo_radius)if (x, y) not in heart_halo_point:heart_halo_point.add((x, y))x += random.randint(-14, 14)y += random.randint(-14, 14)size = random.choice((1, 2, 2))all_points.append((x, y, size))# 轮廓for x, y in self._points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size))# 内容for x, y in self._edge_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size))for x, y in self._center_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size))self.all_points[generate_frame] = all_pointsdef render(self, render_canvas, render_frame):for x, y, size in self.all_points[render_frame % self.generate_frame]:render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):render_canvas.delete('all')render_heart.render(render_canvas, render_frame)main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
if __name__ == '__main__':root = Tk()canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)canvas.pack()heart = Heart()draw(root, canvas, heart)root.title("公众号:木易巷")root.mainloop()

第二种效果图:

代码部分

代码如下仅供参考,可以直接拿去复制粘贴!

<!DOCTYPE HTML>
<!-- saved from url=(0051)https://httishere.gitee.io/notion/v4/love-name.html -->
<!DOCTYPE html PUBLIC "" ""><HTML><HEAD><META content="IE=11.0000"
http-equiv="X-UA-Compatible"><META charset="utf-8">     <TITLE>木易巷</TITLE>
<STYLE type="text/css">body {margin: 0;overflow: hidden;background: #000;}canvas {position: absolute;width: 100%;height: 100%;}#pinkboard {animation: anim 1.5s ease-in-out infinite;-webkit-animation: anim 1.5s ease-in-out infinite;-o-animation: anim 1.5s ease-in-out infinite;-moz-animation: anim 1.5s ease-in-out infinite;}@keyframes anim {0% {transform: scale(0.8);}25% {transform: scale(0.7);}50% {transform: scale(1);}75% {transform: scale(0.7);}100% {transform: scale(0.8);}}@-webkit-keyframes anim {0% {-webkit-transform: scale(0.8);}25% {-webkit-transform: scale(0.7);}50% {-webkit-transform: scale(1);}75% {-webkit-transform: scale(0.7);}100% {-webkit-transform: scale(0.8);}}@-o-keyframes anim {0% {-o-transform: scale(0.8);}25% {-o-transform: scale(0.7);}50% {-o-transform: scale(1);}75% {-o-transform: scale(0.7);}100% {-o-transform: scale(0.8);}}@-moz-keyframes anim {0% {-moz-transform: scale(0.8);}25% {-moz-transform: scale(0.7);}50% {-moz-transform: scale(1);}75% {-moz-transform: scale(0.7);}100% {-moz-transform: scale(0.8);}}#name {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);margin-top: -20px;font-size: 46px;color: #ea80b0;}</STYLE><META name="GENERATOR" content="MSHTML 11.00.10570.1001"></HEAD>
<BODY><CANVAS id="pinkboard"></CANVAS>     <!-- <div id="name">木易巷 ❤️</div> -->  <CANVAS id="canvas"></CANVAS>
<SCRIPT type="text/javascript">const colors = ["#eec996","#8fb7d3","#b7d4c6","#c3bedd","#f1d5e4","#cae1d3","#f3c89d","#d0b0c3","#819d53","#c99294","#cec884","#ff8e70","#e0a111","#fffdf6","#cbd7ac","#e8c6c0","#dc9898","#ecc8ba",]; //用来设置的颜色var canvas = document.getElementById("canvas");var ctx = canvas.getContext("2d");let count = 1;var ww = window.innerWidth;var wh = window.innerHeight;var hearts = [];function init() {requestAnimationFrame(render);canvas.width = ww;canvas.height = wh;for (var i = 0; i < 100; i++) {hearts.push(new Heart());}}function Heart() {this.x = Math.random() * ww;this.y = Math.random() * wh;this.opacity = Math.random() * 0.5 + 0.5;this.vel = {x: (Math.random() - 0.5) * 4,y: (Math.random() - 0.5) * 4,};this.targetScale = Math.random() * 0.15 + 0.02;this.scale = this.targetScale * Math.random();}Heart.prototype.update = function (i) {this.x += this.vel.x;this.y += this.vel.y;this.scale += (this.targetScale - this.scale) * 0.01;if (this.x - this.width > ww || this.x + this.width < 0) {this.scale = 0;this.x = Math.random() * ww;}if (this.y - this.height > wh || this.y + this.height < 0) {this.scale = 0;this.y = Math.random() * wh;}this.width = 473.8;this.height = 408.6;};Heart.prototype.draw = function (i) {ctx.globalAlpha = this.opacity;ctx.font = `${180 * this.scale}px "微软雅黑"`;// ctx.font="20px";ctx.fillStyle = colors[i % 18];ctx.fillText("木易巷",this.x - this.width * 0.5,this.y - this.height * 0.5,this.width,this.height);// ctx.drawImage(//   heartImage,//   this.x - this.width * 0.5,//   this.y - this.height * 0.5,//   this.width,//   this.height// );};function render() {ctx.clearRect(0, 0, ww, wh);// ctx.globalAlpha = 1;// ctx.fillStyle = "rgba(255,255,255,0.3)";// ctx.fillRect(0, 0, ww, wh);for (var i = 0; i < 100; i++) {hearts[i].update(i);hearts[i].draw(i);}requestAnimationFrame(render);}init();// var heartImage = new Image();// heartImage.onload = init();// heartImage.src =//   "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0NzMuOHB4IiBoZWlnaHQ9IjQwOC42cHgiIHZpZXdCb3g9IjAgMCA0NzMuOCA0MDguNiI+PHBhdGggZmlsbD0iI2QzMjkzMiIgZD0iTTQwNC42LDE2LjZDMzg1LjQsNi4xLDM2My41LDAsMzQwLDBjLTQxLjUsMC03OC41LDE4LjktMTAzLDQ4LjVDMjEyLjMsMTguOSwxNzUuMywwLDEzMy44LDAgYy0yMy4zLDAtNDUuMyw2LjEtNjQuNSwxNi42QzI3LjksMzkuNSwwLDgzLjQsMCwxMzMuOWMwLDE0LjQsMi40LDI4LjMsNi42LDQxLjJDMjkuNiwyNzguNCwyMzcsNDA4LjYsMjM3LDQwOC42IHMyMDcuMi0xMzAuMiwyMzAuMi0yMzMuNWM0LjMtMTIuOSw2LjYtMjYuOCw2LjYtNDEuMkM0NzMuOCw4My40LDQ0NS45LDM5LjYsNDA0LjYsMTYuNnoiLz48L3N2Zz4=";window.addEventListener("resize", function () {ww = window.innerWidth;wh = window.innerHeight;});</SCRIPT><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 = "#B22222";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>

第三种效果图:

代码部分

代码如下仅供参考,可以直接拿去复制粘贴!

<!DOCTYPE HTML>
<!DOCTYPE html PUBLIC "" "">
<HTML>
<HEAD><META content="IE=11.0000"http-equiv="X-UA-Compatible"><META charset="utf-8"><TITLE>木易巷</TITLE><STYLE type="text/css">body {margin: 0;overflow: hidden;background: #000;}canvas {position: absolute;width: 100%;height: 100%;}#pinkboard {animation: anim 1.5s ease-in-out infinite;-webkit-animation: anim 1.5s ease-in-out infinite;-o-animation: anim 1.5s ease-in-out infinite;-moz-animation: anim 1.5s ease-in-out infinite;}@keyframes anim {0% {transform: scale(0.8);}25% {transform: scale(0.7);}50% {transform: scale(1);}75% {transform: scale(0.7);}100% {transform: scale(0.8);}}@-webkit-keyframes anim {0% {-webkit-transform: scale(0.8);}25% {-webkit-transform: scale(0.7);}50% {-webkit-transform: scale(1);}75% {-webkit-transform: scale(0.7);}100% {-webkit-transform: scale(0.8);}}@-o-keyframes anim {0% {-o-transform: scale(0.8);}25% {-o-transform: scale(0.7);}50% {-o-transform: scale(1);}75% {-o-transform: scale(0.7);}100% {-o-transform: scale(0.8);}}@-moz-keyframes anim {0% {-moz-transform: scale(0.8);}25% {-moz-transform: scale(0.7);}50% {-moz-transform: scale(1);}75% {-moz-transform: scale(0.7);}100% {-moz-transform: scale(0.8);}}#name {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);margin-top: -20px;font-size: 46px;color: #ea80b0;}</STYLE><META name="GENERATOR" content="MSHTML 11.00.10570.1001">
</HEAD>
<BODY><!--这个是红心-->
<CANVAS id="pinkboard"></CANVAS><!--这个是字体-->
<!--<CANVAS id="canvas"></CANVAS>--><SCRIPT>const colors = ["#eec996","#8fb7d3","#b7d4c6","#c3bedd","#f1d5e4","#cae1d3","#f3c89d","#d0b0c3","#819d53","#c99294","#cec884","#ff8e70","#e0a111","#fffdf6","#cbd7ac","#e8c6c0","#dc9898","#ecc8ba",]; //用来设置的颜色var canvas = document.getElementById("canvas");var ctx = canvas.getContext("2d");let count = 1;var ww = window.innerWidth;var wh = window.innerHeight;var hearts = [];function init() {requestAnimationFrame(render);canvas.width = ww;canvas.height = wh;for (var i = 0; i < 100; i++) {hearts.push(new Heart());}}function Heart() {this.x = Math.random() * ww;this.y = Math.random() * wh;this.opacity = Math.random() * 0.5 + 0.5;this.vel = {x: (Math.random() - 0.5) * 4,y: (Math.random() - 0.5) * 4,};this.targetScale = Math.random() * 0.15 + 0.02;this.scale = this.targetScale * Math.random();}Heart.prototype.update = function (i) {this.x += this.vel.x;this.y += this.vel.y;this.scale += (this.targetScale - this.scale) * 0.01;if (this.x - this.width > ww || this.x + this.width < 0) {this.scale = 0;this.x = Math.random() * ww;}if (this.y - this.height > wh || this.y + this.height < 0) {this.scale = 0;this.y = Math.random() * wh;}this.width = 473.8;this.height = 408.6;};Heart.prototype.draw = function (i) {ctx.globalAlpha = this.opacity;ctx.font = `${180 * this.scale}px "微软雅黑"`;// ctx.font="20px";ctx.fillStyle = colors[i % 18];ctx.fillText("木易巷",this.x - this.width * 0.5,this.y - this.height * 0.5,this.width,this.height);// ctx.drawImage(//   heartImage,//   this.x - this.width * 0.5,//   this.y - this.height * 0.5,//   this.width,//   this.height// );};function render() {ctx.clearRect(0, 0, ww, wh);// ctx.globalAlpha = 1;// ctx.fillStyle = "rgba(255,255,255,0.3)";// ctx.fillRect(0, 0, ww, wh);for (var i = 0; i < 100; i++) {hearts[i].update(i);hearts[i].draw(i);}requestAnimationFrame(render);}init();// var heartImage = new Image();// heartImage.onload = init();// heartImage.src =//   "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0NzMuOHB4IiBoZWlnaHQ9IjQwOC42cHgiIHZpZXdCb3g9IjAgMCA0NzMuOCA0MDguNiI+PHBhdGggZmlsbD0iI2QzMjkzMiIgZD0iTTQwNC42LDE2LjZDMzg1LjQsNi4xLDM2My41LDAsMzQwLDBjLTQxLjUsMC03OC41LDE4LjktMTAzLDQ4LjVDMjEyLjMsMTguOSwxNzUuMywwLDEzMy44LDAgYy0yMy4zLDAtNDUuMyw2LjEtNjQuNSwxNi42QzI3LjksMzkuNSwwLDgzLjQsMCwxMzMuOWMwLDE0LjQsMi40LDI4LjMsNi42LDQxLjJDMjkuNiwyNzguNCwyMzcsNDA4LjYsMjM3LDQwOC42IHMyMDcuMi0xMzAuMiwyMzAuMi0yMzMuNWM0LjMtMTIuOSw2LjYtMjYuOCw2LjYtNDEuMkM0NzMuOCw4My40LDQ0NS45LDM5LjYsNDA0LjYsMTYuNnoiLz48L3N2Zz4=";window.addEventListener("resize", function () {ww = window.innerWidth;wh = window.innerHeight;});
</SCRIPT><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* 请求动画帧多边形填充,立即执行函数,当解析到这一行,代码立即执行,后面的括号可以传递参数*/(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* 像素点的类,prototype可以给类中添加方法*/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 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* 把它们放在一起,立即执行* 执行行为:*      1、渲染一个*/(function (canvas) {//下面的canvas对象获取,由立即执行函数的参数传递了,不需要get获取// var canvas = document.getElementById("pinkboard")//获取上下文容器、new粒子池对象、定义变量粒子比率变量并赋值、定义事件变量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 <= 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);}function gradientColor(startColor, endColor, step) {startRGB = colorRgb(startColor);//转换为rgb数组模式startR = startRGB[0];startG = startRGB[1];startB = startRGB[2];endRGB = colorRgb(endColor);endR = endRGB[0];endG = endRGB[1];endB = endRGB[2];sR = (endR - startR) / step;//总差值sG = (endG - startG) / step;sB = (endB - startB) / step;var colorArr = [];for (var i = 0; i < step; i++) {//计算每一步的hex值var hex = colorHex('rgb(' + parseInt((sR * i + startR)) + ',' + parseInt((sG * i + startG)) + ',' + parseInt((sB * i + startB)) + ')');colorArr.push(hex);}return colorArr;}var colorRgb = function (sColor) {var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;var sColor = sColor.toLowerCase();if (sColor && reg.test(sColor)) {if (sColor.length === 4) {var sColorNew = "#";for (var i = 1; i < 4; i += 1) {sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));}sColor = sColorNew;}//处理六位的颜色值var sColorChange = [];for (var i = 1; i < 7; i += 2) {sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));}return sColorChange;} else {return sColor;}}// 将rgb表示方式转换为hex表示方式var colorHex = function (rgb) {var _this = rgb;var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;if (/^(rgb|RGB)/.test(_this)) {var aColor = _this.replace('rgb', "").replace('(', "").replace(')', "").split(",");var strHex = "#";for (var i = 0; i < aColor.length; i++) {var hex = Number(aColor[i]).toString(16);hex = hex < 10 ? 0 + '' + hex : hex;// 保证每个rgb的值为2位if (hex === "0") {hex += hex;}strHex += hex;}if (strHex.length !== 7) {strHex = _this;}return strHex;} else if (reg.test(_this)) {var aNum = _this.replace(/#/, "").split("");if (aNum.length === 6) {return _this;} else if (aNum.length === 3) {var numHex = "#";for (var i = 0; i < aNum.length; i += 1) {numHex += (aNum[i] + aNum[i]);}return numHex;}} else {return _this;}}var weight = 20;var colorArrAsc = gradientColor('#FF0000', '#0000FF', weight);var colorArrDesc = gradientColor('#0000FF', '#FF0000', weight);// alert(weight + ',' + colorArrAsc.length + ',' + colorArrDesc.length);//用来设置的颜色\// creating the particle image using a dummy canvas//使用虚拟画布创建粒子图像,返回了图像的像素点function getImageAsc(i) {var canvas = document.createElement("canvas");var context = canvas.getContext("2d");canvas.width = settings.particles.size;canvas.height = settings.particles.size;// helper function to create the path// Helper函数来创建路径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 = colorArrAsc[i % (weight+1)];// alert('asc,'+(i % weight));context.fill();// create the imagevar image = new Image();image.src = canvas.toDataURL();return image;};function getImageDesc(i) {var canvas = document.createElement("canvas");var context = canvas.getContext("2d");canvas.width = settings.particles.size;canvas.height = settings.particles.size;// helper function to create the path// Helper函数来创建路径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 = colorArrDesc[i % (weight+1)];// alert('desc,'+(i % (weight+1));context.fill();// create the imagevar image = new Image();image.src = canvas.toDataURL();return image;};var image1 = getImageAsc(0);// render that thing!//定义了一个渲染的方法,使用渲染器,渲染这些点function render() {// next animation frame//下一个动画帧requestAnimationFrame(render);// update time//更新时间,var newTime = new Date().getTime() / 1000,deltaTime = newTime - (time || newTime);time = newTime;// alert(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 particlesparticles.update(deltaTime);particles.draw(context, image1);}// handle (re-)sizing of the canvas//定义一个方法,初始化画布的大小function onResize() {canvas.width = canvas.clientWidth;canvas.height = canvas.clientHeight;}window.onresize = onResize;// delay rendering bootstrap//定义一个延迟呈现引导的方法,10微秒后重设画布,并且重新渲染,模拟心跳的效果setTimeout(function () {onResize();render();}, 10);var x = 0setInterval(function () {if (x <= weight) {image1 = getImageAsc(x);}if (x > weight && x < 2 * weight) {image1 = getImageDesc(x - weight)}if (x == 2 * weight) {image1 = getImageDesc(x - weight)x = 0;}x++;}, 1000);})(document.getElementById("pinkboard"));</SCRIPT><script></script></BODY></HTML>

好啦,本次分享就到这里,祝大家开心~

最后

博主为人老实,无偿解答问题,也会录制一些学习视频❤

如果对您有帮助,希望能给个

最新的爱心代码已就绪 发射成功 速来领取啦相关推荐

  1. Hey! 首先祝贺 SpaceX 发射成功,其次我黑了 NASA 某IT 承包商网络哟~

     聚焦源代码安全,网罗国内外最新资讯! 编译:奇安信代码卫士团队 勒索软件 DopplePaymer 的操纵者首先祝贺 SpaceX 和 NASA 载人龙飞船发射成功,转而宣布感染了NASA 其中一个 ...

  2. 抖音爆火李峋同款爱心代码,简单附带教程,还有烟花代码,手残党也能学会!!

    最近看到抖音爆火的一些HTML代码,有人找"极客G"更新,今天用了几个小时给大家整理出来了下面几个代码,最简单的就是第一个爱心代码,第二个烟花代码可自定义文本,具体看下面. 代码是 ...

  3. 别具一格,原创唯美浪漫情人节表白专辑,(复制就可用)(html5,css3,svg)表白爱心代码(1)

    别具一格,原创唯美浪漫情人节表白专辑, (复制就可用)(html5,css3,svg)表白爱心代码(1) 一. 前言 回眸之间,丰盈了岁月,涟漪了思绪,轻轻落笔,不写伤痕,不写仇怨,只写岁月经历领悟后 ...

  4. 主板检测卡(POST卡)故障代码及排除方法速查表

    主板检测卡(POST卡)故障代码及排除方法速查表 码 Award AMI Phoenix/Tandy3000 00 同FF 同FF 同FF 01 处理器测试1,处理器状态核实,如果测试失败,循环是无限 ...

  5. Java可以用到军事方面吗_恭喜遥三运载火箭发射成功, 浅谈 java 在军事方面的运用!...

    恭喜遥三运载火箭发射成功, 浅谈 java 在军事方面的运用! 恭喜长征五号遥三成功发射! 来聊聊军事系统都是用什么语言编写的 长征五号运载火箭, 是中国运载火箭升级换代的重要工程, 作为中国首型大推 ...

  6. HTML爱心代码 | 一起体验理工男的极致浪漫(电视剧男主同款)

    写在前面 大家好,我是陈橘又青,今天中午刷微博,看到最近<点燃我温暖你>中男主角--理工男李峋的爱心代码撩到了无数人,于是把代码开源分享给大家. 文章目录 写在前面 运行示例 完整代码 保 ...

  7. 趣味编程活动教程(内附简单c语言爱心代码)

    hello!各位宝宝们很高兴大家能够参加此次活动,撒花*★,°*:.☆( ̄▽ ̄)/$:*.°★* . 此次活动的要求是使用任意编程语言,输出"强国有我!" 那么就开始我们的教学之旅 ...

  8. 别具一格,原创唯美浪漫情人节表白专辑,(复制就可用)(html5,css3,svg)表白爱心代码(4)

    别具一格,独此一家,原创唯美浪漫情人节表白专辑 不一样的惊喜哦~!(html5,css3,svg)表白爱心代码(复制就可用)(4) 目录 款式四:时光的记忆款 1.拷贝完整源代码 2.更新时光盒所显示 ...

  9. 将爱心代码设为电脑屏保,俘获少女芳心,还能假装黑客大佬,在酷炫的界面中保护隐私

    将爱心代码设为电脑屏保,俘获少女芳心,还能假装黑客大佬,在酷炫的界面中保护隐私 本文介绍 Hacker Screen Saver 一款开源 Windows 屏保的使用.Hacker Screen Sa ...

最新文章

  1. 漫画 | 在中国,程序媛到底有多难?
  2. [恢]hdu 1412
  3. sphinx.conf listen = 9306:mysql41_Sphinx 安装与使用
  4. Beanutils.copyProperties复制参数不为null的属性
  5. Springboot-读取核心配置文件及自定义配置文件
  6. Error: listen EADDRINUSE: address already in use :::8080
  7. reducebykeyandwindow java_Spark Streaming笔记整理(三):DS的transformation与output操作
  8. mybatisplus 操作另一个数据库的数据_实例分析:python操作数据库项目
  9. 手动给64位centos6.3版本linux的firefox安装Adobe flash player
  10. 剑指offer——16.数值的整数次方
  11. WMware Mac 一些优化
  12. 华为云私有云解决方案的年终答卷
  13. 串联稳压电源与多谐振荡器
  14. PPT文件太大?如何压缩PPT?这几招帮你搞定
  15. JavaMail中的553 Mail from must equal authorized user
  16. Fang Fang HDU - 5455 (思维题)
  17. 追寻宇宙的形状--庞加莱猜想
  18. 光分配网(ODN)一级分光和二级分光的区别及应用场景
  19. 自己尝试使用简单数据集实现决策树 代码——《机器学习实战》
  20. window11无法启动您的相机,0xA00F429F<WindowShowFailed>(OxC00D36BB)

热门文章

  1. Ae 入门系列之九:表达式
  2. 内存地址空间为何用十六进制表示(pamp分析基础)
  3. 基于Java的Android区块链钱包开发(ETH篇)
  4. 将门禁卡写入到手机、手环,加密卡也能写
  5. (五)深入理解蓝牙Mesh的消息格式之“格式与长度”
  6. java中Stack的peek方法
  7. 什么是分布式计算框架(动画演示)?
  8. windows 环境下在anaconda 3中安装python2和python3两个环境(python2和python3共存)
  9. 干货!2017苹果开发者大会发布了啥_看这篇就够了
  10. 背单词App开发日记5(上)