当前位置:   article > 正文

爱心树代码实现爱心表白树源码(免费分享)html+js+css

爱心树代码

html代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>爱心树</title>
  6. <link type="text/css" rel="stylesheet" href="renxi/default.css">
  7. <script type="text/javascript" src="renxi/jquery.min.js"></script>
  8. <script type="text/javascript" src="renxi/jscex.min.js"></script>
  9. <script type="text/javascript" src="renxi/jscex-parser.js"></script>
  10. <script type="text/javascript" src="renxi/jscex-jit.js"></script>
  11. <script type="text/javascript" src="renxi/jscex-builderbase.min.js"></script>
  12. <script type="text/javascript" src="renxi/jscex-async.min.js"></script>
  13. <script type="text/javascript" src="renxi/jscex-async-powerpack.min.js"></script>
  14. <script type="text/javascript" src="renxi/functions.js" charset="utf-8"></script>
  15. <script type="text/javascript" src="renxi/love.js" charset="utf-8"></script>
  16. <style type="text/css">
  17. <!--
  18. .STYLE1 {
  19. color: #666666
  20. }
  21. -->
  22. </style>
  23. </head>
  24. <body>
  25. <audio autoplay="autopaly">
  26. <source src="renxi.mp3" type="audio/mp3" />
  27. </audio>
  28. <div id="main">
  29. <div id="error">(<a href="http://www.google.cn/chrome/intl/zh-CN/landing_chrome.html?hl=zh-CN&brand=CHMI">Chrome</a>)或者火狐(<a href="http://firefox.com.cn/download/">Firefox</a>)浏览器,或者其他游览器的最新版本。</div>
  30. <div id="wrap">
  31. <div id="text">
  32. <div id="code"> <font color="#FF0000"> <span class="say"></span><br>
  33. <span class="say"> </span><br>
  34. <span class="say"> 我知道我不会甜言蜜语,但是我会用行动证明一切</span><br>
  35. <span class="say"> </span><br>
  36. <span class="say"></span><br>
  37. <span class="say"></span><br>
  38. <span class="say"></span><br>
  39. <span class="say">来</span><br>
  40. <span class="say"> </span><br>
  41. <span class="say"><span class="space"></span></span> </font>
  42. </p>
  43. </div>
  44. </div>
  45. <div id="clock-box"> <span class="STYLE1"></span><font color="#33CC00"></font> <span class="STYLE1">已经是……</span>
  46. <div id="clock"></div>
  47. </div>
  48. <canvas id="canvas" width="1100" height="680"></canvas>
  49. </div>
  50. </div>
  51. <script>
  52. </script>
  53. <script>
  54. (function(){
  55. var canvas = $('#canvas');
  56. if (!canvas[0].getContext) {
  57. $("#error").show();
  58. return false; }
  59. var width = canvas.width();
  60. var height = canvas.height();
  61. canvas.attr("width", width);
  62. canvas.attr("height", height);
  63. var opts = {
  64. seed: {
  65. x: width / 2 - 20,
  66. color: "rgb(190, 26, 37)",
  67. scale: 2
  68. },
  69. branch: [
  70. [535, 680, 570, 250, 500, 200, 30, 100, [
  71. [540, 500, 455, 417, 340, 400, 13, 100, [
  72. [450, 435, 434, 430, 394, 395, 2, 40]
  73. ]],
  74. [550, 445, 600, 356, 680, 345, 12, 100, [
  75. [578, 400, 648, 409, 661, 426, 3, 80]
  76. ]],
  77. [539, 281, 537, 248, 534, 217, 3, 40],
  78. [546, 397, 413, 247, 328, 244, 9, 80, [
  79. [427, 286, 383, 253, 371, 205, 2, 40],
  80. [498, 345, 435, 315, 395, 330, 4, 60]
  81. ]],
  82. [546, 357, 608, 252, 678, 221, 6, 100, [
  83. [590, 293, 646, 277, 648, 271, 2, 80]
  84. ]]
  85. ]]
  86. ],
  87. bloom: {
  88. num: 700,
  89. width: 1080,
  90. height: 650,
  91. },
  92. footer: {
  93. width: 1200,
  94. height: 5,
  95. speed: 10,
  96. }
  97. }
  98. var tree = new Tree(canvas[0], width, height, opts);
  99. var seed = tree.seed;
  100. var foot = tree.footer;
  101. var hold = 1;
  102. canvas.click(function(e) {
  103. var offset = canvas.offset(), x, y;
  104. x = e.pageX - offset.left;
  105. y = e.pageY - offset.top;
  106. if (seed.hover(x, y)) {
  107. hold = 0;
  108. canvas.unbind("click");
  109. canvas.unbind("mousemove");
  110. canvas.removeClass('hand');
  111. }
  112. }).mousemove(function(e){
  113. var offset = canvas.offset(), x, y;
  114. x = e.pageX - offset.left;
  115. y = e.pageY - offset.top;
  116. canvas.toggleClass('hand', seed.hover(x, y));
  117. });
  118. var seedAnimate = eval(Jscex.compile("async", function () {
  119. seed.draw();
  120. while (hold) {
  121. $await(Jscex.Async.sleep(10));
  122. }
  123. while (seed.canScale()) {
  124. seed.scale(0.95);
  125. $await(Jscex.Async.sleep(10));
  126. }
  127. while (seed.canMove()) {
  128. seed.move(0, 2);
  129. foot.draw();
  130. $await(Jscex.Async.sleep(10));
  131. }
  132. }));
  133. var growAnimate = eval(Jscex.compile("async", function () {
  134. do {
  135. tree.grow();
  136. $await(Jscex.Async.sleep(10));
  137. } while (tree.canGrow());
  138. }));
  139. var flowAnimate = eval(Jscex.compile("async", function () {
  140. do {
  141. tree.flower(2);
  142. $await(Jscex.Async.sleep(10));
  143. } while (tree.canFlower());
  144. }));
  145. var moveAnimate = eval(Jscex.compile("async", function () {
  146. tree.snapshot("p1", 240, 0, 610, 680);
  147. while (tree.move("p1", 500, 0)) {
  148. foot.draw();
  149. $await(Jscex.Async.sleep(10));
  150. }
  151. foot.draw();
  152. tree.snapshot("p2", 500, 0, 610, 680);
  153. // 会有闪烁不得意这样做, (>﹏<)
  154. canvas.parent().css("background", "url(" + tree.toDataURL('image/png') + ")");
  155. canvas.css("background", "#ffe");
  156. $await(Jscex.Async.sleep(300));
  157. canvas.css("background", "none");
  158. }));
  159. var jumpAnimate = eval(Jscex.compile("async", function () {
  160. var ctx = tree.ctx;
  161. while (true) {
  162. tree.ctx.clearRect(0, 0, width, height);
  163. tree.jump();
  164. foot.draw();
  165. $await(Jscex.Async.sleep(25));
  166. }
  167. }));
  168. var textAnimate = eval(Jscex.compile("async", function () {
  169. var together = new Date();
  170. together.setFullYear(2010,1 , 15); //时间年月日
  171. together.setHours(16); //小时
  172. together.setMinutes(53); //分钟
  173. together.setSeconds(0); //秒前一位
  174. together.setMilliseconds(2); //秒第二位
  175. $("#code").show().typewriter();
  176. $("#clock-box").fadeIn(500);
  177. while (true) {
  178. timeElapse(together);
  179. $await(Jscex.Async.sleep(1000));
  180. }
  181. }));
  182. var runAsync = eval(Jscex.compile("async", function () {
  183. $await(seedAnimate());
  184. $await(growAnimate());
  185. $await(flowAnimate());
  186. $await(moveAnimate());
  187. textAnimate().start();
  188. $await(jumpAnimate());
  189. }));
  190. runAsync().start();
  191. })();
  192. </script>
  193. </body>
  194. </html>

