npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rubiks-js

v1.0.0

Published

A rubiks cube with no dependencies. Can use custom images for faces. The state of the cube can be safed.

Downloads

12

Readme

Rubiks Cube for the web

This library can be used to embed a rubiks cube into any website. Supports mouse and touch, is pure js typed with jsdoc and does not use any libraries.

Usage

First install

npm install rubiks

then create a rubiks cube and start it

import {RubiksCube, defaultTexture, defaultUVs, defaultHoveringColors} from 'rubiks-js'

const rubiksCube = new RubiksCube(
  attributeName,
  defaultTexture,
  defaultUVs,
  defaultHoveringColors,
  trackCenters
)
rubiksCube.start()

webgl is automaticly instanciated when first calling start().

attributeName

Specifies the data attribute on the canvas

<canvas data-rubiks-cube></canvas>
<!-- attributeName = 'data-rubiks-cube' -->

texture

Specifies how the sides of the cube look. Can be used to have pictures instead of plane colors.

uvs

Specifies which sticker (facelet) uses which part of the texture and is a 3 dimensional number array. The first dimension corresponds to a side. This means the indices should range from 0 to 5. Detailed description for side indices can be found here. The second dimension corresponds to one of the nine facelets on a side and should be between 0 and 8. A detailed description for facelet indices can be found here. The last dimenstion is the actual coordinate values and should consist of 4 consecutive x and y pairs. The order of these pairs is important and should either be clockwise or anticlockwise depending on the texture. You may have to experiment.

hoveringColors

Specifies a red, green and blue channel by which the colors of a side is multiplied when hovering over it.

trackCenters

This is only usefull when using images. It is a boolean and if set to true the rotation of all 6 centers is included in the state.

State

This library has a simple way of tracking the current state of the cube. Below is an example of how to store the state inside the url.

rubiks.on()

const url = new URL(location.toString())
const state = url.searchParams.get('state')
if (state != null) {
  rubiksCube.setState(state)
}

rubiksCube.on('change', event => {
  const url = new URL(location.toString())
  url.searchParams.set('state', event.state.toString())
  history.replaceState(null, '', url)
})
rubiksCube.start()

rubiksCube.setState returns true if the state was parsed correctly and false if there was an error.

rubiks.reset()

Resets the state of the cube.

Indices

Indices have the following order: (brackets use default values)

  • First from right (blue) to left (green)
  • Then from bottom (yellow) to top (white)
  • Then from front (red) to back (orange)

Side

| Side | Index | | --- | --- | | right | 0 | | left | 1 | | bottom | 2 | | top | 3 | | front | 4 | | back | 5 |

Facelet

This is a bit more complicated to explain. The indices follow the same rule as the sides. For example if you look at the front side without moving the cube index 0 would be the bottom right facelet and index 1 would be 1 to the left. Thats because the indices go first from left to right and the from bottom to top. This means index 0, 1 and 2 are the bottom row, 3, 4 and 5 the row above and so on. And remember always from left to right. In this example we were able to ignore the third index rule because these facelets where pointed at the front so it doesn't make sense to use this rule. Following the same logic we can always ignore on rule when figuring the facelet indices out.

If you want a visual way to see all indices use the following code and copy the numbers.png image to your source files

import {RubiksCube, defaultHovorvingColors} from 'rubiks-js'

const image = new Image()
image.src = 'number.png' // or 'https://raw.githubusercontent.com/pedeEli/rubiks-cube-v2/main/number.png'

const uvs = Array(6).fill(null).map((_, side) => {
  const bottom = 0.5 + Math.floor(side / 3) * 0.5
  const left = (side % 3) / 3
  return Array(9).fill(null).map((_, sideIndex) => {
    const b = bottom - (sideIndex % 3) / 6
    const t = b - 1 / 6
    const l = left + Math.floor(sideIndex / 3) / 9
    const r = l + 1 / 9

    return [
      l, b,
      r, b,
      r, t,
      l, t
    ]
  })
})

image.addEventListener('load', () => {
  const rubiksCube = new RubiksCube(
    'data-rubiks-cube',
    image,
    uvs,
    defaultHovorvingColors,
    true
  )

  rubiksCube.start()
})