Creating animations using native javascript - javascript

I am interested in creating various kinds and types of javascript animations using traditional javascript (no frameworks) and canvas. My question is can anyone point me to resources (books/sites etc) that would be useful. I know that many frameworks already exist but my learning object is to understand the core language.

Animation is basically an attribute change over time. To achieve this you use setInterval to make sure the change takes effect gradually. In every step you calculate the current value of an attribute by interpolating between two values, considering elapsed time. As a final touch you can add easing to your animations to make transitions look smooth between states. Animation queues can also be added.
Recommended - Roll Your Own Effects Framework by Thomas Fuchs

There is a plethora of articles discussing this, available on the intertubes.
One key technique is to use setTimeout() to modify the opacity of an item, stepwise. This is basically what jquery UI - one of the frameworks you mentioned - does to implement animations, fades, slideouts, etc.

Related

How Greensock animations work under the hood that it's so performant?

There's a speed test in greensock website comparing speed with other animations libraries, JQuery or Even CSS Transitions. It's benchmarking FPS by animating hundreds/thousands of perticles.
The FPS of greensock animations outnumbered everything else. Css transitions & JQuery is not much close to greensock.
I tried searching about greensock but couldn't find much useful information. Most of them aren't explained well.
I'm still a amateur in javascript. If I try to make my own JS animations, those won't be as fast as gsap. Not even close. So it would be great to know what happens underneath the hood. How they optimize that much!
Jack (the creator of GreenSock) tells how GSAP is so fast in this forum post among other places. To recap some of the points there:
Use highly optimized JavaScript across the board (this entails many things like using linked lists, local variables, quick lookup tables, inlining code, bitwise operators, leveraging prototypes instead of recreating functions/variables for each instance, etc.)
Engineer the structure of the platform so that it lends itself very well to high-pressure situations, minimizing function calls and making sure things are gc-friendly.
Make updates in a single update loop that's driven by requestAnimationFrame, only falling back to setTimeout() if necessary.
Cache some important values internally for faster updates.
For CSS transforms, we calculate matrix values and construct either a matrix() or matrix3d() when there's any rotation or skewing because our tests showed that it was faster.
There's no silver bullet that makes it fast. It just is smart in the way that it does things based on over a dozen years of experience.

React color animation synchronization