js代码部分

  1. function random(min, max) {
  2. return min + Math.floor(Math.random() * (max - min + 1));
  3. }
  4. function bezier(cp, t) {
  5. var p1 = cp[0].mul((1 - t) * (1 - t));
  6. var p2 = cp[1].mul(2 * t * (1 - t));
  7. var p3 = cp[2].mul(t * t);
  8. return p1.add(p2).add(p3);
  9. }
  10. function inheart(x, y, r) {
  11. // x^2+(y-(x^2)^(1/3))^2 = 1
  12. // http://www.wolframalpha.com/input/?i=x%5E2%2B%28y-%28x%5E2%29%5E%281%2F3%29%29%5E2+%3D+1
  13. var z = ((x / r) * (x / r) + (y / r) * (y / r) - 1) * ((x / r) * (x / r) + (y / r) * (y / r) - 1) * ((x / r) * (x / r) + (y / r) * (y / r) - 1) - (x / r) * (x / r) * (y / r) * (y / r) * (y / r);
  14. return z < 0;
  15. }
  16. Point = function(x, y) {
  17. this.x = x || 0;
  18. this.y = y || 0;
  19. }
  20. Point.prototype = {
  21. clone: function() {
  22. return new Point(this.x, this.y);
  23. },
  24. add: function(o) {
  25. p = this.clone();
  26. p.x += o.x;
  27. p.y += o.y;
  28. return p;
  29. },
  30. sub: function(o) {
  31. p = this.clone();
  32. p.x -= o.x;
  33. p.y -= o.y;
  34. return p;
  35. },
  36. div: function(n) {
  37. p = this.clone();
  38. p.x /= n;
  39. p.y /= n;
  40. return p;
  41. },
  42. mul: function(n) {
  43. p = this.clone();
  44. p.x *= n;
  45. p.y *= n;
  46. return p;
  47. }
  48. }
  49. Heart = function() {
  50. // x = 16 sin^3 t
  51. // y = 13 cos t - 5 cos 2t - 2 cos 3t - cos 4t
  52. // http://www.wolframalpha.com/input/?i=x+%3D+16+sin%5E3+t%2C+y+%3D+(13+cos+t+-+5+cos+2t+-+2+cos+3t+-+cos+4t)
  53. var points = [], x, y, t;
  54. for (var i = 10; i < 30; i += 0.2) {
  55. t = i / Math.PI;
  56. x = 16 * Math.pow(Math.sin(t), 3);
  57. y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);
  58. points.push(new Point(x, y));
  59. }
  60. this.points = points;
  61. this.length = points.length;
  62. }
  63. Heart.prototype = {
  64. get: function(i, scale) {
  65. return this.points[i].mul(scale || 1);
  66. }
  67. }
  68. Seed = function(tree, point, scale, color) {
  69. this.tree = tree;
  70. var scale = scale || 1
  71. var color = color || '#FF0000';
  72. this.heart = {
  73. point : point,
  74. scale : scale,
  75. color : color,
  76. figure : new Heart(),
  77. }
  78. this.cirle = {
  79. point : point,
  80. scale : scale,
  81. color : color,
  82. radius : 5,
  83. }
  84. }
  85. Seed.prototype = {
  86. draw: function() {
  87. this.drawHeart();
  88. this.drawText();
  89. },
  90. addPosition: function(x, y) {
  91. this.cirle.point = this.cirle.point.add(new Point(x, y));
  92. },
  93. canMove: function() {
  94. return this.cirle.point.y < (this.tree.height + 20);
  95. },
  96. move: function(x, y) {
  97. this.clear();
  98. this.drawCirle();
  99. this.addPosition(x, y);
  100. },
  101. canScale: function() {
  102. return this.heart.scale > 0.2;
  103. },
  104. setHeartScale: function(scale) {
  105. this.heart.scale *= scale;
  106. },
  107. scale: function(scale) {
  108. this.clear();
  109. this.drawCirle();
  110. this.drawHeart();
  111. this.setHeartScale(scale);
  112. },
  113. drawHeart: function() {
  114. var ctx = this.tree.ctx, heart = this.heart;
  115. var point = heart.point, color = heart.color,
  116. scale = heart.scale;
  117. ctx.save();
  118. ctx.fillStyle = color;
  119. ctx.translate(point.x, point.y);
  120. ctx.beginPath();
  121. ctx.moveTo(0, 0);
  122. for (var i = 0; i < heart.figure.length; i++) {
  123. var p = heart.figure.get(i, scale);
  124. ctx.lineTo(p.x, -p.y);
  125. }
  126. ctx.closePath();
  127. ctx.fill();
  128. ctx.restore();
  129. },
  130. drawCirle: function() {
  131. var ctx = this.tree.ctx, cirle = this.cirle;
  132. var point = cirle.point, color = cirle.color,
  133. scale = cirle.scale, radius = cirle.radius;
  134. ctx.save();
  135. ctx.fillStyle = color;
  136. ctx.translate(point.x, point.y);
  137. ctx.scale(scale, scale);
  138. ctx.beginPath();
  139. ctx.moveTo(0, 0);
  140. ctx.arc(0, 0, radius, 0, 2 * Math.PI);
  141. ctx.closePath();
  142. ctx.fill();
  143. ctx.restore();
  144. },
  145. drawText: function() {
  146. var ctx = this.tree.ctx, heart = this.heart;
  147. var point = heart.point, color = heart.color,
  148. scale = heart.scale;
  149. ctx.save();
  150. ctx.strokeStyle = color;
  151. ctx.fillStyle = color;
  152. ctx.translate(point.x, point.y);
  153. ctx.scale(scale, scale);
  154. ctx.moveTo(0, 0);
  155. ctx.lineTo(15, 15);
  156. ctx.lineTo(60, 15);
  157. ctx.stroke();
  158. ctx.moveTo(0, 0);
  159. ctx.scale(0.75, 0.75);
  160. ctx.font = "12px 微软雅黑,Verdana"; // 字号肿么没有用? (ˉ(∞)ˉ)
  161. ctx.fillText("Come Baby", 23, 10);
  162. ctx.restore();
  163. },
  164. clear: function() {
  165. var ctx = this.tree.ctx, cirle = this.cirle;
  166. var point = cirle.point, scale = cirle.scale, radius = 26;
  167. var w = h = (radius * scale);
  168. ctx.clearRect(point.x - w, point.y - h, 4 * w, 4 * h);
  169. },
  170. hover: function(x, y) {
  171. var ctx = this.tree.ctx;
  172. var pixel = ctx.getImageData(x, y, 1, 1);
  173. return pixel.data[3] == 255
  174. }
  175. }
  176. Footer = function(tree, width, height, speed) {
  177. this.tree = tree;
  178. this.point = new Point(tree.seed.heart.point.x, tree.height - height / 2);
  179. this.width = width;
  180. this.height = height;
  181. this.speed = speed || 2;
  182. this.length = 0;
  183. }
  184. Footer.prototype = {
  185. draw: function() {
  186. var ctx = this.tree.ctx, point = this.point;
  187. var len = this.length / 2;
  188. ctx.save();
  189. ctx.strokeStyle = 'rgb(35, 31, 32)';
  190. ctx.lineWidth = this.height;
  191. ctx.lineCap = 'round';
  192. ctx.lineJoin = 'round';
  193. ctx.translate(point.x, point.y);
  194. ctx.beginPath();
  195. ctx.moveTo(0, 0);
  196. ctx.lineTo(len, 0);
  197. ctx.lineTo(-len, 0);
  198. ctx.stroke();
  199. ctx.restore();
  200. if (this.length < this.width) {
  201. this.length += this.speed;
  202. }
  203. }
  204. }
  205. Tree = function(canvas, width, height, opt) {
  206. this.canvas = canvas;
  207. this.ctx = canvas.getContext('2d');
  208. this.width = width;
  209. this.height = height;
  210. this.opt = opt || {};
  211. this.record = {};
  212. this.initSeed();
  213. this.initFooter();
  214. this.initBranch();
  215. this.initBloom();
  216. }
  217. Tree.prototype = {
  218. initSeed: function() {
  219. var seed = this.opt.seed || {};
  220. var x = seed.x || this.width / 2;
  221. var y = seed.y || this.height / 2;
  222. var point = new Point(x, y);
  223. var color = seed.color || '#FF0000';
  224. var scale = seed.scale || 1;
  225. this.seed = new Seed(this, point, scale, color);
  226. },
  227. initFooter: function() {
  228. var footer = this.opt.footer || {};
  229. var width = footer.width || this.width;
  230. var height = footer.height || 5;
  231. var speed = footer.speed || 2;
  232. this.footer = new Footer(this, width, height, speed);
  233. },
  234. initBranch: function() {
  235. var branchs = this.opt.branch || []
  236. this.branchs = [];

css代码部分

  1. -->
  2. </style>
  3. </head>
  4. <body>
  5. <audio autoplay="autopaly">
  6. <source src="renxi.mp3" type="audio/mp3" />
  7. </audio>
  8. <div id="main">
  9. <div id="error">(<a href="http://www.google.cn/chrome/intl/zh-CN/landing_chrome.html?hl=zh-CN&brand=CHMI">Chrome</a>)或者火狐(<a href="http://firefox.com.cn/download/">Firefox</a>)浏览器,或者其他游览器的最新版本。</div>
  10. <div id="wrap">
  11. <div id="text">
  12. <div id="code"> <font color="#FF0000"> <span class="say"></span><br>
  13. <span class="say"> </span><br>
  14. <span class="say"> 我知道我不会甜言蜜语,但是我会用行动证明一切</span><br>
  15. <span class="say"> </span><br>
  16. <span class="say"></span><br>
  17. <span class="say"></span><br>
  18. <span class="say"></span><br>
  19. <span class="say">来</span><br>
  20. <span class="say"> </span><br>
  21. <span class="say"><span class="space"></span></span> </font>
  22. </p>
  23. </div>
  24. </div>
  25. <div id="clock-box"> <span class="STYLE1"></span><font color="#33CC00"></font> <span class="STYLE1">已经是……</span>
  26. <div id="clock"></div>
  27. </div>
  28. <canvas id="canvas" width="1100" height="680"></canvas>
  29. </div>
  30. </div>
  31. <script>
  32. </script>
  33. <script>
  34. (function(){
  35. var canvas = $('#canvas');
  36. if (!canvas[0].getContext) {
  37. $("#error").show();
  38. return false; }
  39. var width = canvas.width();
  40. var height = canvas.height();
  41. canvas.attr("width", width);
  42. canvas.attr("height", height);
  43. var opts = {
  44. seed: {
  45. x: width / 2 - 20,
  46. color: "rgb(190, 26, 37)",
  47. scale: 2
  48. },
  49. branch: [
  50. [535, 680, 570, 250, 500, 200, 30, 100, [
  51. [540, 500, 455, 417, 340, 400, 13, 100, [
  52. [450, 435, 434, 430, 394, 395, 2, 40]
  53. ]],
  54. [550, 445, 600, 356, 680, 345, 12, 100, [
  55. [578, 400, 648, 409, 661, 426, 3, 80]
  56. ]],
  57. [539, 281, 537, 248, 534, 217, 3, 40],
  58. [546, 397, 413, 247, 328, 244, 9, 80, [
  59. [427, 286, 383, 253, 371, 205, 2, 40],
  60. [498, 345, 435, 315, 395, 330, 4, 60]
  61. ]],
  62. [546, 357, 608, 252, 678, 221, 6, 100, [
  63. [590, 293, 646, 277, 648, 271, 2, 80]
  64. ]]
  65. ]]
  66. ],
  67. bloom: {
  68. num: 700,
  69. width: 1080,
  70. height: 650,
  71. },
  72. footer: {
  73. width: 1200,
  74. height: 5,
  75. speed: 10,
  76. }
  77. }
  78. var tree = new Tree(canvas[0], width, height, opts);
  79. var seed = tree.seed;
  80. var foot = tree.footer;
  81. var hold = 1;
  82. canvas.click(function(e) {
  83. var offset = canvas.offset(), x, y;
  84. x = e.pageX - offset.left;
  85. y = e.pageY - offset.top;
  86. if (seed.hover(x, y)) {
  87. hold = 0;
  88. canvas.unbind("click");
  89. canvas.unbind("mousemove");
  90. canvas.removeClass('hand');
  91. }
  92. }).mousemove(function(e){
  93. var offset = canvas.offset(), x, y;
  94. x = e.pageX - offset.left;
  95. y = e.pageY - offset.top;
  96. canvas.toggleClass('hand', seed.hover(x, y));
  97. });
  98. var seedAnimate = eval(Jscex.compile("async", function () {
  99. seed.draw();
  100. while (hold) {
  101. $await(Jscex.Async.sleep(10));
  102. }
  103. while (seed.canScale()) {
  104. seed.scale(0.95);
  105. $await(Jscex.Async.sleep(10));
  106. }
  107. while (seed.canMove()) {
  108. seed.move(0, 2);
  109. foot.draw();
  110. $await(Jscex.Async.sleep(10));
  111. }
  112. }));
  113. var growAnimate = eval(Jscex.compile("async", function () {
  114. do {
  115. tree.grow();
  116. $await(Jscex.Async.sleep(10));
  117. } while (tree.canGrow());
  118. }));
  119. var flowAnimate = eval(Jscex.compile("async", function () {
  120. do {
  121. tree.flower(2);
  122. $await(Jscex.Async.sleep(10));
  123. } while (tree.canFlower());
  124. }));
  125. var moveAnimate = eval(Jscex.compile("async", function () {
  126. tree.snapshot("p1", 240, 0, 610, 680);
  127. while (tree.move("p1", 500, 0)) {
  128. foot.draw();
  129. $await(Jscex.Async.sleep(10));
  130. }
  131. foot.draw();
  132. tree.snapshot("p2", 500, 0, 610, 680);
  133. // 会有闪烁不得意这样做, (>﹏<)
  134. canvas.parent().css("background", "url(" + tree.toDataURL('image/png') + ")");
  135. canvas.css("background", "#ffe");
  136. $await(Jscex.Async.sleep(300));
  137. canvas.css("background", "none");
  138. }));
  139. var jumpAnimate = eval(Jscex.compile("async", function () {
  140. var ctx = tree.ctx;
  141. while (true) {
  142. tree.ctx.clearRect(0, 0, width, height);
  143. tree.jump();
  144. foot.draw();
  145. $await(Jscex.Async.sleep(25));
  146. }
  147. }));
  148. var textAnimate = eval(Jscex.compile("async", function () {
  149. var together = new Date();
  150. together.setFullYear(2010,1 , 15); //时间年月日
  151. together.setHours(16); //小时
  152. together.setMinutes(53); //分钟
  153. together.setSeconds(0); //秒前一位
  154. together.setMilliseconds(2); //秒第二位
  155. $("#code").show().typewriter();
  156. $("#clock-box").fadeIn(500);
  157. while (true) {
  158. timeElapse(together);
  159. $await(Jscex.Async.sleep(1000));
  160. }
  161. }));//全部代码在企鹅裙311075050
  162. var runAsync = eval(Jscex.compile("async", function () {
  163. $await(seedAnimate());

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

闽ICP备14008679号