尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

《 Three.js 三阶魔方:完整版+字母贴纸+计时步数》

《 Three.js 三阶魔方:完整版+字母贴纸+计时步数》
📅 发布时间:2026/7/17 18:31:51

Three.js 三阶魔方【完整版+字母贴纸+计时步数】

<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><title>Three.js 三阶魔方 计时版</title><style>*{margin:0;padding:0;box-sizing:border-box;}body{overflow:hidden;background:#080c1a;font-family:微软雅黑;}canvas{display:block;}.top-ui{position:fixed;top:15px;left:15px;z-index:999;}.btn-group button{padding:8px 14px;margin:0 4px;border:none;border-radius:4px;background:#2378f3;color:#fff;cursor:pointer;}.btn-group button:hover{background:#1662d9;}.data-box{margin-top:10px;padding:8px 12px;background:rgba(0,0,0,0.4);border-radius:4px;color:#fff;font-size:14px;}.info-text{color:#b8c8e8;font-size:13px;margin-top:8px;line-height:1.6;}</style></head><body><divclass="top-ui"><divclass="btn-group"><buttonid="startTimer">开始计时</button><buttonid="stopTimer">暂停计时</button><buttonid="resetAll">全盘重置</button></div><divclass="data-box">用时:<spanid="timeText">00:00.00</span>&nbsp;&nbsp;步数:<spanid="stepText">0</span></div><divclass="info-text">W上 S下 A左 D右 Q前 E后<br>标识:U上 D下 L左 R右 F前 B后</div></div><scriptsrc="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/npm/three@0.158.0/examples/js/controls/OrbitControls.js"></script><script>// 场景初始化constscene=newTHREE.Scene();scene.background=newTHREE.Color(0x080c1a);constcamera=newTHREE.PerspectiveCamera(60,innerWidth/innerHeight,0.1,300);camera.position.set(12,12,19);constrenderer=newTHREE.WebGLRenderer({antialias:true});renderer.setSize(innerWidth,innerHeight);document.body.appendChild(renderer.domElement);// 视角控制constcontrols=newTHREE.OrbitControls(camera,renderer.domElement);controls.enableDamping=true;controls.dampingFactor=0.06;// 灯光scene.add(newTHREE.AmbientLight(0xffffff,0.5));constdirLight=newTHREE.DirectionalLight(0xffffff,0.8);dirLight.position.set(16,26,12);scene.add(dirLight);// 六面颜色+标识字母constfaceData=[{color:0xffffff,text:'F'},{color:0xffff00,text:'B'},{color:0x0077ff,text:'L'},{color:0xff2222,text:'R'},{color:0x00cc55,text:'U'},{color:0xff8800,text:'D'}];constgap=1.07;letcubeList=[];letrubik=newTHREE.Group();// 计时步数变量lettimerRunning=false;letstartTime=null;lettimerInterval=null;letmoveStep=0;consttimeText=document.getElementById('timeText');conststepText=document.getElementById('stepText');// 格式化时间functionformatTime(ms){lets=Math.floor(ms/1000);letmin=String(Math.floor(s/60)).padStart(2,'0');letsec=String(s%60).padStart(2,'0');letmsStr=String(Math.floor((ms%1000)/10)).padStart(2,'0');return`${min}:${sec}.${msStr}`;}// 开始计时document.getElementById('startTimer').onclick=()=>{if(timerRunning)return;timerRunning=true;startTime=Date.now();timerInterval=setInterval(()=>{letnow=Date.now()-startTime;timeText.innerText=formatTime(now);},10);};// 暂停计时document.getElementById('stopTimer').onclick=()=>{timerRunning=false;clearInterval(timerInterval);};// 生成字母贴图functioncreateTextTexture(text){constcanvas=document.createElement('canvas');canvas.width=64;canvas.height=64;constctx=canvas.getContext('2d');ctx.fillStyle='#fff';ctx.fillRect(0,0,64,64);ctx.fillStyle='#000';ctx.font='bold 40px Arial';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText(text,32,32);returnnewTHREE.CanvasTexture(canvas);}// 创建魔方小块functioncreateBlock(x,y,z){constgeo=newTHREE.BoxGeometry(0.93,0.93,0.93);constmats=[];faceData.forEach(item=>{letmat=newTHREE.MeshStandardMaterial({color:item.color,roughness:0.3,metalness:0.1});if(x===0&&y===0&&z===0)mat.map=createTextTexture(item.text);mats.push(mat);});constblock=newTHREE.Mesh(geo,mats);block.userData={x,y,z};block.position.set(x*gap,y*gap,z*gap);returnblock;}// 初始化魔方functioninitRubik(){cubeList=[];rubik.clear();for(letx=-1;x<=1;x++){for(lety=-1;y<=1;y++){for(letz=-1;z<=1;z++){rubik.add(createBlock(x,y,z));}}}scene.add(rubik);}initRubik();// 层旋转functionrotateLayer(axis,pos,dir=1){letgroup=newTHREE.Group();lettargets=[];rubik.children.forEach(c=>{letval=Math.round(c.position[axis]/gap);if(val===pos){targets.push(c);rubik.remove(c);group.add(c);}});scene.add(group);lettotal=Math.PI/2*dir;letcur=0;functionanim(){if(Math.abs(cur)>=Math.abs(total)){group.rotation[axis]=total;targets.forEach(c=>{letwp=newTHREE.Vector3();letq=newTHREE.Quaternion();c.getWorldPosition(wp);c.getWorldQuaternion(q);rubik.attach(c);c.position.copy(wp);c.quaternion.copy(q);});scene.remove(group);// 转动一次+1步moveStep++;stepText.innerText=moveStep;return;}cur+=0.17*dir;group.rotation[axis]=cur;requestAnimationFrame(anim);}anim();}// 键盘控制window.addEventListener('keydown',e=>{switch(e.key.toLowerCase()){case'w':rotateLayer('y',1,1);break;case's':rotateLayer('y',-1,-1);break;case'a':rotateLayer('x',-1,1);break;case'd':rotateLayer('x',1,-1);break;case'q':rotateLayer('z',1,1);break;case'e':rotateLayer('z',-1,-1);break;}});// 全盘重置document.getElementById('resetAll').onclick=()=>{clearInterval(timerInterval);timerRunning=false;timeText.innerText="00:00.00";moveStep=0;stepText.innerText="0";initRubik();};// 窗口适配window.onresize=()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();renderer.setSize(innerWidth,innerHeight);};// 渲染循环functionanimate(){requestAnimationFrame(animate);controls.update();renderer.render(scene,camera);}animate();</script></body></html>

最终全部功能汇总

✅ 标准六色三阶魔方
✅ 中心块 U/D/L/R/F/B 公式字母贴纸
✅ 磨砂真实魔方材质
✅ 键盘 W/A/S/D/Q/E 自由转层
✅ 鼠标拖拽视角 + 滚轮缩放
✅竞速计时系统(分:秒.毫秒)
✅自动统计转动步数
✅ 开始/暂停计时
✅ 一键全盘重置(魔方+时间+步数清零)
✅ 顺滑旋转动画
✅ 全屏自适应

直接保存 HTML 打开就能玩,完全对标线上魔方竞速小游戏~

相关新闻

  • Windows Server 2022网卡驱动配置与优化指南
  • 丢掉那些随时会断的爬虫:如何在生产环境构建一个高可用、零维护成本的多市场行情同步管道?
  • LSP Plugins插件格式对比:CLAP、LV2、VST2、VST3哪个最适合你?

最新新闻

  • 小程序毕设项目:校园传统戏曲文化推广微信小程序的设计与实现 戏曲剧目浏览与文化科普小程序的设计与实现 (源码+文档,讲解、调试运行,定制等)
  • 2026LOGO设计公司推荐排行 全场景适配评测榜 - 极欧测评
  • MCU 上电启动流程
  • 2026石嘴山危房鉴定检测怎么选?老旧房危房鉴定靠谱机构 TOP 结构安全检测+ 报告可查 电话汇总 - 中业金奢再生回收中心
  • Codex安装指南:Node.js环境配置与跨平台避坑实战
  • RA4M2与HS3003的I2C通信实现与优化

日新闻

  • 佛山青少年训练营推荐:军博营地实力顶尖 - 秋山寄远
  • 如何快速上手PvZ2 Gardendless:免费Web版植物大战僵尸2完整指南
  • jiuwen-deepsearch核心功能详解:规划-检索-反思三合一智能工作流

周新闻

  • IX9104 PCIe5.0 高速交换芯片@ACP#完整规格 + 应用场景总结
  • Unity游戏集成Coze智能体:实现NPC智能对话与知识库联动
  • SAP EPIC 建行回单查询:从标准类CL_EPIC_EXAMPLE_CN_CCB_GHTD到Z类的5处关键修改

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号