I have been using react to build out an editor. One of the configurable properties is to dynamically generate background color animation. For example, an infinite fade in and fade out with a x second period.
The problem is that the color animation needs to be same and synchronized in multiple views. CSS animations does not enable that synchronization without being hacky. I want to use 'react-motion' but it does not incorporate timing easily.
Looking for some concrete examples using react + react libraries (ideally react-motion). Thanks in advance.
Not a direct answer to your request for examples using react + react libraries, but, if you're rendering on the client/browser you can use a global var for keeping sync between views. If that is too "hacky" for you, then perhaps finding a common parent up the ladder that both views can be handed a synchronized var/value.
After doing quite a bit of research into all of this, it turns out that the answer is just working at the animation frame level.
CSS animations abstracts all of this away from devs but as a result it gives very little control between animations.
React Motion react-motion while a good library for dealing with transition animations (it's intended purpose) again does not address controls between animations.
They key though exists in the react-motion code. You just have to request animation frames one after the other using raf library and then calculate the animations per view using the common frame info. It's pretty tedious and no real generic solution here.

CSS3 vs Javascript/jQuery [duplicate]

I'm working on an iPad HTML5 app and I've already implemented ontouch support to trigger events faster and I'm using jQuery to target the elements easier, but for the animations I'm using CSS3 transitions
What do you think is faster? using jQuery animations since I already have imported the library or use CSS3 transitions when targeting elements with jQuery?
According to this link, jQuery animation is much slower then css animation.
Reason can be because jquery has to modify the props of the DOM element using timers and a loop. The CSS is part of the browser engine . which depends pretty much on hardware of system. You can also check that in profiling of Chrome or Firefox.
CSS animations will almost always be faster.
A head to head comparison of CSS transitions and jQuery's animate.
Rather than setting a timer to run repeatedly, transitions are handled
natively by the browser. In my rather unscientific testing,
transitions are always quicker, running with a higher frame rate,
especially with high numbers of elements. They also have the advantage
that colours can be animated easily, rather than having to rely on
plugins.
http://css.dzone.com/articles/css3-transitions-vs-jquery
Related Question:
Performance of CSS Transitions vs. JS animation packages
CSS3 Transitions should be faster because they are native to the browser.
Its css3 its faster and consumes lesser memory and is smoother.
CSS processor is written in C++ and native code executes very fast whereas jQuery (JavaScript) is an interpreted language and the browser can't predict JavaScript ahead in time.
http://dev.opera.com/articles/view/css3-vs-jquery-animations/
View the above link to know about the experiments held over CSS3 vs jQuery
This article (http://css-tricks.com/myth-busting-css-animations-vs-javascript/) does an excellent comparison of CSS transforms vs. jQuery animations vs. GSAP (another JavaScript library).
CSS animations are significantly faster than jQuery. However, on most devices and browsers I tested, the JavaScript-based GSAP performed even better than CSS animations
So CSS transforms are faster than jQuery animations, but don't let this make you assume that CSS transforms are faster than JavaScript. GSAP shows that JavaScript can outperform CSS.
CSS3 will be faster as it comes standard with the browser where as JQuery is another file that has to be loaded, however I have found that depending on the animation that JQuery can run a lot smoother. Sometimes it's also nice to experiment with pure Javascript now and again.
The Mozilla developer documentation raises some interesting points regarding CSS3 animation:
Letting the browser control the animation sequence lets the browser
optimize performance and efficiency by, for example, reducing the
update frequency of animations running in tabs that aren't currently
visible.
WebKit (which powered Safari) also makes use of hardware accelerated compositing, which can have a much greater effect on performance than anything Javascript can do at this time. (I think this will change very soon though as more functions are added to manage calculations) This is because it will take advantage of dedicated hardware if it available to perform the calculations, rather than making it happen through a translated language like Javascript.
I am not 100% certain whether WebKit on the iPad is hardware accelerated; however it would stand to reason that because it is standardized and increasing in popularity, that this would only improve with time.
From here
A head to head comparison of CSS transitions and jQuery's animate.
Rather than setting a timer to run repeatedly, transitions are handled natively
by the browser.
In my rather unscientific testing, transitions are always quicker, running with a
higher frame rate, especially with high numbers of elements. They also have the
advantage that colours can be animated easily, rather than having to rely on
plugins.
A test here along with this conclusion.
Javascript animations based on timers can never be as quick as native
animations,
as they don't have access to enough of browser to make the same optimisations.
These animations should be used as a fallback only in legacy browsers.
Also notice this,
CSS3 animations are terriffic but do use a lot of your processor’s
power.
There is no way to fine tune the animation with CSS3 the same way you can using a
framework like jQuery. So, as long as CSS3 animations aren’t CPU friendly you
better stick with jQuery.
If you're using jQuery/javascript animation with the canvas tag (which if I'm not mistaken still relies upon a timer... new to playing around with it though), then it gives you the advantage of hardware acceleration with javascript. If you're just looking to move something around when you hover then transitions work great. CSS transitions may run a little smoother but it's a trade off, you're relinquishing a ton of javascript control over the animation by using transitions. I like to keep style in CSS and behavior in JS - isn't that how it's supposed to work anyway? CSS transitions kind of broke that logic...
Native is supposed to be faster, but if browser developers are sloppy (or lazy), they write bad code, which leads to poor results with CSS animations (or transitions).
Nowadays, jQuery has a plugin which overides the "animation" function with an "improved" one. see Velocity. I'm not getting into other ways to animate the DOM with javascript because it's outside the scope of this question.
So, as-is, jQuery is slower than CSS. also, CSS is easier to write because you already have the element style probably, so adding a few rules is easy, compared to a situation where you need to start writing JS somewhere and manage it..but for complex, heavy stuff, JS is faster, sadly.
A very good article about this exact question - http://davidwalsh.name/css-js-animation

Canvas vs CSS3 for Web Application

A very common question, but almost all comparison I've seen is mainly focused on games with a lot of interaction.
What I'll be working on is a web application that manipulate objects one at a time. For example, the object can be either an image or a text, then it can be replaced, resized, rotated, zoomed in, and deleted.
If the manipulations applied to many objects, I know that canvas will be a better choice but here the manipulation only can be done one at a time to one object only. Each container will at most have about 30 object in it, and I'll be working on multiple containers (maybe around 20 containers) that will be hidden or shown depends on the interaction.
The question is whether to use Canvas or CSS3? What I'm looking is the performance issue and complexity of the app.
I don't have a lot of experience with canvas but as far as I know if you use it together with requestAnimationFrame the performance is pretty similar to CSS animations. You should also consider that CSS animations are very limited when it comes to working with complex animations.

How could this jQuery slider be optimised ? (Slow, laggy)

I have written a Javascript/jQuery script. Its purpose is to serve as a responsive background image slider.
It is viewable here:
http://new.upserver.co.uk/rbis/
My question is this;
While the code functions correctly, it can be slow/laggy. Especially when additional animation related plugins appear on the same page (i.e. nivoslider).
I have seen many similar sliders that handle chunky images perform effortlessly and am concerned and wondering how this code could be optimised to perform better? the javascript in question is located in file http://new.upserver.co.uk/rbis/rbis.js
Many thanks for all suggestions.
Regards
jQuery animate is rarely the right tool for providing animation. Take a look at this rather contrived example I produced recently: http://css3.bradshawenterprises.com/demos/speed.php.
I would recommend using CSS transitions in normal browsers, and only using the old way of doing it using javascript in old browsers.
A good way to do that is to use something like http://playground.benbarnett.net/jquery-animate-enhanced/, though I have had some issues with that, and instead use my own methods of doing it.

Categories

Resources