当前位置:   article > 正文

Web前端:html烟花代码

Web前端:html烟花代码

代码如下:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>fireworks烟花!</title>
  6. <style>
  7. html,body{
  8. margin:0px;
  9. width:100%;
  10. height:100%;
  11. overflow:hidden;
  12. background:#000;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <canvas id="canvas" style="position:absolute;width:100%;height:100%;z-index:8888"></canvas>
  18. <canvas style="position:absolute;width:100%;height:100%;z-index:9999" class="canvas" ></canvas>
  19. <div class="overlay">
  20. <div class="tabs">
  21. <div class="tabs-labels"><span class="tabs-label">Commands</span><span class="tabs-label">Info</span><span class="tabs-label">Share</span></div>
  22. <div class="tabs-panels">
  23. <ul class="tabs-panel commands">
  24. </ul>
  25. </div>
  26. </div>
  27. </div>
  28. <script>
  29. function initVars(){
  30. pi=Math.PI;
  31. ctx=canvas.getContext("2d");
  32. canvas.width=canvas.clientWidth;
  33. canvas.height=canvas.clientHeight;
  34. cx=canvas.width/2;
  35. cy=canvas.height/2;
  36. playerZ=-25;
  37. playerX=playerY=playerVX=playerVY=playerVZ=pitch=yaw=pitchV=yawV=0;
  38. scale=600;
  39. seedTimer=0;seedInterval=5,seedLife=100;gravity=.02;
  40. seeds=new Array();
  41. sparkPics=new Array();
  42. s="https://cantelope.org/NYE/";
  43. for(i=1;i<=10;++i){
  44. sparkPic=new Image();
  45. sparkPic.src=s+"spark"+i+".png";
  46. sparkPics.push(sparkPic);
  47. }
  48. sparks=new Array();
  49. pow1=new Audio(s+"pow1.ogg");
  50. pow2=new Audio(s+"pow2.ogg");
  51. pow3=new Audio(s+"pow3.ogg");
  52. pow4=new Audio(s+"pow4.ogg");
  53. frames = 0;
  54. }
  55. function rasterizePoint(x,y,z){
  56. var p,d;
  57. x-=playerX;
  58. y-=playerY;
  59. z-=playerZ;
  60. p=Math.atan2(x,z);
  61. d=Math.sqrt(x*x+z*z);
  62. x=Math.sin(p-yaw)*d;
  63. z=Math.cos(p-yaw)*d;
  64. p=Math.atan2(y,z);
  65. d=Math.sqrt(y*y+z*z);
  66. y=Math.sin(p-pitch)*d;
  67. z=Math.cos(p-pitch)*d;
  68. var rx1=-1000,ry1=1,rx2=1000,ry2=1,rx3=0,ry3=0,rx4=x,ry4=z,uc=(ry4-ry3)*(rx2-rx1)-(rx4-rx3)*(ry2-ry1);
  69. if(!uc) return {x:0,y:0,d:-1};
  70. var ua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc;
  71. var ub=((rx2-rx1)*(ry1-ry3)-(ry2-ry1)*(rx1-rx3))/uc;
  72. if(!z)z=.000000001;
  73. if(ua>0&&ua<1&&ub>0&&ub<1){
  74. return {
  75. x:cx+(rx1+ua*(rx2-rx1))*scale,
  76. y:cy+y/z*scale,
  77. d:Math.sqrt(x*x+y*y+z*z)
  78. };
  79. }else{
  80. return {
  81. x:cx+(rx1+ua*(rx2-rx1))*scale,
  82. y:cy+y/z*scale,
  83. d:-1
  84. };
  85. }
  86. }
  87. function spawnSeed(){
  88. seed=new Object();
  89. seed.x=-50+Math.random()*100;
  90. seed.y=25;
  91. seed.z=-50+Math.random()*100;
  92. seed.vx=.1-Math.random()*.2;
  93. seed.vy=-1.5;//*(1+Math.random()/2);
  94. seed.vz=.1-Math.random()*.2;
  95. seed.born=frames;
  96. seeds.push(seed);
  97. }
  98. function splode(x,y,z){
  99. t=5+parseInt(Math.random()*150);
  100. sparkV=1+Math.random()*2.5;
  101. type=parseInt(Math.random()*3);
  102. switch(type){
  103. case 0:
  104. pic1=parseInt(Math.random()*10);
  105. break;
  106. case 1:
  107. pic1=parseInt(Math.random()*10);
  108. do{ pic2=parseInt(Math.random()*10); }while(pic2==pic1);
  109. break;
  110. case 2:
  111. pic1=parseInt(Math.random()*10);
  112. do{ pic2=parseInt(Math.random()*10); }while(pic2==pic1);
  113. do{ pic3=parseInt(Math.random()*10); }while(pic3==pic1 || pic3==pic2);
  114. break;
  115. }
  116. for(m=1;m<t;++m){
  117. spark=new Object();
  118. spark.x=x; spark.y=y; spark.z=z;
  119. p1=pi*2*Math.random();
  120. p2=pi*Math.random();
  121. v=sparkV*(1+Math.random()/6)
  122. spark.vx=Math.sin(p1)*Math.sin(p2)*v;
  123. spark.vz=Math.cos(p1)*Math.sin(p2)*v;
  124. spark.vy=Math.cos(p2)*v;
  125. switch(type){
  126. case 0: spark.img=sparkPics[pic1]; break;
  127. case 1:
  128. spark.img=sparkPics[parseInt(Math.random()*2)?pic1:pic2];
  129. break;
  130. case 2:
  131. switch(parseInt(Math.random()*3)){
  132. case 0: spark.img=sparkPics[pic1]; break;
  133. case 1: spark.img=sparkPics[pic2]; break;
  134. case 2: spark.img=sparkPics[pic3]; break;
  135. }
  136. break;
  137. }
  138. spark.radius=25+Math.random()*50;
  139. spark.alpha=1;
  140. spark.trail=new Array();
  141. sparks.push(spark);
  142. }
  143. switch(parseInt(Math.random()*4)){
  144. case 0: pow=new Audio(s+"pow1.ogg"); break;
  145. case 1: pow=new Audio(s+"pow2.ogg"); break;
  146. case 2: pow=new Audio(s+"pow3.ogg"); break;
  147. case 3: pow=new Audio(s+"pow4.ogg"); break;
  148. }
  149. d=Math.sqrt((x-playerX)*(x-playerX)+(y-playerY)*(y-playerY)+(z-playerZ)*(z-playerZ));
  150. pow.volume=1.5/(1+d/10);
  151. pow.play();
  152. }
  153. function doLogic(){
  154. if(seedTimer<frames){
  155. seedTimer=frames+seedInterval*Math.random()*10;
  156. spawnSeed();
  157. }
  158. for(i=0;i<seeds.length;++i){
  159. seeds[i].vy+=gravity;
  160. seeds[i].x+=seeds[i].vx;
  161. seeds[i].y+=seeds[i].vy;
  162. seeds[i].z+=seeds[i].vz;
  163. if(frames-seeds[i].born>seedLife){
  164. splode(seeds[i].x,seeds[i].y,seeds[i].z);
  165. seeds.splice(i,1);
  166. }
  167. }
  168. for(i=0;i<sparks.length;++i){
  169. if(sparks[i].alpha>0 && sparks[i].radius>5){
  170. sparks[i].alpha-=.01;
  171. sparks[i].radius/=1.02;
  172. sparks[i].vy+=gravity;
  173. point=new Object();
  174. point.x=sparks[i].x;
  175. point.y=sparks[i].y;
  176. point.z=sparks[i].z;
  177. if(sparks[i].trail.length){
  178. x=sparks[i].trail[sparks[i].trail.length-1].x;
  179. y=sparks[i].trail[sparks[i].trail.length-1].y;
  180. z=sparks[i].trail[sparks[i].trail.length-1].z;
  181. d=((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y)+(point.z-z)*(point.z-z));
  182. if(d>9){
  183. sparks[i].trail.push(point);
  184. }
  185. }else{
  186. sparks[i].trail.push(point);
  187. }
  188. if(sparks[i].trail.length>5)sparks[i].trail.splice(0,1);
  189. sparks[i].x+=sparks[i].vx;
  190. sparks[i].y+=sparks[i].vy;
  191. sparks[i].z+=sparks[i].vz;
  192. sparks[i].vx/=1.075;
  193. sparks[i].vy/=1.075;
  194. sparks[i].vz/=1.075;
  195. }else{
  196. sparks.splice(i,1);
  197. }
  198. }
  199. p=Math.atan2(playerX,playerZ);
  200. d=Math.sqrt(playerX*playerX+playerZ*playerZ);
  201. d+=Math.sin(frames/80)/1.25;
  202. t=Math.sin(frames/200)/40;
  203. playerX=Math.sin(p+t)*d;
  204. playerZ=Math.cos(p+t)*d;
  205. yaw=pi+p+t;
  206. }
  207. function rgb(col){
  208. var r = parseInt((.5+Math.sin(col)*.5)*16);
  209. var g = parseInt((.5+Math.cos(col)*.5)*16);
  210. var b = parseInt((.5-Math.sin(col)*.5)*16);
  211. return "#"+r.toString(16)+g.toString(16)+b.toString(16);
  212. }
  213. function draw(){
  214. ctx.clearRect(0,0,cx*2,cy*2);
  215. ctx.fillStyle="#ff8";
  216. for(i=-100;i<100;i+=3){
  217. for(j=-100;j<100;j+=4){
  218. x=i;z=j;y=25;
  219. point=rasterizePoint(x,y,z);
  220. if(point.d!=-1){
  221. size=250/(1+point.d);
  222. d = Math.sqrt(x * x + z * z);
  223. a = 0.75 - Math.pow(d / 100, 6) * 0.75;
  224. if(a>0){
  225. ctx.globalAlpha = a;
  226. ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  227. }
  228. }
  229. }
  230. }
  231. ctx.globalAlpha=1;
  232. for(i=0;i<seeds.length;++i){
  233. point=rasterizePoint(seeds[i].x,seeds[i].y,seeds[i].z);
  234. if(point.d!=-1){
  235. size=200/(1+point.d);
  236. ctx.fillRect(point.x-size/2,point.y-size/2,size,size);
  237. }
  238. }
  239. point1=new Object();
  240. for(i=0;i<sparks.length;++i){
  241. point=rasterizePoint(sparks[i].x,sparks[i].y,sparks[i].z);
  242. if(point.d!=-1){
  243. size=sparks[i].radius*200/(1+point.d);
  244. if(sparks[i].alpha<0)sparks[i].alpha=0;
  245. if(sparks[i].trail.length){
  246. point1.x=point.x;
  247. point1.y=point.y;
  248. switch(sparks[i].img){
  249. case sparkPics[0]:ctx.strokeStyle="#f84";break;
  250. case sparkPics[1]:ctx.strokeStyle="#84f";break;
  251. case sparkPics[2]:ctx.strokeStyle="#8ff";break;
  252. case sparkPics[3]:ctx.strokeStyle="#fff";break;
  253. case sparkPics[4]:ctx.strokeStyle="#4f8";break;
  254. case sparkPics[5]:ctx.strokeStyle="#f44";break;
  255. case sparkPics[6]:ctx.strokeStyle="#f84";break;
  256. case sparkPics[7]:ctx.strokeStyle="#84f";break;
  257. case sparkPics[8]:ctx.strokeStyle="#fff";break;
  258. case sparkPics[9]:ctx.strokeStyle="#44f";break;
  259. }
  260. for(j=sparks[i].trail.length-1;j>=0;--j){
  261. point2=rasterizePoint(sparks[i].trail[j].x,sparks[i].trail[j].y,sparks[i].trail[j].z);
  262. if(point2.d!=-1){
  263. ctx.globalAlpha=j/sparks[i].trail.length*sparks[i].alpha/2;
  264. ctx.beginPath();
  265. ctx.moveTo(point1.x,point1.y);
  266. ctx.lineWidth=1+sparks[i].radius*10/(sparks[i].trail.length-j)/(1+point2.d);
  267. ctx.lineTo(point2.x,point2.y);
  268. ctx.stroke();
  269. point1.x=point2.x;
  270. point1.y=point2.y;
  271. }
  272. }
  273. }
  274. ctx.globalAlpha=sparks[i].alpha;
  275. ctx.drawImage(sparks[i].img,point.x-size/2,point.y-size/2,size,size);
  276. }
  277. }
  278. }
  279. function frame(){
  280. if(frames>100000){
  281. seedTimer=0;
  282. frames=0;
  283. }
  284. frames++;
  285. draw();
  286. doLogic();
  287. requestAnimationFrame(frame);
  288. }
  289. window.addEventListener("resize",()=>{
  290. canvas.width=canvas.clientWidth;
  291. canvas.height=canvas.clientHeight;
  292. cx=canvas.width/2;
  293. cy=canvas.height/2;
  294. });
  295. initVars();
  296. frame();
  297. </script>
  298. <script>
  299. var S = {
  300. init: function () {
  301. var action = window.location.href,
  302. i = action.indexOf('?a=');
  303. S.Drawing.init('.canvas');
  304. document.body.classList.add('body--ready');
  305. if (i !== -1) {
  306. S.UI.simulate(decodeURI(action).substring(i + 3));
  307. } else {
  308. S.UI.simulate('|#countdown 3||祝|X|X|X|学|业|顺|利|所得皆所愿|所想皆如意!|#rectangle|');
  309. }
  310. S.Drawing.loop(function () {
  311. S.Shape.render();
  312. });
  313. }
  314. };
  315. S.Drawing = (function () {
  316. var canvas,
  317. context,
  318. renderFn
  319. requestFrame = window.requestAnimationFrame ||
  320. window.webkitRequestAnimationFrame ||
  321. window.mozRequestAnimationFrame ||
  322. window.oRequestAnimationFrame ||
  323. window.msRequestAnimationFrame ||
  324. function(callback) {
  325. window.setTimeout(callback, 1000 / 60);
  326. };
  327. return {
  328. init: function (el) {
  329. canvas = document.querySelector(el);
  330. context = canvas.getContext('2d');
  331. this.adjustCanvas();
  332. window.addEventListener('resize', function (e) {
  333. S.Drawing.adjustCanvas();
  334. });
  335. },
  336. loop: function (fn) {
  337. renderFn = !renderFn ? fn : renderFn;
  338. this.clearFrame();
  339. renderFn();
  340. requestFrame.call(window, this.loop.bind(this));
  341. },
  342. adjustCanvas: function () {
  343. canvas.width = window.innerWidth;
  344. canvas.height = window.innerHeight;
  345. },
  346. clearFrame: function () {
  347. context.clearRect(0, 0, canvas.width, canvas.height);
  348. },
  349. getArea: function () {
  350. return { w: canvas.width, h: canvas.height };
  351. },
  352. drawCircle: function (p, c) {
  353. context.fillStyle = c.render();
  354. context.beginPath();
  355. context.arc(p.x, p.y, p.z, 0, 2 * Math.PI, true);
  356. context.closePath();
  357. context.fill();
  358. }
  359. }
  360. }());
  361. S.UI = (function () {
  362. var canvas = document.querySelector('.canvas'),
  363. interval,
  364. isTouch = false, //('ontouchstart' in window || navigator.msMaxTouchPoints),
  365. currentAction,
  366. resizeTimer,
  367. time,
  368. maxShapeSize = 30,
  369. firstAction = true,
  370. sequence = [],
  371. cmd = '#';
  372. function formatTime(date) {
  373. var h = date.getHours(),
  374. m = date.getMinutes(),
  375. m = m < 10 ? '0' + m : m;
  376. return h + ':' + m;
  377. }
  378. function getValue(value) {
  379. return value && value.split(' ')[1];
  380. }
  381. function getAction(value) {
  382. value = value && value.split(' ')[0];
  383. return value && value[0] === cmd && value.substring(1);
  384. }
  385. function timedAction(fn, delay, max, reverse) {
  386. clearInterval(interval);
  387. currentAction = reverse ? max : 1;
  388. fn(currentAction);
  389. if (!max || (!reverse && currentAction < max) || (reverse && currentAction > 0)) {
  390. interval = setInterval(function () {
  391. currentAction = reverse ? currentAction - 1 : currentAction + 1;
  392. fn(currentAction);
  393. if ((!reverse && max && currentAction === max) || (reverse && currentAction === 0)) {
  394. clearInterval(interval);
  395. }
  396. }, delay);
  397. }
  398. }
  399. function reset(destroy) {
  400. clearInterval(interval);
  401. sequence = [];
  402. time = null;
  403. destroy && S.Shape.switchShape(S.ShapeBuilder.letter(''));
  404. }
  405. function performAction(value) {
  406. var action,
  407. value,
  408. current;
  409. // overlay.classList.remove('overlay--visible');
  410. sequence = typeof(value) === 'object' ? value : sequence.concat(value.split('|'));
  411. // input.value = '';
  412. // checkInputWidth();
  413. timedAction(function (index) {
  414. current = sequence.shift();
  415. action = getAction(current);
  416. value = getValue(current);
  417. switch (action) {
  418. case 'countdown':
  419. value = parseInt(value) || 10;
  420. value = value > 0 ? value : 10;
  421. timedAction(function (index) {
  422. if (index === 0) {
  423. if (sequence.length === 0) {
  424. S.Shape.switchShape(S.ShapeBuilder.letter(''));
  425. } else {
  426. performAction(sequence);
  427. }
  428. } else {
  429. S.Shape.switchShape(S.ShapeBuilder.letter(index), true);
  430. }
  431. }, 1000, value, true);
  432. break;
  433. case 'rectangle':
  434. value = value && value.split('x');
  435. value = (value && value.length === 2) ? value : [maxShapeSize, maxShapeSize / 2];
  436. S.Shape.switchShape(S.ShapeBuilder.rectangle(Math.min(maxShapeSize, parseInt(value[0])), Math.min(maxShapeSize, parseInt(value[1]))));
  437. break;
  438. case 'circle':
  439. value = parseInt(value) || maxShapeSize;
  440. value = Math.min(value, maxShapeSize);
  441. S.Shape.switchShape(S.ShapeBuilder.circle(value));
  442. break;
  443. case 'time':
  444. var t = formatTime(new Date());
  445. if (sequence.length > 0) {
  446. S.Shape.switchShape(S.ShapeBuilder.letter(t));
  447. } else {
  448. timedAction(function () {
  449. t = formatTime(new Date());
  450. if (t !== time) {
  451. time = t;
  452. S.Shape.switchShape(S.ShapeBuilder.letter(time));
  453. }
  454. }, 1000);
  455. }
  456. break;
  457. default:
  458. S.Shape.switchShape(S.ShapeBuilder.letter(current[0] === cmd ? 'What?' : current));
  459. }
  460. }, 2000, sequence.length);
  461. }
  462. function checkInputWidth(e) {
  463. if (input.value.length > 18) {
  464. ui.classList.add('ui--wide');
  465. } else {
  466. ui.classList.remove('ui--wide');
  467. }
  468. if (firstAction && input.value.length > 0) {
  469. ui.classList.add('ui--enter');
  470. } else {
  471. ui.classList.remove('ui--enter');
  472. }
  473. }
  474. function bindEvents() {
  475. document.body.addEventListener('keydown', function (e) {
  476. input.focus();
  477. if (e.keyCode === 13) {
  478. firstAction = false;
  479. reset();
  480. performAction(input.value);
  481. }
  482. });
  483. canvas.addEventListener('click', function (e) {
  484. overlay.classList.remove('overlay--visible');
  485. });
  486. }
  487. function init() {
  488. bindEvents();
  489. // input.focus();
  490. isTouch && document.body.classList.add('touch');
  491. }
  492. // Init
  493. init();
  494. return {
  495. simulate: function (action) {
  496. performAction(action);
  497. }
  498. }
  499. }());
  500. S.UI.Tabs = (function () {
  501. var tabs = document.querySelector('.tabs'),
  502. labels = document.querySelector('.tabs-labels'),
  503. triggers = document.querySelectorAll('.tabs-label'),
  504. panels = document.querySelectorAll('.tabs-panel');
  505. function activate(i) {
  506. triggers[i].classList.add('tabs-label--active');
  507. panels[i].classList.add('tabs-panel--active');
  508. }
  509. function bindEvents() {
  510. labels.addEventListener('click', function (e) {
  511. var el = e.target,
  512. index;
  513. if (el.classList.contains('tabs-label')) {
  514. for (var t = 0; t < triggers.length; t++) {
  515. triggers[t].classList.remove('tabs-label--active');
  516. panels[t].classList.remove('tabs-panel--active');
  517. if (el === triggers[t]) {
  518. index = t;
  519. }
  520. }
  521. activate(index);
  522. }
  523. });
  524. }
  525. function init() {
  526. activate(0);
  527. bindEvents();
  528. }
  529. // Init
  530. init();
  531. }());
  532. S.Point = function (args) {
  533. this.x = args.x;
  534. this.y = args.y;
  535. this.z = args.z;
  536. this.a = args.a;
  537. this.h = args.h;
  538. };
  539. S.Color = function (r, g, b, a) {
  540. this.r = r;
  541. this.g = g;
  542. this.b = b;
  543. this.a = a;
  544. };
  545. S.Color.prototype = {
  546. render: function () {
  547. return 'rgba(' + this.r + ',' + + this.g + ',' + this.b + ',' + this.a + ')';
  548. }
  549. };
  550. S.Dot = function (x, y) {
  551. this.p = new S.Point({
  552. x: x,
  553. y: y,
  554. z: 5,
  555. a: 1,
  556. h: 0
  557. });
  558. this.e = 0.07;
  559. this.s = true;
  560. this.c = new S.Color(255, 255, 255, this.p.a);
  561. this.t = this.clone();
  562. this.q = [];
  563. };
  564. S.Dot.prototype = {
  565. clone: function () {
  566. return new S.Point({
  567. x: this.x,
  568. y: this.y,
  569. z: this.z,
  570. a: this.a,
  571. h: this.h
  572. });
  573. },
  574. _draw: function () {
  575. this.c.a = this.p.a;
  576. S.Drawing.drawCircle(this.p, this.c);
  577. },
  578. _moveTowards: function (n) {
  579. var details = this.distanceTo(n, true),
  580. dx = details[0],
  581. dy = details[1],
  582. d = details[2],
  583. e = this.e * d;
  584. if (this.p.h === -1) {
  585. this.p.x = n.x;
  586. this.p.y = n.y;
  587. return true;
  588. }
  589. if (d > 1) {
  590. this.p.x -= ((dx / d) * e);
  591. this.p.y -= ((dy / d) * e);
  592. } else {
  593. if (this.p.h > 0) {
  594. this.p.h--;
  595. } else {
  596. return true;
  597. }
  598. }
  599. return false;
  600. },
  601. _update: function () {
  602. if (this._moveTowards(this.t)) {
  603. var p = this.q.shift();
  604. if (p) {
  605. this.t.x = p.x || this.p.x;
  606. this.t.y = p.y || this.p.y;
  607. this.t.z = p.z || this.p.z;
  608. this.t.a = p.a || this.p.a;
  609. this.p.h = p.h || 0;
  610. } else {
  611. if (this.s) {
  612. this.p.x -= Math.sin(Math.random() * 3.142);
  613. this.p.y -= Math.sin(Math.random() * 3.142);
  614. } else {
  615. this.move(new S.Point({
  616. x: this.p.x + (Math.random() * 50) - 25,
  617. y: this.p.y + (Math.random() * 50) - 25,
  618. }));
  619. }
  620. }
  621. }
  622. d = this.p.a - this.t.a;
  623. this.p.a = Math.max(0.1, this.p.a - (d * 0.05));
  624. d = this.p.z - this.t.z;
  625. this.p.z = Math.max(1, this.p.z - (d * 0.05));
  626. },
  627. distanceTo: function (n, details) {
  628. var dx = this.p.x - n.x,
  629. dy = this.p.y - n.y,
  630. d = Math.sqrt(dx * dx + dy * dy);
  631. return details ? [dx, dy, d] : d;
  632. },
  633. move: function (p, avoidStatic) {
  634. if (!avoidStatic || (avoidStatic && this.distanceTo(p) > 1)) {
  635. this.q.push(p);
  636. }
  637. },
  638. render: function () {
  639. this._update();
  640. this._draw();
  641. }
  642. }
  643. S.ShapeBuilder = (function () {
  644. var gap = 13,
  645. shapeCanvas = document.createElement('canvas'),
  646. shapeContext = shapeCanvas.getContext('2d'),
  647. fontSize = 500,
  648. fontFamily = 'Avenir, Helvetica Neue, Helvetica, Arial, sans-serif';
  649. function fit() {
  650. shapeCanvas.width = Math.floor(window.innerWidth / gap) * gap;
  651. shapeCanvas.height = Math.floor(window.innerHeight / gap) * gap;
  652. shapeContext.fillStyle = 'red';
  653. shapeContext.textBaseline = 'middle';
  654. shapeContext.textAlign = 'center';
  655. }
  656. function processCanvas() {
  657. var pixels = shapeContext.getImageData(0, 0, shapeCanvas.width, shapeCanvas.height).data;
  658. dots = [],
  659. pixels,
  660. x = 0,
  661. y = 0,
  662. fx = shapeCanvas.width,
  663. fy = shapeCanvas.height,
  664. w = 0,
  665. h = 0;
  666. for (var p = 0; p < pixels.length; p += (4 * gap)) {
  667. if (pixels[p + 3] > 0) {
  668. dots.push(new S.Point({
  669. x: x,
  670. y: y
  671. }));
  672. w = x > w ? x : w;
  673. h = y > h ? y : h;
  674. fx = x < fx ? x : fx;
  675. fy = y < fy ? y : fy;
  676. }
  677. x += gap;
  678. if (x >= shapeCanvas.width) {
  679. x = 0;
  680. y += gap;
  681. p += gap * 4 * shapeCanvas.width;
  682. }
  683. }
  684. return { dots: dots, w: w + fx, h: h + fy };
  685. }
  686. function setFontSize(s) {
  687. shapeContext.font = 'bold ' + s + 'px ' + fontFamily;
  688. }
  689. function isNumber(n) {
  690. return !isNaN(parseFloat(n)) && isFinite(n);
  691. }
  692. function init() {
  693. fit();
  694. window.addEventListener('resize', fit);
  695. }
  696. // Init
  697. init();
  698. return {
  699. imageFile: function (url, callback) {
  700. var image = new Image(),
  701. a = S.Drawing.getArea();
  702. image.onload = function () {
  703. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  704. shapeContext.drawImage(this, 0, 0, a.h * 0.6, a.h * 0.6);
  705. callback(processCanvas());
  706. };
  707. image.onerror = function () {
  708. callback(S.ShapeBuilder.letter('What?'));
  709. }
  710. image.src = url;
  711. },
  712. circle: function (d) {
  713. var r = Math.max(0, d) / 2;
  714. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  715. shapeContext.beginPath();
  716. shapeContext.arc(r * gap, r * gap, r * gap, 0, 2 * Math.PI, false);
  717. shapeContext.fill();
  718. shapeContext.closePath();
  719. return processCanvas();
  720. },
  721. letter: function (l) {
  722. var s = 0;
  723. setFontSize(fontSize);
  724. s = Math.min(fontSize,
  725. (shapeCanvas.width / shapeContext.measureText(l).width) * 0.8 * fontSize,
  726. (shapeCanvas.height / fontSize) * (isNumber(l) ? 1 : 0.45) * fontSize);
  727. setFontSize(s);
  728. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  729. shapeContext.fillText(l, shapeCanvas.width / 2, shapeCanvas.height / 2);
  730. return processCanvas();
  731. },
  732. rectangle: function (w, h) {
  733. var dots = [],
  734. width = gap * w,
  735. height = gap * h;
  736. for (var y = 0; y < height; y += gap) {
  737. for (var x = 0; x < width; x += gap) {
  738. dots.push(new S.Point({
  739. x: x,
  740. y: y,
  741. }));
  742. }
  743. }
  744. return { dots: dots, w: width, h: height };
  745. }
  746. };
  747. }());
  748. S.Shape = (function () {
  749. var dots = [],
  750. width = 0,
  751. height = 0,
  752. cx = 0,
  753. cy = 0;
  754. function compensate() {
  755. var a = S.Drawing.getArea();
  756. cx = a.w / 2 - width / 2;
  757. cy = a.h / 2 - height / 2;
  758. }
  759. return {
  760. shuffleIdle: function () {
  761. var a = S.Drawing.getArea();
  762. for (var d = 0; d < dots.length; d++) {
  763. if (!dots[d].s) {
  764. dots[d].move({
  765. x: Math.random() * a.w,
  766. y: Math.random() * a.h
  767. });
  768. }
  769. }
  770. },
  771. switchShape: function (n, fast) {
  772. var size,
  773. a = S.Drawing.getArea();
  774. width = n.w;
  775. height = n.h;
  776. compensate();
  777. if (n.dots.length > dots.length) {
  778. size = n.dots.length - dots.length;
  779. for (var d = 1; d <= size; d++) {
  780. dots.push(new S.Dot(a.w / 2, a.h / 2));
  781. }
  782. }
  783. var d = 0,
  784. i = 0;
  785. while (n.dots.length > 0) {
  786. i = Math.floor(Math.random() * n.dots.length);
  787. dots[d].e = fast ? 0.25 : (dots[d].s ? 0.14 : 0.11);
  788. if (dots[d].s) {
  789. dots[d].move(new S.Point({
  790. z: Math.random() * 20 + 10,
  791. a: Math.random(),
  792. h: 18
  793. }));
  794. } else {
  795. dots[d].move(new S.Point({
  796. z: Math.random() * 5 + 5,
  797. h: fast ? 18 : 30
  798. }));
  799. }
  800. dots[d].s = true;
  801. dots[d].move(new S.Point({
  802. x: n.dots[i].x + cx,
  803. y: n.dots[i].y + cy,
  804. a: 1,
  805. z: 5,
  806. h: 0
  807. }));
  808. n.dots = n.dots.slice(0, i).concat(n.dots.slice(i + 1));
  809. d++;
  810. }
  811. for (var i = d; i < dots.length; i++) {
  812. if (dots[i].s) {
  813. dots[i].move(new S.Point({
  814. z: Math.random() * 20 + 10,
  815. a: Math.random(),
  816. h: 20
  817. }));
  818. dots[i].s = false;
  819. dots[i].e = 0.04;
  820. dots[i].move(new S.Point({
  821. x: Math.random() * a.w,
  822. y: Math.random() * a.h,
  823. a: 0.3, //.4
  824. z: Math.random() * 4,
  825. h: 0
  826. }));
  827. }
  828. }
  829. },
  830. render: function () {
  831. for (var d = 0; d < dots.length; d++) {
  832. dots[d].render();
  833. }
  834. }
  835. }
  836. }());
  837. S.init();
  838. </script>
  839. </body>
  840. </html>

该代码内含文字,文字内容可自由发挥,找到代码中这一行,修改内容即可

 S.UI.simulate('|#countdown 3||祝|X|X|X|学|业|顺|利|所得皆所愿|所想皆如意!|#rectangle|');

运行结果如下:

 

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

闽ICP备14008679号