Html 3D Cube Control

For a while I have been experimenting with creating 3D objects in a regular web browser without resorting to plugins such as Flash, Silverlight etc. The Cube shown here is adapted from another 3D website I made earlier.

WebGL enables us to render complex 3D models in a browser. However, here I will be looking at creating a 3D object using the various CSS3 transform commands that can take a regular 2D Html element and project it into 3D space.

The advantage of using 3D transforms on regular Html elements is you can project things like Iframe content, images or any other html element into 3D creating some really neat effects.

Compatibility

As the 3D cube shown here uses CSS3 Matrix Transforms rather than the plain Rotate, Scale, Translate methods used by some other 3D Cubes, it works well in IE11. As well as IE11, it has been tested in Firefox, Chrome, Safari and Mobile Safari for iOS.

Html Source

The important bit here is we include the "Cube.js" file. We also need JQuery, that is included as well.

The Div with the ID my3Dsurface will contain our 3D cube. Notice the data-facewidth="256" attribute. This specifies the size of our cube faces.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Css3 3D Sample</title>
    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/cube.js"></script>
    <script src="js/sample.js"></script>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
    <div id="my3Dsurface" class="js3dsurface" data-facewidth="256"></div>
</body>
</html>

Javascript Source

Next we create an instance of Js3DSurface and pass it an array of content that will be displayed on each face of our cube. In this example we are using images and simple html content. You can add more complex html such as Iframes, Video tags etc.

We also add 3 event handlers to enable us to spin the cube using the mouse. When the mouse moves with the left mouse button down, we call adjRot passing it the new X and Y position of the mouse.

var cube;
var mouseisdown = false;

$(document).ready(function () {
    var faces = new Array();
    faces.push('<img src="images/manic-miner-willy.jpg" />');
    faces.push('<img src="images/terminator-t-800-head-design.png" />');
    faces.push('<div class="plain"><h1>3D Cube!</h1><p>Some plain html content to be projected onto the side of the cube! Works in the latest versions of Chrome, Firefox, IE, Safari, Safari Mobile and other Html5 modern browsers.</div>');
    faces.push('<img src="images/sinclair-c5.jpg" />');
    faces.push('<img src="images/terminator-t-800-head-design.png" />');
    faces.push('<img src="images/sinclair-c5.jpg" />');

    cube = new Js3DSurface("my3Dsurface", faces);

    cube._backSurface.addEventListener('mousemove', mousemove, false);
    document.body.addEventListener('mousedown', mousedown, false);
    document.body.addEventListener('mouseup', mouseup, false);
});

function mousedown(e) {
    mouseisdown = true;
    if (e != null) {
        e.stopPropagation();
        e.preventDefault();
    }

    return false;
}

function mouseup(e) {
    mouseisdown = false;
    if (e != null) {
        e.stopPropagation();
        e.preventDefault();
    }
    return false;
}

function mousemove(e) {
    if (mouseisdown) {
        var _off = $(cube._backSurface).offset();
        var x = e.clientX - _off.left;
        var y = e.clientY - _off.top;
        y += $(window).scrollTop();

        adjRot(x, y);
    }
}

function adjRot(x, y) {
    var w = $(cube._backSurface).width();
    var h = $(cube._backSurface).height();
    var step = 10;
    var ry = (((x - (w / 2)) / w) * step);
    var rx = (((y - (h / 2)) / h) * step);

    cube._cubes[0]._Torque._Y = ry;
    cube._cubes[0]._Torque._X = rx;
}

Grab the sample code yourself from the link below and start creating cool 3D project Html cubes!

Post your Comments

 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 500

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 551

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 579

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

3 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 564

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 386

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 602

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

1 Rating/5 Average
Fly Bird Fly
Jan 10, 2016
Plays: 445

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 520

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 750

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

1 Rating/5 Average
Fruit Slicer
Jan 02, 2016
Plays: 441

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average