On the 1st of July our company launched a brand new homepage. Since this homepage includes various animations, our front end team had to decide how we would approach this task in order to create these animations.
There are many ways to create an animation for a website/webapp: You can choose from Flash, Gif, SVG, JS/jQuery, CSS(3), Canvas, WebGL, etc. However, since Flash is not supported on mobile devices (and neither is WebGL), and Gif’s are too simple (and jQuery is the old way of doing animations), those won’t be discussed in this article. Thus, this blog post is going to focus on JS+Canvas, SVG and CSS(3).
And you will probably need to write a lots of code to achieve a simple canvas animations. The good thing in choosing canvas animations, is the browser support.
Every major browser does fully support canvas animations, and IE supports them as well, but only from version 9+. Since IE8 has only 10% market share it should not be a big issue.
Here’s a working example with code. We had to create a football field with players running and interacting with the field.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
btw, I am pretty confident that I could reduce the amount of code by 50% if needed.
If you need to create simple objects in SVG, by all means use RaphaelJS. It’s quite easy to do it from scratch, and the required lines of code are almost 0. For example, the following code creates a circle of radius 20 located at the coordinates X,Y 10,10 of a SVG element painted with Raphael of size 450x450.
This includes any kind of geometrical shape you can imagine. For instance, the Centralway animation for the Digital Marketing section was crafted with RaphaelJS; they are just simple circles and lines as well as RaphaelJS icons drawen in linear succession. Since we wanted to draw the “target” asset as soon as the user scrolled down to that section and we needed some really specific control on the timing of the elements, RaphaelJS was a natural.
So what about native SVG animations? They look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Full Code Example here
The major drawback of these animations is the development process. Since you will need to edit those files by hand and know the SVG API in order to modify the SVGs properly, you are pretty much on your own. If there is to be any change to the animation, you have no variables or base references in order to short-cut your way through the change. No free lunch. Maybe there are SVG editors that can help you in this task, but I have yet to see a good one.
In our second animation, we have used a CSS(3) (Transitions+Keyframes). CSS(3) animations are by far not as powerful as JS animations, but since 99% of all animations are simple ones, there will be no downside in choosing CSS(3) over JS. Creating CSS(3) animations is easy going if you have already experience in using CSS. As with canvas, the browser support is pretty good (finally…) for CSS animations and is supported by all major browsers (inlcuding IE9+!).
The code complexity is by far smaller than with canvas, and you also need dramatically less code compared to canvas.
Here a working example with needed code. Here the goal was to create a light bulb with our company logo in it. The light bulb is being turned OFF/ON and vice-versa. This animation was completely written in CSS, no images were needed, not even for the light bulb. We are using a PNG only for the light, but this due a lack of time, it is absolutely possible to re-write the light also in CSS. It just takes a time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
Performance (CPU vs GPU)
JS/Canvas/SVG/CSS(3)/etc.. are all using a different way of rendering animations, and some of them also support a hardware acceleration. JS/SVG are depending only on CPU, and it is pretty easy to overload the CPU if you are not an experienced JS programmer. Canvas is usually (99%) also rendered by CPU, but there are certain conditions under which canvas can leverage a GPU for rendering as well. On the other side, CSS(3) is depending fully on the GPU, and is also capable of GPU hardware acceleration, which will significantly boost the performance of such an animation. In most cases, and for sure in web app cases, there is already a huge JS code which is completely interpreted already by CPU, so there is no reason for overloading the CPU with an extra work thru JS animations (which always use a lots of performance). The GPU’s work in web apps is only interpreting CSS styles, which by current GPU core frequencies is a piece of cake for the GPU, so we can easily let the GPU render the animations in CSS3.
The conclusion: use animations in CSS3 in order to export the rendering to GPU and let the CPU just interpret the JS.
More on this topic could be find in here: http://www.html5rocks.com/en/tutorials/speed/html5/#toc-hardware-accell
Our front end engineering team at Centralway is planing to create an open source repository with all possible examples on how to create an animation for web. More about this will follow soon.
Not for us. Doesn’t fit our use case and we had immediate issues with a few of the features.
Centralway is a cloud company – we aim to use 100% cloud-based tools – so for issues and information sharing we use Atlassian’s suite of tools, including JIRA and Confluence, for office-apps (think spreadsheets, documents, etc.) we use Google Apps and for source code we use github, etc, etc.
Centralway is also a multi-location company (or, at least, we will be very soon). We’re also keen on remote work. To support this we use 2 tools: First, for a/synchronous communication and information sharing we use Atlassian HipChat. The second is for live-communication: we use Google Hangouts as it integrates nicely with Google Apps (surprise).
Trying to facilitate the feeling of working in a team, even when that team is distributed (so-called “wide teams”). In addition, we want to reduce the friction of switching between text-chat (in HipChat) with live face-to-face conversations.
We have teams where some members are in the same room, others are in a different one or working from home.
A solution to try: Sqwiggle
Tricky to type, but easy to understand, Sqwiggle brings together face-to-face communication and text communication in one package. The primary view is a set of webcam images (updated every X seconds) of the team. Text chat is available, just like HipChat. But to enter a face-to-face discussion with a team member you just click on their face – straight away you’re brought into a live discussion. In reality, really, you never left as you can always see everyone (even if it’s not in exact real time).
A solution, tried
So I signed us up and myself and 5 others gave it a whirl. We lasted perhaps all of 10 minutes. Obviously we didn’t give it a full trial so we’re not saying that our impression is correct: your mileage may vary ‘cos ours certainly did.
A solution, failed.
We’ll bullet-point this:
- Quick setup, yay!
- Liked the fact you could have multiple discussions at once
- Great concept
- No same-room audio cancellation (so can’t use it with people in the same room)
- No screensharing (Hangouts have ‘em)
- Lots of poor connections
- Only 4 people per conversation
- Found the Mac app hangs randomly
- Allow to “Ping” User. User might be busy, but receives a notification that someone wants to chat;
- people go “WTF is this? Why not hangouts?”– confusing interface
- Why not hangouts? At the very least… it works
The last two comments demonstrate the trouble we’d have introducing this even if we’d found it awesomely useful.
For us, it’s a promising tool, but it’s not ready for prime time – or at least for us – yet. I think this might be a possible solution if each team member were in their own home but not for including remote team members with a part of the team being in together in a single room, as is our use case. We also have some good solutions (HipChat + Hangouts) for that, though.
We’ll take a look at different loading and parsing techniques, such as
- asynchronous loading
- lazy parsing
and their impact on performance, as shown in the following table.
|Version||Sync loading & parsing||Async loading (+ parsing time)|
|Unminified||580 ms||250 (+ 400) ms|
|Minified||490 ms||250 (+ 280) ms|
We also provide a test harness for you to play around
All the things described in the following article were tested on a Nexus 4, running Android 4.2.2.
The app is built using Cordova 2.9.
This post is not about that framework, it was just a sufficiently large file I had on my computer. We also tried Angular.js and got similar results, see below.
The test rig
This (reliably) gives a timing around 250 ms on the Nexus 4 until the alert is fired.
If you want to have a go at this, check out our test harness on Github.
Round 1: Loading a huge JS file, no frills
Now if you add the following line right below the
cordova.js like so:
the time it takes to fire the alert explodes (given the file is large enough). In our case we tried a 1.4 MB file and loading now took 1.5 seconds, which is perceivably longer.
So the time until
deviceready is fired is 6x the original time for the page to become ready.
Round 2: Loading with the “async” property
Changing the script tag for
has no measurable effect on the time it takes to get to the alert.
Round 3: Adding the script dynamically
Our test script now looks like this:
and at the end of the
js/super_huge.js we add
alert(new Date().getTime() - window.tStart);
so we get a second alert when the super_huge.js is parsed.
This gives the following results:
|Time to deviceready (ms)||Time to eval script (ms)|
So this means the page load is not blocked and evaluation of the huge script file and only approx. 66% of the initial evaluation time.
Round 4: Loading with type=“text/plain”, parse later
It’s basically the code from the previous experiment but has the following line below the cordova.js line:
<script type="text/plain" src="js/super_huge.js"></script>
How about the results? Sadly no improvement compared to the previous experiment. This round actually tells us that loading files isn’t the problem (as suspected because we’re not doing network roundtrips but read from the local filesystem) – parsing is.
That leads to the next round of fun: is minimizing the code helping or harming?
Round 5: Minification yea or nay?
Okay, so running the file through a minifier brings down file size from 1.4 MB to 884 KB. Pretty nice. But does it make parsing harder?
|Time to deviceready (ms)||Time to eval script (ms)|
So not only did we save on disk usage, but also the time until the script is evaluated is down by ~45% of the original evaluation time. So minification is a cheap win.
How about not-so-large files?
Let’s try with Angular, which is 848k uncompressed and 80k minified.
|Time to device ready (ms)||Normal, synchronous loading (ms)||Async loading (evaluated, ms)|
|Time to device ready (ms)||Normal, synchronous loading (ms)||Async loading (evaluated, ms)|
What to take away from this
- Load time is irrelevant, parsing time is the droid you’re looking for.
- Minification doesn’t harm but gives you some gain.
- Asynchronously loading the JS is a great idea, as it makes your app start faster. Do not try to use the “async” property, it doesn’t help.
- Avoid large, complex JS files.
In this post I will try to describe how I learned a lot more about myself by simply ‘listen & learn’ from other great developers and using testing tools. I learn great things at work. Day by day. It’s because we have software engineers who give constructive criticism to the code and therefore contribute to make you a better programmer.
How it all started
I have 6 years of experience in writing professional CSS and HTML. I’m obsessed with writing good markup and saving every bit & byte of network-traffic to deliver a fast web application and a good user experience. But sometimes, we are lazy and this leads us to write bad code without knowing.
I had dozens of errors and warnings and of course – a lot of things to improve in my code. For every developer who is confident on his work that is frustrating at first. I agree with the statement “nobody is perfect” but this case took the cake.
What am I doing here?
grunt csslint in the command line I started investigating every single error and warning.
Starting at “too many font-size declarations” and stuff like “be careful using !important”.
I fixed every single line of my CSS just to satisfy CSSLint and to stop annoying me.
But after a while I started asking myself: Why am I doing this?
A couple of minutes later I realised: I’m actually not doing this to make a simple tool stop complaing about my code but much more to understand the reason behind this. So the real answer is quite simple: Quality is important and makes a product much better. Writing HTML & CSS is easy but writing accessible, simple and maintainable code which performs well can be rather a challenge – especially when you are not testing and measuring the outcome.
Line by line, fixing those issues, I realised that I refactored almost half of my code. I changed and optimised CSS classes, added more abstraction to CSS classes and finally wrote better and more maintainable code, which is future-proof.
This experience made me learn about myself and gave me a good insight on how I should write code next time. It’s great to have people which you can learn from and it’s also great if you can rely on people when you get stuck in the process. We should all learn more from our teammates and take some of their experiences, because we all want to get better at what we do.
So, next time, before looking for excuses to cover yourself just try to understand what your teammates want to tell you and how you can do things better. With this is mind: Try by yourself and take your time to get better – it’s worth it and you will see that things are getting much better.
When it comes to mobile application development, there’s far too many evangelism out there.
The web fans sing their
“write once, deploy everywhere”
the native party yells back
“But it’s slow and doesn’t look native!”
but both lack the understanding of the advantages and disadvantages of each approach, as well as the rationalism to re-decide on this matter, based on the project at hand.
Does native always mean better?
Certainly no. Facebook is a great example for this. Their claim was, that the Android app’s quirks and issues originated in its hybrid nature. The reality is: The new native Android app does not really work any better and Sencha gave an impressive proof that a non-native app could do better.
So a native app is not, by definition, better. If you write inefficient code, the performance will be bad and the development iterations may be longer, as you can’t easily test on many different platforms simultaneously without having to repeat modifications in the code for each platform. If you also run a web version of your project, you can easily open your application to desktops, laptops and mobile users of different operating systems.
The downside is that it requires more thought and it can be pretty tricky to debug and profile your application on the target devices as the toolchain isn’t quite there yet. Generally you could say that – when you haven’t done it before – it requires more time and work to get the best results.
You should pick the right tool for the job, which can be a native or web app. It can even be a hybrid app.
The tricky thing is to find out when to use what.
Quick overview of what’s what
- Native: Requires code tailored to each supported platform but gives you direct access to the native features.
- Mobile web: The same code runs on many (if not all) platforms. Can also be delivered to desktop browsers, but can not be installed via app stores and can not access all device features. As the code is run in the browser which runs on top of the operating system, some features may be slower than native features.
- Hybrid: Combination. Same code can run everywhere, all native features can be accessed and native components can be used to get a good performance. Hybrid apps can also be distributed in the app stores.
If you have a big team of experienced mobile developers for the different platforms at hand, you can of course leverage that.
But when you have a team of experienced web developers and need to quickly get an app out there (maybe even flanked by a web app), mobile web or hybrid apps are a great tool.
With the web being an open and fast-evolving platform, you can also build sophisticated, large scale applications using only the web technologies, but keep in mind that it’s still in the pioneering stage.
And the thing many people miss is: It’s not only good for prototyping and quick iterations – it is a viable option for building great apps.
Busting some myths about “native” and “hybrid”
It needs to look&feel native, right?
First of all: Yes, if you want to achieve perfectly native look&feel, you’re not going to have a good time with a hybrid approach. But do you really want that?
Let’s look at the Foursquare app for a second:
and why not take another look at the Facebook app:
© six revisions, see their great article about native vs. mobile web
It’s easy to spot that even the native Facebook-App doesn’t really look native. And that’s no problem! The user doesn’t really care, as long as it looks great and feels right.
This is something most people get wrong – you can have your very own look in your apps. Stop mimicking the native look and feel and start delivering something great and useful to your users.
But to get in the stores, you need native, right?
No you don’t. You can for example use PhoneGap to package your hybrid app for many different platforms (e.g. iOS, Android, Windows phone, Blackberry).
Okay, but what if I need the native features, like the camera
Again, PhoneGap is the answer.
But how do you build a great app?
There are many different ways to build great hybrid apps. To achieve the best results, you should focus on the following aspects:
- Responsiveness – the app has to respond quickly to user input
- It has to feel right – don’t imitate the perfect native transitions. Use decent transitions that are not giving the impression of “wanna be native”
- Be aware that the user won’t always have a good internet connection (or any at all) – leverage local storage whenever you can and minimize the data to transfer
- Use native components (e.g via PhoneGap plugins) whenever needed
- be careful: Not all platforms already support every HTML 5 feature. Provide fallbacks
- test your app on different devices, especially on older ones
Martin Naumann presenting at JS Zurich
Whenever you write a new web application, you have a bunch of boilerplate code. In node.js you would require a bunch of packages, setup routing, databases … yadda yadda. Do I hear you yawning already? Same here. Meteor.js allows you to get rid of all of this stuff. Just hack something cool and it will take the boring housekeeping stuff for you.