当前位置:   article > 正文

html5式程序猿表白_html5动态奶茶浪漫登录

html5动态奶茶浪漫登录
前端开发whqet,csdn,王海庆,whqet,前端开发专家

今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁、结婚已经五年,但是也不可以偷懒,于是有了这个效果

水平有限,浏览器兼容不好,请大家使用chrome浏览器获得最佳效果。

------------------------------------

在线研究点这里,下载收藏点这里.

------------------------------------------------------

程序猿and程序媛,大胆秀出你的爱吧。利用html5 canvas实现动态的文字粒子效果,效果如下。






实现原理

1. canvas里面实现描边文字

2. 利用getImageData获得描边文字的像素矩阵

3. 将粒子效果绑定在描边文字上

整个效果如下。

html文件非常简单。

<canvas id="canvas" width="1000" height="600"></canvas>
css文件如下。

  1. body {
  2. background: #000;
  3. text-align: center;
  4. font-family: "simhei"
  5. }
  6. canvas {
  7. margin: auto;
  8. position: absolute;
  9. left: 0;
  10. right:0;
  11. top: 0;
  12. bottom: 0;
  13. }
js文件至关重要了。

  1. BLUR = false;
  2. PULSATION = true;
  3. PULSATION_PERIOD = 1000;
  4. PARTICLE_RADIUS = 6;
  5. /* disable blur before using blink */
  6. BLINK = false;
  7. GLOBAL_PULSATION = false;
  8. QUALITY = 2; /* 0 - 5 */
  9. /* set false if you prefer rectangles */
  10. ARC = true;
  11. /* trembling + blur = fun */
  12. TREMBLING = 0; /* 0 - infinity */
  13. FANCY_FONT = "Arial";
  14. BACKGROUND = "#000";
  15. BLENDING = true;
  16. /* if empty the text will be a random number */
  17. var TEXT;
  18. num = 0;
  19. TEXTArray = ["海庆", "深爱", "春玲", "直到","永远"];
  20. QUALITY_TO_FONT_SIZE = [10, 12, 40, 50, 200, 350];
  21. QUALITY_TO_SCALE = [20, 6, 4, 1, 0.9, 0.5];
  22. QUALITY_TO_TEXT_POS = [10, 20, 40, 60, 170, 280];
  23. window.onload = function () {
  24. document.body.style.backgroundColor = BACKGROUND;
  25. var canvas = document.getElementById("canvas");
  26. var ctx = canvas.getContext("2d");
  27. var W = canvas.width;
  28. var H = canvas.height;
  29. var tcanvas = document.createElement("canvas");
  30. var tctx = tcanvas.getContext("2d");
  31. tcanvas.width = W;
  32. tcanvas.height = H;
  33. total_area = W * H;
  34. total_particles = 1000;
  35. single_particle_area = total_area / total_particles;
  36. area_length = Math.sqrt(single_particle_area);
  37. var particles = [];
  38. for (var i = 1; i <= total_particles; i++) {
  39. particles.push(new particle(i));
  40. }
  41. function particle(i) {
  42. this.r = Math.round(Math.random() * 255 | 0);
  43. this.g = Math.round(Math.random() * 255 | 0);
  44. this.b = Math.round(Math.random() * 255 | 0);
  45. this.alpha = 1;
  46. this.x = (i * area_length) % W;
  47. this.y = (i * area_length) / W * area_length;
  48. /* randomize delta to make particles sparkling */
  49. this.deltaOffset = Math.random() * PULSATION_PERIOD | 0;
  50. this.radius = 0.1 + Math.random() * 2;
  51. }
  52. var positions = [];
  53. function new_positions() {
  54. TEXT = TEXTArray[num];
  55. if (num < TEXTArray.length - 1) {
  56. num++;
  57. } else {
  58. num = 0;
  59. }
  60. //alert(TEXT);
  61. tctx.fillStyle = "white";
  62. tctx.fillRect(0, 0, W, H)
  63. tctx.fill();
  64. tctx.font = "bold " + QUALITY_TO_FONT_SIZE[QUALITY] + "px " + FANCY_FONT;
  65. tctx.strokeStyle = "black";
  66. tctx.fillStyle="#f00";
  67. tctx.strokeText(TEXT,20, 60);
  68. //tctx.fillText(TEXT,30, 50);
  69. image_data = tctx.getImageData(0, 0, W, H);
  70. pixels = image_data.data;
  71. positions = [];
  72. for (var i = 0; i < pixels.length; i = i + 2) {
  73. if (pixels[i] != 255) {
  74. position = {
  75. x: (i / 2 % W | 0) * QUALITY_TO_SCALE[QUALITY] | 0,
  76. y: (i / 2 / W | 0) * QUALITY_TO_SCALE[QUALITY] | 0
  77. }
  78. positions.push(position);
  79. }
  80. }
  81. get_destinations();
  82. }
  83. function draw() {
  84. var now = Date.now();
  85. ctx.globalCompositeOperation = "source-over";
  86. if (BLUR) ctx.globalAlpha = 0.1;
  87. else if (!BLUR && !BLINK) ctx.globalAlpha = 1.0;
  88. ctx.fillStyle = BACKGROUND;
  89. ctx.fillRect(0, 0, W, H)
  90. if (BLENDING) ctx.globalCompositeOperation = "lighter";
  91. for (var i = 0; i < particles.length; i++) {
  92. p = particles[i];
  93. if (isNaN(p.x)) continue
  94. ctx.beginPath();
  95. ctx.fillStyle = "rgb(" + p.r + ", " + p.g + ", " + p.b + ")";
  96. ctx.fillStyle = "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.alpha + ")";
  97. if (BLINK) ctx.globalAlpha = Math.sin(Math.PI * mod * 1.0);
  98. if (PULSATION) { /* this would be 0 -> 1 */
  99. var mod = ((GLOBAL_PULSATION ? 0 : p.deltaOffset) + now) % PULSATION_PERIOD / PULSATION_PERIOD;
  100. mod = Math.sin(mod * Math.PI);
  101. } else var mod = 1;
  102. var offset = TREMBLING ? TREMBLING * (-1 + Math.random() * 2) : 0;
  103. var radius = PARTICLE_RADIUS * p.radius;
  104. if (!ARC) {
  105. ctx.fillRect(offset + p.x - mod * radius / 2 | 0, offset + p.y - mod * radius / 2 | 0, radius * mod,
  106. radius * mod);
  107. } else {
  108. ctx.arc(offset + p.x | 0, offset + p.y | 0, radius * mod, Math.PI * 2, false);
  109. ctx.fill();
  110. }
  111. p.x += (p.dx - p.x) / 10;
  112. p.y += (p.dy - p.y) / 10;
  113. }
  114. }
  115. function get_destinations() {
  116. for (var i = 0; i < particles.length; i++) {
  117. pa = particles[i];
  118. particles[i].alpha = 1;
  119. var distance = [];
  120. nearest_position = 0;
  121. if (positions.length) {
  122. for (var n = 0; n < positions.length; n++) {
  123. po = positions[n];
  124. distance[n] = Math.sqrt((pa.x - po.x) * (pa.x - po.x) + (pa.y - po.y) * (pa.y - po.y));
  125. if (n > 0) {
  126. if (distance[n] <= distance[nearest_position]) {
  127. nearest_position = n;
  128. }
  129. }
  130. }
  131. particles[i].dx = positions[nearest_position].x;
  132. particles[i].dy = positions[nearest_position].y;
  133. particles[i].distance = distance[nearest_position];
  134. var po1 = positions[nearest_position];
  135. for (var n = 0; n < positions.length; n++) {
  136. var po2 = positions[n];
  137. distance = Math.sqrt((po1.x - po2.x) * (po1.x - po2.x) + (po1.y - po2.y) * (po1.y - po2.y));
  138. if (distance <= 5) {
  139. positions.splice(n, 1);
  140. }
  141. }
  142. } else {
  143. //particles[i].alpha = 0;
  144. }
  145. }
  146. }
  147. function init() {
  148. new_positions();
  149. setInterval(draw, 30);
  150. setInterval(new_positions, 2000);
  151. }
  152. init();
  153. };

----------------------------------------------------------

前端开发whqet,关注web前端开发,分享相关资源,欢迎点赞,欢迎拍砖。
---------------------------------------------------------------------------------------------------------

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

闽ICP备14008679号