当前位置:   article > 正文

创意特效分享:用代码绘制网页上的爱心_web心形代码

web心形代码

在网页设计中,为了增加用户体验和吸引用户的注意力,常常需要添加一些特效来增添页面的互动性和趣味性。其中,爱心特效是一种常见且受欢迎的效果,能够在用户与网页进行交互时展现出迷人的动态效果。

通过使用HTML、CSS和JavaScript,我们可以编写简单而优雅的代码,将一个或多个爱心图案绘制到网页上。

目录

绘制多个爱心图案并赋予动画效果

跳动的爱心


绘制多个爱心图案并赋予动画效果

特效预览

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>HTML5 Canvas爱心动画特效</title>
  6. <style>
  7. html, body {
  8. height: 100%;
  9. padding: 0;
  10. margin: 0;
  11. background: #000;
  12. }
  13. canvas {
  14. width: 100%;
  15. height: 100%;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div style="text-align:center;clear:both;">
  21. <script src="/gg_bd_ad_720x90.js" type="text/javascript"></script>
  22. <script src="/follow.js" type="text/javascript"></script>
  23. </div>
  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>

跳动的爱心

效果预览

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>HTML5 Css爱心动画特效</title>
  6. </head>
  7. <body>
  8. <div class="heart">
  9. <div class="left"></div>
  10. <div class="right"></div>
  11. </div>
  12. </body>
  13. <style>
  14. body {
  15. margin: 0;
  16. background-color: black;
  17. }
  18. .heart {
  19. width: 200px;
  20. height: 200px;
  21. /*auto 左右自动相等 */
  22. margin: 100px auto;
  23. background-color: black;
  24. /* 播放动画 动画名称 时间 匀速 无限播放 反向 */
  25. animation: beat 1s linear infinite alternate;
  26. }
  27. /* 关键帧 */
  28. @keyframes beat {
  29. /* 开始画面 */
  30. 0% {
  31. transform: scale(1);
  32. }
  33. /* 结束画面 */
  34. 100% {
  35. transform: scale(1.1);
  36. }
  37. }
  38. .left,
  39. .right {
  40. width: 100px;
  41. height: 160px;
  42. float: left;
  43. background-color: pink;
  44. /* 圆角 顺时针 左上 右上 右下 左下 半径 */
  45. border-radius: 50px 50px 0 0;
  46. /* 盒子阴影 水平方向 垂直方向 模糊半径 扩展 颜色 */
  47. box-shadow: 0px 0px 20px 0px pink;
  48. }
  49. .left {
  50. /* 变换 移动 旋转 */
  51. transform: translateX(29px) rotate(-45deg);
  52. }
  53. .right {
  54. transform: translateX(-29px) rotate(45deg);
  55. }
  56. </style>
  57. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/92719
推荐阅读
相关标签
  

闽ICP备14008679号