• js模拟制作超逼真的雪花效果


    《春雪》

    唐·东方虬

    春雪满空来,触处似花开。

    不知园里树,若个是真梅。


    《红林擒近·寿词·满路花》

    宋·陈允平

    三万六千顷,玉壶天地寒。庾岭封的皪,淇园折琅。漠漠梨花烂漫,纷纷柳絮飞残。直疑潢潦惊翻,斜风溯狂澜。

    对此频胜赏,一醉饱清欢。呼童翦韭,和冰先荐春盘。怕东风吹散,留尊待月,倚阑莫惜今夜看。

    在陈允平笔下,雪是绝美的。

    人生当如这漫天的飞雪,肆意地飘洒。这是我对人生的感悟。

    今天我们用js来模拟雪,让雪花飞扬在代码的世界,因为喜欢雪,我收集了一些关于雪的代码。

    js制作的透明超炫酷雪花效果 (mubanmao.top)


    js制作的漂亮雪花图案

    js制作的唯美温柔落雪效果

    js制作的超唯美雪花飘落效果

    雪舞飞花,醉人如痴!

    代码欣赏:

    1. var G1Code;
    2. (function() {
    3. G1Code = function(x, y) {
    4. var self = this;
    5. self.x = x;
    6. self.y = y;
    7. };
    8. var members = {
    9. toString: function() {
    10. var self = this;
    11. // Added rounding
    12. return "G1 X" + self.x.toFixed(2) + " Y" + self.y.toFixed(2);
    13. },
    14. // Rotate the XY point of the GCode
    15. rotate: function(theta) {
    16. var self = this;
    17. var oldX = self.x;
    18. var oldY = self.y;
    19. self.x = oldX * Math.cos(theta) - oldY * Math.sin(theta);
    20. self.y = oldX * Math.sin(theta) + oldY * Math.cos(theta);
    21. },
    22. // Add relative moves
    23. relative_move: function(xMove, yMove) {
    24. var self = this;
    25. var oldX = self.x;
    26. var oldY = self.y;
    27. self.x = oldX + xMove;
    28. self.y = oldY + yMove;
    29. },
    30. // Clone Method
    31. clone: function() {
    32. var self = this;
    33. return new G1Code(self.x, self.y);
    34. }
    35. }
    36. // Copy over members to prototype
    37. for (var key in members) {
    38. G1Code.prototype[key] = members[key];
    39. };
    40. })();
    41. var PolyLine;
    42. (function() {
    43. PolyLine = function() {
    44. this.listofcodes = [];
    45. };
    46. var members = {
    47. toString: function() {
    48. var self = this;
    49. var output = "";
    50. for(gcode in self.listofcodes) {
    51. output += self.listofcodes[gcode] + "\n"
    52. }
    53. return output;
    54. },
    55. draw: function(ctx) {
    56. var self = this;
    57. ctx.beginPath();
    58. var code = self.listofcodes[0];
    59. ctx.moveTo(code.x, code.y);
    60. for(var n=1; n < self.listofcodes.length; n++) {
    61. code = self.listofcodes[n];
    62. ctx.lineTo(code.x, code.y);
    63. }
    64. ctx.closePath();
    65. },
    66. // add a single G1Code to the list
    67. append: function(gcode) {
    68. var self = this;
    69. self.listofcodes.push(gcode);
    70. },
    71. // add another PolyLine to the end of this PolyLine
    72. extend: function(polyline) {
    73. var self = this;
    74. for(gcode in polyline.listofcodes) {
    75. self.listofcodes.push(polyline.listofcodes[gcode].clone());
    76. }
    77. },
    78. // method to make a clone of the myPolyLine
    79. clone: function() {
    80. var self = this;
    81. var cloned = new PolyLine();
    82. for(gcode in self.listofcodes) {
    83. cloned.append(self.listofcodes[gcode].clone());
    84. }
    85. return cloned;
    86. },
    87. // rotate each individual G1Code within
    88. rotate: function(angle) {
    89. var self = this;
    90. for(gcode in self.listofcodes) {
    91. self.listofcodes[gcode].rotate(angle);
    92. }
    93. },
    94. // mirror the list of G1Codes around the x axis
    95. // this may be a counter-intuitive name - rename to mirrorY?
    96. mirrorX: function() {
    97. var self = this;
    98. for(gcode in self.listofcodes) {
    99. self.listofcodes[gcode].y = -1*(self.listofcodes[gcode].y);
    100. }
    101. },
    102. // reverse the order of the list of G1Codes
    103. reverse: function() {
    104. var self = this;
    105. self.listofcodes.reverse();
    106. }
    107. };
    108. // Copy over members to prototype
    109. for (var key in members) {
    110. PolyLine.prototype[key] = members[key];
    111. };
    112. })();
    113. var Snowflake;
    114. (function() {
    115. Snowflake = function(options) {
    116. this.options = {
    117. /** {Integer} Number of arms of the snowflake */
    118. numArms: 6,
    119. /** {Integer} Length of arms of the snowflake */
    120. armLength: 100,
    121. /** {Integer} Thickness of arms of the snowflake */
    122. armThickness: 3,
    123. /** {Integer} Number of spikes on the arms of the snowflake */
    124. numSpikes: 4,
    125. /** {Number} */
    126. spacer: 0.5
    127. };
    128. for (var key in options) {
    129. this.options[key] = options[key];
    130. }
    131. this.__gapSize = (this.options.armLength/this.options.numSpikes)/2;
    132. };
    133. /*
    134. ---------------------------------------------------------------------------
    135. PRIVATE FUNCTIONS
    136. ---------------------------------------------------------------------------
    137. */
    138. /**
    139. * Generates a random integer that is between two integers.
    140. *
    141. * @param from {Integer} The integer to start from.
    142. * @param to {Integer} The integer to end with.
    143. *
    144. * @return {Integer} Random integer in between the two given integers.
    145. **/
    146. function randomInt(from, to) {
    147. return Math.floor(Math.random() * (to - from + 1) + from);
    148. };
    149. /**
    150. * @param degrees {Number} Angle in degrees
    151. *
    152. * @return {Number} Angle in radians
    153. **/
    154. function radians(degrees) {
    155. return degrees*(Math.PI/180);
    156. };
    157. var members = {
    158. /*
    159. ---------------------------------------------------------------------------
    160. INTERNAL FIELDS ::
    161. ---------------------------------------------------------------------------
    162. */
    163. /** {Number} Set in constructor */
    164. __gapSize: 0,
    165. /*
    166. ---------------------------------------------------------------------------
    167. PUBLIC API
    168. ---------------------------------------------------------------------------
    169. */
    170. /**
    171. * Draws the snowflake
    172. *
    173. * @param ctx {2D Context} The 2D graphics context from a canvas element.
    174. */
    175. draw: function(ctx) {
    176. var self = this;
    177. var spikyArm = new PolyLine(),
    178. thisGCode = new G1Code(self.options.armThickness, self.options.armThickness/2);
    179. spikyArm.append(thisGCode);
    180. var angle = radians(30);
    181. for(var n=0; n < self.options.numSpikes; n++) {
    182. var spikeLength = Math.random()*(self.options.armLength/2),
    183. // spikeLength = arm_length/2.0,
    184. x1 = self.options.spacer + self.__gapSize*(n*2),
    185. y1 = self.options.armThickness/2,
    186. x2 = self.options.spacer + x1 + spikeLength*Math.cos(angle),
    187. y2 = spikeLength*Math.sin(angle),
    188. x3 = self.options.spacer + x1 + self.__gapSize,
    189. y3 = self.options.armThickness/2;
    190. spikyArm.append(new G1Code(x1, y1));
    191. spikyArm.append(new G1Code(x2, y2));
    192. spikyArm.append(new G1Code(x3, y3));
    193. };
    194. thisGCode = new G1Code(self.options.armLength, self.options.armThickness/2);
    195. spikyArm.append(thisGCode);
    196. // make a mirror image of the first half of the arm
    197. otherHalf = spikyArm.clone();
    198. otherHalf.mirrorX();
    199. otherHalf.reverse();
    200. // make a pointy tip
    201. thisGCode = new G1Code(self.options.armLength+(self.options.armLength/10), 0);
    202. // join em together
    203. spikyArm.append(thisGCode);
    204. spikyArm.extend(otherHalf);
    205. // join together 6 rotated copies of the spiky arm
    206. var thisGCodeStar = new PolyLine();
    207. for(var a=0; a < self.options.numArms; a++) {
    208. spikyArm.rotate(radians(-(360/self.options.numArms)));
    209. thisGCodeStar.extend(spikyArm);
    210. }
    211. thisGCodeStar.draw(ctx);
    212. }
    213. }
    214. // Copy over members to prototype
    215. for (var key in members) {
    216. Snowflake.prototype[key] = members[key];
    217. }
    218. })();

  • 相关阅读:
    List 集合的一些常用操作
    jdbc连接数据库
    【中兴】web训练营~一文带你走进前端 百图制作
    php——laravel缓存cache
    Oracle 数据库集群常用巡检命令
    openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
    HC-SR505人体红外报警系统
    Spring Cloud - 通过 Gateway webflux 编程实现网关异常处理
    基于人工智能标记语言 (AIML)和任务型对话系统(Task)的深度智能对话机器人demo
    Spring Cloud Alibaba微服务第28章之Harbor安装以及镜像推送
  • 原文地址:https://blog.csdn.net/kongzhonghu/article/details/126621307