定制网站爱心代码李峋同款爱心 python html

目录


前言

定制网站最近那个电视剧很火,定制网站就是搞爱心代码的,定制网站本人兴趣使然,定制网站在网上搜集了一些代码,定制网站经过一定修改,定制网站做一个小总结。

一、python

运行

定制网站主要用的包都是那么几个,csdn搜pycharm定制网站怎么导包就可以。

1.python 第一个

成品效果

 

调整思路

HEART_COLOR = "#EEAEEE"  #定制网站引号内修改颜色!定制网站颜色代码放在文章末尾

定制网站双引号里面可以在csdn搜RGB颜色,比如,定制网站直接看里面的对照表,把包括#定制网站的数字或字母替换就可定制网站以换颜色了

代码如下

  1. import random
  2. from math import sin, cos, pi, log
  3. from tkinter import *
  4. CANVAS_WIDTH = 840 # 画布的宽
  5. CANVAS_HEIGHT = 680 # 画布的高
  6. CANVAS_CENTER_X = CANVAS_WIDTH / 2 # 画布中心的X轴坐标
  7. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # 画布中心的Y轴坐标
  8. IMAGE_ENLARGE = 11 # 放大比例
  9. HEART_COLOR = "#EEAEEE" #引号内修改颜色!颜色代码放在文章末尾
  10. def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
  11. """
  12. “爱心函数生成器”
  13. :param shrink_ratio: 放大比例
  14. :param t: 参数
  15. :return: 坐标
  16. """
  17. # 基础函数
  18. x = 17 * (sin(t) ** 3)
  19. y = -(16 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(3 * t))
  20. # 放大
  21. #x *= shrink_ratio
  22. #y *= shrink_ratio
  23. x*=IMAGE_ENLARGE
  24. y*=IMAGE_ENLARGE
  25. # 移到画布中央
  26. x += CANVAS_CENTER_X
  27. y += CANVAS_CENTER_Y
  28. return int(x), int(y)
  29. def scatter_inside(x, y, beta=0.15):
  30. """
  31. 随机内部扩散
  32. :param x: 原x
  33. :param y: 原y
  34. :param beta: 强度
  35. :return: 新坐标
  36. """
  37. ratio_x = - beta * log(random.random())
  38. ratio_y = - beta * log(random.random())
  39. dx = ratio_x * (x - CANVAS_CENTER_X)
  40. dy = ratio_y * (y - CANVAS_CENTER_Y)
  41. return x - dx, y - dy
  42. def shrink(x, y, ratio):
  43. """
  44. 抖动
  45. :param x: 原x
  46. :param y: 原y
  47. :param ratio: 比例
  48. :return: 新坐标
  49. """
  50. force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6) # 这个参数...
  51. dx = ratio * force * (x - CANVAS_CENTER_X)
  52. dy = ratio * force * (y - CANVAS_CENTER_Y)
  53. return x - dx, y - dy
  54. def curve(p):
  55. """
  56. 自定义曲线函数,调整跳动周期
  57. :param p: 参数
  58. :return: 正弦
  59. """
  60. # 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)
  61. return 2 * (2 * sin(4 * p)) / (2 * pi)
  62. class Heart:
  63. """
  64. 爱心类
  65. """
  66. def __init__(self, generate_frame=20):
  67. self._points = set() # 原始爱心坐标集合
  68. self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
  69. self._center_diffusion_points = set() # 中心扩散效果点坐标集合
  70. self.all_points = {} # 每帧动态点坐标
  71. self.build(2000)
  72. self.random_halo = 1000
  73. self.generate_frame = generate_frame
  74. for frame in range(generate_frame):
  75. self.calc(frame)
  76. def build(self, number):
  77. # 爱心
  78. for _ in range(number):
  79. t = random.uniform(0, 2 * pi) # 随机不到的地方造成爱心有缺口
  80. x, y = heart_function(t)
  81. self._points.add((x, y))
  82. # 爱心内扩散
  83. for _x, _y in list(self._points):
  84. for _ in range(3):
  85. x, y = scatter_inside(_x, _y, 0.05)
  86. self._edge_diffusion_points.add((x, y))
  87. # 爱心内再次扩散
  88. point_list = list(self._points)
  89. for _ in range(10000):
  90. x, y = random.choice(point_list)
  91. x, y = scatter_inside(x, y, 0.27)
  92. self._center_diffusion_points.add((x, y))
  93. @staticmethod
  94. def calc_position(x, y, ratio):
  95. # 调整缩放比例
  96. force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.420) # 魔法参数
  97. dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
  98. dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
  99. return x - dx, y - dy
  100. def calc(self, generate_frame):
  101. ratio = 15 * curve(generate_frame / 10 * pi) # 圆滑的周期的缩放比例
  102. halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
  103. halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
  104. all_points = []
  105. # 光环
  106. heart_halo_point = set() # 光环的点坐标集合
  107. for _ in range(halo_number):
  108. t = random.uniform(0, 2 * pi) # 随机不到的地方造成爱心有缺口
  109. x, y = heart_function(t, shrink_ratio=-15) # 魔法参数
  110. x, y = shrink(x, y, halo_radius)
  111. if (x, y) not in heart_halo_point:
  112. # 处理新的点
  113. heart_halo_point.add((x, y))
  114. x += random.randint(-60, 60)
  115. y += random.randint(-60, 60)
  116. size = random.choice((1, 1, 2))
  117. all_points.append((x, y, size))
  118. all_points.append((x+20, y+20, size))
  119. all_points.append((x-20, y -20, size))
  120. all_points.append((x+20, y - 20, size))
  121. all_points.append((x - 20, y +20, size))
  122. # 轮廓
  123. for x, y in self._points:
  124. x, y = self.calc_position(x, y, ratio)
  125. size = random.randint(1, 3)
  126. all_points.append((x, y, size))
  127. # 内容
  128. for x, y in self._edge_diffusion_points:
  129. x, y = self.calc_position(x, y, ratio)
  130. size = random.randint(1, 2)
  131. all_points.append((x, y, size))
  132. for x, y in self._center_diffusion_points:
  133. x, y = self.calc_position(x, y, ratio)
  134. size = random.randint(1, 2)
  135. all_points.append((x, y, size))
  136. self.all_points[generate_frame] = all_points
  137. def render(self, render_canvas, render_frame):
  138. for x, y, size in self.all_points[render_frame % self.generate_frame]:
  139. render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
  140. def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
  141. render_canvas.delete('all')
  142. render_heart.render(render_canvas, render_frame)
  143. main.after(1, draw, main, render_canvas, render_heart, render_frame + 1)
  144. if __name__ == '__main__':
  145. root = Tk()
  146. canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
  147. canvas.pack()
  148. heart = Heart()
  149. draw(root, canvas, heart)
  150. root.mainloop()

2.python第二个

成品效果

 

调整思路

HEART_COLOR = "#EEAEEE"  #心的颜色

