DORCHESTER3D

Html5 CSS3 3D Cube In Javascript

Posted: Jan 04, 2015

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.

Live Preview

Here is a sample of the cube using some images and text. You can use the mouse to spin the cube.

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();
    }

    returnfalse;
}

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

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!

File Download

Click the link below to download the file(s):

3D Cube in Css3: 3dcss3.zip (141.27kb)

Disclaimer: All files are provided as-is without warranty of any kind.

Submit a Comment

Comments

elaApr 15, 2016 17:05

Hi this is one am searching for. Can i make it rotate around y axis.

PSWMay 09, 2016 14:57

Thanks for your great work!

aquilesJul 23, 2016 02:04

hey great work, but I need this fixed image when the user requires moving then the image will move , how do I do that ?

© 2024 dorchester3d.com