1 line
8.6 KiB
Plaintext
1 line
8.6 KiB
Plaintext
{"version":3,"sources":["snow.js"],"names":["_document$getElementB","snowConf","isSnowing","requestAnimationFrame","window","mozRequestAnimationFrame","webkitRequestAnimationFrame","msRequestAnimationFrame","callback","setTimeout","canvas","document","getElementById","ctx","getContext","flakeCount","mX","mY","flakes","width","innerWidth","height","innerHeight","snow","clearRect","minDist","i","flake","x","y","x2","y2","dist","Math","sqrt","xcomp","ycomp","deltaV","velX","velY","speed","cos","step","stepSize","fillStyle","opacity","reset","beginPath","arc","size","PI","fill","floor","random","startSnow","push","angle","addEventListener","e","clientX","clientY","localStorage","setItem","classList","add","removeItem","remove","_window$kr","getItem","kr","notMobile","init"],"mappings":"AAAA,cAAA,WAAK,IAAAA,EAEGC,EACY,IADZA,EAES,IAFTA,EAGO,gBAHPA,EAIM,EAJNA,EAKO,GALPA,EAMS,GANTA,EAOU,GAIVC,GAAY,EAEVC,EAAwBC,OAAOD,uBAAyBC,OAAOC,0BAA4BD,OAAOE,6BAA+BF,OAAOG,yBAA2B,SAASC,GAAUJ,OAAOK,WAAWD,EAAU,IAAK,KAC7NJ,OAAOD,sBAAwBA,EAC/B,IAAMO,EAASC,SAASC,eAAe,QACjCC,EAAMH,EAAOI,WAAW,MACxBC,EAAad,EACfe,GAAM,IAAKC,GAAM,IACjBC,EAAS,GACbR,EAAOS,MAAQf,OAAOgB,WACtBV,EAAOW,OAASjB,OAAOkB,YACvB,IAAMC,EAAO,SAAPA,IACF,GAAKrB,EAAL,CAKAW,EAAIW,UAAU,EAAG,EAAGd,EAAOS,MAAOT,EAAOW,QAGzC,IADA,IAAMI,EAAUxB,EACPyB,EAAI,EAAGA,EAAIX,EAAYW,IAAI,CAChC,IAAIC,EAAQT,EAAOQ,GACbE,EAAIZ,EAAIa,EAAIZ,EACZa,EAAKH,EAAMC,EAAGG,EAAKJ,EAAME,EACzBG,EAAOC,KAAKC,MAAMN,EAAIE,IAAKF,EAAIE,IAAOD,EAAIE,IAAKF,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AMnB,KAAKoB,SAAS3C,EAAOW,QAC1C4B,EAAyB,EAAdhB,KAAKoB,SAAcpD,EAC9BuC,EAAyB,EAAdP,KAAKoB,SAAcpD,EAC9B4C,EAAyB,GAAdZ,KAAKoB,SAAgBpD,EACtCiB,EAAOqC,KAAK,CACRf,MAAOA,EACPF,KAAM,EAAGC,KAAMC,EACfZ,EAAGA,EAAGC,EAAGA,EACToB,KAAMA,EACNN,SAAWV,KAAKoB,SAAY,GAAKpD,EACjCyC,KAAM,EACNc,MAAO,IACPX,QAASA,IAIjBtB,KAGJZ,SAAS8C,iBAAiB,aAAa,SAACC,GAAK1C,EAAK0C,EAAEC,QAAS1C,EAAKyC,EAAEE,WAEpExD,OAAOqD,iBAAiB,UAAS,WAAK/C,EAAOS,MAAQf,OAAOgB,WAAYV,EAAOW,OAASjB,OAAOkB,eAE/F,QAAAtB,EAAAW,SAASC,eAAe,sBAAxB,IAAAZ,GAAAA,EAAwCyD,iBAAiB,SAAS,WAE1DvD,GAEAO,YAAW,WAEPP,GAAY,IACb,KACH2D,aAAaC,QAAQ,mBAAmB,GACxCpD,EAAOqD,UAAUC,IAAI,cAGrB9D,GAAY,EACZ2D,aAAaI,WAAW,mBACxBvD,EAAOqD,UAAUG,OAAO,YAExBZ,QAvDK,WAAM,IAAAa,EAGf,GADmE,OAA5CN,aAAaO,QAAQ,qBACG,KAAzB,QAAAD,EAAA/D,OAAOiE,UAAP,IAAAF,OAAA,EAAAA,EAAWG,WAI7B,OAFApE,GAAY,OACZQ,EAAOqD,UAAUC,IAAI,YAIzBV,IAkDJiB,GAzIJ","file":"../js/snow.min.js","sourcesContent":["(()=>{\r\n // 设置雪花参数\r\n let snowConf = {\r\n flakeCount: 100,\r\n minDist: 150,\r\n color: \"255, 255, 255\",\r\n size: 2,\r\n speed: 0.5,\r\n opacity: 0.3,\r\n stepsize: .5,\r\n };\r\n\r\n // 记录下雪状态\r\n let isSnowing = true;\r\n\r\n const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function(callback){window.setTimeout(callback, 1000/60);};\r\n window.requestAnimationFrame = requestAnimationFrame;\r\n const canvas = document.getElementById(\"snow\");\r\n const ctx = canvas.getContext(\"2d\");\r\n const flakeCount = snowConf.flakeCount;\r\n let mX = -100, mY = -100;\r\n let flakes = [];\r\n canvas.width = window.innerWidth;\r\n canvas.height = window.innerHeight;\r\n const snow = () => {\r\n if (!isSnowing) {\r\n return; // 结束\r\n }\r\n\r\n // 清空画布\r\n ctx.clearRect(0, 0, canvas.width, canvas.height);\r\n\r\n const minDist = snowConf.minDist;\r\n for (let i = 0; i < flakeCount; i++){\r\n let flake = flakes[i];\r\n const x = mX, y = mY;\r\n const x2 = flake.x, y2 = flake.y;\r\n const dist = Math.sqrt((x - x2)*(x - x2) + (y - y2)*(y - y2));\r\n if (dist < minDist) {\r\n const force = minDist / (dist*dist);\r\n const xcomp = (x - x2) / dist;\r\n const ycomp = (y - y2) / dist;\r\n const deltaV = force / 2;\r\n flake.velX -= deltaV * xcomp;\r\n flake.velY -= deltaV * ycomp;\r\n } else {\r\n flake.velX *= 0.98;\r\n if (flake.velY < flake.speed && (flake.speed - flake.velY > .01)) {\r\n flake.velY += (flake.speed - flake.velY) * .01;\r\n }\r\n flake.velX += Math.cos(flake.step += .05) * flake.stepSize;\r\n }\r\n ctx.fillStyle = \"rgba(\" + snowConf.color + \", \" + flake.opacity + \")\";\r\n flake.y += flake.velY;\r\n flake.x += flake.velX;\r\n if(flake.y >= canvas.height || flake.y <= 0){\r\n reset(flake);\r\n }\r\n if(flake.x >= canvas.width || flake.x <= 0){\r\n reset(flake);\r\n }\r\n ctx.beginPath();\r\n ctx.arc(flake.x, flake.y, flake.size, 0, Math.PI*2);\r\n ctx.fill();\r\n }\r\n requestAnimationFrame(snow);\r\n };\r\n const reset = (flake)=>{\r\n flake.x = Math.floor(Math.random()*canvas.width);\r\n flake.y = 0;\r\n flake.size = (Math.random()*3)+2;\r\n flake.speed = (Math.random()*1)+0.5;\r\n flake.velY = flake.speed;\r\n flake.velX = 0;\r\n flake.opacity = (Math.random()*0.5)+0.3;\r\n };\r\n // 初始化函数\r\n const init = () => {\r\n // 判断当前是否应该下雪\r\n const isSnowDisabled = localStorage.getItem('kr-disable-snow') !== null;\r\n if (isSnowDisabled || window.kr?.notMobile === false) {\r\n // 用户禁用了或者是移动端,就不下雪了\r\n isSnowing = false;\r\n canvas.classList.add('disabled');\r\n return;\r\n }\r\n\r\n startSnow();\r\n };\r\n const startSnow = () => {\r\n // 生成初始雪花\r\n for (let i = 0; i < flakeCount; i++) {\r\n const x = Math.floor(Math.random()*canvas.width);\r\n const y = Math.floor(Math.random()*canvas.height);\r\n const size = (Math.random()*3) + snowConf.size;\r\n const speed = (Math.random()*1) + snowConf.speed;\r\n const opacity = (Math.random()*0.5) + snowConf.opacity;\r\n flakes.push({\r\n speed: speed,\r\n velX: 0, velY: speed,\r\n x: x, y: y,\r\n size: size,\r\n stepSize: (Math.random()) / 30 * snowConf.stepsize,\r\n step: 0,\r\n angle: 180,\r\n opacity: opacity\r\n });\r\n }\r\n // 开始下雪\r\n snow();\r\n };\r\n // 雪花避让鼠标\r\n document.addEventListener(\"mousemove\", (e)=>{mX = e.clientX, mY = e.clientY});\r\n // 窗口大小调整\r\n window.addEventListener(\"resize\",()=>{canvas.width = window.innerWidth; canvas.height = window.innerHeight;});\r\n // 监听下雪状态改变事件\r\n document.getElementById(\"snow-switch\")?.addEventListener(\"click\", () => {\r\n // 下雪状态按钮被点击,切换下雪状态\r\n if (isSnowing) {\r\n // 停停停\r\n setTimeout(() => {\r\n // 让淡化动画播放完成后再停止\r\n isSnowing = false;\r\n }, 600);\r\n localStorage.setItem('kr-disable-snow', true);\r\n canvas.classList.add('disabled');\r\n } else {\r\n // 来了来了\r\n isSnowing = true;\r\n localStorage.removeItem('kr-disable-snow');\r\n canvas.classList.remove('disabled');\r\n\r\n startSnow();\r\n }\r\n });\r\n\r\n // 初始化\r\n init();\r\n})();"]} |