查看: 7|回复: 0

孔明灯特效源码

[复制链接]
  • 打卡等级:偶尔看看
  • 打卡总天数:7
  • 打卡月天数:0
  • 打卡总奖励:32
  • 最近打卡:2025-02-27 09:26:17

95

主题

2

回帖

4385

积分

管理员

BSAY管理者

积分
4385

管理员一周年

发表于 2025-2-27 09:29:35 | 显示全部楼层 |阅读模式
一个有意思的小玩意儿,孔明灯
1_1733909989690.webp


  1.     <div class="moon"></div>
  2.     <style>
  3.     @keyframes rotateAnimationX {
  4.             0% {
  5.                 transform: rotateX(0deg) rotateZ(0deg);
  6.             }

  7.             25% {
  8.                 transform: rotateX(5deg) rotateZ(5deg);
  9.             }

  10.             50% {
  11.                 transform: rotateX(0deg) rotateZ(0deg);
  12.             }

  13.             75% {
  14.                 transform: rotateX(-5deg) rotateZ(-5deg);
  15.             }

  16.             100% {
  17.                 transform: rotateX(0deg) rotateZ(0deg);
  18.             }
  19.         }

  20.         @keyframes rotateAnimationY {
  21.             0% {
  22.                 transform: rotateY(0deg);

  23.             }

  24.             100% {
  25.                 transform: rotateY(360deg);
  26.             }
  27.         }

  28.         @keyframes shape {
  29.             0% {
  30.                 background: radial-gradient(at 50% 90%, yellow 10%, #ffae00 40%, red);
  31.             }

  32.             50% {
  33.                 background: radial-gradient(at 50% 90%, yellow 20%, #ffae00 50%, red);
  34.             }

  35.             100% {
  36.                 background: radial-gradient(at 50% 90%, yellow 10%, #ffae00 40%, red);
  37.             }
  38.         }
  39. * {
  40.             margin: 0;
  41.             padding: 0;
  42.         }

  43.         html,
  44.         body {
  45.             width: 100%;
  46.             height: 100%;

  47.             background: linear-gradient(to bottom, #000022, #000033);
  48.         }

  49.         .moon {
  50.             width: 200px;
  51.             height: 200px;
  52.             border-radius: 50%;
  53.             background: radial-gradient(circle at center, #f6f6f6, #e5e5e5);
  54.             box-shadow: 0 0 30px 10px rgba(255, 255, 255, 0.5);

  55.             position: absolute;
  56.             top: 100px;
  57.             left: 300px;
  58.         }
  59.         </style>
  60.         <script>
  61.         var blessings = [
  62.   "心想事成",
  63.   "平安健康",
  64.   "万事如意",
  65.   "吉祥如意",
  66.   "阖家欢乐",
  67.   "财源广进",
  68. ];

  69. class Light {
  70.   constructor(s) {
  71.     const w = 200,
  72.       h = 400;
  73.     this.scale = s;

  74.     this.lightWidth = w;
  75.     this.lightHeight = h;

  76.     this.faceWidth = this.lightWidth / 2;
  77.     this.faceHeight = this.lightHeight / 2;
  78.     this.faceTop = 0;
  79.     this.sin60Width = this.faceWidth * Math.sin(60 * (Math.PI / 180));
  80.     this.faceCenter = this.lightWidth / 2 - this.faceWidth / 2;

  81.     this.fireWidth = this.faceWidth * 0.4;
  82.     this.fireHeight = this.fireWidth * 2;

  83.     this.isUpdate = true;
  84.     this.isRotate = this.scale > .4 ? true : false;

  85.     this.init();
  86.   }

  87.   init() {
  88.     const wrap = this.initWrap();
  89.     const light = this.initLight();
  90.     const fire = this.initFire();

  91.     wrap.appendChild(light);
  92.     light.appendChild(fire);

  93.     const body = document.getElementsByTagName("body")[0];

  94.     body.appendChild(wrap);
  95.   }

  96.   initWrap() {
  97.     const wrap = document.createElement("div");
  98.     wrap.className = "light-wrap";
  99.     wrap.style.position = "fixed";
  100.     wrap.style.zIndex = this.lightHeight + this.lightWidth;

  101.     let top = parseInt(Math.random() * window.innerHeight);
  102.     let left = parseInt(Math.random() * window.innerWidth);

  103.     wrap.style.top = top + "px";
  104.     wrap.style.left = left + "px";
  105.     wrap.style.transform = `scale(${this.scale})`

  106.     wrap.style.height = this.lightHeight + "px";
  107.     wrap.style.width = this.lightWidth + "px";

  108.     const update = () => {
  109.       const step = 0.2;

  110.       top - step * 2 > -this.lightHeight
  111.         ? (top -= step * 2)
  112.         : (top = window.innerHeight);
  113.       left - step > -this.lightWidth
  114.         ? (left -= step)
  115.         : (left = window.innerHeight);
  116.       wrap.style.top = top + "px";
  117.       wrap.style.left = left + "px";
  118.       setTimeout(update, 1000 / 30);
  119.     };
  120.     if (this.isUpdate) {
  121.       update();
  122.     }

  123.     return wrap;
  124.   }

  125.   initLight() {
  126.     const light = document.createElement("div");
  127.     light.className = "light";

  128.     light.style.height = this.lightHeight + "px";
  129.     light.style.width = this.lightWidth + "px";

  130.     light.style.position = "relative";

  131.     light.style.perspective = this.faceWidth * 10 + "px";
  132.     light.style.perspectiveOrigin = `center ${this.lightHeight / 2}px`;
  133.     light.style.transformStyle = "preserve-3d";

  134.     light.style.position = "relative";

  135.     light.style.animation = `rotateAnimationX ${
  136.       parseInt(Math.random() * 10) + 5
  137.     }s linear infinite`;

  138.     for (let i = 0; i < 6; i++) {
  139.       const face = this.initFace(i * 60, i);
  140.       light.appendChild(face);
  141.     }

  142.     return light;
  143.   }

  144.   initFace(deg, _i) {
  145.     const face = document.createElement("div");
  146.     face.className = "face";

  147.     face.style.height = this.faceHeight + "px";
  148.     face.style.width = this.faceWidth + "px";

  149.     face.style.transformStyle = "preserve-3d";
  150.     face.style.position = "absolute";

  151.     face.style.top = 0;
  152.     face.style.left = this.faceCenter + "px";
  153.     face.style.transformOrigin = `50% 50% ${this.sin60Width}px`;

  154.     face.style.transform = `translateZ(${
  155.       -1 * this.sin60Width
  156.     }px) rotateY(${deg}deg)`;

  157.     const rotate = () => {
  158.       deg++;
  159.       face.style.transform = `translateZ(${
  160.         -1 * this.sin60Width
  161.       }px) rotateY(${deg}deg)`;
  162.       setTimeout(rotate, 1000 / 30);
  163.     };
  164.     this.isRotate && rotate();

  165.     const faceTop = this.initFaceTop();
  166.     const faceSqure = this.initFaceSqure(_i);
  167.     const faceLine = this.initFaceLine();

  168.     face.appendChild(faceTop);
  169.     face.appendChild(faceSqure);
  170.     face.appendChild(faceLine);

  171.     return face;
  172.   }

  173.   initFaceTop() {
  174.     const faceTop = document.createElement("div");
  175.     faceTop.className = "face-top";

  176.     faceTop.style.width = this.faceWidth + "px";
  177.     faceTop.style.height =
  178.       this.sin60Width / Math.cos(30 * (Math.PI / 180)) + "px";
  179.     faceTop.style.background = `radial-gradient(at 50% 90%, yellow 10%, #ffae00 40%, red)`;
  180.     faceTop.style.transform = `rotateX(-60deg)`;
  181.     faceTop.style.transformOrigin = `0 100%`;
  182.     faceTop.style.clipPath = `polygon(50% 0%, 0% 100%, 100% 100%)`;

  183.     return faceTop;
  184.   }

  185.   initFaceSqure(_i) {
  186.     const faceSqure = document.createElement("div");
  187.     faceSqure.className = "face-squre";

  188.     faceSqure.style.height = this.faceHeight + "px";
  189.     faceSqure.style.width = this.faceWidth + "px";
  190.     faceSqure.style.background = `radial-gradient(at 50% 70%, #0d0034 10%, #381460 40%, red)`;
  191.     faceSqure.style.animation = `shape 2s linear infinite`;

  192.     const word = this.initWord(_i);
  193.     faceSqure.appendChild(word);

  194.     return faceSqure;
  195.   }

  196.   initWord(_i) {
  197.     const word = document.createElement("div");
  198.     word.className = "word";

  199.     word.innerText = blessings[_i];

  200.     word.style.height = "100%";
  201.     word.style.width = "100%";

  202.     word.style.textAlign = "center";
  203.     word.style.writingMode = "vertical-rl";
  204.     word.style.lineHeight = this.faceWidth + "px";
  205.     word.style.transform = "scaleX(-1)";
  206.     word.style.letterSpacing = "2px";
  207.     word.style.fontFamily = "楷体";
  208.     word.style.fontWeight = "bold";
  209.     word.style.userSelect = 'none'
  210.     word.style.color = 'rgb(126 0 0)'

  211.     word.style.fontSize = parseInt(this.faceWidth / 4) + "px";

  212.     return word;
  213.   }

  214.   initFaceLine() {
  215.     const faceLine = document.createElement("div");
  216.     faceLine.className = "face-line";

  217.     faceLine.style.width = this.faceWidth + "px";
  218.     faceLine.style.height =
  219.       this.sin60Width / Math.cos(30 * (Math.PI / 180)) + "px";
  220.     faceLine.style.background = `linear-gradient(to right, transparent 48%, black 48%, transparent 52%)`;
  221.     faceLine.style.transform = `rotateX(60deg)`;
  222.     faceLine.style.transformOrigin = `0 0`;

  223.     return faceLine;
  224.   }

  225.   initFire() {
  226.     const fire = document.createElement("div");
  227.     fire.className = "fire";

  228.     fire.style.position = `absolute`;
  229.     fire.style.top =
  230.       this.faceHeight + this.faceWidth - 0.2 * this.faceWidth + "px";
  231.     fire.style.left = this.lightWidth / 2 - this.fireWidth / 2 + "px";
  232.     fire.style.width = this.fireWidth + "px";
  233.     fire.style.height = this.fireHeight + "px";
  234.     fire.style.perspective = this.faceWidth * 10 + "px";
  235.     fire.style.perspectiveOrigin = `center ${this.faceHeight}px`;
  236.     fire.style.transformStyle = `preserve-3d`;

  237.     const firEItem = this.initFireItem(30);
  238.     fire.appendChild(firEItem);
  239.     return fire;
  240.   }

  241.   initFireItem(d) {
  242.     const fireItem = document.createElement("div");

  243.     fireItem.style.position = `absolute`;
  244.     fireItem.style.height = `100%`;
  245.     fireItem.style.width = `100%`;
  246.     fireItem.style.background = `radial-gradient(at 50% 80%, yellow 10%, #8B0000 20%, transparent 40%, transparent)`;
  247.     fireItem.style.transform = `rotateY(${d}deg)`;

  248.     return fireItem;
  249.   }
  250. }

  251. const lightList = []

  252.     for (let i = 0; i < 18; i++) {
  253.         lightList.push(Math.random() * (.8 - .2) + .2)
  254.     }

  255.     lightList.sort().forEach(i => new Light(i))
  256.     </script>
复制代码



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表