调整颜色的思路同上

代码如下

  1. import random
  2. from math import sin, cos, pi, log
  3. from tkinter import *
  4. CANVAS_WIDTH = 640 # 画布的宽
  5. CANVAS_HEIGHT = 480 # 画布的高
  6. CANVAS_CENTER_X = CANVAS_WIDTH / 2 # 画布中心的X轴坐标
  7. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # 画布中心的Y轴坐标
  8. IMAGE_ENLARGE = 11 # 放大比例
  9. HEART_COLOR = "pink" # 心的颜色
  10. def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
  11. """
  12. “爱心函数生成器”
  13. :param shrink_ratio: 放大比例
  14. :param t: 参数
  15. :return: 坐标
  16. """
  17. # 基础函数
  18. x = 16 * (sin(t) ** 3)
  19. y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
  20. # 放大
  21. x *= shrink_ratio
  22. y *= shrink_ratio
  23. # 移到画布中央
  24. x += CANVAS_CENTER_X
  25. y += CANVAS_CENTER_Y
  26. return int(x), int(y)
  27. def scatter_inside(x, y, beta=0.15):
  28. """
  29. 随机内部扩散
  30. :param x: 原x
  31. :param y: 原y
  32. :param beta: 强度
  33. :return: 新坐标
  34. """
  35. ratio_x = - beta * log(random.random())
  36. ratio_y = - beta * log(random.random())
  37. dx = ratio_x * (x - CANVAS_CENTER_X)
  38. dy = ratio_y * (y - CANVAS_CENTER_Y)
  39. return x - dx, y - dy
  40. def shrink(x, y, ratio):
  41. """
  42. 抖动
  43. :param x: 原x
  44. :param y: 原y
  45. :param ratio: 比例
  46. :return: 新坐标
  47. """
  48. force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6) # 这个参数...
  49. dx = ratio * force * (x - CANVAS_CENTER_X)
  50. dy = ratio * force * (y - CANVAS_CENTER_Y)
  51. return x - dx, y - dy
  52. def curve(p):
  53. """
  54. 自定义曲线函数,调整跳动周期
  55. :param p: 参数
  56. :return: 正弦
  57. """
  58. return 4 * (2 * sin(4 * p)) / (2 * pi)
  59. class Heart:
  60. """
  61. 爱心类
  62. """
  63. def __init__(self, generate_frame=20):
  64. self._points = set() # 原始爱心坐标集合
  65. self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
  66. self._center_diffusion_points = set() # 中心扩散效果点坐标集合
  67. self.all_points = {} # 每帧动态点坐标
  68. self.build(2000)
  69. self.random_halo = 1000
  70. self.generate_frame = generate_frame
  71. for frame in range(generate_frame):
  72. self.calc(frame)
  73. def build(self, number):
  74. # 爱心
  75. for _ in range(number):
  76. t = random.uniform(0, 2 * pi) # 随机不到的地方造成爱心有缺口
  77. x, y = heart_function(t)
  78. self._points.add((x, y))
  79. # 爱心内扩散
  80. for _x, _y in list(self._points):
  81. for _ in range(3):
  82. x, y = scatter_inside(_x, _y, 0.05)
  83. self._edge_diffusion_points.add((x, y))
  84. # 爱心内再次扩散
  85. point_list = list(self._points)
  86. for _ in range(4000):
  87. x, y = random.choice(point_list)
  88. x, y = scatter_inside(x, y, 0.17)
  89. self._center_diffusion_points.add((x, y))
  90. @staticmethod
  91. def calc_position(x, y, ratio):
  92. # 调整缩放比例
  93. force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)
  94. dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
  95. dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
  96. return x - dx, y - dy
  97. def calc(self, generate_frame):
  98. ratio = 10 * curve(generate_frame / 10 * pi) # 圆滑的周期的缩放比例
  99. halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
  100. halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
  101. all_points = []
  102. # 光环
  103. heart_halo_point = set() # 光环的点坐标集合
  104. for _ in range(halo_number):
  105. t = random.uniform(0, 2 * pi) # 随机不到的地方造成爱心有缺口
  106. x, y = heart_function(t, shrink_ratio=11)
  107. x, y = shrink(x, y, halo_radius)
  108. if (x, y) not in heart_halo_point:
  109. # 处理新的点
  110. heart_halo_point.add((x, y))
  111. x += random.randint(-11, 11)
  112. y += random.randint(-11, 11)
  113. size = random.choice((1, 2, 2))#控制外围粒子的大小
  114. all_points.append((x, y, size))
  115. # 轮廓
  116. for x, y in self._points:
  117. x, y = self.calc_position(x, y, ratio)
  118. size = random.randint(1, 3)
  119. all_points.append((x, y, size))
  120. # 内容
  121. for x, y in self._center_diffusion_points:
  122. x, y = self.calc_position(x, y, ratio)
  123. size = random.randint(1, 2)
  124. all_points.append((x, y, size))
  125. self.all_points[generate_frame] = all_points
  126. def render(self, render_canvas, render_frame):
  127. for x, y, size in self.all_points[render_frame % self.generate_frame]:
  128. render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
  129. def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
  130. render_canvas.delete('all')
  131. render_heart.render(render_canvas, render_frame)
  132. main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
  133. if __name__ == '__main__':
  134. root = Tk() # 一个Tk
  135. canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
  136. canvas.pack()
  137. heart = Heart() # 心
  138. draw(root, canvas, heart) # 开始画画~
  139. root.mainloop()

二、HTML

html的个人感觉更好,因为有浏览器就支持运行,py的后期得导出成exe才可以在没有环境设备运行,html手机电脑都可以的。

1.第一个

输出样例

调整

<title>canvas爱心</title>   这个canvas爱心字样随便调整可以自己编辑的名字之类的

