• Stars
    star
    455
  • Rank 93,875 (Top 2 %)
  • Language
    CoffeeScript
  • Created over 11 years ago
  • Updated about 7 years ago

Reviews

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

Repository Details

convert your DOM into physical objects using this awesome jQuery wrapper around box2d-web

#box2d-jQuery

v0.8

hi, in a moment of madness i become an offical sponsor of the jQuery Europe 2013 conference in Vienna, well that was the moment i decided to code a jquery plugin i wish existed but strangly didn't - until now

jquery.box2d.js is a simple jquery plugin that transforms DOM elements into actual physical objects. well, physical in a flat 2d-browser-world that is. just go to

and start to KICK STUFF AROUND (with your mouse/touchpad pointer - or using your fingers on modern iOS devices.)

oh, and before i forget, stop by the jquery conference, it will be cool (i promise), and use the voucher code 'fullstackoptimization' for 5% off.

##acknowledgements

thx to box2dweb http://code.google.com/p/box2dweb/ which is the engine that works in the background.

thx to PÃ¥l Smitt-Amundsen i based this project heavily on the stuff i found in his blog (and lab experiments) and thx for giving me permission to reuse his stuff open source style.

if you have questions, bug-reports and/or feature requests, please use the github-issue tracker and/or ping me at twitter (@enzenhofer)

##how to contribute to box2d-jquery

please see BUILD.md

also there is currently a RaspberryPi4PullRequests Challenge

##how to use jquery.box2d.js

that's simple

i.e.: $("h1, div, img").box2d({'y-velocity':5}); or if you want more details just look at the source of hello-world.html

  <!doctype html><!-- it's HTML5 time -->

  <html>
  <head><title>hello world box2d-jquery example</title>
  </head>

  <style>
    /* it's a good idea to give block elements a width, otherwise they go all the way (to the width of theire parent)*/
    h1 {background-color: lightgray; width: 500px;}
    div {width: 500px; background-color: lightgray;}
  </style>

  <body>

  <h1>Hello World Lorem Ipsum</h1>

  <!-- it's a very good idea to give img an width and height, as physical 2d objects need them to make any sense -->
  <img src="http://placekitten.com/400/300" width="400px" height="300px">

  <div>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat ...</div>
  
  <!-- it's a jquery plugin, so make sure you include jquery -->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

  <!-- include the production version of jquery.box2d.js -->
  <script src="./js/lib/jquery.box2d.min.js"></script>

  <!-- your script starts here -->
  <script> 
    //wait until the DOM is ready, otherwise strange things might happen 
    $(document).ready(function() { 
      //select DOM nodes and make them fall down with a speed of 5px per step 
      $("h1, div, img").box2d({'y-velocity':5});
    });
  </script>
  <!-- that's it -->

  </body>
  </html>

so well, yes, after you have a DOM ready and included all the necessary dependencies (jquery and jquery.box2d.js) all you have to do it

  $("#some_element").box2d({'y-velocity':10});

with this simpel jquery function call, you

  • initalize a 2d world
  • with a top (window top), left (window left), right (document width) and bottom (document height) boundary
  • where everything falls down, with about 10px per step
  • and you made the element with the id 'some_element' part of that cruel world

it is important to note, that every first call to .box2d() initalized the world. every other, follow up call only adds stuff to this world (well, we have to create a world sometime)

.box2d() takes a few options

  {
  'y-velocity': 10, /* everything falls down, default 0 */
  'x-velocity': 10, /* everything falls right, default 0 */
  'debug': false, /* fancy debug modus using canvas, default false */
  'static': false, /* true ... it stays | false ... it moves, default false */
  'density': 1.2 ,/* think weight (relational to its size) between 0 and n, default 1.5 */
  'restitution': 0.4,/* think: bounciness, from 0 to 1, default 0.4 */
  'friction': 0.3, /* think: slideiness, from 0 to 1, default 0.3 */
  'shape': 'box' /* default "box", but could also be a "circle", it it's not a "circle", it's a "box"*/
  }

if you want to be more specific about density, restitution and friction i recommend the great box2d manual.

Density
The fixture density is used to compute the mass properties of the parent body. The density can be zero or positive. You should generally use similar densities for all your fixtures. This will improve stacking stability.

