html5 式程序员表白

html5 式程序员表白

王海庆 于 星期三, 04/06/2014 - 00:44 提交

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

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

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

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

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

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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2hxZXQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="border:0px; max-width:100%; height:auto">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2hxZXQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="border:0px; max-width:100%; height:auto">

实现原理

1. canvas里面实现描边文字

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

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

整个效果例如以下。

html文件很easy。

[html] view plaincopy

  1. <canvas id="canvas" width="1000" height="600"></canvas>

css文件例如以下。

[css] view plaincopy

  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文件至关重要了。

[javascript] view plaincopy

  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. };
时间: 2024-08-05 11:14:09

html5 式程序员表白的相关文章

超级炫酷的HTML5互动式程序员求职简历

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

程序员表白系列源码

程序员表白系列源码 这里只是借花献佛 曾经也有人进行过整理 http://download.csdn.net/album/detail/1501

程序员表白程序,开放源码,不断更新(第三篇:第二弹)

首先感谢hackerzhou同志,是他给了我激情和想法,感谢他的开源精神,造福大家. 这一波主要内容集中在网页这里,我一直想找一个通用或简易办法,能使大部分人都能使用"表白"这份礼物,如果使用网页,那么就要会建站,要服务器,要域名,除了代码还需要配置,有点麻烦,我这里使用的都是新浪云服务器,可以免费建一些网站,操作也比较简单,不会建站的可以来问我. 这一章的主要内容是展示这些表白的页面. 一.loveyue1 演示地址:http://loveyue1.sinaapp.com 效果如图:

程序员表白程序,开放源码,不断更新(第二篇)

表白第一弹的内容发出去后,收到很多人的祝福和建议,很感谢大家的捧场,2014年经历了很多事,自己的创业路失败,重新找工作,一直忙碌,也没有再修改代码,现准备重新拾起来,把第一弹完善,然后送出表白第二弹,第三弹的设想己完成,正在coding中. 这一篇的主要内容是把完善后的第一弹的程序放出,顺便讲解一下这里的一些新的东西. 源码地址:https://github.com/wuxia2001/mylove.git APK地址:http://zhushou.360.cn/detail/index/so

程序员表白

在我的class里,你永远是private static final 我不想把我对你的爱写在注释里,于是 printf,echo,System.out.println,Console.WriteLine等等替我告诉你 你若问我多爱你,我会告诉你,在我的程序里,无论是常量,变量,数据类型还是方法.都有你的影子. 若这份爱非要有个期限,那等下面的循环自行停止运行 for (int i=0; i>=0; i++){ System.out.println("我爱你!!!"); }

【h5程序员表白页面】表白,带计时功能代码

代码修改教程: 注:以下代码都在index.html里面,小白勿修改js代码,大神无视!!!(下载地址和演示地址在文章底部) 上图被涂黑的地方可以输入男生和女生的姓名,红框里面的字都可以替换成自己的话,尽量字数和原来差不多!!!! 上图是时间模块,在括号里依次填入你们相识的时间,24小时制.如果显示不正常,请清除浏览器cookie试试! 上图是背景音乐地址,可以替换,原来是双截棍,哈哈哈哈!一点也不和谐吧!!!!! 下载: 链接:http://pan.baidu.com/s/1c1oomJm 密

浪漫程序员 HTML5爱心表白动画

我们程序员在追求爱情方面也是非常浪漫的,下面是一位同学利用自己所学的HTML5知识自制的HTML5爱心表白动画,画面非常温馨甜蜜,这样的创意很容易打动女孩,如果你是单身的程序员,也赶紧来制作自己的爱心表白动画吧. 在线演示源码下载 转载自:http://www.html5tricks.com/html5-heart-animation.html 浪漫程序员 HTML5爱心表白动画

程序员用HTML5制作的爱心树表白动画

体验效果:http://keleyi.com/keleyi/phtml/html5/31.htm HTML代码如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8" > <title>程序员用HTML5制作的爱心树表白动画- 柯乐义</title><base tar

在理论分析家和程序员眼中,html5是大不相同的

技术的真正趋势还是由技术是否低成本.高效率.高产出.高用户体验等决定的,当然也有大小公司商业博弈等偶然因素决定.在整个决定进程中,程序员的经验.直觉和喜好占有很大的决定权重.当一个技术流行,那么程序员往往会借鉴以前的技术或创造新的技术来完善这种流行,让其开发实现更快速.更友好.更强大:而后,新技术中又会脱颖而出一个亮点技术引出新的流行--周而复始.现在主流的技术就是对移动平台的应用开发,而html5就是借鉴以前PC时代的B/S和C/S架构对当前移动应用开发的一个完善,会在短期内成为流行的亮点技术