psychopyko welcome to psychopyko.com

19Aug/12

Sandbox: CSS transforms and animations

CSS Cube

Cube - created by CSS!

You are now reading the first of my "Sandbox" posts :)

The idea of this "Sandbox" is that there are so many cool things on the web that I keep seeing and wanting to learn/try out. I'm very much a hands-on learning person, so the sandbox is going to be a collection of my random explorations of things that have caught my attention. Hopefully I'll be able to regularly update the sandbox!

The first thing to go into my sandbox is an exploration of CSS transforms and animations. On Thursday, I went to "What do you know?" (it's awesome!) and listened to Patrick talked about "Moving to a 3D web with CSS", in which he showed us how easy it was to make a cube using CSS.

Been meaning to look into CSS transforms for a long time, so this was a sign! Thus I spent most of my Saturday hacking at CSS :) For the impatient, here's the source code (under 2012-08-18).

Here are two tutorials that I found extremely helpful when poking around with the CSS:

The above two links will give you a good start on your CSS animation journey, so instead of writing a tutorial, I'm going to highlight some "gotchas" that I hit yesterday.

Animation is cool - but you have to define it!

When you see something like this:

-webkit-animation: spin 8s infinite linear;

Don't be tricked! The "spin" is actually not a built-in animation, you have to define it yourself. At first, it might seem like more work, but it's quite simple and it also means you can specify how the animation works. For example, here is my 'spin' definition:

@-webkit-keyframes spin {
   from { -webkit-transform: rotateY(0) rotateX(0); }
   to { -webkit-transform: rotateY(360deg) rotateX(360deg); }
}

I've decided to rotate on both the X and Y axis to make the rotation more interesting. The syntax is fairly simple (but powerful) once you get the hang of it. For a detailed explanation of the syntax have a look at: Keyframe Animation Syntax

Required properties

Now I could be wrong here (please correct me if I'm wrong!), but at least from my experience so far it appears that you need these two properties for things to work:

  1. position: absolute;
    Seems to be needed for both 2D and 3D animations, otherwise the transforms don't seem to have something to 'anchor' to.
  2. -webkit-transform-style: preserve-3d;
    Needed for 3D animations if you want your shape to keep it's 3D-ness when it's being transformed/animated

The axes rotate with the element when you transform it

By default, if you are looking at a webpage, the x-axis is the horizontal axis, the y-axis is the vertical axis and the z-axis the one coming out of the screen. The origin is the '-webkit-transform-origin' of the element, and like the cartesian plane, right is positive x, up is positive y, and out of the screen towards you is positive z.

Now, when you transform your element, the axes for that element rotates with it - that is, each element has it's own set of axes. So, if you rotate an element 90deg by the x-axis '-webkit-transform: rotateX(90deg)', the positive y-axis is now pointing out behind the screen (away from you) and the z-axis is now what the y-axis used to be.

Each element can only have one transform property

A transform property for an element, looks something like this:

-webkit-transform: rotateX(90deg) translateZ(100px);

Now, if you apply another transform to this element (maybe via animation) that new transform property will override the initial property. It is quite annoying that you can't stack the transform properties, but the 'transform' property is actually a single property (and not shorthand) so you can't specify to change just one type of transform.

This means, if you want a more complicated animation, you will need to wrap the element in a div and animate both the inner and outer div. This can cause your HTML to become quite ugly, but can't see a way around this.

Upgrade your browsers!

I did all my development using Chrome, and after everything was working, added the '-moz' versions to the CSS, opened it in Firefox and the cube didn't rotate! After spending some 15min of debugging, realised I was using an old-ish version of Firefox (version 8). Upgraded to version 12 and the cube magically rotates :)

 

That's all I've got for now, hopefully it has been helpful and/or made you want to go and try some CSS for yourself. If you missed it, you can see the code here (under 2012-08-18).

28Nov/09

Firefox Multitouch Gestures

For those of you who are unaware, I have converted to the "dark side" and now own and use a MacBook Pro laptop at home. One of the awesome things about the laptop is the multitouch trackpad. Used as part of the OS it is great - swipe 4 fingers down triggers expose swipe them up show desktop and a couple of others.

Having said that I spend a good chunk of my time on Firefox while using my Mac and there are some very useful multitouch gestures you can add! There are a couple of other tutorials out there already, but for some reason the ones I've found only talk about the twist left/right to move to the previous/next tab. Being curious me, I went exploring and found out how to get some more equally helpful gestures working!

» Continue reading "Firefox Multitouch Gestures"

28Aug/08

How to use Google Charts API with 6 examples

Few months ago I had a good look into Google Charts API and it was great! If you are not aware, Google Charts API basically allows you to create pretty graphs simply by using an URL with correct parameters specified. It is fairly flexible, with many things you can change and customise - which is where it becomes bit complicated. It is quite easy to generate a chart, but it wasn't as easy to generate the exact chart I needed. So below are 6 examples charts with explanations and tips that will hopefully help you understand Google Charts better.

A few notes before I start

  1. The data/labels for all the graphs here have no meaning at all - just examples that I have come up with.
  2. I'll explain each parameter probably only once (since they are generally the same across all graphs). If something is different or special I'll explain it again.
  3. If you have any other tips/hints please share :)

» Continue reading "How to use Google Charts..."

19Aug/07

nice borders

After setting up my blog, and having made the decision to post more regularly and with more useful content I spent yesterday thinking...what should I post about? Well, thanks to airewindel who asked me a question on deviantArt I got it!

If you visit my gallery you will notice that the more recent photos all have a border around them. The question was "how do you get the [border] and sig in everytime, the same?" I'll write about the borders first, and then the sig - as the border one is much easier :)

» Continue reading "nice borders"