Friction
Friction is used to make objects slide along each other realistically. Box2D supports static and dynamic friction, but uses the same parameter for both. Friction is simulated accurately in Box2D and the friction strength is proportional to the normal force (this is called Coulomb friction). The friction parameter is usually set between 0 and 1, but can be any non-negative value. A friction value of 0 turns off friction and a value of 1 makes the friction strong. When the friction force is computed between two shapes, Box2D must combine the friction parameters of the two parent fixtures. So if one fixture has zero friction then the contact will have zero friction.

Restitution
Restitution is used to make objects bounce. The restitution value is usually set to be between 0 and 1. Consider dropping a ball on a table. A value of zero means the ball won't bounce. This is called an inelastic collision. A value of one means the ball's velocity will be exactly reflected. This is called a perfectly elastic collision.

about the world initialization

with the first .box2d() call you initalize the world. currently

  • y-velocity
  • x-velocity
  • debug

are first call / world initialization only calls.

with the world world initialization you create boundaries. these are invisible and static (unmoveable) and try to mimic the "normal" HTML-document boundaries.

the boundaries are objects like any other object (but invisble and unmoveable), so the have the density, restitution and friction of the first .box2d() call (default: density=1.5, restitution=0.4, friction=0.3)

things to mind

ok, this is very important, you should read this twice:

things must have a width and height and it's a good idea to set them explicit.

