• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

jshistory-cn

🇨🇳 《JavaScript 二十年》中文版
TypeScript
4,205
star
2

beam

✨ Expressive WebGL
JavaScript
516
star
3

react-ssd1306

📟 A React Renderer for SSD1306 OLED chip on Raspberry Pi.
C
360
star
4

mocha1995

☕️ The world's first JavaScript engine written in 1995 by Brendan Eich, now compiled back to JS and WASM!
C
290
star
5

sinomap

🌎 Super lightweight canvas map lib.
JavaScript
107
star
6

webgl-seminar

代码清晰、直接、可追溯的一系列 WebGL 示例
JavaScript
105
star
7

learn-wgpu-cn

🇨🇳 《Learn Wgpu》中文版
Rust
104
star
8

nativebird

🐦 Bluebird alternative within ~200 loc
JavaScript
78
star
9

minimal-js-runtime

A toy JavaScript runtime based on QuickJS and libuv.
C
66
star
10

bumpover

🚧 Async data transforming with simple rules.
JavaScript
65
star
11

merry8

📺 Chip-8 emulator for web.
JavaScript
53
star
12

vue-cmap

Vue China map visualizing component, supports drilldown and lazy loading.
JavaScript
39
star
13

fe-native-lang

Native language guide for FE developers.
C
31
star
14

crdt-and-local-first

A slide - WIP
Vue
28
star
15

HTML-Toy-Parser

一个玩具级的 HTML 转虚拟 DOM 编译器
JavaScript
27
star
16

ove-lang

👓 ove-lang, a language for his true fans.
HTML
26
star
17

vue-springbud

不是最简洁的 Vue 生产环境模板
JavaScript
24
star
18

MiyooSDK

🐳 Docker environment for developing Miyoo Linux apps.
Dockerfile
23
star
19

blog

📝 My tech blog.
JavaScript
22
star
20

psp-js

Modern JavaScript runtime for Sony PSP, based on rust-psp and QuickJS.
Rust
19
star
21

nano-mvc

Demo MVC framework in 40 lines, 1KB
JavaScript
18
star
22

slate-doc-cn

🇨🇳 Translation of Slate.js official doc.
14
star
23

rx-elevator-demo

Reactive Demo
JavaScript
14
star
24

flylog

Front end AOP logging and monotoring tool.
Vue
13
star
25

naming-style-demo

前端框架命名风格比较
JavaScript
12
star
26

learn-cs

💾 Resources learning basics.
Assembly
11
star
27

ustc-gpa

GPA calculator for USTCers
JavaScript
10
star
28

naiveScroll

Tiny jQuery full page scroll effect plugin.
JavaScript
10
star
29

repeater

📼 Record browser events as visual test case.
JavaScript
9
star
30

js-framework-intro

JavaScript 框架设计入门
9
star
31

render-adapters-poc

POC for customizing UI framework renderers.
JavaScript
6
star
32

gomoku

JavaScript Gomuku AI for Web
JavaScript
5
star
33

eslint-plugin-pangu-comment

Pangu whitespace for Chinese comments.
JavaScript
4
star
34

zlayer

⚡️ Image render layer with GPU acceleration.
JavaScript
4
star
35

psp-test-app

Simple test app based on rust-psp
Rust
4
star
36

icard-ustc

Consume record analyzer for USTCers
Python
3
star
37

examples

Static HTML examples just for fun :)
HTML
2
star
38

pages-cn

My Blog Dist HTML Pages
HTML
2
star
39

compilExpt

Experimental compiler-related JS project.
JavaScript
2
star
40

rollup-scaffold

A simple rollup config scaffold
JavaScript
2
star
41

visue

vue visualizing dev tool
JavaScript
2
star
42

slate-playground

Pluggable editor playground.
JavaScript
1
star
43

sikuli-coc

HTML
1
star
44

ck.js

cookie lib in 2 lines
JavaScript
1
star
45

legs

Light Easy Gulp Scaffold
JavaScript
1
star
46

n7books

Second hand book exchange platform for USTCers
PHP
1
star
47

nano-computed

explain how computed works in 30 lines.
JavaScript
1
star
48

imdoc

markdown documentation generator
CSS
1
star
49

Markdown-Table-Converter

Edit and reformat markdown table.
JavaScript
1
star
50

tennis-match-recorder

Tennis match recorder and more
JavaScript
1
star
51

ustc-ring

Graduation Ring Exchange Platfrom for USTCers
HTML
1
star