Code Monkey home page Code Monkey logo

sprite3d.js's Introduction

Sprite3D.js [v2]

A library for generating and manipulating CSS 3D transforms

Overview

Sprite3D.js makes it easy to manipulate HTML elements through CSS 3D transforms using a ActionScript-like syntax and hierarchy, and a bunch of chainable accessor methods.

A Sprite3D object is a simple wrapper around a DOM element providing helper functions and properties. You can conveniently use CSS styling to determine the aspect of you element, and CSS transitions to animate your stuff.

In many cases, 3D transforms are GPU-accelerated, giving you an incredible performance boost.

Usage

###1 - Settle Begin by creating a Stage, which will be the root container of all your 3D stuff.

var stage = Sprite3D.stage(); //create a blank div centered in the page

or

var stage = Sprite3D.stage( document.getElementById("myContainer") );

###2 - Populate Create and position your sprites. Every time you finish altering an element's position, you should call the update() method. It applies the new transform informations in the page.

The manual update process was chosen for performance reasons, as it does not fire un-needed redraws of the DOM tree.

var sprite = Sprite3D.create() // you can provide an existing element if you don't want a blank div
	.className("kitten")
	.position( -200, 10, -300 )
	.rotation( 80, 0, 5 )
	.addEventListener( "mousedown", onMouseDown, false )
	.update()
stage.addChild( sprite );

###3 - Interact When listening for events, the handler function receives two arguments : the regular DOM event, and a reference to the target Sprite3D object.

function onMouseDown( event ):void
{
	event.target.z(1000).update();
	event.preventDefault();
}

Don't forget to use CSS transitions to animate the changes, so you don't need to set an interval to animate everything :)

Syntax update

v1: Sprite3D.createCenteredContainer()
v2: Sprite3D.stage()

var s = Sprite3D.create();
var scale = 2;

v1: s.setPosition( px, py, pz )
v2: s.position( px, py, pz )

v1: s.setScale( scale, scale, scale )
v2: s.scale( scale ) // or s.scale( 2, 2, 1 ) if different values are needed

v1: s.setRegistrationPoint( 23, 54, 0 )
v2: s.origin( 23, 54 ) // third parameter can be omitted

v1: s.setTransformOrigin( "50", "100" ) // in v1, you had to provide value as Strings
v2: s.transformOrigin( 50, 100 ) // or s.transformOrigin( "50%", "100%" )

I dropped the "set" prefix everywhere I could

  • setPosition() > position()
  • setScale() > scale()
  • setRotation() > rotation()
  • setX() > x()
  • setY() > y()

I changed to getter/setter functions "ร  la" jQuery. All getters are chainable.

  • setter: mySprite.x(200)
  • getter: var p = mySprite.x()
  • chaining: mySprite.x(200).rotationY(50).update()

##Browser support (Feb.2012)

###Desktop browsers :

  • Safari, Chrome are OK
  • Firefox 10+ has support, quality is improving with time
  • IE 10 has been announced with great support, needs testing
  • Opera has people working on 3D transforms (no release date)

###Mobile browsers:

  • iOS' Mobile Safari : OK
  • BlackBerry Tablet OS : OK (major performance progress with the 2.0 beta)
  • Android 4 : OK (Android 3 for tablets has a rather poor support)

##Credits

sprite3d.js's People

Contributors

boblemarin avatar drench avatar

Watchers

kbright817 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.