ok, you say no big deal, but well. take the simple a simpel H1 or even a simple DIV element. i.e.: take a look at development-setup.html (debug enabled) the H1 goes all the way, the whole width of it's parent. that's because it's a block element. these things do stuff like this, but normaly you don't see it (and you don't see the box aroun the block element). so the H1 will float strangely to the not-DOM-box-model experienced user. give it a width and everything is fine.

also IMAGES if you don't give image a widht or height, either via attributes or css-styles, they do not have any height or widht - until they are fully loaded. this can take a while, and can lead to strange effects. sometimes everything will work fine, othertimes everything is slightly screwed up. give your images an explicit width and height and everthing will be fine (and faster, too).

in general it works best on not deeply nested HTML, sometimes there are strange behaviours and/or performance issues when selecting nestes HTML elements. this must be solved in future version.

box2d-jquery will complain on the console that it found anything without width and height.

look ma HTML attributes!!!

yip, they are back! you can define static, shape, density, restitution and friction for each dom element.

  <div data-box2d-static='true' style="widht:200px; height:200px;"></div>
  <div data-box2d-shape='circle' style="widht:200px; height:200px;"></div>
  <div data-box2d-density="1.5" box2d-restitution="0.4" box2d-friction="0.3" style="widht:200px; height:200px;"></div>

note: these are just inital box2d object creation values, changing these attribute later (i.e. via jquery) has (currently) no effect.

HTML attributes > jquery options assignment, nuff said!

ok, that's bascally it. any question? bugs (there are some, especially on non-newest-stuff browsers (i.e.: ipad 1))? other stuff? please use the issue tracker, i'm collectiong bugs right now.

##in a nutshell, how does it work? ok, you want the itty-gritty technical details well, look at the source, that's the only real way. but in short

  • we clone the selected DOM elements into absolute possitioned ... well ... clones.
  • we animate them via dynamically (javascript styly) set CSS3 transforms / translate / rotate combos
  • the values for this CSS/javascript mambo-jumbo are calculted via box2d-web

TODOs

  • better touch handling (currently scroll and links are not useable on touch devices)
  • better android support
  • get touch handling out of core
  • better bodysnatch of nested HTML elements
  • performance profiling
  • collision detection
  • colliosn dection events
  • make y/x-volocity changeable after world initialization
  • change world boundaries with window resize
  • changes in the box2d HTML attributes should change the objects

##license Copyright (C) 2012 - 305678 Franz Enzenhofer (see in source code licenses for other copyright holders)

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

More Repositories

1

lsd

LSD - line-square-dot: an addicting game
JavaScript
158
star
2

bubbles

little unfinished particles game - MIT license
CoffeeScript
79
star
3

f19n-obtrusive-livetest

A sandboxed, extendable testing chrome extension and framework! It runs pre-defined and custom tests on each page that you visit.
JavaScript
55
star
4

ddd-legacy

ddd - dot dot dot - a game of dots
JavaScript
54
star
5

qrpwd

qrcode password protected
JavaScript
52
star
6

lalo.li

short voice message service
JavaScript
36
star
7

miniqr

CoffeeScript
24
star
8

searchbydrawing

search google images by drawing
JavaScript
22
star
9

nolog

a node based event driven real time logfile parser
JavaScript
21
star
10

robotstxt

robots.txt parser coffeescript
CoffeeScript
21
star
11

parseUri

parseUri is a function which splits any well-formed URI into its parts, all of which are optional. Its combination of accuracy, flexibility, and brevity is unrivaled.
JavaScript
18
star
12

thisismy

JavaScript
16
star
13

rewriteai

JavaScript
9
star
14

onsnap.js

makes you snap (and throws a CustomEvent in case you do)
JavaScript
8
star
15

search-by

search the internet using drawings & webcam
CoffeeScript
7
star
16

thisismy-extension

JavaScript
7
star
17

whatlang

simple language detection lib for node.js
6
star
18

Spiegel

an HTML5 mirror - lets make old scholl analog mirrors superfluous!!!
CoffeeScript
5
star
19

vienna.js

everything about vienna.js
CoffeeScript
5
star
20

apimaker

a node js URI-string to API converter
JavaScript
4
star
21

car

CoffeeScript
4
star
22

webmaster-tools-downloads

webmaster-tools-downloads/
Python
4
star
23

social-proof-no-dependencies

CoffeeScript
3
star
24

webrobotstxt

web interface to the robotstxt parser
CoffeeScript
3
star
25

gol

Game of Life
JavaScript
3
star
26

lighthouse-script

JavaScript
3
star
27

understanding-seo

Book "Understanding SEO"
3
star
28

canio

simple functional canvas effects lib
CoffeeScript
2
star
29

stripeq1

text only view bookmarklet for google cache
2
star
30

tupalocomapi

JavaScript
2
star
31

Sinne

make getUserMedia simple with some high level abstractions
CoffeeScript
2
star
32

blanka

simple audio visualizer (with getUserMedia support)
CoffeeScript
2
star
33

cms--

a bare bones cms with even less features in node.js
2
star
34

spinner

JavaScript
2
star
35

webcambooth

a flash webcambooth
ActionScript
2
star
36

search-by-writting

JavaScript
2
star
37

batch

process functions in a batch
JavaScript
2
star
38

ReadFileBy

ReadFileBy.Paste/Drop/FileSelect
2
star
39

seostartpage

a simple startpage seo tool
1
star
40

farbzauber

transform any image in a magic coloring page
1
star
41

jsconfeu

jsconfeu twitter wall on the command line
CoffeeScript
1
star
42

canwaste

an <canvas> lib that loves to waste <canvas>
JavaScript
1
star
43

franzenzenhofer.github.com

franzenzenhofer.github.com
JavaScript
1
star
44

masel

masel - ein tropfen privacy - a drop of privacy
JavaScript
1
star
45

snappyturtle

JavaScript
1
star
46

countdown

a countdown
CoffeeScript
1
star
47

qfi

a function processing queue for node.js
JavaScript
1
star
48

viennajs-app

JavaScript
1
star
49

drinkfinder

find your next drink
JavaScript
1
star
50

jquery.franz.coffee

making HTML5 in-browser drag&drop, copy&paste, fileselect useable
CoffeeScript
1
star
51

FE

FE functional canvas editing
CoffeeScript
1
star
52

wegifm

gif
JavaScript
1
star
53

chromata-effects

fork of chromata, based on the very awesome http://www.michaelbromley.co.uk/experiments/chromata/
JavaScript
1
star
54

box2audio

box2djquery and the audio api
JavaScript
1
star
55

ffflink---franz-freaky-fast-link-prefetch-hints-

https://plus.google.com/+IlyaGrigorik/posts/ahSpGgohSDo
CoffeeScript
1
star
56

simple-functional-robots-txt-parser

JavaScript
1
star
57

franz

Franz.coffee - a Functional HTML5 Canvas Photo Effects Lib in CoffeeScript (Hipster Alert)
CoffeeScript
1
star
58

copy-and-fetch

HTML
1
star
59

circles

HTML
1
star