当前位置:   article > 正文

Python爱心表白代码(python)_爱心代码编程python可复制

爱心代码编程python可复制

简单来说李峋同款爱心就是一个动态的心型效果,主要 Python 代码实现如下:

  1. def __init__(self, generate_frame=20):
  2. # 原始爱心坐标集合
  3. self._points = set()
  4. # 边缘扩散效果点坐标集合
  5. self._edge_diffusion_points = set()
  6. # 中心扩散效果点坐标集合
  7. self._center_diffusion_points = set()
  8. # 每帧动态点坐标
  9. self.all_points = {}
  10. self.build(2000)
  11. self.random_halo = 1000
  12. self.generate_frame = generate_frame
  13. for frame in range(generate_frame):
  14. self.calc(frame)
  15. def build(self, number):
  16. for _ in range(number):
  17. t = random.uniform(0, 2 * pi)
  18. x, y = heart(t)
  19. self._points.add((x, y))
  20. # 爱心内扩散
  21. for _x, _y in list(self._points):
  22. for _ in range(3):
  23. x, y = scatter_inside(_x, _y, 0.05)
  24. self._edge_diffusion_points.add((x, y))
  25. # 爱心内再次扩散
  26. point_list = list(self._points)
  27. for _ in range(4000):
  28. x, y = random.choice(point_list)
  29. x, y = scatter_inside(x, y, 0.17)
  30. self._center_diffusion_points.add((x, y))
  31. @staticmethodstaticmethod
  32. def calc_position(x, y, ratio):
  33. force = 1 / (((x - X) ** 2 +
  34. (y - Y) ** 2) ** 0.520)
  35. dx = ratio * force * (x - X) + random.randint(-1, 1)
  36. dy = ratio * force * (y - Y) + random.randint(-1, 1)
  37. return x - dx, y - dy
  38. def calc(self, generate_frame):
  39. ratio = 10 * curve(generate_frame / 10 * pi)
  40. halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
  41. halo_number = int(
  42. 3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
  43. all_points = []
  44. # 光环
  45. heart_halo_point = set()
  46. for _ in range(halo_number):
  47. t = random.uniform(0, 2 * pi)
  48. x, y = heart(t, shrink_ratio=11.6)
  49. x, y = shrink(x, y, halo_radius)
  50. if (x, y) not in heart_halo_point:
  51. heart_halo_point.add((x, y))
  52. x += random.randint(-14, 14)
  53. y += random.randint(-14, 14)
  54. size = random.choice((1, 2, 2))
  55. all_points.append((x, y, size))
  56. # 轮廓
  57. for x, y in self._points:
  58. x, y = self.calc_position(x, y, ratio)
  59. size = random.randint(1, 3)
  60. all_points.append((x, y, size))
  61. # 内容
  62. for x, y in self._edge_diffusion_points:
  63. x, y = self.calc_position(x, y, ratio)
  64. size = random.randint(1, 2)
  65. all_points.append((x, y, size))
  66. self.all_points[generate_frame] = all_points
  67. for x, y in self._center_diffusion_points:
  68. x, y = self.calc_position(x, y, ratio)
  69. size = random.randint(1, 2)
  70. all_points.append((x, y, size))
  71. self.all_points[generate_frame] = all_points

实现效果如下:

  1. 满屏爱心代码(修改名字版本)
  2. <!DOCTYPE html>
  3. <!-- saved from url=(0051)https://httishere.gitee.io/notion/v4/love-name.html -->
  4. <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title> Love you </title>
  6. <style type="text/css">
  7. body {
  8. margin: 0;
  9. overflow: hidden;
  10. background: #000;
  11. }
  12. canvas {
  13. position: absolute;
  14. width: 100%;
  15. height: 100%;
  16. }
  17. #pinkboard {
  18. animation: anim 1.5s ease-in-out infinite;
  19. -webkit-animation: anim 1.5s ease-in-out infinite;
  20. -o-animation: anim 1.5s ease-in-out infinite;
  21. -moz-animation: anim 1.5s ease-in-out infinite;
  22. }
  23. @keyframes anim {
  24. 0% {
  25. transform: scale(0.8);
  26. }
  27. 25% {
  28. transform: scale(0.7);
  29. }
  30. 50% {
  31. transform: scale(1);
  32. }
  33. 75% {
  34. transform: scale(0.7);
  35. }
  36. 100% {
  37. transform: scale(0.8);
  38. }
  39. }
  40. @-webkit-keyframes anim {
  41. 0% {
  42. -webkit-transform: scale(0.8);
  43. }
  44. 25% {
  45. -webkit-transform: scale(0.7);
  46. }
  47. 50% {
  48. -webkit-transform: scale(1);
  49. }
  50. 75% {
  51. -webkit-transform: scale(0.7);
  52. }
  53. 100% {
  54. -webkit-transform: scale(0.8);
  55. }
  56. }
  57. @-o-keyframes anim {
  58. 0% {
  59. -o-transform: scale(0.8);
  60. }
  61. 25% {
  62. -o-transform: scale(0.7);
  63. }
  64. 50% {
  65. -o-transform: scale(1);
  66. }
  67. 75% {
  68. -o-transform: scale(0.7);
  69. }
  70. 100% {
  71. -o-transform: scale(0.8);
  72. }
  73. }
  74. @-moz-keyframes anim {
  75. 0% {
  76. -moz-transform: scale(0.8);
  77. }
  78. 25% {
  79. -moz-transform: scale(0.7);
  80. }
  81. 50% {
  82. -moz-transform: scale(1);
  83. }
  84. 75% {
  85. -moz-transform: scale(0.7);
  86. }
  87. 100% {
  88. -moz-transform: scale(0.8);
  89. }
  90. }
  91. #name {
  92. position: absolute;
  93. top: 50%;
  94. left: 50%;
  95. transform: translate(-50%, -50%);
  96. margin-top: -20px;
  97. font-size: 46px;
  98. color: #ea80b0;
  99. }
  100. </style>
  101. <script src="./ Love you _files/monitors.3.6.36.cn.js.下载" async="" crossorigin="anonymous"></script><script src="./ Love you _files/sentry.3.6.36.cn.js.下载" async="" crossorigin="anonymous"></script></head>
  102. <body>
  103. <canvas id="pinkboard" width="1707" height="868"></canvas>
  104. <canvas id="canvas" width="1707" height="868"></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. "kawsar",
  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.heig

修改满屏文字操作步骤

将上方的代码全部复制

在电脑新建一个txt文件,命名love.txt

打开txt文件,黏贴代码

将双引号的文件给成你想要展示的文字,保存

将txt文件后缀改成 .hmtl

到此这篇关于Python爱心代码(李峋同款)的文章就介绍到这了,更多相关Python爱心代码请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/597572
推荐阅读
相关标签
  

闽ICP备14008679号