Giter VIP home page Giter VIP logo

freecube's Introduction

Freecube

⚛ Solve Rubik's Cube with WebGL in 10KB.

banner

Freecube renders and animates Rubik's Cube with raw WebGL, plus a tiny rule-based solver showing how CFOP works.

animate-demo

Usage

npm install freecube
import { Cube, Solver } from 'freecube'

const canvas = document.querySelector('#gl')
const cube = new Cube(canvas)
const solver = new Solver(cube)

cube.render(30, -45) // Render the cube with X and Y rotation.
cube.shuffle(20, true) // Shuffle it with animation.
solver.solve() // Generate solution with CFOP algorithm.

API

Cube

new Cube(canvas: CanvasElement, moves: Moves)

Main class emulating Rubik's Cube, it maintains cube state in this.blocks and optional WebGL instance in this.gl.

  • canvas - DOM canvas element for rendering, can be null for "headless" case.
  • moves - Array of cube moves, .e.g., ['R', 'U', 'F'].

animate

(move: String|Moves, duration: number) => Promise

Animate the cube moves with single or multi move, uses cube.move under the hood. you can set move speed with optional durataion args. The promise returned will be resolved on animation ends.

getBlock

(coord: Coord) => Block

Get block data in the cube. coord shapes as [0, 1, -1] and the block returns in { positions: Array, colors: Array } format. block.positions is the vertex positions of the block, and block.colors represents block colors in [F, B, U, D, R, L] sequence.

move

(move: String|Moves) => Cube

Update cube state with moves passed in, uses cube.rotate under the hood. Just use it as animate without animation. Chain calling like cube.move('U').move('R') is supported.

Only F / B / L / R / U / D and their counter moves can be used for now. "Advanced" turns like M / r / x / R2 are not supported.

rotate

(center: Coord, clockwise: boolean) => Cube

Rotate cube face in 90 degree. center coord is the center block of the face, clockwise for rotate direction.

render

(rX: number, rY: number, moveFace: String, moveAngle: number)

Renders the cube with rX and rY as overall rotation, and moveFace and moveAngle for a cube face rotation.

shuffle

(n: number, animate: false) => Cube|Promise

Shuffles the cube. Returns cube instance if animation not used, and promise on shuffle animation ends.

Solver

Rule-based module solving Rubik's Cube with CFOP algorithm.

solve

() => Array<Moves>

Solve the cube state with all CFOP steps. Returns array of 4 series of moves.

solveCross

() => Moves

Returns the moves to build a cross.

solveF2L

() => Moves

Returns the moves to build first two layers.

solveOLL

() => Moves

Returns the moves to pass OLL.

solvePLL

() => Moves

Returns the moves to pass PLL.

License

MIT

freecube's People

Contributors

doodlewind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

freecube's Issues

视角旋转操作优化

当用鼠标上下拖动把魔方翻到“背面”时,左右的拖动就反了,感觉很不自然。我见到过在任何角度都能自然地拖动的设计,在我的理解中这应该需要用四元数来表示视角并计算拖动(然而我并不懂这之后的数学了……

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.