代码如下

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>canvas爱心</title>
  6. <style>
  7. html, body {
  8. height: 100%;
  9. padding: 0;
  10. margin: 0;
  11. background: #000;
  12. }
  13. canvas {
  14. position: absolute;
  15. width: 100%;
  16. height: 100%;
  17. }</style>
  18. </head>
  19. <body>
  20. <canvas id="pinkboard"></canvas>
  21. <script>
  22. /*
  23. * Settings
  24. */
  25. var settings = {
  26. particles: {
  27. length: 500, // maximum amount of particles
  28. duration: 2, // particle duration in sec
  29. velocity: 100, // particle velocity in pixels/sec
  30. effect: -0.75, // play with this for a nice effect
  31. size: 30, // particle size in pixels
  32. },
  33. };
  34. /*
  35. * RequestAnimationFrame polyfill by Erik M?ller
  36. */
  37. (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)}}}());
  38. /*
  39. * Point class
  40. */
  41. var Point = (function() {
  42. function Point(x, y) {
  43. this.x = (typeof x !== 'undefined') ? x : 0;
  44. this.y = (typeof y !== 'undefined') ? y : 0;
  45. }
  46. Point.prototype.clone = function() {
  47. return new Point(this.x, this.y);
  48. };
  49. Point.prototype.length = function(length) {
  50. if (typeof length == 'undefined')
  51. return Math.sqrt(this.x * this.x + this.y * this.y);
  52. this.normalize();
  53. this.x *= length;
  54. this.y *= length;
  55. return this;
  56. };
  57. Point.prototype.normalize = function() {
  58. var length = this.length();
  59. this.x /= length;
  60. this.y /= length;
  61. return this;
  62. };
  63. return Point;
  64. })();
  65. /*
  66. * Particle class
  67. */
  68. var Particle = (function() {
  69. function Particle() {
  70. this.position = new Point();
  71. this.velocity = new Point();
  72. this.acceleration = new Point();
  73. this.age = 0;
  74. }
  75. Particle.prototype.initialize = function(x, y, dx, dy) {
  76. this.position.x = x;
  77. this.position.y = y;
  78. this.velocity.x = dx;
  79. this.velocity.y = dy;
  80. this.acceleration.x = dx * settings.particles.effect;
  81. this.acceleration.y = dy * settings.particles.effect;
  82. this.age = 0;
  83. };
  84. Particle.prototype.update = function(deltaTime) {
  85. this.position.x += this.velocity.x * deltaTime;
  86. this.position.y += this.velocity.y * deltaTime;
  87. this.velocity.x += this.acceleration.x * deltaTime;
  88. this.velocity.y += this.acceleration.y * deltaTime;
  89. this.age += deltaTime;
  90. };
  91. Particle.prototype.draw = function(context, image) {
  92. function ease(t) {
  93. return (--t) * t * t + 1;
  94. }
  95. var size = image.width * ease(this.age / settings.particles.duration);
  96. context.globalAlpha = 1 - this.age / settings.particles.duration;
  97. context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
  98. };
  99. return Particle;
  100. })();
  101. /*
  102. * ParticlePool class
  103. */
  104. var ParticlePool = (function() {
  105. var particles,
  106. firstActive = 0,
  107. firstFree = 0,
  108. duration = settings.particles.duration;
  109. function ParticlePool(length) {
  110. // create and populate particle pool
  111. particles = new Array(length);
  112. for (var i = 0; i < particles.length; i++)
  113. particles[i] = new Particle();
  114. }
  115. ParticlePool.prototype.add = function(x, y, dx, dy) {
  116. particles[firstFree].initialize(x, y, dx, dy);
  117. // handle circular queue
  118. firstFree++;
  119. if (firstFree == particles.length) firstFree = 0;
  120. if (firstActive == firstFree ) firstActive++;
  121. if (firstActive == particles.length) firstActive = 0;
  122. };
  123. ParticlePool.prototype.update = function(deltaTime) {
  124. var i;
  125. // update active particles
  126. if (firstActive < firstFree) {
  127. for (i = firstActive; i < firstFree; i++)
  128. particles[i].update(deltaTime);
  129. }
  130. if (firstFree < firstActive) {
  131. for (i = firstActive; i < particles.length; i++)
  132. particles[i].update(deltaTime);
  133. for (i = 0; i < firstFree; i++)
  134. particles[i].update(deltaTime);
  135. }
  136. // remove inactive particles
  137. while (particles[firstActive].age >= duration && firstActive != firstFree) {
  138. firstActive++;
  139. if (firstActive == particles.length) firstActive = 0;
  140. }
  141. };
  142. ParticlePool.prototype.draw = function(context, image) {
  143. // draw active particles
  144. if (firstActive < firstFree) {
  145. for (i = firstActive; i < firstFree; i++)
  146. particles[i].draw(context, image);
  147. }
  148. if (firstFree < firstActive) {
  149. for (i = firstActive; i < particles.length; i++)
  150. particles[i].draw(context, image);
  151. for (i = 0; i < firstFree; i++)
  152. particles[i].draw(context, image);
  153. }
  154. };
  155. return ParticlePool;
  156. })();
  157. /*
  158. * Putting it all together
  159. */
  160. (function(canvas) {
  161. var context = canvas.getContext('2d'),
  162. particles = new ParticlePool(settings.particles.length),
  163. particleRate = settings.particles.length / settings.particles.duration, // particles/sec
  164. time;
  165. // get point on heart with -PI <= t <= PI
  166. function pointOnHeart(t) {
  167. return new Point(
  168. 160 * Math.pow(Math.sin(t), 3),
  169. 130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
  170. );
  171. }
  172. // creating the particle image using a dummy canvas
  173. var image = (function() {
  174. var canvas = document.createElement('canvas'),
  175. context = canvas.getContext('2d');
  176. canvas.width = settings.particles.size;
  177. canvas.height = settings.particles.size;
  178. // helper function to create the path
  179. function to(t) {
  180. var point = pointOnHeart(t);
  181. point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
  182. point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350;
  183. return point;
  184. }
  185. // create the path
  186. context.beginPath();
  187. var t = -Math.PI;
  188. var point = to(t);
  189. context.moveTo(point.x, point.y);
  190. while (t < Math.PI) {
  191. t += 0.01; // baby steps!
  192. point = to(t);
  193. context.lineTo(point.x, point.y);
  194. }
  195. context.closePath();
  196. // create the fill
  197. context.fillStyle = '#ea80b0';
  198. context.fill();
  199. // create the image
  200. var image = new Image();
  201. image.src = canvas.toDataURL();
  202. return image;
  203. })();
  204. // render that thing!
  205. function render() {
  206. // next animation frame
  207. requestAnimationFrame(render);
  208. // update time
  209. var newTime = new Date().getTime() / 1000,
  210. deltaTime = newTime - (time || newTime);
  211. time = newTime;
  212. // clear canvas
  213. context.clearRect(0, 0, canvas.width, canvas.height);
  214. // create new particles
  215. var amount = particleRate * deltaTime;
  216. for (var i = 0; i < amount; i++) {
  217. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  218. var dir = pos.clone().length(settings.particles.velocity);
  219. particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
  220. }
  221. // update and draw particles
  222. particles.update(deltaTime);
  223. particles.draw(context, image);
  224. }
  225. // handle (re-)sizing of the canvas
  226. function onResize() {
  227. canvas.width = canvas.clientWidth;
  228. canvas.height = canvas.clientHeight;
  229. }
  230. window.onresize = onResize;
  231. // delay rendering bootstrap
  232. setTimeout(function() {
  233. onResize();
  234. render();
  235. }, 10);
  236. })(document.getElementById('pinkboard'));</script>
  237. </body>
  238. </html>

 

2.第二个html

输出示例

调整一样方式

