Giter Site home page Giter Site logo

purescript-3d-cube's Introduction

PureScript 3D Cube

Try it out here

This was my attempt at learning the basics of PureScript and functional programming for the first time by making a 3D rotating cube in the browser that the user can interact with.

What it does

The PureScript code, once compiled, generates a JavaScript file. This file must be included in an HTML page (along with JQuery) and it draws a cube in the center of the page. The user can use their mouse to rotate the cube around it's 3 axes by clicking-and-dragging with their mouse.

Swiping the mouse quickly while clicking and dragging generates a velocity that is added to the cube so that the cube starts spinning. Swiping while the cube is already in motions just adds the new velocity to the current velocity (both magnitude and direction).

Clicking also generates friction on the cube and slows it down. The longer the mouse is held, the more the cube decelerates until standstill.

Project Structure

The main directories here are

  1. src/: Contains the PureScript source files.

    • Main.purs is the entry point of the app. It draws the cube and handles mouse events and cube rotation.

    • Matrices.purs contains some data structures and functions to deal with the transformation matrix (matrix3d) and rotation vector (rotate3d) in CSS including converting them between a Matrix format and String form for the CSS transform property.

  2. docs/: Contains the output of the code to be viewed in a browser.

    • docs/index.html can be accessed from the GitHub pages link of this project.

    • docs/js/cube.js contains the compiled js file obtained by compiling the PureScript code in src/.

    • (older commits)docs/reference/: I used this folder to place an HTML, CSS and JavaScript file for me to try out and test ideas before trying to implement them in PureScript.

  3. bower_components/: I extended the Foreign Function Interface of the Control.Monad.Eff.JQuery module from the purescript-jquery package to include a function (getCss) to get CSS properties from a node since it only contained a function to set CSS properties.

Global constants

There are some global constants in Main that can be changed to affect the cube behavior.

  • framesPerSecond: Specifies the expected frames drawn per second by the browser. Used to calculated the required degrees/frame for cube rotation given the degrees/second.

  • speedSensitivity: Specifies the interval at which the mouse position is polled in milliseconds. A lower value means the mouse must be idle for a longer period of time before letting go of the mouse button to be registered as a velocity of 0. Default value is 25 milliseconds.

  • rotationScale: Multiplied by the mouse velocity of pixels/second to convert into degrees/sec. A value of 1.0 means 1 pixel/sec is taken as 1 degree/second. Default value is 0.4

  • decelRate: Specifies the rate at which the cube should slow down from touch friction in degrees/frame2 (divide degrees/sec2 value by framesPerSecond to get degrees/frame2). Default value is 20 degrees/sec2 (~0.33 degrees/frame2).

Compiling

  1. Compile the source code by using

    pulp build -O --to /path/to/output.js
    
  2. Create a blank HTML file that includes at least 2 scripts: JQuery and the output script from step 1.

  3. Open the HTML file in a browser.

purescript-3d-cube's People

Contributors

georgevarghese185 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.