当前位置:   article > 正文

程序员的浪漫——给她专属的圣诞树_程序员给树命名

程序员给树命名

人名定制

此次分享的圣诞节祝福代码,除了自己上传音乐之后,还可以添加祝福人名,效果如下:

教程

  1. 获取压缩包之后,解压得到一个"圣诞树代码.html"文件

  2. 把html重命名为"圣诞树代码.txt"

  3. 使用文本文件打开"圣诞树代码.txt"

  4. 通过搜索工具(快捷键ctrl + f)搜索关键字:圣诞节快乐。在前面添加你要送达祝福的人名,如“菲菲,”

  1. 保存(快捷键ctrl + s)

  2. 再把"圣诞树代码.txt"重命令为"圣诞树代码.html"

代码下载

  1. <!DOCTYPE html>
  2. <html lang="en" >
  3. <!-- <a href="https://jq.qq.com/?_wv=1027&k=9qBOcgnG" style="position: fixed;top: 10%;right: 10px;width: 40px;height: 40px;z-index: 999;background: #acea1147;line-height: 35px;border-radius: 50%;padding: 2.5px;text-align: center;color: #ddd;">更多</a> -->
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>圣诞快乐 - Musical Christmas Lights</title>
  7. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
  8. <link rel="stylesheet" href="./style.css">
  9. <style>
  10. body, html {
  11. width: 100%;
  12. height: 100%;
  13. overflow: hidden;
  14. font-family: "Press Start 2P", cursive;
  15. }
  16. .center {
  17. position: absolute;
  18. top: 0;
  19. bottom: 0;
  20. left: 0;
  21. right: 0;
  22. color: #fff;
  23. display: flex;
  24. align-items: center;
  25. }
  26. .center .text-center {
  27. text-align: center;
  28. width: 100%;
  29. }
  30. * {
  31. box-sizing: border-box;
  32. }
  33. body {
  34. margin: 0;
  35. height: 100vh;
  36. overflow: hidden;
  37. display: flex;
  38. align-items: center;
  39. justify-content: center;
  40. background: #161616;
  41. color: #c5a880;
  42. font-family: sans-serif;
  43. }
  44. label {
  45. display: inline-block;
  46. background-color: #161616;
  47. padding: 16px;
  48. border-radius: 0.3rem;
  49. cursor: pointer;
  50. margin-top: 1rem;
  51. width: 300px;
  52. border-radius: 10px;
  53. border: 1px solid #c5a880;
  54. text-align: center;
  55. }
  56. ul {
  57. list-style-type: none;
  58. padding: 0;
  59. margin: 0;
  60. }
  61. .btn {
  62. background-color: #161616;
  63. border-radius: 10px;
  64. color: #c5a880;
  65. border: 1px solid #c5a880;
  66. padding: 16px;
  67. width: 300px;
  68. margin-bottom: 16px;
  69. line-height: 1.5;
  70. cursor: pointer;
  71. }
  72. .separator {
  73. font-weight: bold;
  74. text-align: center;
  75. width: 300px;
  76. margin: 16px 0px;
  77. color: #a07676;
  78. }
  79. .title {
  80. color: #a07676;
  81. font-weight: bold;
  82. font-size: 1.25rem;
  83. margin-bottom: 16px;
  84. }
  85. .text-loading {
  86. font-size: 2rem;
  87. }
  88. </style>
  89. </head>
  90. <body>
  91. <!-- partial:index.partial.html -->
  92. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/build/three.min.js"></script>
  93. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/EffectComposer.js"></script>
  94. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/RenderPass.js"></script>
  95. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/ShaderPass.js"></script>
  96. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/shaders/CopyShader.js"></script>
  97. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/shaders/LuminosityHighPassShader.js"></script>
  98. <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/UnrealBloomPass.js"></script>
  99. <div id="mainWindow">
  100. <div id="overlay">
  101. <ul>
  102. <li class="title">选一首音乐吧</li>
  103. <li>
  104. <button class="btn" id="btnA" type="button">
  105. Jingle Bells
  106. </button>
  107. </li>
  108. <li><button class="btn" id="btnB" type="button">圣诞结 - 陈奕迅</button></li>
  109. <!-- <li><button class="btn" id="btnC" type="button">No room at the inn by TRG Banks</button></li>
  110. <li><button class="btn" id="btnD" type="button">Jingle Bell Swing by Mark Smeby</button></li>
  111. <li class="separator">OR</li>
  112. <li><button class="btn" id="btnB" type="button">Last Christmas by Tailor Swift, for my girl.</button></li> -->
  113. <li class="separator">或者</li>
  114. <li>
  115. <input type="file" id="upload" hidden />
  116. <label for="upload">上传音乐文件</label>
  117. </li>
  118. </ul>
  119. </div>
  120. </div>
  121. <!-- partial -->
  122. <script>
  123. const { PI, sin, cos } = Math;
  124. const TAU = 2 * PI;
  125. const map = (value, sMin, sMax, dMin, dMax) => {
  126. return dMin + ((value - sMin) / (sMax - sMin)) * (dMax - dMin);
  127. };
  128. const range = (n, m = 0) =>
  129. Array(n)
  130. .fill(m)
  131. .map((i, j) => i + j);
  132. const rand = (max, min = 0) => min + Math.random() * (max - min);
  133. const randInt = (max, min = 0) => Math.floor(min + Math.random() * (max - min));
  134. const randChoise = (arr) => arr[randInt(arr.length)];
  135. const polar = (ang, r = 1) => [r * cos(ang), r * sin(ang)];
  136. let scene, camera, renderer, analyser;
  137. let step = 0;
  138. const uniforms = {
  139. time: { type: "f", value: 0.0 },
  140. step: { type: "f", value: 0.0 },
  141. };
  142. const params = {
  143. exposure: 1,
  144. bloomStrength: 0.9,
  145. bloomThreshold: 0,
  146. bloomRadius: 0.5,
  147. };
  148. let composer;
  149. const fftSize = 2048;
  150. const totalPoints = 4000;
  151. const listener = new THREE.AudioListener();
  152. const audio = new THREE.Audio(listener);
  153. document.querySelector("input").addEventListener("change", uploadAudio, false);
  154. const buttons = document.querySelectorAll(".btn");
  155. buttons.forEach((button, index) =>
  156. button.addEventListener("click", () => loadAudio(index))
  157. );
  158. function init() {
  159. // const overlay = document.getElementById("overlay");
  160. // overlay.remove();
  161. content = '菲菲, 圣诞节快乐';
  162. document.getElementById("overlay").innerHTML = '<div class="center"><div class="text-center"><h1>'+content+'</h1></div></div>';
  163. scene = new THREE.Scene();
  164. renderer = new THREE.WebGLRenderer({ antialias: true });
  165. renderer.setPixelRatio(window.devicePixelRatio);
  166. renderer.setSize(window.innerWidth, window.innerHeight);
  167. document.body.appendChild(renderer.domElement);
  168. camera = new THREE.PerspectiveCamera(
  169. 60,
  170. window.innerWidth / window.innerHeight,
  171. 1,
  172. 1000
  173. );
  174. camera.position.set(-0.09397456774197047,-2.5597086635726947,24.420789670889008)
  175. camera.rotation.set(0.10443543723052419,-0.003827152981119352,0.0004011488708739715)
  176. const format = renderer.capabilities.isWebGL2
  177. ? THREE.RedFormat
  178. : THREE.LuminanceFormat;
  179. uniforms.tAudioData = {
  180. value: new THREE.DataTexture(analyser.data, fftSize / 2, 1, format),
  181. };
  182. addPlane(scene, uniforms, 3000);
  183. addSnow(scene, uniforms);
  184. range(10).map((i) => {
  185. addTree(scene, uniforms, totalPoints, [20, 0, -20 * i]);
  186. addTree(scene, uniforms, totalPoints, [-20, 0, -20 * i]);
  187. });
  188. const renderScene = new THREE.RenderPass(scene, camera);
  189. const bloomPass = new THREE.UnrealBloomPass(
  190. new THREE.Vector2(window.innerWidth, window.innerHeight),
  191. 1.5,
  192. 0.4,
  193. 0.85
  194. );
  195. bloomPass.threshold = params.bloomThreshold;
  196. bloomPass.strength = params.bloomStrength;
  197. bloomPass.radius = params.bloomRadius;
  198. composer = new THREE.EffectComposer(renderer);
  199. composer.addPass(renderScene);
  200. composer.addPass(bloomPass);
  201. addListners(camera, renderer, composer);
  202. animate();
  203. }
  204. function animate(time) {
  205. analyser.getFrequencyData();
  206. uniforms.tAudioData.value.needsUpdate = true;
  207. step = (step + 1) % 1000;
  208. uniforms.time.value = time;
  209. uniforms.step.value = step;
  210. composer.render();
  211. requestAnimationFrame(animate);
  212. }
  213. function loadAudio(i) {
  214. if (i==4) {
  215. document.getElementById("overlay").innerHTML =
  216. '<div class="text-loading">这首歌献给我可爱的女朋友!一起来听吧...</div>';
  217. } else {
  218. document.getElementById("overlay").innerHTML =
  219. '<div class="text-loading">闭上双眼,默数三秒,圣诞节马上到来!...</div>';
  220. }
  221. const files = [
  222. // "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/no_curator/Simon_Panrucker/Happy_Christmas_You_Guys/Simon_Panrucker_-_01_-_Snowflakes_Falling_Down.mp3",
  223. "https://cdn.jsdelivr.net/gh/koffuxu/cool-team/images/jinglebell.ogg",
  224. "https://cdn.jsdelivr.net/gh/koffuxu/cool-team/images/sdj.ogg",
  225. // "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/no_curator/Dott/This_Christmas/Dott_-_01_-_This_Christmas.mp3",
  226. // "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/TRG_Banks/TRG_Banks_Christmas_Album/TRG_Banks_-_12_-_No_room_at_the_inn.mp3",
  227. // "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Mark_Smeby/En_attendant_Nol/Mark_Smeby_-_07_-_Jingle_Bell_Swing.mp3",
  228. // // "./radios/last_christmas.mp3",
  229. // "./christmas_tree/radios/last_christmas.mp3",
  230. ];
  231. const file = files[i];
  232. const loader = new THREE.AudioLoader();
  233. loader.load(file, function (buffer) {
  234. audio.setBuffer(buffer);
  235. audio.play();
  236. analyser = new THREE.AudioAnalyser(audio, fftSize);
  237. init();
  238. });
  239. }
  240. function uploadAudio(event) {
  241. document.getElementById("overlay").innerHTML =
  242. '<div class="text-loading">圣诞节来了...</div>';
  243. const files = event.target.files;
  244. const reader = new FileReader();
  245. reader.onload = function (file) {
  246. var arrayBuffer = file.target.result;
  247. listener.context.decodeAudioData(arrayBuffer, function (audioBuffer) {
  248. audio.setBuffer(audioBuffer);
  249. audio.play();
  250. analyser = new THREE.AudioAnalyser(audio, fftSize);
  251. init();
  252. });
  253. };
  254. reader.readAsArrayBuffer(files[0]);
  255. }
  256. function addTree(scene, uniforms, totalPoints, treePosition) {
  257. const vertexShader = `
  258. attribute float mIndex;
  259. varying vec3 vColor;
  260. varying float opacity;
  261. uniform sampler2D tAudioData;
  262. float norm(float value, float min, float max ){
  263. return (value - min) / (max - min);
  264. }
  265. float lerp(float norm, float min, float max){
  266. return (max - min) * norm + min;
  267. }
  268. float map(float value, float sourceMin, float sourceMax, float destMin, float destMax){
  269. return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
  270. }
  271. void main() {
  272. vColor = color;
  273. vec3 p = position;
  274. vec4 mvPosition = modelViewMatrix * vec4( p, 1.0 );
  275. float amplitude = texture2D( tAudioData, vec2( mIndex, 0.1 ) ).r;
  276. float amplitudeClamped = clamp(amplitude-0.4,0.0, 0.6 );
  277. float sizeMapped = map(amplitudeClamped, 0.0, 0.6, 1.0, 20.0);
  278. opacity = map(mvPosition.z , -200.0, 15.0, 0.0, 1.0);
  279. gl_PointSize = sizeMapped * ( 100.0 / -mvPosition.z );
  280. gl_Position = projectionMatrix * mvPosition;
  281. }
  282. `;
  283. const fragmentShader = `
  284. varying vec3 vColor;
  285. varying float opacity;
  286. uniform sampler2D pointTexture;
  287. void main() {
  288. gl_FragColor = vec4( vColor, opacity );
  289. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  290. }
  291. `;
  292. const shaderMaterial = new THREE.ShaderMaterial({
  293. uniforms: {
  294. ...uniforms,
  295. pointTexture: {
  296. value: new THREE.TextureLoader().load(`https://assets.codepen.io/3685267/spark1.png`),
  297. },
  298. },
  299. vertexShader,
  300. fragmentShader,
  301. blending: THREE.AdditiveBlending,
  302. depthTest: false,
  303. transparent: true,
  304. vertexColors: true,
  305. });
  306. const geometry = new THREE.BufferGeometry();
  307. const positions = [];
  308. const colors = [];
  309. const sizes = [];
  310. const phases = [];
  311. const mIndexs = [];
  312. const color = new THREE.Color();
  313. for (let i = 0; i < totalPoints; i++) {
  314. const t = Math.random();
  315. const y = map(t, 0, 1, -8, 10);
  316. const ang = map(t, 0, 1, 0, 6 * TAU) + (TAU / 2) * (i % 2);
  317. const [z, x] = polar(ang, map(t, 0, 1, 5, 0));
  318. const modifier = map(t, 0, 1, 1, 0);
  319. positions.push(x + rand(-0.3 * modifier, 0.3 * modifier));
  320. positions.push(y + rand(-0.3 * modifier, 0.3 * modifier));
  321. positions.push(z + rand(-0.3 * modifier, 0.3 * modifier));
  322. color.setHSL(map(i, 0, totalPoints, 1.0, 0.0), 1.0, 0.5);
  323. colors.push(color.r, color.g, color.b);
  324. phases.push(rand(1000));
  325. sizes.push(1);
  326. const mIndex = map(i, 0, totalPoints, 1.0, 0.0);
  327. mIndexs.push(mIndex);
  328. }
  329. geometry.setAttribute(
  330. "position",
  331. new THREE.Float32BufferAttribute(positions, 3).setUsage(
  332. THREE.DynamicDrawUsage
  333. )
  334. );
  335. geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
  336. geometry.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
  337. geometry.setAttribute("phase", new THREE.Float32BufferAttribute(phases, 1));
  338. geometry.setAttribute("mIndex", new THREE.Float32BufferAttribute(mIndexs, 1));
  339. const tree = new THREE.Points(geometry, shaderMaterial);
  340. const [px, py, pz] = treePosition;
  341. tree.position.x = px;
  342. tree.position.y = py;
  343. tree.position.z = pz;
  344. scene.add(tree);
  345. }
  346. function addSnow(scene, uniforms) {
  347. const vertexShader = `
  348. attribute float size;
  349. attribute float phase;
  350. attribute float phaseSecondary;
  351. varying vec3 vColor;
  352. varying float opacity;
  353. uniform float time;
  354. uniform float step;
  355. float norm(float value, float min, float max ){
  356. return (value - min) / (max - min);
  357. }
  358. float lerp(float norm, float min, float max){
  359. return (max - min) * norm + min;
  360. }
  361. float map(float value, float sourceMin, float sourceMax, float destMin, float destMax){
  362. return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
  363. }
  364. void main() {
  365. float t = time* 0.0006;
  366. vColor = color;
  367. vec3 p = position;
  368. p.y = map(mod(phase+step, 1000.0), 0.0, 1000.0, 25.0, -8.0);
  369. p.x += sin(t+phase);
  370. p.z += sin(t+phaseSecondary);
  371. opacity = map(p.z, -150.0, 15.0, 0.0, 1.0);
  372. vec4 mvPosition = modelViewMatrix * vec4( p, 1.0 );
  373. gl_PointSize = size * ( 100.0 / -mvPosition.z );
  374. gl_Position = projectionMatrix * mvPosition;
  375. }
  376. `;
  377. const fragmentShader = `
  378. uniform sampler2D pointTexture;
  379. varying vec3 vColor;
  380. varying float opacity;
  381. void main() {
  382. gl_FragColor = vec4( vColor, opacity );
  383. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  384. }
  385. `;
  386. function createSnowSet(sprite) {
  387. const totalPoints = 300;
  388. const shaderMaterial = new THREE.ShaderMaterial({
  389. uniforms: {
  390. ...uniforms,
  391. pointTexture: {
  392. value: new THREE.TextureLoader().load(sprite),
  393. },
  394. },
  395. vertexShader,
  396. fragmentShader,
  397. blending: THREE.AdditiveBlending,
  398. depthTest: false,
  399. transparent: true,
  400. vertexColors: true,
  401. });
  402. const geometry = new THREE.BufferGeometry();
  403. const positions = [];
  404. const colors = [];
  405. const sizes = [];
  406. const phases = [];
  407. const phaseSecondaries = [];
  408. const color = new THREE.Color();
  409. for (let i = 0; i < totalPoints; i++) {
  410. const [x, y, z] = [rand(25, -25), 0, rand(15, -150)];
  411. positions.push(x);
  412. positions.push(y);
  413. positions.push(z);
  414. color.set(randChoise(["#f1d4d4", "#f1f6f9", "#eeeeee", "#f1f1e8"]));
  415. colors.push(color.r, color.g, color.b);
  416. phases.push(rand(1000));
  417. phaseSecondaries.push(rand(1000));
  418. sizes.push(rand(4, 2));
  419. }
  420. geometry.setAttribute(
  421. "position",
  422. new THREE.Float32BufferAttribute(positions, 3)
  423. );
  424. geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
  425. geometry.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
  426. geometry.setAttribute("phase", new THREE.Float32BufferAttribute(phases, 1));
  427. geometry.setAttribute(
  428. "phaseSecondary",
  429. new THREE.Float32BufferAttribute(phaseSecondaries, 1)
  430. );
  431. const mesh = new THREE.Points(geometry, shaderMaterial);
  432. scene.add(mesh);
  433. }
  434. const sprites = [
  435. "https://assets.codepen.io/3685267/snowflake1.png",
  436. "https://assets.codepen.io/3685267/snowflake2.png",
  437. "https://assets.codepen.io/3685267/snowflake3.png",
  438. "https://assets.codepen.io/3685267/snowflake4.png",
  439. "https://assets.codepen.io/3685267/snowflake5.png",
  440. ];
  441. sprites.forEach((sprite) => {
  442. createSnowSet(sprite);
  443. });
  444. }
  445. function addPlane(scene, uniforms, totalPoints) {
  446. const vertexShader = `
  447. attribute float size;
  448. attribute vec3 customColor;
  449. varying vec3 vColor;
  450. void main() {
  451. vColor = customColor;
  452. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  453. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  454. gl_Position = projectionMatrix * mvPosition;
  455. }
  456. `;
  457. const fragmentShader = `
  458. uniform vec3 color;
  459. uniform sampler2D pointTexture;
  460. varying vec3 vColor;
  461. void main() {
  462. gl_FragColor = vec4( vColor, 1.0 );
  463. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  464. }
  465. `;
  466. const shaderMaterial = new THREE.ShaderMaterial({
  467. uniforms: {
  468. ...uniforms,
  469. pointTexture: {
  470. value: new THREE.TextureLoader().load(`https://assets.codepen.io/3685267/spark1.png`),
  471. },
  472. },
  473. vertexShader,
  474. fragmentShader,
  475. blending: THREE.AdditiveBlending,
  476. depthTest: false,
  477. transparent: true,
  478. vertexColors: true,
  479. });
  480. const geometry = new THREE.BufferGeometry();
  481. const positions = [];
  482. const colors = [];
  483. const sizes = [];
  484. const color = new THREE.Color();
  485. for (let i = 0; i < totalPoints; i++) {
  486. const [x, y, z] = [rand(-25, 25), 0, rand(-150, 15)];
  487. positions.push(x);
  488. positions.push(y);
  489. positions.push(z);
  490. color.set(randChoise(["#93abd3", "#f2f4c0", "#9ddfd3"]));
  491. colors.push(color.r, color.g, color.b);
  492. sizes.push(1);
  493. }
  494. geometry.setAttribute(
  495. "position",
  496. new THREE.Float32BufferAttribute(positions, 3).setUsage(
  497. THREE.DynamicDrawUsage
  498. )
  499. );
  500. geometry.setAttribute(
  501. "customColor",
  502. new THREE.Float32BufferAttribute(colors, 3)
  503. );
  504. geometry.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
  505. const plane = new THREE.Points(geometry, shaderMaterial);
  506. plane.position.y = -8;
  507. scene.add(plane);
  508. }
  509. function addListners(camera, renderer, composer) {
  510. document.addEventListener("keydown", (e) => {
  511. const { x, y, z } = camera.position;
  512. // console.log(`camera.position.set(${x},${y},${z})`);
  513. const { x: a, y: b, z: c } = camera.rotation;
  514. // console.log(`camera.rotation.set(${a},${b},${c})`);
  515. });
  516. window.addEventListener(
  517. "resize",
  518. () => {
  519. const width = window.innerWidth;
  520. const height = window.innerHeight;
  521. camera.aspect = width / height;
  522. camera.updateProjectionMatrix();
  523. renderer.setSize(width, height);
  524. composer.setSize(width, height);
  525. },
  526. false
  527. );
  528. }
  529. </script>
  530. </body>
  531. </html>

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

闽ICP备14008679号