<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Hayes &#187; cube</title>
	<atom:link href="http://www.paulrhayes.com/tag/cube/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulrhayes.com</link>
	<description>The web musings and experiments of.</description>
	<lastBuildDate>Sun, 06 Nov 2011 11:31:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS 3D cube with touch gestures, click and drag</title>
		<link>http://www.paulrhayes.com/2010-09/3d-css-cube-ii-touch-gestures-click-and-drag/</link>
		<comments>http://www.paulrhayes.com/2010-09/3d-css-cube-ii-touch-gestures-click-and-drag/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 22:20:49 +0000</pubDate>
		<dc:creator>Paul Hayes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[cube]]></category>
		<category><![CDATA[perspective]]></category>
		<category><![CDATA[transformations]]></category>
		<category><![CDATA[transitions]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://www.paulrhayes.com/?p=108</guid>
		<description><![CDATA[An updated 3D cube that comes with touch gestures and click ‘n’ drag.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.paulrhayes.com/experiments/cube-3d/touch.html">Experiment: 3D cube with touch gestures and click and drag</a></p>
<p><a href="http://www.paulrhayes.com/experiments/cube-3d/touch.html"><img src="http://host.trivialbeing.org/up/small/3d-cube-ii.png" alt="3D cube"></a></p>
<p>An update on the <a href="http://www.paulrhayes.com/2009-07/animated-css3-cube-interface-using-3d-transforms/">original 3D cube</a> (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.</p>
<p>One year on and the cube still only works in Safari. Chrome, which says it supports <code>webkit-perspective</code> and <code>webkit-transform</code>, still renders differently. Firefox doesn’t support 3D transforms <i><a href="https://developer.mozilla.org/En/CSS/Using_CSS_transforms">yet</a></i>.</p>
<p>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 <a href="http://remysharp.com">Remy Sharp</a> and his rubik’s experiment, which got me started with a lot of the basics.</p>
<h3>Touch tweaks</h3>
<p>Pixel values for touch positions are found in <code>event.originalEvent.touches[0].pageX</code>, instead of <code>event.pageX</code>. 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. </p>
<p>JavaScript prevents single touch default events — e.g. scrolling and text selection, but if it detects more than one touch (<code>event.originalEvent.touches.length</code>) the cube won’t rotate, so pinch and zoom will still work. This is a compromise.</p>
<p>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.</p>
<h3>Better CSS</h3>
<p>The cube is created exactly as before, but I’ve simplified the markup a little — dropping the <em>face</em> and <em>number</em> class names in favour of CSS3 selectors:</p>
<pre>
#cube > div:first-child  {
-webkit-transform: rotateX(90deg) translateZ(200px);
}

#cube > div:nth-child(2) {
-webkit-transform: translateZ(200px);
}

#cube > div:nth-child(3) {
-webkit-transform: rotateY(90deg) translateZ(200px);
text-align: center;
}

#cube > div:nth-child(4) {
-webkit-transform: rotateY(180deg) translateZ(200px);
}

#cube > div:nth-child(5) {
-webkit-transform: rotateY(-90deg) translateZ(200px);
}

