• HTML爱心代码 | 《点燃我温暖你》中男主角——理工男李峋同款


    运行示例:

     完整代码:

    不知道怎么运行的,或者想添加背景图片的往后看!!!!

    HTML代码如下:

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

    不是计算机专业的同学往下看 怎么运行!!

    首先,在电脑桌面右击新建一个文本文档,并将上面给的代码复制粘贴进去,保存。

    然后重命名这个文本文档,把后缀.txt改为.html格式

     

    点击“是”即可,这时候这个文件就变成你浏览器的图标了,双击即可打开(也可以发给你喜欢的人,他可以直接在电脑上打开)

    如果文件没有变成浏览器图标的话,右击这个文件,选择打开方式用你的浏览器打开即可。

    看下效果:

     

     后缀改了但是还是不行!原因如下:

    部分朋友反应的修改了文件扩展名后没有弹出是否更改的选项,应该是你的电脑没有开启文件扩展名选项,大家打开“此电脑”后,点击查看——显示——勾选文件扩展名,然后再去修改即可! 

     

     添加背景图片:

    在上面代码的第17行“position: absolute;”后添加下面这两行代码

    1. background-image: url("love.jpg");
    2. background-size: contain;

     然后在桌面把你想添加的背景图片命名为love.jpg即可!

    效果图:

    修改爱心颜色

    1.找到你想要修改的颜色的 RGB色值(点击查询)
    2.第215行代码“context.fillStyle = '#ea80b0';”中的 ea80b0 改成你想要颜色的色值即可。

  • 相关阅读:
    【代码随想录算法训练Day65】卡码网47.参加科学大会、卡码网94. 城市间货物运输 I
    git解决ssh: Could not resolve hostname gitlab.xxxx.com
    中贝通信-603220 三季报分析(20231120)
    三翼鸟:产品不会变,场景实时变
    求你了,别在高并发场景中使用悲观锁了!
    安装Canal1.1.5 并监控mysql8的binlog
    Linux 命令使用笔记【sysctl】
    Go语言内置类型和函数
    JS--对象数组深拷贝的方法
    uniapp播放mp4省流方案
  • 原文地址:https://blog.csdn.net/weixin_64103049/article/details/127810821