当前位置:   article > 正文

JavaScript---动态爱心代码_html粒子爱心代码

html粒子爱心代码

效果展示:

 

源码如下:

可更改背景图、背景色以及爱心颜色。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>动态爱心代码</title>
  8. <style>
  9. html,
  10. body {
  11. height: 100%;
  12. padding: 0;
  13. margin: 0;
  14. /* 背景颜色 */
  15. background: #000;
  16. /* 此处可调节背景颜色及背景图 */
  17. background-image: url(bg.gif);
  18. /* 背景图片垂直、水平均居中 */
  19. background-position: center center;
  20. /* 背景图片不平铺 */
  21. background-repeat: no-repeat;
  22. /* 当内容高度>图片高度时,背景图像的位置相对于电脑窗口固定 */
  23. background-attachment: fixed;
  24. /* 让背景图片基于窗口大小伸缩 */
  25. background-size: cover;
  26. }
  27. canvas {
  28. position: absolute;
  29. /* 此处可更改爱心位置 */
  30. width: 100%;
  31. height: 100%;
  32. /* width: 500px;
  33. height: 500px; */
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <canvas id="pinkboard"></canvas>
  39. <script>
  40. // 设置粒子
  41. var settings = {
  42. particles: {
  43. length: 500, // 最大颗粒量
  44. duration: 2, // 粒子持续时间(秒)
  45. velocity: 100, // 粒子速度(以像素/秒为单位)
  46. effect: -0.65, // 影响粒子的扩散
  47. size: 30, // 粒径(像素)
  48. },
  49. };
  50. (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) } } }());
  51. // 点类
  52. var Point = (function () {
  53. function Point(x, y) {
  54. this.x = (typeof x !== 'undefined') ? x : 0;
  55. this.y = (typeof y !== 'undefined') ? y : 0;
  56. }
  57. Point.prototype.clone = function () {
  58. return new Point(this.x, this.y);
  59. };
  60. Point.prototype.length = function (length) {
  61. if (typeof length == 'undefined')
  62. return Math.sqrt(this.x * this.x + this.y * this.y);
  63. this.normalize();
  64. this.x *= length;
  65. this.y *= length;
  66. return this;
  67. };
  68. Point.prototype.normalize = function () {
  69. var length = this.length();
  70. this.x /= length;
  71. this.y /= length;
  72. return this;
  73. };
  74. return Point;
  75. })();
  76. //粒子类
  77. var Particle = (function () {
  78. function Particle() {
  79. this.position = new Point();
  80. this.velocity = new Point();
  81. this.acceleration = new Point();
  82. this.age = 0;
  83. }
  84. Particle.prototype.initialize = function (x, y, dx, dy) {
  85. this.position.x = x;
  86. this.position.y = y;
  87. this.velocity.x = dx;
  88. this.velocity.y = dy;
  89. this.acceleration.x = dx * settings.particles.effect;
  90. this.acceleration.y = dy * settings.particles.effect;
  91. this.age = 0;
  92. };
  93. Particle.prototype.update = function (deltaTime) {
  94. this.position.x += this.velocity.x * deltaTime;
  95. this.position.y += this.velocity.y * deltaTime;
  96. this.velocity.x += this.acceleration.x * deltaTime;
  97. this.velocity.y += this.acceleration.y * deltaTime;
  98. this.age += deltaTime;
  99. };
  100. Particle.prototype.draw = function (context, image) {
  101. function ease(t) {
  102. return (--t) * t * t + 1;
  103. }
  104. var size = image.width * ease(this.age / settings.particles.duration);
  105. context.globalAlpha = 1 - this.age / settings.particles.duration;
  106. context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
  107. };
  108. return Particle;
  109. })();
  110. //粒子池类
  111. var ParticlePool = (function () {
  112. var particles,
  113. firstActive = 0,
  114. firstFree = 0,
  115. duration = settings.particles.duration;
  116. function ParticlePool(length) {
  117. // 创建并填充粒子池
  118. particles = new Array(length);
  119. for (var i = 0; i < particles.length; i++)
  120. particles[i] = new Particle();
  121. }
  122. ParticlePool.prototype.add = function (x, y, dx, dy) {
  123. particles[firstFree].initialize(x, y, dx, dy);
  124. // 处理循环队列
  125. firstFree++;
  126. if (firstFree == particles.length) firstFree = 0;
  127. if (firstActive == firstFree) firstActive++;
  128. if (firstActive == particles.length) firstActive = 0;
  129. };
  130. ParticlePool.prototype.update = function (deltaTime) {
  131. var i;
  132. // 更新活性粒子
  133. if (firstActive < firstFree) {
  134. for (i = firstActive; i < firstFree; i++)
  135. particles[i].update(deltaTime);
  136. }
  137. if (firstFree < firstActive) {
  138. for (i = firstActive; i < particles.length; i++)
  139. particles[i].update(deltaTime);
  140. for (i = 0; i < firstFree; i++)
  141. particles[i].update(deltaTime);
  142. }
  143. // 去除非活性颗粒
  144. while (particles[firstActive].age >= duration && firstActive != firstFree) {
  145. firstActive++;
  146. if (firstActive == particles.length) firstActive = 0;
  147. }
  148. };
  149. ParticlePool.prototype.draw = function (context, image) {
  150. // 绘制活性粒子
  151. if (firstActive < firstFree) {
  152. for (i = firstActive; i < firstFree; i++)
  153. particles[i].draw(context, image);
  154. }
  155. if (firstFree < firstActive) {
  156. for (i = firstActive; i < particles.length; i++)
  157. particles[i].draw(context, image);
  158. for (i = 0; i < firstFree; i++)
  159. particles[i].draw(context, image);
  160. }
  161. };
  162. return ParticlePool;
  163. })();
  164. // 将粒子整合在一起
  165. (function (canvas) {
  166. var context = canvas.getContext('2d'),
  167. particles = new ParticlePool(settings.particles.length),
  168. particleRate = settings.particles.length / settings.particles.duration, // particles/sec
  169. time;
  170. // 得到心形点 -PI <= t <= PI
  171. function pointOnHeart(t) {
  172. return new Point(
  173. 160 * Math.pow(Math.sin(t), 3),
  174. 130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
  175. );
  176. }
  177. // 使用虚拟画布创建粒子图像
  178. var image = (function () {
  179. var canvas = document.createElement('canvas'),
  180. context = canvas.getContext('2d');
  181. canvas.width = settings.particles.size;
  182. canvas.height = settings.particles.size;
  183. // 用于创建路径的帮助程序函数
  184. function to(t) {
  185. var point = pointOnHeart(t);
  186. point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
  187. point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350;
  188. return point;
  189. }
  190. // 创建路径
  191. context.beginPath();
  192. var t = -Math.PI;
  193. var point = to(t);
  194. context.moveTo(point.x, point.y);
  195. while (t < Math.PI) {
  196. t += 0.01; // baby steps!
  197. point = to(t);
  198. context.lineTo(point.x, point.y);
  199. }
  200. context.closePath();
  201. // 创建填充
  202. context.fillStyle = '#ff8585';//爱心颜色
  203. context.fill();
  204. // 创建图像
  205. var image = new Image();
  206. image.src = canvas.toDataURL();
  207. return image;
  208. })();
  209. // 渲染
  210. function render() {
  211. // 下一个动画帧
  212. requestAnimationFrame(render);
  213. //更新时间
  214. var newTime = new Date().getTime() / 1000,
  215. deltaTime = newTime - (time || newTime);
  216. time = newTime;
  217. // 透明画布
  218. context.clearRect(0, 0, canvas.width, canvas.height);
  219. // 创建新粒子
  220. var amount = particleRate * deltaTime;
  221. for (var i = 0; i < amount; i++) {
  222. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  223. var dir = pos.clone().length(settings.particles.velocity);
  224. particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
  225. }
  226. // 更新和绘制粒子
  227. particles.update(deltaTime);
  228. particles.draw(context, image);
  229. }
  230. // 处理(重新)调整画布的大小
  231. function onResize() {
  232. canvas.width = canvas.clientWidth;
  233. canvas.height = canvas.clientHeight;
  234. }
  235. window.onresize = onResize;
  236. // 延迟渲染引导
  237. setTimeout(function () {
  238. onResize();
  239. render();
  240. }, 10);
  241. })(document.getElementById('pinkboard'));</script>
  242. </body>
  243. </html>

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号