@curtishughes/rubiks-cube
Version:
<img src="assets/classic.gif" height="150px" />
1 lines • 4.75 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("three")):"function"==typeof define&&define.amd?define(["three"],t):"object"==typeof exports?exports.RubiksCube=t(require("three")):e.RubiksCube=t(e.three)}(self,(function(e){return(()=>{"use strict";var t={293:t=>{t.exports=e}},i={};function s(e){if(i[e])return i[e].exports;var n=i[e]={exports:{}};return t[e](n,n.exports,s),n.exports}s.d=(e,t)=>{for(var i in t)s.o(t,i)&&!s.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},s.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),s.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};return(()=>{s.r(n),s.d(n,{default:()=>o});var e,t=s(293);class i extends t.Mesh{constructor({position:e,materials:i}){super(new t.BoxGeometry(1,1,1),i),this.position.x=e.x,this.position.y=e.y,this.position.z=e.z}}!function(e){e.x="x",e.y="y",e.z="z"}(e||(e={}));class o{constructor(e,i,s=1e3,n="100%",o="100%"){this.materials=i,this.speed=s,this.locked=!1,this.camera=new t.PerspectiveCamera,this.camera.position.set(4,4,4),this.camera.lookAt(0,-.33,0),this.scene=new t.Scene,this.scene.add(...this.generateCubeCluster()),this.renderer=new t.WebGLRenderer({antialias:!0,canvas:e,alpha:!0}),this.renderer.domElement.style.width=n,this.renderer.domElement.style.height=o,this.resize(),this.render()}resize(){const e=this.renderer.domElement,t=window.devicePixelRatio,i=e.clientWidth*t|0,s=e.clientHeight*t|0;e.width===i&&e.height===s||(this.camera.aspect=e.clientWidth/e.clientHeight,this.camera.updateProjectionMatrix(),this.renderer.setSize(i,s,!1))}async F(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&1===e.position.z));await this.rotate(n,e.z,t,s)}async B(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&-1===e.position.z));await this.rotate(n,e.z,t,s)}async U(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&1===e.position.y));await this.rotate(n,e.y,t,s)}async D(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&-1===e.position.y));await this.rotate(n,e.y,t,s)}async L(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&-1===e.position.x));await this.rotate(n,e.x,t,s)}async R(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&1===e.position.x));await this.rotate(n,e.x,t,s)}async f(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&(1===e.position.z||0===e.position.z)));await this.rotate(n,e.z,t,s)}async b(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&(-1===e.position.z||0===e.position.z)));await this.rotate(n,e.z,t,s)}async u(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&(1===e.position.y||0===e.position.y)));await this.rotate(n,e.y,t,s)}async d(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&(-1===e.position.y||0===e.position.y)));await this.rotate(n,e.y,t,s)}async l(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&(-1===e.position.x||0===e.position.x)));await this.rotate(n,e.x,t,s)}async r(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i&&(1===e.position.x||0===e.position.x)));await this.rotate(n,e.x,t,s)}async x(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i));await this.rotate(n,e.x,t,s)}async y(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i));await this.rotate(n,e.y,t,s)}async z(t=!0,s=this.speed){const n=this.scene.children.filter((e=>e instanceof i));await this.rotate(n,e.z,t,s)}async rotate(e,i,s=!1,n){if(!this.locked){this.locked=!0;const o=e.reduce(((e,t)=>e.add(t)),new t.Object3D);this.scene.add(o),await this.rotateObject(o,i,s,n);for(let e=o.children.length-1;e>=0;e--){const t=o.children[e];this.scene.attach(t),t.position.set(Math.round(t.position.x),Math.round(t.position.y),Math.round(t.position.z))}this.scene.remove(o),this.locked=!1}}async rotateObject(i,s,n,o,r){return new Promise((async a=>{window.requestAnimationFrame((async c=>{const h=r||c,d=c-h,l=(n?-1:1)*t.MathUtils.degToRad(90*Math.min(d/o,1));switch(s){case e.x:i.rotation.set(l,0,0);break;case e.y:i.rotation.set(0,l,0);break;case e.z:i.rotation.set(0,0,l)}return d<=o?a(this.rotateObject(i,s,n,o,h)):a()}))}))}render(){window.requestAnimationFrame(this.render.bind(this)),this.renderer.render(this.scene,this.camera)}generateCubeCluster(){const e=[];for(let s=-1;s<2;s++)for(let n=-1;n<2;n++)for(let o=-1;o<2;o++){const r=new i({position:new t.Vector3(o,n,s),materials:this.materials});e.push(r)}return e}}})(),n})()}));