An update to the original 3D cube (from July 2009 no less), I’ve added touch gesture support (iOS) and click-and-drag behaviour. Animation is incredibly smooth on the iPhone, even the 3G model. Arrow key presses will still rotate the cube, and ESC will reset it.
Experiment: 3D cube with touch gestures and click and drag
One year on and the cube still only works in Safari. Chrome, which says it supports webkit-perspective and webkit-transform, still renders differently. Firefox doesn’t support 3D transforms yet.
It works relatively simply: on click the start co-ordinates are saved and on drag the difference between current drag position and starting co-ordinates are applied to the cube as a transform, which is completed after the specified transition duration. Many thanks to Remy Sharp and his rubik’s experiment, which got me started with a lot of the basics.
Touch tweaks
Pixel values for touch positions are found in event.originalEvent.touches[0].pageX, instead of event.pageX. Using ‘start minus current’ pixel values led the cube to rotate more than intended on the iPhone. To correct, and for intuitive behaviour, the difference is reduced by a factor of four.
JavaScript prevents single touch default events — e.g. scrolling and text selection, but if it detects more than one touch (event.originalEvent.touches.length) the cube won’t rotate, so pinch and zoom will still work. This is a compromise.
A 200ms transition duration suits the browser, but on touch devices it felt sluggish, so I’ve upped it to 50ms so the cube is always at your finger-tips.
Better CSS
The cube is created exactly as before, but I’ve simplified the markup a little — dropping the face and number class names in favour of CSS3 selectors:
#cube > div:first-child {
-webkit-transform: rotateX(90deg) translateZ(200px);
-moz-transform: rotateX(90deg) translateZ(200px);
}
#cube > div:nth-child(2) {
-webkit-transform: translateZ(200px);
-moz-transform: translateZ(200px);
}
#cube > div:nth-child(3) {
-webkit-transform: rotateY(90deg) translateZ(200px);
-moz-transform: rotateY(90deg) translateZ(200px);
text-align: center;
}
#cube > div:nth-child(4) {
-webkit-transform: rotateY(180deg) translateZ(200px);
-moz-transform: rotateY(180deg) translateZ(200px);
}
#cube > div:nth-child(5) {
-webkit-transform: rotateY(-90deg) translateZ(200px);
-moz-transform: rotateY(-90deg) translateZ(200px);
}
#cube > div:nth-child(6) {
-webkit-transform: rotateX(-90deg) rotate(180deg) translateZ(200px);
-moz-transform: rotateX(-90deg) rotate(180deg) translateZ(200px);
}
Any questions?
This is quite a speedy write-up, if anything needs explaining I’m happy to go into a bit more detail.
Discussion
I was impressed by your original cube and I am working on integrating this in an iPhone-only web site that I did for myself to track stock options. Now this is single series only and I want to use the cube to be able to present multiple series each on a cube face.
The original 3D example has all JS and CSS in line in the HTML. That makes it easy to study and use and adapt. The new example with touch gestures refers to Javascript and CSS files that are not available for the user. That makes it much harder to see how it works.
So, if you have time, please re-organize your latest example. And keep up the good work.
Kind regards, André Hartman, Belgium.
Hey, it’s hard to figure out how to use this code myself. Do you not post source files? There are some references in the code that you don’t talk about
Any chances to see youtube videos face on the i-phone?
View source on the experiment will reveal all. I’ve also put links to the CSS and JS at the top of the experiment page.
I’ve tried to get this to work in both Chromium 8.0.552.215 and the 11.00 beta version of Opera but to no avail. (I’m on an Ubuntu 8.04 box if that matters.)
No cube, no rotations, just a square with an embedded video.
Paul this is amazing.
I am alright with html & Css but I seem to be having real problems getting the motion from the javascript.
http://johnbenton.tv/newSite/html/paulHayes.html
Thanks if you get a chance,
I look forward to seeing this Css 3D working more.
Thanks
JB
Whoop. Got it!
Thanks Paul, your work is inspirational
J
Freakin’ awesome! Both cubes… Found out about them (and you) in magazine “CSS Design Essentials” from UK. Jaw-dropping work! (Flash what???)
Thank you so much for publishing these goodies!
Amazing! So smooth! But when I view the demo, there is half a YouTube video overlaid on the cube. Why is that? I am using Safari 5.0.3 on Windows XP. Lastly, saw this in the latest issue of .net magazine (April 2011, page 81).
Hey Paul! Great work with this one, I’ve been messing around with it a bit for a project of mine, but I’ve run into a little problem. You see, I get the cube moving and all that, no problems, untill I decide to scale it a bit bigger. It would appear that, once the size is altered, my cube still turns but the central axis upon which it turns doesn’t seem to be correctly centered anymore. So, everytime it turns, it shifts position slightly. The cube itself seems fine, but it just isn’t centered as neatly anymore, making any additional design and content around it kinda tricky.
Sorry to bother, and keep up the good work!
Nevermind, fixed it. Silly me.
Wow that’s brilliant!
Using your code and some modifications I built a 3D cube menu for an university project.
May I share “our” code with the public (after cleaning it up…)?
Thanks for sharing!
I love the CSS animations, and all that is easy to understand, however it would be great to have a deeply explained java script touch controls and it would be great if it was pure commented java script no outside jquery sources or anything so as to really be able to pull apart and learn
Please can u go in a bit detail of the changes from the previous version in all the 3 files?
Nice!! How to fix the problem of blur when using “-webkit-perspective”? I have not found any solution.
hi, I have been trying to recreate your cube for about a month, I nor to any of my webdesigner friends have any idea of how to get it to work. What are we doing wrong?? can you help me? is there a more specific instructional or anything. I think this and all of your experiments are genius. Thanks.
Paul– very impressive indeed. I created a demo of my own where I essentially remote control the 3D cube: http://glebche.appspot.com/3dcube
I added a few script lines to make the cube rotate by rotating your phone.
http://tjoadesign.nl/blog/?p=350
This shows a single plane when we try to rotate it
on android. What must be wrong
really fine work. Of course, there are demos like this elsewhere (e.g., in WebGL), but what’s so impressive about your demo is that you used only the basic tools.
Hi, amazing work!
I have a question:
I tried to use your experiment.js and worked great, but the movement ends exactly when you release the finger or the mouse. Do you know a way to add inertia at the movement, to make it continue a bit after the end of the touch and then slow down to the stop?
Paul, what browsers does this work with as of April 2013?
This seems to work on latest Chrome for Mac, Firefox and Safari for Mac/Windows.
It appears to not work in IE10 and Chrome on Windows (all faces collapse into one).
CSS 3D Transforms are “supposed to” work on all of these according to sites like html5readiness.com . Is this accurate?
Why would it work on Chrome on my Mac, but not same version of chrome on my Windows PC?
Thanks,
Don
Hi Don,
In some of the browsers that support 3D transforms, some do not support the “transform-style: preserve-3d property”.
This is the case with IE10 at least.
I ought to update the browser capabilities test.
Comment