代码如下(这是看这个博主的):

  1. <!DOCTYPE html>
  2. <!-- saved from url=(0033)http://junior-l.gitee.io/to-lili/ -->
  3. <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>💗</title>
  5. <style>
  6. canvas {
  7. position: absolute;
  8. left: 0;
  9. top: 0;
  10. width: 100%;
  11. height: 100%;
  12. background-color: rgba(0, 0, 0, .2);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <canvas id="heart" width="1920" height="947"></canvas>
  18. <script>
  19. window.requestAnimationFrame =
  20. window.__requestAnimationFrame ||
  21. window.requestAnimationFrame ||
  22. window.webkitRequestAnimationFrame ||
  23. window.mozRequestAnimationFrame ||
  24. window.oRequestAnimationFrame ||
  25. window.msRequestAnimationFrame ||
  26. (function () {
  27. return function (callback, element) {
  28. var lastTime = element.__lastTime;
  29. if (lastTime === undefined) {
  30. lastTime = 0;
  31. }
  32. var currTime = Date.now();
  33. var timeToCall = Math.max(1, 33 - (currTime - lastTime));
  34. window.setTimeout(callback, timeToCall);
  35. element.__lastTime = currTime + timeToCall;
  36. };
  37. })();
  38. window.isDevice = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(((navigator.userAgent || navigator.vendor || window.opera)).toLowerCase()));
  39. var loaded = false;
  40. var init = function () {
  41. if (loaded) return;
  42. loaded = true;
  43. var mobile = window.isDevice;
  44. var koef = mobile ? 0.5 : 1;
  45. var canvas = document.getElementById('heart');
  46. var ctx = canvas.getContext('2d');
  47. var width = canvas.width = koef * innerWidth;
  48. var height = canvas.height = koef * innerHeight;
  49. var rand = Math.random;
  50. ctx.fillStyle = "rgba(0,0,0,1)";
  51. ctx.fillRect(0, 0, width, height);
  52. var heartPosition = function (rad) {
  53. //return [Math.sin(rad), Math.cos(rad)];
  54. return [Math.pow(Math.sin(rad), 3), -(15 * Math.cos(rad) - 5 * Math.cos(2 * rad) - 2 * Math.cos(3 * rad) - Math.cos(4 * rad))];
  55. };
  56. var scaleAndTranslate = function (pos, sx, sy, dx, dy) {
  57. return [dx + pos[0] * sx, dy + pos[1] * sy];
  58. };
  59. window.addEventListener('resize', function () {
  60. width = canvas.width = koef * innerWidth;
  61. height = canvas.height = koef * innerHeight;
  62. ctx.fillStyle = "rgba(0,0,0,1)";
  63. ctx.fillRect(0, 0, width, height);
  64. });
  65. var traceCount = mobile ? 20 : 50;
  66. var pointsOrigin = [];
  67. var i;
  68. var dr = mobile ? 0.3 : 0.1;
  69. for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 210, 13, 0, 0));
  70. for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 150, 9, 0, 0));
  71. for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 90, 5, 0, 0));
  72. var heartPointsCount = pointsOrigin.length;
  73. var targetPoints = [];
  74. var pulse = function (kx, ky) {
  75. for (i = 0; i < pointsOrigin.length; i++) {
  76. targetPoints[i] = [];
  77. targetPoints[i][0] = kx * pointsOrigin[i][0] + width / 2;
  78. targetPoints[i][1] = ky * pointsOrigin[i][1] + height / 2;
  79. }
  80. };
  81. var e = [];
  82. for (i = 0; i < heartPointsCount; i++) {
  83. var x = rand() * width;
  84. var y = rand() * height;
  85. e[i] = {
  86. vx: 0,
  87. vy: 0,
  88. R: 2,
  89. speed: rand() + 5,
  90. q: ~~(rand() * heartPointsCount),
  91. D: 2 * (i % 2) - 1,
  92. force: 0.2 * rand() + 0.7,
  93. f: "hsla(0," + ~~(40 * rand() + 60) + "%," + ~~(60 * rand() + 20) + "%,.3)",
  94. trace: []
  95. };
  96. for (var k = 0; k < traceCount; k++) e[i].trace[k] = {x: x, y: y};
  97. }
  98. var config = {
  99. traceK: 0.4,
  100. timeDelta: 0.01
  101. };
  102. var time = 0;
  103. var loop = function () {
  104. var n = -Math.cos(time);
  105. pulse((1 + n) * .5, (1 + n) * .5);
  106. time += ((Math.sin(time)) < 0 ? 9 : (n > 0.8) ? .2 : 1) * config.timeDelta;
  107. ctx.fillStyle = "rgba(0,0,0,.1)";
  108. ctx.fillRect(0, 0, width, height);
  109. for (i = e.length; i--;) {
  110. var u = e[i];
  111. var q = targetPoints[u.q];
  112. var dx = u.trace[0].x - q[0];
  113. var dy = u.trace[0].y - q[1];
  114. var length = Math.sqrt(dx * dx + dy * dy);
  115. if (10 > length) {
  116. if (0.95 < rand()) {
  117. u.q = ~~(rand() * heartPointsCount);
  118. } else {
  119. if (0.99 < rand()) {
  120. u.D *= -1;
  121. }
  122. u.q += u.D;
  123. u.q %= heartPointsCount;
  124. if (0 > u.q) {
  125. u.q += heartPointsCount;
  126. }
  127. }
  128. }
  129. u.vx += -dx / length * u.speed;
  130. u.vy += -dy / length * u.speed;
  131. u.trace[0].x += u.vx;
  132. u.trace[0].y += u.vy;
  133. u.vx *= u.force;
  134. u.vy *= u.force;
  135. for (k = 0; k < u.trace.length - 1;) {
  136. var T = u.trace[k];
  137. var N = u.trace[++k];
  138. N.x -= config.traceK * (N.x - T.x);
  139. N.y -= config.traceK * (N.y - T.y);
  140. }
  141. ctx.fillStyle = u.f;
  142. for (k = 0; k < u.trace.length; k++) {
  143. ctx.fillRect(u.trace[k].x, u.trace[k].y, 1, 1);
  144. }
  145. }
  146. ctx.fillStyle = "rgba(255,255,255,1)";
  147. for (i = u.trace.length + 13; i--;) ctx.fillRect(targetPoints[i][0], targetPoints[i][1], 2, 2);
  148. window.requestAnimationFrame(loop, canvas);
  149. };
  150. loop();
  151. };
  152. var s = document.readyState;
  153. if (s === 'complete' || s === 'loaded' || s === 'interactive') init();
  154. else document.addEventListener('DOMContentLoaded', init, false);
  155. </script>
  156. </body></html>

3.第三个html

样子

 

备注

这个我个人感觉还是蛮好看的,值得注意的是,里面的字是也能动的,可以把♥瓜瓜♥改成你想要的字眼(代码里面ctrl+f 找瓜瓜字眼 替换即可),titile也同理

