-
-
Web Audio
I'm getting a bit obsessed with the Web Audio API just now. That's not a bad thing, of course, I just wish the browsers would all catch up.
Ages ago, I mentioned the SoundScape maps mashup Max and I made at the 5apps hackathon. Finally, here's the video of the two of us presenting it at a Nokia Berlin Tech Talk.
Creative tutorials
This goes along with the series of Web Audio API introductory tutorials I'm writing over at CreativeJS.
-
Using CSS :target to control state
There can be a fair amount of discussion provoked by the phrase "CSS only" or "Built entirely in HTML and CSS" when it comes to tech demos. The discussion generally revolves around the fact that what the phrase actually means 99% of the time is "Using CSS for the graphical bits but tracking the mouse using JS" or "Using CSS transforms for most of it but using JS to calculate the values" or any number of variations on that theme.
Now, I'm not saying that's a bad thing at all, I quite happily called my interpretation of Super Mario Kart CSS-only even though without JS, Yoshi wouldn't make it to the first bend. What these demos are doing, essentially, is using CSS as the rendering layer where previously, JS would have been used. By doing so, you even get some nice hardware acceleration thrown in. Not too bad.
Why fall back?
The reason we fall back to JS for a lot of things is because CSS is a declarative language (see my post on CSS testing to learn more). However you say things should be is how they are. In CSS, you can't set a variable to one value, do something with it then set it to a different value and do something else. For a start, you don't have variables. Even if you did, the style sheet is read top-to-bottom before anything is done with it. The variable would always have the final value assigned to it, even the first time you used it. I'm simplifying a bit but not much. If you want to do anything clever, you generally need to resort to JS, an interpreted language.
Really CSS only
If you want to make something properly interactive but entirely CSS, you have to find a way to modify the state of the DOM so that different sets of styles can be applied to the same sets of elements. We actually do this all the time when we change the colour of a link on :hover. The user interacts with the page, the state of the page is changed slightly, a different style applies. There's a lot you can do now that you have a way to modify the page based on user interaction. Hooray for pseudo-classes!
An extremely important addition to the set of pseudo-classes available in CSS3 is the :target class. This becomes set on an element when the element is the target of a clicked link. Think of in-page jump links. When you click on one and the URL changes to 'blah.html#the-thing-you-clicked', the element with
id="the-thing-you-clicked"
gets the :target pseudo-class. Now you can affect it and its children with new styles. Now it becomes interesting, you can click on something on one bit of the page and it cause something to happen on another bit of the page.Multiple-nested states
By nesting several elements around the thing you're intending to modify, you can now create a set of states entirely controlled by CSS. For example, with this HTML:
And this CSS:<div id="red"> <div id="blue"> <div id="green"> <div id="yellow"> <p>Hello.</p> </div> </div> </div> </div> <ul> <li><a href="#red">Red</a></li> <li><a href="#blue">Blue</a></li> <li><a href="#green">Green</a></li> <li><a href="#yellow">Yellow</a></li> <li><a href="#default">Default</a></li> </ul>
p { color:black; } #red:target p { color:red; } #blue:target p { color:blue; } #green:target p { color:green; } #yellow:target p { color:yellow; }
You can change the colour of the text by clicking on the link. Without any JS.
Shiny demos
This still isn't perfect. There are still going to be many things that JS is best for, calculations being one, keyboard input being another. To try and find the best way to show this off, I tried to update CSS Mario Kart to be entirely CSS and I almost got there but wasn't 100% successful.
I then started to build a zoetrope in CSS, found someone else had already done it and decided to modify it.
Maybe soon, the phrase "CSS only" will really mean "CSS only. And a bit of HTML."
-
Web Audio API Part 1
I'm enjoying the chance to write for another site that I get with creativejs.com but it does leave me less time to create new posts for here.
Having said that, there's nothing wrong with the occasional cross-post. Here's the first part of the introduction to Web Audio that I promised a couple of weeks ago:
-
CSSSquircle
After a light-hearted challenge in work, I started trying to replicate a Squircle in CSS. This is a bit tricky as it can't be created using simple border-radius.
Fortunately, somebody else already did the hard work. There's a project on GitHub by Robert Dougan which contains a fairly accurate squircle (good enough for screen, anyway). I decided to reproduce it reducing the number of elements used.
Here it is using pseudo-elements on a paragraph and a link tag.
Now to convince the designers at work to use it.