-
-
Poor man's touch interface
Here's a little code snippet for today.
While I was making the 3D CSS Mario Kart, I needed a simple, drop-in bit of code to handle touch interfaces. I looked through some old experiments and found a rough version of the code below. It was based on this KeyboardController by Stack Overflow user Bob Ince.
It doesn't do anything clever. All it does is provide a simple way to attach functionality to touch events in different areas of the screen – top-left, top-center, top-right, middle-left, middle-center, middle-right, bottom-left, bottom-center, bottom-right. My apologies to any Brits for the spelling of centre as 'center'. It's the Internet, we have to.
How to use
Include this code:
function TouchController(areas, repeat) { var touchtimer; document.onmousedown = document.ontouchstart = document.ontouchmove = function(e) { var position; e.preventDefault(); e.touches = [{'clientX':e.pageX,'clientY':e.pageY}]; switch(true) { case (e.touches[0].clientY<window.innerHeight/3) : position = 'top'; break; case (e.touches[0].clientY>(2*window.innerHeight)/3) : position = 'bottom'; break; default : position = 'middle'; break; } position+='-'; switch(true) { case (e.touches[0].clientX<window.innerWidth/3) : position += 'left'; break; case (e.touches[0].clientX>(2*window.innerWidth)/3) : position += 'right'; break; default : position += 'center'; break; } if (!(position in areas)) { return true; } areas[position](); if (repeat!==0) { clearInterval(touchtimer); touchtimer= setInterval(areas[position], repeat); } return false; }; // Cancel timeout document.onmouseup = document.ontouchend= function(e) { clearInterval(touchtimer); }; };
Now, all you need to do to attach a function to a touch event in the top-left area of the screen is:
TouchController({ 'top-left': function() { topLeftFunction();} }, 20);
I use this for direction control in the Mario Kart experiment which maps exactly onto the cursor keys used for the normal control.
TouchController({ 'top-left': function() { // UP + LEFT drawMap.move({y: 2}); drawMap.move({z: drawMap.z + 2}); drawMap.sprite(-1) }, 'top-center': function() { // UP drawMap.move({y: 2}); }, 'top-right': function() { // UP + RIGHT drawMap.move({y: 2}); drawMap.move({z: drawMap.z - 2}); drawMap.sprite(1) }, 'middle-left': function() { // LEFT drawMap.move({z: drawMap.z + 2}); drawMap.sprite(-1) }, 'middle-right': function() { // RIGHT drawMap.move({z: drawMap.z - 2}); drawMap.sprite(1) }, 'bottom-left': function() { // DOWN + LEFT drawMap.move({y: - 2}); drawMap.move({z: drawMap.z + 2}); drawMap.sprite(-1) }, 'bottom-center': function() { drawMap.move({y: - 2}); }, 'bottom-right': function() { // DOWN + RIGHT drawMap.move({y: - 2}); drawMap.move({z: drawMap.z - 2}); drawMap.sprite(1) }, }, 20);
If you need anything clever or you need two or more touches, you should use something else. This is just a simple drop-in for when you're copy-paste coding and want to include touch-screen support.
-
Super Mario Kart CSS
Silly CSS Fun for a Sunday Afternoon
Yesterday, I decided to mess around with 3D CSS transforms. I've used them here and there for various things (the flip animations in Shelvi.st, for example) but nothing silly.
My mind wandered back to an early HTML5 canvas demo I saw ages ago where Jacob Seidelin had written Super Mario Kart in JS and I wondered if it would be possible to do the pixel-pushing part of that demo in CSS.
An hour later and we have this:
Yes, it's silly. Yes, it's nothing like playing Mario Kart and, no, there isn't any acceleration. That wasn't the point, however. View the source to see the description of the rotations and transforms.
-
Silly CSS Gradient
Okay, here's a bit of silliness. I did a little presentation yesterday with some slides which I built using Hakim El Hattab's 3D CSS Slideshow. I decided to have some fun with the last slide.
The Looney Tunes “That's all Folks” end title using radial gradients.
-
That Version-y Control-y Stuff
Just in case you don’t know, a Version Control System (VCS) is pretty much the most important thing ever in the history of anything ever. Just think about it conceptually: the ability to stand back and look at everything you’ve ever done, choose the good bits, drop the bad bits, mop up the mistakes… it gives you the freedom to “Play with it ‘til it breaks” with the safety net that you can always go back and fix it again.
You Get To Play With Time Itself!
There are many, many debates about the different methodologies you can use with version control and which technologies are best suited to which methodologies and which interfaces are best suited to which technologies and so on ad nauseam. The main concepts of working with any VCS are essentially the same, however.
- It must be almost invisible.
- It must be completely reliable
- You mustn’t rely on it
It must be almost invisible
If you have never used a VCS before, it must fit perfectly into your workflow otherwise you won’t do it. It isn’t until you’ve been saved from potential disaster by a system that you will truly appreciate it and see the value in working it into your process. If you have or are currently using a VCS, think about when and how you use it. Does it require launching a new application or does it run in the background? Do you have to manually check things in or is it seamless versioning? The more effort required to mark a version, the less often you’ll do it as it breaks the flow of development
On the other hand, if the process is completely invisible, you might forget it’s there. It’s exactly the same as assuming your changes are going live on the server – the moment you assume it happens, it doesn’t. You still need some level of manual checking.
It must be completely reliable
This is fairly obvious, hopefully. You need the security that your version system is working otherwise you might again be tempted to miss it out – why take the extra step required to version a file if it’s not going to be there when I need it?
If you’re hosting your own repositories internally to your organisation, don’t put it on a server older than you are. The safest route to go down is contracted external hosting. That way, it’s someone’s job to make sure your datums are secure.
You mustn’t rely on it
Always have a backup. Always have a copy. Always have an escape route. Or something. You should have picked up the theme by now. Version Control Systems are great but as soon as it is the only thing standing between you and a complete failure to deliver a project, it will fail. Or not. It’s funny like that.