当前位置:   article > 正文

JS特效第151弹:HTML5制作ascii码表文字特效_用html做nike鞋码表

用html做nike鞋码表

          HTML5制作ascii码表文字特效是一款基于HTML5 Canvas绘制的科技酷炫的文字3D旋转特效,先来看看效果:

        一部分关键的代码如下:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>HTML5制作ascii码表文字特效</title>
  6. <style>
  7. body {
  8. overflow: hidden;
  9. background-color: #000;
  10. }
  11. canvas {
  12. width: 100%;
  13. height: 100vh;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <canvas></canvas><script>
  19. window.onload = () => {
  20. const CANVAS = document.getElementsByTagName("canvas")[0];
  21. const CTX = CANVAS.getContext("2d");
  22. const CHARS = [];
  23. const MAX_CHARS = 200;
  24. const SEPARATION = 1.5;
  25. let ww, wh, camera;
  26. class Vector {
  27. constructor(x, y, z) {
  28. this.x = x;
  29. this.y = y;
  30. this.z = z;
  31. }
  32. rotate(dir, ang) {
  33. const X = this.x;
  34. const Y = this.y;
  35. const Z = this.z;
  36. const SIN = Math.sin(ang);
  37. const COS = Math.cos(ang);
  38. if (dir === "x") {
  39. this.y = Y * COS - Z * SIN;
  40. this.z = Y * SIN + Z * COS;
  41. } else if (dir === "y") {
  42. this.x = X * COS - Z * SIN;
  43. this.z = X * SIN + Z * COS;
  44. }
  45. }
  46. project() {
  47. const ZP = this.z + camera.z;
  48. const DIV = ZP / 600;
  49. const XP = (this.x + camera.x) / DIV;
  50. const YP = (this.y + camera.y) / DIV;
  51. const CENTER = getCenter();
  52. return [XP + CENTER[0], YP + CENTER[1], ZP];
  53. }
  54. }
  55. class Char {
  56. constructor(letter, pos) {
  57. this.letter = letter;
  58. this.pos = pos;
  59. }
  60. rotate(dir, ang) {
  61. this.pos.rotate(dir, ang);
  62. }
  63. render() {
  64. const PIXEL = this.pos.project();
  65. const XP = PIXEL[0];
  66. const YP = PIXEL[1];
  67. const MAX_SIZE = 50;
  68. const SIZE = (1 / PIXEL[2] * MAX_SIZE) | 0;
  69. const BRIGHTNESS = SIZE / MAX_SIZE;
  70. const COL = `rgba(255, 255, ${100 * BRIGHTNESS | 0 + 150}, ${BRIGHTNESS})`;
  71. CTX.beginPath();
  72. CTX.fillStyle = COL;
  73. CTX.font = SIZE + "px monospace";
  74. CTX.fillText(this.letter, XP, YP);
  75. CTX.fill();
  76. CTX.closePath();
  77. }
  78. }
  79. function getCenter() {
  80. return [ww / 2, wh / 2];
  81. }
  82. function signedRandom() {
  83. return Math.random() - Math.random();
  84. }
  85. function render() {
  86. for (let i = 0; i < CHARS.length; i++) {
  87. CHARS[i].render();
  88. }
  89. }
  90. let time = 0;
  91. function update() {
  92. CTX.clearRect(0, 0, ww, wh);
  93. for (let i = 0; i < CHARS.length; i++) {
  94. const DX = 0.005 * Math.sin(time * 0.001);
  95. const DY = 0.005 * Math.cos(time * 0.001);
  96. CHARS[i].rotate("x", DX);
  97. CHARS[i].rotate("y", DY);
  98. }
  99. ++time;
  100. }
  101. function loop() {
  102. window.requestAnimationFrame(loop);
  103. update();
  104. render();
  105. }
  106. function getRandomInt(min, max) {
  107. return Math.floor(Math.random() * (max - min + 1)) + min;
  108. };
  109. function createChars() {
  110. for (let i = 0; i < MAX_CHARS; i++) {
  111. const CHARACTER = String.fromCharCode((Math.random() * 93 + 34) | 0);
  112. const X = signedRandom() * SEPARATION;
  113. const Y = signedRandom() * SEPARATION;
  114. const Z = signedRandom() * SEPARATION;
  115. const POS = new Vector(X, Y, Z);
  116. const CHAR = new Char(CHARACTER, POS);
  117. CHARS.push(CHAR);
  118. }
  119. }
  120. function setDim() {
  121. ww = window.innerWidth;
  122. wh = window.innerHeight;
  123. CANVAS.width = ww;
  124. CANVAS.height = wh;
  125. }
  126. function initCamera() {
  127. camera = new Vector(0, 0, SEPARATION + 1);
  128. }
  129. window.onresize = setDim;
  130. (() => {
  131. setDim();
  132. initCamera();
  133. createChars();
  134. loop();
  135. })();
  136. };
  137. </script>
  138. <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
  139. <p>适用浏览器:360、FireFox、Chrome、Opera、傲游、搜狗、世界之窗. 不支持Safari、IE8及以下浏览器。</p>
  140. </div>
  141. </body>
  142. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/870968
推荐阅读
相关标签
  

闽ICP备14008679号