代码

  1. <!DOCTYPE html>
  2. <!-- saved from url=(0046)https://httishere.gitee.io/notion/v4/love-name -->
  3. <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>💗 Love you 💗 My Dear 瓜瓜💗</title>
  5. <style type="text/css">
  6. body {
  7. margin: 0;
  8. overflow: hidden;
  9. background: #000;
  10. }
  11. canvas {
  12. position: absolute;
  13. width: 100%;
  14. height: 100%;
  15. }
  16. #pinkboard {
  17. animation: anim 1.5s ease-in-out infinite;
  18. -webkit-animation: anim 1.5s ease-in-out infinite;
  19. -o-animation: anim 1.5s ease-in-out infinite;
  20. -moz-animation: anim 1.5s ease-in-out infinite;
  21. }
  22. @keyframes anim {
  23. 0% {
  24. transform: scale(0.8);
  25. }
  26. 25% {
  27. transform: scale(0.7);
  28. }
  29. 50% {
  30. transform: scale(1);
  31. }
  32. 75% {
  33. transform: scale(0.7);
  34. }
  35. 100% {
  36. transform: scale(0.8);
  37. }
  38. }
  39. @-webkit-keyframes anim {
  40. 0% {
  41. -webkit-transform: scale(0.8);
  42. }
  43. 25% {
  44. -webkit-transform: scale(0.7);
  45. }
  46. 50% {
  47. -webkit-transform: scale(1);
  48. }
  49. 75% {
  50. -webkit-transform: scale(0.7);
  51. }
  52. 100% {
  53. -webkit-transform: scale(0.8);
  54. }
  55. }
  56. @-o-keyframes anim {
  57. 0% {
  58. -o-transform: scale(0.8);
  59. }
  60. 25% {
  61. -o-transform: scale(0.7);
  62. }
  63. 50% {
  64. -o-transform: scale(1);
  65. }
  66. 75% {
  67. -o-transform: scale(0.7);
  68. }
  69. 100% {
  70. -o-transform: scale(0.8);
  71. }
  72. }
  73. @-moz-keyframes anim {
  74. 0% {
  75. -moz-transform: scale(0.8);
  76. }
  77. 25% {
  78. -moz-transform: scale(0.7);
  79. }
  80. 50% {
  81. -moz-transform: scale(1);
  82. }
  83. 75% {
  84. -moz-transform: scale(0.7);
  85. }
  86. 100% {
  87. -moz-transform: scale(0.8);
  88. }
  89. }
  90. #name {
  91. position: absolute;
  92. top: 50%;
  93. left: 50%;
  94. transform: translate(-50%, -50%);
  95. margin-top: -20px;
  96. font-size: 46px;
  97. color: #ea80b0;
  98. }
  99. </style>
  100. </head>
  101. <body>
  102. <canvas id="pinkboard" width="1920" height="947"></canvas>
  103. <!-- <div id="name">瓜瓜💗 ❤️</div> -->
  104. <canvas id="canvas" width="1920" height="947"></canvas>
  105. <script type="text/javascript">
  106. const colors = [
  107. "#eec996",
  108. "#8fb7d3",
  109. "#b7d4c6",
  110. "#c3bedd",
  111. "#f1d5e4",
  112. "#cae1d3",
  113. "#f3c89d",
  114. "#d0b0c3",
  115. "#819d53",
  116. "#c99294",
  117. "#cec884",
  118. "#ff8e70",
  119. "#e0a111",
  120. "#fffdf6",
  121. "#cbd7ac",
  122. "#e8c6c0",
  123. "#dc9898",
  124. "#ecc8ba",
  125. ]; //用来设置的颜色
  126. var canvas = document.getElementById("canvas");
  127. var ctx = canvas.getContext("2d");
  128. let count = 1;
  129. var ww = window.innerWidth;
  130. var wh = window.innerHeight;
  131. var hearts = [];
  132. function init() {
  133. requestAnimationFrame(render);
  134. canvas.width = ww;
  135. canvas.height = wh;
  136. for (var i = 0; i < 100; i++) {
  137. hearts.push(new Heart());
  138. }
  139. }
  140. function Heart() {
  141. this.x = Math.random() * ww;
  142. this.y = Math.random() * wh;
  143. this.opacity = Math.random() * 0.5 + 0.5;
  144. this.vel = {
  145. x: (Math.random() - 0.5) * 4,
  146. y: (Math.random() - 0.5) * 4,
  147. };
  148. this.targetScale = Math.random() * 0.15 + 0.02;
  149. this.scale = this.targetScale * Math.random();
  150. }
  151. Heart.prototype.update = function (i) {
  152. this.x += this.vel.x;
  153. this.y += this.vel.y;
  154. this.scale += (this.targetScale - this.scale) * 0.01;
  155. if (this.x - this.width > ww || this.x + this.width < 0) {
  156. this.scale = 0;
  157. this.x = Math.random() * ww;
  158. }
  159. if (this.y - this.height > wh || this.y + this.height < 0) {
  160. this.scale = 0;
  161. this.y = Math.random() * wh;
  162. }
  163. this.width = 473.8;
  164. this.height = 408.6;
  165. };
  166. Heart.prototype.draw = function (i) {
  167. ctx.globalAlpha = this.opacity;
  168. ctx.font = `${180 * this.scale}px "微软雅黑"`;
  169. // ctx.font="20px";
  170. ctx.fillStyle = colors[i % 18];
  171. ctx.fillText(
  172. "💗瓜瓜💗",
  173. this.x - this.width * 0.5,
  174. this.y - this.height * 0.5,
  175. this.width,
  176. this.height
  177. );
  178. // ctx.drawImage(
  179. // heartImage,
  180. // this.x - this.width * 0.5,
  181. // this.y - this.height * 0.5,
  182. // this.width,
  183. // this.height
  184. // );
  185. };
  186. function render() {
  187. ctx.clearRect(0, 0, ww, wh);
  188. // ctx.globalAlpha = 1;
  189. // ctx.fillStyle = "rgba(255,255,255,0.3)";
  190. // ctx.fillRect(0, 0, ww, wh);
  191. for (var i = 0; i < 100; i++) {
  192. hearts[i].update(i);
  193. hearts[i].draw(i);
  194. }
  195. requestAnimationFrame(render);
  196. }
  197. init();
  198. // var heartImage = new Image();
  199. // heartImage.onload = init();
  200. // heartImage.src =
  201. // "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0NzMuOHB4IiBoZWlnaHQ9IjQwOC42cHgiIHZpZXdCb3g9IjAgMCA0NzMuOCA0MDguNiI+PHBhdGggZmlsbD0iI2QzMjkzMiIgZD0iTTQwNC42LDE2LjZDMzg1LjQsNi4xLDM2My41LDAsMzQwLDBjLTQxLjUsMC03OC41LDE4LjktMTAzLDQ4LjVDMjEyLjMsMTguOSwxNzUuMywwLDEzMy44LDAgYy0yMy4zLDAtNDUuMyw2LjEtNjQuNSwxNi42QzI3LjksMzkuNSwwLDgzLjQsMCwxMzMuOWMwLDE0LjQsMi40LDI4LjMsNi42LDQxLjJDMjkuNiwyNzguNCwyMzcsNDA4LjYsMjM3LDQwOC42IHMyMDcuMi0xMzAuMiwyMzAuMi0yMzMuNWM0LjMtMTIuOSw2LjYtMjYuOCw2LjYtNDEuMkM0NzMuOCw4My40LDQ0NS45LDM5LjYsNDA0LjYsMTYuNnoiLz48L3N2Zz4=";
  202. window.addEventListener("resize", function () {
  203. ww = window.innerWidth;
  204. wh = window.innerHeight;
  205. });
  206. </script>
  207. <script>
  208. /*
  209. * Settings
  210. */
  211. var settings = {
  212. particles: {
  213. length: 500, // maximum amount of particles
  214. duration: 2, // particle duration in sec
  215. velocity: 100, // particle velocity in pixels/sec
  216. effect: -0.75, // play with this for a nice effect
  217. size: 30, // particle size in pixels
  218. },
  219. };
  220. /*
  221. * RequestAnimationFrame polyfill by Erik M?ller
  222. */
  223. (function () {
  224. var b = 0;
  225. var c = ["ms", "moz", "webkit", "o"];
  226. for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) {
  227. window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"];
  228. window.cancelAnimationFrame =
  229. window[c[a] + "CancelAnimationFrame"] ||
  230. window[c[a] + "CancelRequestAnimationFrame"];
  231. }
  232. if (!window.requestAnimationFrame) {
  233. window.requestAnimationFrame = function (h, e) {
  234. var d = new Date().getTime();
  235. var f = Math.max(0, 16 - (d - b));
  236. var g = window.setTimeout(function () {
  237. h(d + f);
  238. }, f);
  239. b = d + f;
  240. return g;
  241. };
  242. }
  243. if (!window.cancelAnimationFrame) {
  244. window.cancelAnimationFrame = function (d) {
  245. clearTimeout(d);
  246. };
  247. }
  248. })();
  249. /*
  250. * Point class
  251. */
  252. var Point = (function () {
  253. function Point(x, y) {
  254. this.x = typeof x !== "undefined" ? x : 0;
  255. this.y = typeof y !== "undefined" ? y : 0;
  256. }
  257. Point.prototype.clone = function () {
  258. return new Point(this.x, this.y);
  259. };
  260. Point.prototype.length = function (length) {
  261. if (typeof length == "undefined")
  262. return Math.sqrt(this.x * this.x + this.y * this.y);
  263. this.normalize();
  264. this.x *= length;
  265. this.y *= length;
  266. return this;
  267. };
  268. Point.prototype.normalize = function () {
  269. var length = this.length();
  270. this.x /= length;
  271. this.y /= length;
  272. return this;
  273. };
  274. return Point;
  275. })();
  276. /*
  277. * Particle class
  278. */
  279. var Particle = (function () {
  280. function Particle() {
  281. this.position = new Point();
  282. this.velocity = new Point();
  283. this.acceleration = new Point();
  284. this.age = 0;
  285. }
  286. Particle.prototype.initialize = function (x, y, dx, dy) {
  287. this.position.x = x;
  288. this.position.y = y;
  289. this.velocity.x = dx;
  290. this.velocity.y = dy;
  291. this.acceleration.x = dx * settings.particles.effect;
  292. this.acceleration.y = dy * settings.particles.effect;
  293. this.age = 0;
  294. };
  295. Particle.prototype.update = function (deltaTime) {
  296. this.position.x += this.velocity.x * deltaTime;
  297. this.position.y += this.velocity.y * deltaTime;
  298. this.velocity.x += this.acceleration.x * deltaTime;
  299. this.velocity.y += this.acceleration.y * deltaTime;
  300. this.age += deltaTime;
  301. };
  302. Particle.prototype.draw = function (context, image) {
  303. function ease(t) {
  304. return --t * t * t + 1;
  305. }
  306. var size = image.width * ease(this.age / settings.particles.duration);
  307. context.globalAlpha = 1 - this.age / settings.particles.duration;
  308. context.drawImage(
  309. image,
  310. this.position.x - size / 2,
  311. this.position.y - size / 2,
  312. size,
  313. size
  314. );
  315. };
  316. return Particle;
  317. })();
  318. /*
  319. * ParticlePool class
  320. */
  321. var ParticlePool = (function () {
  322. var particles,
  323. firstActive = 0,
  324. firstFree = 0,
  325. duration = settings.particles.duration;
  326. function ParticlePool(length) {
  327. // create and populate particle pool
  328. particles = new Array(length);
  329. for (var i = 0; i < particles.length; i++)
  330. particles[i] = new Particle();
  331. }
  332. ParticlePool.prototype.add = function (x, y, dx, dy) {
  333. particles[firstFree].initialize(x, y, dx, dy);
  334. // handle circular queue
  335. firstFree++;
  336. if (firstFree == particles.length) firstFree = 0;
  337. if (firstActive == firstFree) firstActive++;
  338. if (firstActive == particles.length) firstActive = 0;
  339. };
  340. ParticlePool.prototype.update = function (deltaTime) {
  341. var i;
  342. // update active particles
  343. if (firstActive < firstFree) {
  344. for (i = firstActive; i < firstFree; i++)
  345. particles[i].update(deltaTime);
  346. }
  347. if (firstFree < firstActive) {
  348. for (i = firstActive; i < particles.length; i++)
  349. particles[i].update(deltaTime);
  350. for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
  351. }
  352. // remove inactive particles
  353. while (
  354. particles[firstActive].age >= duration &&
  355. firstActive != firstFree
  356. ) {
  357. firstActive++;
  358. if (firstActive == particles.length) firstActive = 0;
  359. }
  360. };
  361. ParticlePool.prototype.draw = function (context, image) {
  362. // draw active particles
  363. if (firstActive < firstFree) {
  364. for (i = firstActive; i < firstFree; i++)
  365. particles[i].draw(context, image);
  366. }
  367. if (firstFree < firstActive) {
  368. for (i = firstActive; i < particles.length; i++)
  369. particles[i].draw(context, image);
  370. for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
  371. }
  372. };
  373. return ParticlePool;
  374. })();
  375. /*
  376. * Putting it all together
  377. */
  378. (function (canvas) {
  379. var context = canvas.getContext("2d"),
  380. particles = new ParticlePool(settings.particles.length),
  381. particleRate =
  382. settings.particles.length / settings.particles.duration, // particles/sec
  383. time;
  384. // get point on heart with -PI <= t <= PI
  385. function pointOnHeart(t) {
  386. return new Point(
  387. 160 * Math.pow(Math.sin(t), 3),
  388. 130 * Math.cos(t) -
  389. 50 * Math.cos(2 * t) -
  390. 20 * Math.cos(3 * t) -
  391. 10 * Math.cos(4 * t) +
  392. 25
  393. );
  394. }
  395. // creating the particle image using a dummy canvas
  396. var image = (function () {
  397. var canvas = document.createElement("canvas"),
  398. context = canvas.getContext("2d");
  399. canvas.width = settings.particles.size;
  400. canvas.height = settings.particles.size;
  401. // helper function to create the path
  402. function to(t) {
  403. var point = pointOnHeart(t);
  404. point.x =
  405. settings.particles.size / 2 +
  406. (point.x * settings.particles.size) / 350;
  407. point.y =
  408. settings.particles.size / 2 -
  409. (point.y * settings.particles.size) / 350;
  410. return point;
  411. }
  412. // create the path
  413. context.beginPath();
  414. var t = -Math.PI;
  415. var point = to(t);
  416. context.moveTo(point.x, point.y);
  417. while (t < Math.PI) {
  418. t += 0.01; // baby steps!
  419. point = to(t);
  420. context.lineTo(point.x, point.y);
  421. }
  422. context.closePath();
  423. // create the fill
  424. context.fillStyle = "#ea80b0";
  425. context.fill();
  426. // create the image
  427. var image = new Image();
  428. image.src = canvas.toDataURL();
  429. return image;
  430. })();
  431. // render that thing!
  432. function render() {
  433. // next animation frame
  434. requestAnimationFrame(render);
  435. // update time
  436. var newTime = new Date().getTime() / 1000,
  437. deltaTime = newTime - (time || newTime);
  438. time = newTime;
  439. // clear canvas
  440. context.clearRect(0, 0, canvas.width, canvas.height);
  441. // create new particles
  442. var amount = particleRate * deltaTime;
  443. for (var i = 0; i < amount; i++) {
  444. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  445. var dir = pos.clone().length(settings.particles.velocity);
  446. particles.add(
  447. canvas.width / 2 + pos.x,
  448. canvas.height / 2 - pos.y,
  449. dir.x,
  450. -dir.y
  451. );
  452. }
  453. // update and draw particles
  454. particles.update(deltaTime);
  455. particles.draw(context, image);
  456. }
  457. // handle (re-)sizing of the canvas
  458. function onResize() {
  459. canvas.width = canvas.clientWidth;
  460. canvas.height = canvas.clientHeight;
  461. }
  462. window.onresize = onResize;
  463. // delay rendering bootstrap
  464. setTimeout(function () {
  465. onResize();
  466. render();
  467. }, 10);
  468. })(document.getElementById("pinkboard"));
  469. // 参考:https://blog.csdn.net/weixin_57038822/article/details/121644240
  470. </script>
  471. </body></html>

