yanhua.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. (function ($) {
  2. $.fn.fireworks = function (options) {
  3. options = options || {};
  4. options.width = options.width || $(this).width();
  5. options.height = options.height || $(this).height();
  6. var fireworksField = this,
  7. SCREEN_WIDTH = options.width,
  8. SCREEN_HEIGHT = options.height,
  9. rockets = [],
  10. particles = [];
  11. var canvas = document.createElement("canvas");
  12. canvas.style.width = SCREEN_WIDTH + 'px';
  13. canvas.style.height = SCREEN_HEIGHT + 'px';
  14. canvas.width = SCREEN_WIDTH;
  15. canvas.height = SCREEN_HEIGHT;
  16. canvas.style.position = 'absolute';
  17. canvas.style.top = '0px';
  18. canvas.style.left = '0px';
  19. var context = canvas.getContext('2d');
  20. var mousePos = {};
  21. var gravity = 0.05;
  22. var raf;
  23. $(fireworksField).append(canvas);
  24. //document.onmousemove = mouseMove;
  25. setInterval(launch, 1000);
  26. setInterval(function () {
  27. mousePos = {
  28. x: Math.random() * SCREEN_WIDTH,
  29. y: Math.random() * SCREEN_HEIGHT / 2
  30. }
  31. }, 1000);
  32. //setInterval(loop, 20);
  33. raf = window.requestAnimationFrame(loop);
  34. //function mouseMove(ev) {
  35. // ev = ev || window.event;
  36. // mousePos = mousePosition(ev);
  37. //}
  38. //function mousePosition(ev) {
  39. // if (ev.pageX || ev.pageY) {
  40. // return { x: ev.pageX, y: ev.pageY };
  41. // }
  42. // return {
  43. // x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
  44. // y: ev.clientY + document.body.scrollTop - document.body.clientTop
  45. // };
  46. //}
  47. function launch() {
  48. if (rockets.length < 10) {
  49. var rocket = new Rocket(SCREEN_WIDTH / 2);
  50. rocket.v.x = Math.random() * 6 - 3;
  51. rocket.v.y = Math.random() * -30 - 4;
  52. rocket.color = Math.floor(Math.random() * 360 / 10) * 10;
  53. rockets.push(rocket);
  54. }
  55. }
  56. function loop() {
  57. var existRockets = [];
  58. var existParticles = [];
  59. if (SCREEN_WIDTH != window.innerWidth) {
  60. canvas.width = SCREEN_WIDTH = window.innerWidth;
  61. }
  62. if (SCREEN_HEIGHT != window.innerHeight) {
  63. canvas.height = SCREEN_HEIGHT = window.innerHeight;
  64. }
  65. context.fillStyle = "rgba(0,0,0,0.02)";
  66. context.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  67. //context.save();
  68. for (var i = 0; i < rockets.length; i++) {
  69. rockets[i].update();
  70. rockets[i].render(context);
  71. //explosion rules
  72. //1.above the 4/5 screen
  73. //2.close to the mouse
  74. //3.1% random chance above the 1/2 screen
  75. var distance = Math.sqrt(Math.pow(mousePos.x - rockets[i].pos.x, 2) + Math.pow(mousePos.y - rockets[i].pos.y, 2));
  76. var randomChance = (rockets[i].pos.y < SCREEN_HEIGHT / 2) ? (Math.random() * 100 <= 1) : false;
  77. if (rockets[i].pos.y < SCREEN_HEIGHT / 5 || distance <= 50 || randomChance) {
  78. rockets[i].explode();
  79. } else {
  80. existRockets.push(rockets[i]);
  81. }
  82. }
  83. rockets = existRockets;
  84. for (var i = 0; i < particles.length; i++) {
  85. particles[i].update();
  86. if (particles[i].exist()) {
  87. particles[i].render(context);
  88. existParticles.push(particles[i]);
  89. }
  90. }
  91. particles = existParticles;
  92. //context.restore();
  93. raf = window.requestAnimationFrame(loop);
  94. }
  95. //the particles object
  96. function Particle(pos) {
  97. this.pos = {
  98. x: pos ? pos.x : 0,
  99. y: pos ? pos.y : 0
  100. };
  101. this.v = {
  102. x: 0,
  103. y: 0
  104. };
  105. this.resistance = 0.005;
  106. this.size = 5;
  107. this.shrink = 0.99;
  108. this.alpha = 1;
  109. this.fade = 0.97;
  110. this.color = "";
  111. }
  112. Particle.prototype.update = function () {
  113. this.v.x += this.resistance;
  114. this.v.y -= this.resistance;
  115. //gravity
  116. this.v.y += gravity;
  117. this.pos.x += this.v.x;
  118. this.pos.y += this.v.y;
  119. this.size *= this.shrink;
  120. this.alpha *= this.fade;
  121. }
  122. Particle.prototype.render = function () {
  123. var x = this.pos.x,
  124. y = this.pos.y,
  125. r = this.size / 2;
  126. var gradient = context.createRadialGradient(x, y, 0.1, x, y, r);
  127. gradient.addColorStop(0, "rgba(255, 255, 255, " + this.alpha + ")");
  128. gradient.addColorStop(0.9, "hsla(" + this.color + ", 100%, 50%, 1)");
  129. gradient.addColorStop(1, "rgba(0, 0, 0, 0)");
  130. context.fillStyle = gradient;
  131. context.beginPath();
  132. context.arc(x, y, r, 0, 2 * Math.PI, true);
  133. context.closePath();
  134. context.fill();
  135. }
  136. Particle.prototype.exist = function () {
  137. if (this.alpha >= 0.02 && this.size > 0.8) {
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. //the rocket object
  144. function Rocket(x) {
  145. Particle.apply(this, [{
  146. x: x,
  147. y: SCREEN_HEIGHT
  148. }]);
  149. }
  150. (function () {
  151. var Super = function () { };
  152. Super.prototype = Particle.prototype;
  153. Rocket.prototype = new Super();
  154. })();
  155. Rocket.prototype.constructor = Rocket;
  156. Rocket.prototype.update = function () {
  157. this.pos.x += this.v.x;
  158. this.pos.y += this.v.y;
  159. }
  160. Rocket.prototype.render = function (context) {
  161. var x = this.pos.x,
  162. y = this.pos.y,
  163. r = this.size / 2;
  164. var gradient = context.createRadialGradient(x, y, 0.1, x, y, r);
  165. gradient.addColorStop(0, "#ffff00");
  166. gradient.addColorStop(1, "rgba(0, 0, 0, 0.01)");
  167. context.fillStyle = gradient;
  168. context.beginPath();
  169. context.arc(x, y, r, 0, 2 * Math.PI, true);
  170. context.closePath();
  171. context.fill();
  172. }
  173. Rocket.prototype.explode = function () {
  174. var count = getGaussianDistributionNumber(240, 20);
  175. for (var i = 0; i <= count; i++) {
  176. var particle = new Particle(this.pos);
  177. var angle = Math.random() * Math.PI * 2;
  178. var speed = getGaussianDistributionNumber(2.5, 0.3);
  179. particle.size = 3;
  180. particle.v.x = Math.cos(angle) * speed;
  181. particle.v.y = Math.sin(angle) * speed;
  182. particle.color = this.color;
  183. particles.push(particle);
  184. }
  185. }
  186. function getGaussianDistributionNumber(mean, std_dev) {
  187. var U1 = -Math.random() + 1;
  188. var U2 = -Math.random() + 1;
  189. var R = Math.sqrt(-2 * Math.log(U2));
  190. var a = 2 * Math.PI * U1;
  191. var Z = R * Math.cos(a);
  192. return mean + (Z * std_dev);
  193. }
  194. }
  195. }(jQuery));
  196. function YanHua(el) {
  197. $(el).fireworks();
  198. }
  199. function YanHua1(el) {
  200. var canvas = $(el)[0];
  201. canvas.width = $(window).width();
  202. canvas.height = $(window).height();
  203. var ctx = canvas.getContext('2d');
  204. // resize
  205. $(window).on('resize', function () {
  206. canvas.width = $(window).width();
  207. canvas.height = $(window).height();
  208. ctx.fillStyle = 'rgba(255, 255, 255, 0)';
  209. ctx.fillRect(0, 0, canvas.width, canvas.height);
  210. });
  211. // init
  212. ctx.fillStyle = 'rgba(255, 255, 255, 0)';
  213. ctx.fillRect(0, 0, canvas.width, canvas.height);
  214. // objects
  215. var listFire = [];
  216. var listFirework = [];
  217. var fireNumber = 10;
  218. var center = { x: canvas.width / 2, y: canvas.height / 2 };
  219. var range = 100;
  220. for (var i = 0; i < fireNumber; i++) {
  221. var fire = {
  222. x: Math.random() * range / 2 - range / 4 + center.x,
  223. y: Math.random() * range * 2 + canvas.height,
  224. size: Math.random() + 0.5,
  225. fill: '#fd1',
  226. vx: Math.random() - 0.5,
  227. vy: -(Math.random() + 4),
  228. ax: Math.random() * 0.02 - 0.01,
  229. far: Math.random() * range + (center.y - range)
  230. };
  231. fire.base = {
  232. x: fire.x,
  233. y: fire.y,
  234. vx: fire.vx
  235. };
  236. //
  237. listFire.push(fire);
  238. }
  239. function randColor() {
  240. var r = Math.floor(Math.random() * 256);
  241. var g = Math.floor(Math.random() * 256);
  242. var b = Math.floor(Math.random() * 256);
  243. var color = 'rgb($r, $g, $b)';
  244. color = color.replace('$r', r);
  245. color = color.replace('$g', g);
  246. color = color.replace('$b', b);
  247. return color;
  248. }
  249. (function loop() {
  250. requestAnimationFrame(loop);
  251. update();
  252. draw();
  253. })();
  254. function update() {
  255. for (var i = 0; i < listFire.length; i++) {
  256. var fire = listFire[i];
  257. //
  258. if (fire.y <= fire.far) {
  259. // case add firework
  260. var color = randColor();
  261. for (var i = 0; i < fireNumber * 5; i++) {
  262. var firework = {
  263. x: fire.x,
  264. y: fire.y,
  265. size: Math.random() + 1.5,
  266. fill: color,
  267. vx: Math.random() * 5 - 2.5,
  268. vy: Math.random() * -5 + 1.5,
  269. ay: 0.05,
  270. alpha: 1,
  271. life: Math.round(Math.random() * range / 2) + range / 2
  272. };
  273. firework.base = {
  274. life: firework.life,
  275. size: firework.size
  276. };
  277. listFirework.push(firework);
  278. }
  279. // reset
  280. fire.y = fire.base.y;
  281. fire.x = fire.base.x;
  282. fire.vx = fire.base.vx;
  283. fire.ax = Math.random() * 0.02 - 0.01;
  284. }
  285. //
  286. fire.x += fire.vx;
  287. fire.y += fire.vy;
  288. fire.vx += fire.ax;
  289. }
  290. for (var i = listFirework.length - 1; i >= 0; i--) {
  291. var firework = listFirework[i];
  292. if (firework) {
  293. firework.x += firework.vx;
  294. firework.y += firework.vy;
  295. firework.vy += firework.ay;
  296. firework.alpha = firework.life / firework.base.life;
  297. firework.size = firework.alpha * firework.base.size;
  298. firework.alpha = firework.alpha > 0.6 ? 1 : firework.alpha;
  299. //
  300. firework.life--;
  301. if (firework.life <= 0) {
  302. listFirework.splice(i, 1);
  303. }
  304. }
  305. }
  306. }
  307. function draw() {
  308. // clear
  309. ctx.globalCompositeOperation = 'source-over';
  310. ctx.globalAlpha = 0.18;
  311. ctx.fillStyle = 'rgba(255, 255, 255, 0)';
  312. ctx.fillRect(0, 0, canvas.width, canvas.height);
  313. // re-draw
  314. ctx.globalCompositeOperation = 'screen';
  315. ctx.globalAlpha = 1;
  316. for (var i = 0; i < listFire.length; i++) {
  317. var fire = listFire[i];
  318. ctx.beginPath();
  319. ctx.arc(fire.x, fire.y, fire.size, 0, Math.PI * 2);
  320. ctx.closePath();
  321. ctx.fillStyle = fire.fill;
  322. ctx.fill();
  323. }
  324. for (var i = 0; i < listFirework.length; i++) {
  325. var firework = listFirework[i];
  326. ctx.globalAlpha = firework.alpha;
  327. ctx.beginPath();
  328. ctx.arc(firework.x, firework.y, firework.size, 0, Math.PI * 2);
  329. ctx.closePath();
  330. ctx.fillStyle = firework.fill;
  331. ctx.fill();
  332. }
  333. }
  334. }