情人节你们过,表白代码我来写(HTML+CSS+JS)

x33g5p2x  于2022-02-14 转载在 HTML5  
字(6.1k)|赞(0)|评价(0)|浏览(458)

雷迪森安的乡亲们,欢迎来到老实人的前端课堂,这情人节的,我把爱心都给你们准备好了,今天我们来写个爱心吧。

正片

注意看到最后哦,下面还有一个更好看的特效,这个是个动态的♥♥

结构就两行

  1. <script src="https://wow.techbrood.com/libs/jquery/jquery-1.11.1.min.js"></script>
  2. <canvas id="canvas"></canvas>

样式

以下样式大部分是js代码渲染上dom后表现出来的

  1. body {
  2. margin: 0;
  3. font-family: Helvetica, Arial;
  4. font-weight: 100;
  5. overflow: hidden;
  6. }
  7. canvas {
  8. background-color: rgb(244, 244, 244);
  9. }
  10. footer {
  11. position: fixed;
  12. bottom: 0;
  13. width: 100%;
  14. }
  15. footer img {
  16. border-radius: 100%;
  17. width: 30px;
  18. height: 30px;
  19. margin-left: 10px;
  20. }
  21. footer span {
  22. display: block;
  23. padding: 10px;
  24. display: flex;
  25. justify-content: flex-end;
  26. align-items: center;
  27. }

表现