#cube > div:nth-child(6) {
-webkit-transform: rotateX(-90deg) rotate(180deg) translateZ(200px) ;
}
</pre>
<h3>Any questions?</h3>
<p>This is quite a speedy write-up, if anything needs explaining I’m happy to go into a bit more detail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulrhayes.com/2010-09/3d-css-cube-ii-touch-gestures-click-and-drag/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>3D Cube using 2D CSS transformations</title>
		<link>http://www.paulrhayes.com/2009-04/3d-cube-using-css-transformations/</link>
		<comments>http://www.paulrhayes.com/2009-04/3d-cube-using-css-transformations/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 23:54:28 +0000</pubDate>
		<dc:creator>Paul Hayes</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[cube]]></category>
		<category><![CDATA[transformations]]></category>
		<category><![CDATA[transitions]]></category>

		<guid isPermaLink="false">http://www.fofronline.com/?p=67</guid>
		<description><![CDATA[Emulate 3D using 2D transforms to create a cube-like illusion.]]></description>
			<content:encoded><![CDATA[<p><strong>Update (July 21st):</strong> Using newly released (currently only in Webkit Nightly releases) 3D transforms, a three dimensional, rotating cube with correct perspective is possible, and as a bonus–the cube can be navigated using arrow keys. Check out the latest demo, “<a href="/2009-07/animated-css3-cube-interface-using-3d-transforms/">Animated CSS3 cube using 3D transforms</a>”.</p>
<p>The impression of a three dimensional cube can be created using modern CSS techniques, without the need for JavaScript, imagery, canvas or SVG. Using the proprietary transform property to skew and rotate shaded rectangles, individual cube faces can combine to form a 3D object. Currently only supported in recent WebKit and Gecko based browsers, most importantly Firefox 3.5+ <code>-moz-transform</code> (<a href="https://developer.mozilla.org/En/CSS/CSS_transform_functions">documentation</a>) and Safari 3.2+ <code>-webkit-transform</code> (<a href="http://webkit.org/specs/CSSVisualEffects/CSSTransforms.html">documentation</a>).<br />
<span id="more-67"></span></p>
<p>To demonstrate the power of this effect a second experiment with multiple cubes and proprietary WebKit transitions is also available.</p>
<h3>Results</h3>
<p><a href="/experiments/cube/index.html">A 3D cube created with CSS</a><br />
<strong>Update (June 7th):</strong> Altered CSS slightly to use skew(x,y) rather than skewY, the latter of which is not supported in Safari 3 / Chrome.<br />
Supported browsers: Safari 3.2+, Google Chrome, Firefox 3.5+</p>
<p><a href="/experiments/cube/multiCubes.html">Experiment with multiple cubes and CSS transitions, still no JavaScript</a><br />
Supported browsers: Safari 4+, Google Chrome</p>
<p class="center"><a href="http://host.trivialbeing.org/up/multiple-cubes-css.png"><img src="http://host.trivialbeing.org/up/small/multiple-cubes-css.png" alt="Multiple cubes created using CSS" /></a></p>
<h3>How To</h3>
<p>Similar to my previous experiments, the HTML markup is very simple. Each of the faces has its own DIV, class and content. The top face requires some extra markup to aid the transformation, more on that shortly.</p>
<pre class="prettyprint">
&lt;div class=&quot;cube&quot;&gt;
	&lt;div class=&quot;topFace&quot;&gt;
		&lt;div&gt;
			Content
		&lt;/div&gt;
	&lt;/div&gt;
	&lt;div class=&quot;leftFace&quot;&gt;
		Content
	&lt;/div&gt;
	&lt;div class=&quot;rightFace&quot;&gt;
		Content
	&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>A short disclaimer, the geometry in this example is ‘fudged’, in that the values have been adjusted to appear roughly correct. I know that the dimensions are slightly out of whack, this is merely to save my head from mathematics and to get the concept out there quickly for people to see. With that said, let’s crack on with the CSS.</p>
<p>Each of the three rectangles is given a slightly different shade of gray to give the impression of depth, in this example the left face is in shadow. The faces are each positioned absolutely, relative to the cube container. Each face is 200 x 200 pixels, including 10 pixels of padding.</p>
<pre class="prettyprint">
.cube {
	position: relative;
	top: 200px;
}

.rightFace,
.leftFace,
.topFace div {
	padding: 10px;
	width: 180px;
	height: 180px;
}

.rightFace,
.leftFace,
.topFace {
	position: absolute;
}
</pre>
<p>Now for the fun bit. The left and right rectangles are skewed by ±30˚ along the vertical axis, with the right face shifted left by 200px, cleanly lining up the two edges to create a corner that is center aligned.</p>
<pre class='prettyprint'>
.leftFace {
	-webkit-transform: skewY(30deg);
	-moz-transform: skewY(30deg);
	background-color: #ccc;
}

.rightFace {
	-webkit-transform: skewY(-30deg);
	-moz-transform: skewY(-30deg);
	background-color: #ddd;
	left: 200px;
}
</pre>
<p>The top face proves more problematic; it needs to be skewed, scaled, rotated and positioned. The skew is the same, –30˚ along the vertical axis, this skewed rectangle must then be rotated clockwise by 60˚. Rotating the rectangle itself leads to a change in orientation of its content, a container must be added and then rotated.</p>
<p>A simple way of creating a top face without resorting to maths is to duplicate the left and right rectangles, skew them in the opposite directions (by inverting the sign, e.g. left face is now skewed by –30˚) and position them against the existing faces to create a diamond shape between the two sets. Now use positioning and scaling to fill this diamond and form the top face, deleting the duplicates when finished. My results led to a scaling factor of 1.16 in the Y direction which I have accounted for by reducing the font-size by the same factor.</p>
<pre class='prettyprint'>
.topFace div {
	-webkit-transform: skewY(-30deg) scaleY(1.16);
	-moz-transform: skewY(-30deg) scaleY(1.16);
	background-color: #eee;
	font-size: 0.862em;
}

.topFace {
	-webkit-transform: rotate(60deg);
	-moz-transform: rotate(60deg);
	top: -158px;
	left: 100px;
}
</pre>
<p>The final CSS looks like this:</p>
<pre class='prettyprint'>
.cube {
	position: relative;
	top: 200px;
}

.rightFace,
.leftFace,
.topFace div {
	padding: 10px;
	width: 180px;
	height: 180px;
}

.rightFace,
.leftFace,
.topFace {
	position: absolute;
}

.leftFace {
	-webkit-transform: skewY(30deg);
	-moz-transform: skewY(30deg);
	background-color: #ccc;
}

.rightFace {
	-webkit-transform: skewY(-30deg);
	-moz-transform: skewY(-30deg);
	background-color: #ddd;
	left: 200px;
}

.topFace div {
	-webkit-transform: skewY(-30deg) scaleY(1.16);
	-moz-transform: skewY(-30deg) scaleY(1.16);
	background-color: #eee;
	font-size: 0.862em;
}

.topFace {
	-webkit-transform: rotate(60deg);
	-moz-transform: rotate(60deg);
	top: -158px;
	left: 100px;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.paulrhayes.com/2009-04/3d-cube-using-css-transformations/feed/</wfw:commentRss>
		<slash:comments>58</slash:comments>
		</item>
	</channel>
</rss>