3.第四个html

样例

 代码调整思路

x位置直接记事本打开ctrl+f 找亲爱的 编辑文案

然后对这行字的编辑(样式上)

是这个,改这里的信息就行哪个参数不会就搜一下他的作用 比如第一行是字体,你可以百度 比如这个,都是一样道理。

代码

  1. <!DOCTYPE html>
  2. <html>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <head>
  5. <title>LoveHTT</title>
  6. </head>
  7. <body> <canvas id="canvas"></canvas>
  8. <style type="text/css">
  9. body {
  10. margin: 0;
  11. padding: 0;
  12. overflow: hidden;
  13. }
  14. </style>
  15. <script type="text/javascript">
  16. var canvas = document.getElementById('canvas');
  17. var ctx = canvas.getContext('2d');
  18. canvas.height = window.innerHeight;
  19. canvas.width = window.innerWidth;
  20. var texts = 'I LOVE U'.split('');
  21. var fontSize = 16;
  22. var columns = canvas.width / fontSize;
  23. // 用于计算输出文字时坐标,所以长度即为列数
  24. var drops = [];
  25. //初始值
  26. for (var x = 0; x < columns; x++) {
  27. drops[x] = 1;
  28. }
  29. function draw() {
  30. //让背景逐渐由透明到不透明
  31. ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
  32. ctx.fillRect(0, 0, canvas.width, canvas.height);
  33. //文字颜色
  34. ctx.fillStyle = '#f584b7';
  35. ctx.font = fontSize + 'px arial';
  36. //逐行输出文字
  37. for (var i = 0; i < drops.length; i++) {
  38. var text = texts[Math.floor(Math.random() * texts.length)];
  39. ctx.fillText(text, i * fontSize, drops[i] * fontSize);
  40. if (drops[i] * fontSize > canvas.height || Math.random() > 0.95) {
  41. drops[i] = 0;
  42. }
  43. drops[i]++;
  44. }
  45. }
  46. setInterval(draw, 33);
  47. </script>
  48. </body>
  49. </html>
  50. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  51. <HTML>
  52. <HEAD>
  53. <TITLE> love</TITLE>
  54. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  55. <META NAME="Generator" CONTENT="EditPlus">
  56. <META NAME="Author" CONTENT="">
  57. <META NAME="Keywords" CONTENT="">
  58. <META NAME="Description" CONTENT="">
  59. <meta charset="UTF-8">
  60. <style>
  61. html, body {
  62. height: 100%;
  63. padding: 0;
  64. margin: 0;
  65. background: rgb(0, 0, 0);
  66. }
  67. canvas {
  68. position: absolute;
  69. width: 100%;
  70. height: 100%;
  71. }
  72. #child{
  73. position: fixed;
  74. top:50%;
  75. left: 50%;
  76. margin-top: -75px;
  77. margin-left:-100px ;
  78. }
  79. h4{font-family:"STKaiti";
  80. font-size: 40px;
  81. color:#f584b7;
  82. position: relative;
  83. top: -70px;left: -65px;}
  84. </style>
  85. </head>
  86. <body>
  87. <div id="child" ><h4>💗致亲爱的X女士</h4></div><!--这里写名字❤!!!-->
  88. <canvas id ="pinkboard"></canvas>
  89. <!-- <canvas id= "canvas"></canvas> -->
  90. <script>
  91. /*
  92. * Settings
  93. */
  94. var settings = {
  95. particles: {
  96. length: 500, // maximum amount of particles
  97. duration: 2, // particle duration in sec
  98. velocity: 100, // particle velocity in pixels/sec
  99. effect: -0.75, // play with this for a nice effect
  100. size: 30, // particle size in pixels
  101. },
  102. };
  103. /*
  104. * RequestAnimationFrame polyfill by Erik Möller
  105. */
  106. (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)}}}());
  107. /*
  108. * Point class
  109. */
  110. var Point = (function() {
  111. function Point(x, y) {
  112. this.x = (typeof x !== 'undefined') ? x : 0;
  113. this.y = (typeof y !== 'undefined') ? y : 0;
  114. }
  115. Point.prototype.clone = function() {
  116. return new Point(this.x, this.y);
  117. };
  118. Point.prototype.length = function(length) {
  119. if (typeof length == 'undefined')
  120. return Math.sqrt(this.x * this.x + this.y * this.y);
  121. this.normalize();
  122. this.x *= length;
  123. this.y *= length;
  124. return this;
  125. };
  126. Point.prototype.normalize = function() {
  127. var length = this.length();
  128. this.x /= length;
  129. this.y /= length;
  130. return this;
  131. };
  132. return Point;
  133. })();
  134. /*
  135. * Particle class
  136. */
  137. var Particle = (function() {
  138. function Particle() {
  139. this.position = new Point();
  140. this.velocity = new Point();
  141. this.acceleration = new Point();
  142. this.age = 0;
  143. }
  144. Particle.prototype.initialize = function(x, y, dx, dy) {
  145. this.position.x = x;
  146. this.position.y = y;
  147. this.velocity.x = dx;
  148. this.velocity.y = dy;
  149. this.acceleration.x = dx * settings.particles.effect;
  150. this.acceleration.y = dy * settings.particles.effect;
  151. this.age = 0;
  152. };
  153. Particle.prototype.update = function(deltaTime) {
  154. this.position.x += this.velocity.x * deltaTime;
  155. this.position.y += this.velocity.y * deltaTime;
  156. this.velocity.x += this.acceleration.x * deltaTime;
  157. this.velocity.y += this.acceleration.y * deltaTime;
  158. this.age += deltaTime;
  159. };
  160. Particle.prototype.draw = function(context, image) {
  161. function ease(t) {
  162. return (--t) * t * t + 1;
  163. }
  164. var size = image.width * ease(this.age / settings.particles.duration);
  165. context.globalAlpha = 1 - this.age / settings.particles.duration;
  166. context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
  167. };
  168. return Particle;
  169. })();
  170. /*
  171. * ParticlePool class
  172. */
  173. var ParticlePool = (function() {
  174. var particles,
  175. firstActive = 0,
  176. firstFree = 0,
  177. duration = settings.particles.duration;
  178. function ParticlePool(length) {
  179. // create and populate particle pool
  180. particles = new Array(length);
  181. for (var i = 0; i < particles.length; i++)
  182. particles[i] = new Particle();
  183. }
  184. ParticlePool.prototype.add = function(x, y, dx, dy) {
  185. particles[firstFree].initialize(x, y, dx, dy);
  186. // handle circular queue
  187. firstFree++;
  188. if (firstFree == particles.length) firstFree = 0;
  189. if (firstActive == firstFree ) firstActive++;
  190. if (firstActive == particles.length) firstActive = 0;
  191. };
  192. ParticlePool.prototype.update = function(deltaTime) {
  193. var i;
  194. // update active particles
  195. if (firstActive < firstFree) {
  196. for (i = firstActive; i < firstFree; i++)
  197. particles[i].update(deltaTime);
  198. }
  199. if (firstFree < firstActive) {
  200. for (i = firstActive; i < particles.length; i++)
  201. particles[i].update(deltaTime);
  202. for (i = 0; i < firstFree; i++)
  203. particles[i].update(deltaTime);
  204. }
  205. // remove inactive particles
  206. while (particles[firstActive].age >= duration && firstActive != firstFree) {
  207. firstActive++;
  208. if (firstActive == particles.length) firstActive = 0;
  209. }
  210. };
  211. ParticlePool.prototype.draw = function(context, image) {
  212. // draw active particles
  213. if (firstActive < firstFree) {
  214. for (i = firstActive; i < firstFree; i++)
  215. particles[i].draw(context, image);
  216. }
  217. if (firstFree < firstActive) {
  218. for (i = firstActive; i < particles.length; i++)
  219. particles[i].draw(context, image);
  220. for (i = 0; i < firstFree; i++)
  221. particles[i].draw(context, image);
  222. }
  223. };
  224. return ParticlePool;
  225. })();
  226. /*
  227. * Putting it all together
  228. */
  229. (function(canvas) {
  230. var context = canvas.getContext('2d'),
  231. particles = new ParticlePool(settings.particles.length),
  232. particleRate = settings.particles.length / settings.particles.duration, // particles/sec
  233. time;
  234. // get point on heart with -PI <= t <= PI
  235. function pointOnHeart(t) {
  236. return new Point(
  237. 160 * Math.pow(Math.sin(t), 3),
  238. 130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
  239. );
  240. }
  241. // creating the particle image using a dummy canvas
  242. var image = (function() {
  243. var canvas = document.createElement('canvas'),
  244. context = canvas.getContext('2d');
  245. canvas.width = settings.particles.size;
  246. canvas.height = settings.particles.size;
  247. // helper function to create the path
  248. function to(t) {
  249. var point = pointOnHeart(t);
  250. point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
  251. point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350;
  252. return point;
  253. }
  254. // create the path
  255. context.beginPath();
  256. var t = -Math.PI;
  257. var point = to(t);
  258. context.moveTo(point.x, point.y);
  259. while (t < Math.PI) {
  260. t += 0.01; // baby steps!
  261. point = to(t);
  262. context.lineTo(point.x, point.y);
  263. }
  264. context.closePath();
  265. // create the fill
  266. context.fillStyle = '#ea80b0';
  267. context.fill();
  268. // create the image
  269. var image = new Image();
  270. image.src = canvas.toDataURL();
  271. return image;
  272. })();
  273. // render that thing!
  274. function render() {
  275. // next animation frame
  276. requestAnimationFrame(render);
  277. // update time
  278. var newTime = new Date().getTime() / 1000,
  279. deltaTime = newTime - (time || newTime);
  280. time = newTime;
  281. // clear canvas
  282. context.clearRect(0, 0, canvas.width, canvas.height);
  283. // create new particles
  284. var amount = particleRate * deltaTime;
  285. for (var i = 0; i < amount; i++) {
  286. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  287. var dir = pos.clone().length(settings.particles.velocity);
  288. particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
  289. }
  290. // update and draw particles
  291. particles.update(deltaTime);
  292. particles.draw(context, image);
  293. }
  294. // handle (re-)sizing of the canvas
  295. function onResize() {
  296. canvas.width = canvas.clientWidth;
  297. canvas.height = canvas.clientHeight;
  298. }
  299. window.onresize = onResize;
  300. // delay rendering bootstrap
  301. setTimeout(function() {
  302. onResize();
  303. render();
  304. }, 10);
  305. })(document.getElementById('pinkboard'));
  306. </script>
  307. </BODY>
  308. <!--
  309. <audio controls>
  310. <source src="Alan Walker-Faded.mp3" type="audio/ogg">
  311. <source src="Alan Walker-Faded.mp3" type="audio/mpeg">
  312. </audio >
  313. -->
  314. </HTML>

总结

本文简单介绍了几种爱心代码,希望对大家有所帮助,求赞欧~~~
另外,python想送给你的那个 TA的话可以打包为exe的,我后期再补一下教程,然后运行有问题的可以在评论区说明~

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发