关键代码,满满注释哦,这我可够意思了哈,不懂的话再私下问我吧。

  1. //---------- MAIN FUNCTION ----------
  2. function init() {
  3. renderable = [];
  4. path = new Shape(new Heart(), new Point(cw / 2, ch / 2), new Color(244, 244, 244, 1), {
  5. scale: 10
  6. });
  7. animate();
  8. }
  9. function animate() {
  10. addShape(angle);
  11. addShape(angle2);
  12. angle += 0.05;
  13. angle2 -= 0.05;
  14. ctx.clearRect(0, 0, cw, ch);
  15. for (var i = 0; i < renderable.length; i++) {
  16. renderable[i].draw(canvas);
  17. if (renderable[i].scale <= 0) renderable.splice(i, 1);
  18. }
  19. path.draw(canvas);
  20. requestAnimationFrame(animate);
  21. }
  22. function addShape(angle) {
  23. renderable.push(new Shape(new Circle(5), new Point(cw / 2, ch / 2), new Color(253, 192, 192, 1), {
  24. position: (((path.geometry.getPositionByAngle(angle)).multiply(path.scale)).add(path.position.clone())),
  25. decay: 0.1,
  26. scale: 3
  27. }));
  28. }
  29. //-----------------------------------
  30. var canvas, ctx, cw, ch;
  31. var FPS = 60;
  32. var renderable = [],
  33. path,
  34. path2,
  35. angle = 0,
  36. angle2 = 0,
  37. color;
  38. var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
  39. return setTimeout(callback, FPS);
  40. };
  41. window.onload = function() {
  42. initCanvas();
  43. ctx.clearRect(0, 0, cw, ch);
  44. init();
  45. }
  46. window.onresize = function() {
  47. initCanvas();
  48. ctx.clearRect(0, 0, cw, ch);
  49. };
  50. function initCanvas() {
  51. canvas = document.getElementById('canvas');
  52. ctx = canvas.getContext('2d');
  53. cw = window.innerWidth;
  54. ch = window.innerHeight;
  55. canvas.width = cw;
  56. canvas.height = ch;
  57. }
  58. //---------------- APP UTIL ------------------
  59. function getRandomNumber(min, max) {
  60. return Math.random() * (max - min + 1) + min;
  61. }
  62. //---------- EXTRACT OF MY NEXT FRAMEWORK ------------
  63. //----------- PROTOTYPES -------------
  64. //===== GEOMETRY =====
  65. //----- SHAPE -----
  66. function Shape(geometry, position, color, properties) {
  67. this.position = (position == null || position.classname != "Point") ? new Point() : position;
  68. this.geometry = (geometry == null || geometry.constructor.name != "GenericObject") ? new Circle() : geometry;
  69. this.color = (color == null || color.classname != "Color") ? null : color;
  70. this.lineColor = null;
  71. this.lineWidth = 1;
  72. this.scale = 1;
  73. this.decay = 0;
  74. if (properties != null) this.setProperties(properties);
  75. GenericObject.call(this, "Shape");
  76. }
  77. Shape.prototype = new GenericObject();
  78. Shape.prototype.setProperties = function(properties) {
  79. for (var p in properties) {
  80. this[p] = properties[p];
  81. }
  82. }
  83. Shape.prototype.draw = function(canvas) {
  84. if (this.scale > 0) {
  85. var ctx = canvas.getContext('2d');
  86. var cw = canvas.width;
  87. var ch = canvas.height;
  88. ctx.beginPath();
  89. if (this.lineWidth > 0 && (this.lineColor != null && this.lineColor.classname == "Color")) {
  90. ctx.strokeStyle = this.lineColor.getRGBA();
  91. ctx.lineWidth = this.lineWidth;
  92. }
  93. if (this.color != null && this.color.classname == "Color") {
  94. ctx.fillStyle = this.color.getRGBA();
  95. }
  96. switch (this.geometry.classname) {
  97. case "Circle":
  98. ctx.arc(this.position.x, this.position.y, this.geometry.radius * this.scale, 0, Math.PI * 2);
  99. break;
  100. case "Heart":
  101. for (var i = 0; i < Math.PI * 2; i += 0.05) {
  102. var p = this.geometry.getPositionByAngle(i);
  103. p.multiply(this.scale);
  104. p.add(this.position);
  105. if (i == 0) ctx.moveTo(p.x, p.y);
  106. else ctx.lineTo(p.x, p.y);
  107. }
  108. break;
  109. }
  110. if (this.lineColor != null && this.lineColor.classname == "Color") ctx.stroke();
  111. if (this.color != null) ctx.fill();
  112. this.scale -= this.decay;
  113. }
  114. }
  115. //----- POINT -----
  116. function Point(x, y) {
  117. this.x = (x != null && !isNaN(x)) ? x : 0;
  118. this.y = (y != null && !isNaN(y)) ? y : 0;
  119. GenericObject.call(this, "Point");
  120. }
  121. Point.prototype = new GenericObject();
  122. Point.prototype.add = function(p2) {
  123. if (p2.classname != this.classname) return null;
  124. this.x += p2.x;
  125. this.y += p2.y;
  126. return this;
  127. }
  128. Point.prototype.multiply = function(scale) {
  129. this.x *= scale;
  130. this.y *= scale;
  131. return this;
  132. }
  133. //----- CIRCLE -----
  134. function Circle(radius) {
  135. this.center = new Point();
  136. this.radius = (radius != null && !isNaN(radius)) ? radius : 1;
  137. GenericObject.call(this, "Circle");
  138. }
  139. Circle.prototype = new GenericObject();
  140. //----- HEART -----
  141. function Heart(scale) {
  142. this.scale = (scale != null && !isNaN(scale)) ? scale : 1;
  143. GenericObject.call(this, "Heart");
  144. }
  145. Heart.prototype = new GenericObject();
  146. Heart.prototype.getPositionByAngle = function(angle) {
  147. if (angle == null || isNaN(angle)) return null;
  148. var x = 16 * Math.pow(Math.sin(angle), 3);
  149. var y = -(13 * Math.cos(angle) - 5 * Math.cos(2 * angle) - 2 * Math.cos(3 * angle) - Math.cos(4 * angle));
  150. return new Point(x * this.scale, y * this.scale);
  151. }
  152. //===== COLOR =====
  153. function Color(r, g, b, a) {
  154. this.r = (r != null || isNaN(r)) ? r : 0;
  155. this.g = (g != null || isNaN(g)) ? g : 0;
  156. this.b = (b != null || isNaN(b)) ? b : 0;
  157. this.a = (a != null || isNaN(a)) ? a : 1;
  158. this.hex = this.toHex();
  159. GenericObject.call(this, "Color");
  160. }
  161. Color.prototype = new GenericObject();
  162. Color.prototype.toHex = function() {
  163. var bin = this.r << 16 | this.g << 8 | this.b;
  164. return (function(h) {
  165. return "#" + new Array(7 - h.length).join("0") + h
  166. })(bin.toString(16).toUpperCase())
  167. }
  168. Color.prototype.getRGBA = function() {
  169. return "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
  170. }
  171. Color.prototype.setHex = function(hex) {
  172. this.r = hex >> 16;
  173. this.g = hex >> 8 & 0xFF;
  174. this.b = hex & 0xFF;
  175. this.hex = this.toHex();
  176. }
  177. //===== GENERICOBJECT =====
  178. function GenericObject(name) {
  179. this.classname = name;
  180. }
  181. GenericObject.prototype.clone = function() {
  182. var copy = new GenericObject(this.classname);
  183. for (var attr in this) {
  184. if (this.hasOwnProperty(attr)) {
  185. if (this[attr].constructor.name == "GenericObject") copy[attr] = this[attr].clone();
  186. else copy[attr] = this[attr];
  187. }
  188. }
  189. return copy;
  190. }

不学没思路,看完是不是感觉也不难啊?你做出来了吗?

还有下面这个情人节代码我就不写了,更多表白代码可以在下方公z号获取~

做好的网页效果,如何通过发链接给别人看?

1.1解决部署上线~> 部署上线工具(永久免费使用)

1.不需要买服务器就能部署线上,全世界都能访问你的连接啦, 这里给大家推荐一个程序员必备神器~ 简单易懂, 简直神器 ~ 需要可在下方公z号:前端老实人,获取

2.就是把你的代码效果做好了以后, 部署到线上, 把链接发给别人, 就可以让对方通过你的连接点击进去, 就能看到你的网页效果啦, 电脑端和手机端都可以噢! (不然别人看你的网页都要发文件过去,体验感不太好~)

最后

博主为人老实,无偿解答问题,也会录制一些学习视频教同学们知识❤

如果对您有帮助,希望能给个👍/评论/收藏

您的三连~是对我创作最大的动力支持

关注:前端老实人👇领取源码哦~

相关文章

最新文章

更多