Continuous rotation animation using css3 or js? - javascript

I have some objects in an animation which are continously animating in a rotation back and forth using css3. To do this I have created a declaration like so:
#-webkit-keyframes wiggle {
0% {-webkit-transform:rotate(12deg);}
50% {-webkit-transform:rotate(-6deg);}
100% {-webkit-transform:rotate(12deg);}
}
And each object I want to use this for I do the following:
.p4, .p5, .p6 {
-webkit-animation-name: wiggle;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
}
Since I want some random between each of the objects I have the following altering animation durations:
.p4 {
-webkit-animation-duration: 5s;
}
.p5 {
-webkit-animation-duration: 7s;
}
.p6 {
-webkit-animation-duration: 8s;
}
And so on...
This works ok - (only testing in Chrome so far). But it doesn't seem to be very optimal.
I would like to know whether there is a faster way to achieve this. Or a leaner way. I believe I could perform this with JS but I'm not sure what is going to be more lightweight on the end users resources.
Is there a better way to achieve this sort of basic animation with less resources - and if so, how?
In addition to this, if I were to create this same animation using jquery for example, how could I actually test the memory usage required? I found something recently to test memory usage of tabs but it appears the results are inconsistent. ie sometimes 1 tab is using more memory than the other and vice versa even though the code remains the same.
Thanks for any pointers.

Stick with the CSS3 animations if they work as intended, and if needed do something in JS as a fallback for non supporting browsers.
Replacing the CSS3 with JS is not really leaner or less resource intensive, quite the opposite as CSS3 seems to have smoother animations and in some browsers will use hardware accelaration and the GPU, something not possible with JS (at least not easily).
All in all you'll end up writing more in JS, and perhaps use a library or plugin for rotating animations as well. It will be less smooth in most browsers, and use more resources as well as JS will have to set the css tranform values several times each second for a somewhat smooth animation.

Related

CSS3 keyframes in JavaScript

Is it possible using animation keyframes in JavaScript? If your answer is no, please tell me how can I use codes like this?
I wrote my jQuery plugin but I don't know how can I use keyframes.
animate() doesn't work (translateZ...)
You first need to research css3 keyframes then maybe make a small example for yourself to get a basic understanding.
jQuery.Keyframes allows you to generate css3 keyframes and attach events on the fly with javascript.
Here is a third party library that allows for a variety of different translations.
http://ricostacruz.com/jquery.transit/
If that still doesnt so what you need you can refer to this question
Set Webkit Keyframes Values Using Javascript Variable
which will show you how to inject rules into the CSSOM dynamically using jQuery
Somewhat working fiddle for Firefox
uses
#keyframes rotate {
0% {-moz-transform: rotate(-10deg);}
100% {-moz-transform: rotate(-10deg);}
}
and other vendor prefix specifics. There seems to be some kind of problem where sometimes the animation will trigger but other times it wont. Haven t been able to figure out why

How to animate DOM elements with JavaScript/CSS

I'm working on a chess game with some strategic visual overlays. One of them requires some light animation of pieces and squares, specifically, a slow and steady pulsing. My board is a with each being a single square. Pieces are shown by setting the background-image in CSS to an .svg of the piece it contains.
Can this be done in CSS? I'm using the latest browsers with no support for legacies, so I can use all the nifty CSS3 stuff. Another option I was thinking was to set the background-image of the board to an animated .gif of the piece pulsing. Would this work?
Are there any other ways to do this I haven't mentioned? I would like to avoid packages/frameworks, but I am using jQuery.
CLARIFICATION:
I want to make the chess piece kind of pulse (flash?) in place slowly for emphasis. I want it to be a slow, subtle, and consistent pulse that persists until another event turns it off.
It sounds like you're looking for CSS animations.
Take a look here: http://www.w3.org/TR/css3-animations
In particular you'll need the following timing functions:
animation-name, to specify the set of keyframes to use.
animation-duration, to specify the speed of the animation.
animation-iteration-count, to repeat the animation.
animation-direction, to alternate the direction of the animation.
And you'll need to create some keyframes, which let you specify what CSS properties are modified by the animation.
Also, you'll need vendor prefixes on everything, so you need to write -webkit-animation-name rather than animation-name (for example), and repeat everything for -moz and other vendors.
Here's an example for webkit that creates a pulsating opacity effect. You can experiment with the properties in the from and to sections to animate size, color, etc.
.chess-piece {
-webkit-animation-duration: 1s;
-webkit-animation-name: pulse;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
}
#-webkit-keyframes pulse {
from {
opacity: 0.5;
}
to {
opacity: 1;
}
}
JSFiddle example for webkit and moz
That is quite possible and easy to do. As I understand it, you want to make some kind of glowing animation of the active chess figure?
Here's how I would go implementing a solution: Create 2 pngs, one with the chess figure in its unflashy ordinary state and one with its fully illuminated state. Then use jQuery to change the opacity of the illuminated state from 0 to 100 and back to 0 again.You can send the jQuery "on animation finish" signals to achieve this. You can than just use some kind of simplified observer pattern to cancel the effect once an event occurs.

Slow jQuery animation on iPad2

I'm currently building iPad-optimised website. I have some chunks of text that I should show/hide when user touches appropriate button. The text is inside of < p> tag. I use jQuery for toggling the visibility of the text:
$("my selector").toggle("fast");
But it is really choppy ... Is there some iOS specific way to do the animation, maybe another framework or something else ?
I don't think it should be that slow ...
There are several animation scripts that targets iOS, but the basics are that you should use CSS animations instead, and more specifically the translate3d property (for positioning) that triggers the iOS hardware.
For toggling opacity, you can use a regular -webkit-transition and toggleClass, f.ex:
p { -webkit-transition: opacity 0.2s linear; opacity:1 }
p.hide { opacity:0 }
and then:
$("my selector").toggleClass('hide');
I made a simple demo for you here: http://jsfiddle.net/rQFZd/
You can detect touch devices and serve css animation specifically to those that supports (and prefers) it.
EDIT: example of animating height: http://jsfiddle.net/rQFZd/1/. I’m not sure about iOS performance though, but I think it should be better than jQuery.
You can also add another container and then use translate3d to reposition the element instead of shrinking it, that should most certainly be smoother on iOS. Example: http://jsfiddle.net/rQFZd/2/

Transparent background progress bar?

I'm trying to find an animated progress bar with a transparent background. I would prefer to achieve it using JQuery, Javascript and/or CSS. Is this possible? I'd like something much like this: http://www.fcm.travel/progress_bar.gif Has anyone come across such a thing?
The other questions I've seen on here show static bars, nothing animated much like the example.
You can make anything transparent with CSS. The following makes 75% opacity (25% transparent)
style="opacity: .75; filter: alpha(opacity=75)"
(The extra filter rule is for IE)
Edit: If you care that it works as often as possible on older browsers, you would add the old -webkit- and -moz- rules
style="opacity: .75; filter: alpha(opacity=75); -moz-opacity: .75; -webkit-opacity: .75"
In the event the browser does not support opacity settings, it would automatically default back to 100% opacity which is not transparent or translucent at all
I'm not sure what you mean by transparent, but jquery ui has a progress bar: http://jqueryui.com/demos/progressbar/#animated
Technically, Animated PNG would be neat for this. However the support for it isn't really that wide last time I checked.
So what you could do, seeing you are open for JavaScript solutions, is create a spritemap image based on 24 bit PNG's so it has nice transparancy, that contains every state for the animation. Then with JavaScript and a decent enough timer/interval, simply change the background position for the image so it looks like it's showing a live animation.
I've just made something that would interest You few days ago, take a look at this progress bar

iPad css3 animation flickers after keyboard use

I'm developing an app for the iPad using HTML5/CSS3. I'm not using any framework and am just using whatever is natively supported on the device. I have created some css3 animations to emulate the typical iOS sliding left or sliding right when navigating between screens. Here's an example of the slide left animation which is taking advantage of the iPad's CSS3 hardware acceleration: (the ipad is running 4.2).
/*************************************************
Slide Left
*************************************************/
.screen.slideleft{
-webkit-animation-duration: 0.5s;
-webkit-animation-timing-function: ease-in-out;
}
.screen.slideleft.outgoing{
z-index: 50 !important;
-webkit-animation-name: slideleft-outgoing;
}
.screen.slideleft.incoming{
z-index: 100 !important;
-webkit-animation-name: slideleft-incoming;
}
#-webkit-keyframes slideleft-outgoing{
from { -webkit-transform: translate3d(0%,0,0); }
to { -webkit-transform: translate3d(-100%,0,0); }
}
#-webkit-keyframes slideleft-incoming{
from { -webkit-transform: translate3d(100%,0,0); }
to { -webkit-transform: translate3d(0%,0,0); }
}
I also have this CSS which I've attempted to use to fix the flicker:
.incoming,
.outgoing{
display: block !important;
-webkit-backface-visibility: hidden;
}
This works great until the iPad keyboard is used. After which point all the animations flicker severely.
I've been looking for examples of an iPad HTML5 app that uses the keyboard and doesn't have flickers afterwards, but haven't turned up much. The jqTouch demos exhibit the same behavior on the iPad (although I know they were designed for the iPhone).
I've turned up a few posts/questions of similar questions but have never found a good answer. I've been through http://css3animator.com/2010/12/fight-the-flicker-making-your-css3-animation-bomb-proof/ and the articles linked there but haven't had any success.
Any other suggestions?
Update 1/13 # 9am
I've added this css and it helped a lot:
.incoming *,
.outgoing *{
-webkit-backface-visibility: hidden;
-webkit-transform: translate3d(0,0,0); /* This helps with the flicker a lot. */
}
The foreground elements don't seem to flicker anymore, but the backgrounds still do. Still looking for some help or helpful resources on Mobile Safari's memory handling tactics.
Update 1/16 # 11pm
Increasing the z-index as suggested by anonymous. Didn't seem to make a difference.
Update 1/17 # 8:30am
I've posted a demo of the problem here.
The transitions between screens work great...until you tap/click inside one of the form fields. After the keyboard slides up and returns, all the transitions flicker. Go to the URL inside the iOS simulator or on an actual iPad to see what I'm talking about.
This is an old question, but I thought I'd share my experience.
I've been having issues with outrageous flickering (on css3 animations) on the iPad (as well as the iPhone, but in that case only in portrait view). I was able to completely resolve all of the flickering issues by setting :
-webkit-perspective: 0;
On the elements being animated. I'm not sure why this works, but it does (tested on iOS 4.2+, both iPad (1 and 2) and iPhone 4).
Update: I've just become aware of an issue with Chrome when setting the value of that attribute to 1. It works just fine when it's 0, so I've updated the above appropriately.
Looking at your source, the translate3d(0,0,0) isn't applied until the transition starts?
Try
.screen{
-webkit-transform: translate3d(0,0,0);
}
or
.screen *, .screen{
-webkit-transform: translate3d(0,0,0);
}
The flicker is probably the hardware acceleration kicking in (it currently only works on 3d translated elements).
I had the same issue, but i was able to reduce the flicker to almost unnoticeable by applying the fix described here and here:
http://code.google.com/p/jqtouch/issues/detail?id=301
https://github.com/senchalabs/jQTouch/issues/issue/130
Basically set the z-index of the page you a are moving out to -1 and after the transistion back to 1
I know this is a dinosaur old question, but there is a solution for this issue and it is quite lightweight and very simple.
document.getElementById('clicked_input').addEventListener('focus', function(e){
e.stopPropagation();
},false);
When i was tackling this issue too, I thought I tried everything - eventually the only thing that helped, was to create a modal window (position: absolute) outside of the app's container div, and also set the app's container div to display:false; when the keyboard was coming up. While it worked it was ugly, I tested everything to see what caused the event and it seemed that when the 'focus event' bubbled up, every 3d transform gets messed up (in flickering and performance).
Preventing the event of bubbling solved this issue completely - quite mind boggling that such a hated bug had such a simple solution?
You're not going to like me saying this, but JavaScript may be the answer you're looking for. I fear that when you bring the keyboard up, the process of rendering the HTML loses priority. With a continually updating script, like a setInterval loop, the iPad will have no choice but to render as planned. Explicit code requires no hacks.
I agree with Ben, you should probably set transforms on the classes themselves as well:
/*************************************************
Slide Left
*************************************************/
.screen.slideleft{
-webkit-animation-duration: 0.5s;
-webkit-animation-timing-function: ease-in-out;
-webkit-transform: translate3d(0,0,0);
}
.screen.slideleft.outgoing{
z-index: 50 !important;
-webkit-animation-name: slideleft-outgoing;
-webkit-transform: translate3d(-100%,0,0);
}
.screen.slideleft.incoming{
z-index: 100 !important;
-webkit-animation-name: slideleft-incoming;
-webkit-transform: translate3d(0,0,0);
}
#-webkit-keyframes slideleft-outgoing{
from { -webkit-transform: translate3d(0,0,0); }
to { -webkit-transform: translate3d(-100%,0,0); }
}
#-webkit-keyframes slideleft-incoming{
from { -webkit-transform: translate3d(100%,0,0); }
to { -webkit-transform: translate3d(0,0,0); }
}
If that doesn't work, I'd be curious to test if only translating the X with translateX(-100%) fixes the problem. (Not necessarily a fix, because you don't have hardware acceleration without 3D transforms, but would help narrow down the problem.)
Ultimately, there really wasn't a fix for this issue. It seems like form elements in WebKit on the iPad cause problems with flickering.
My workaround was that on the onblur of each form element, I refreshed the page using hash tags to ensure it refreshed to the exact same state. It still caused a "flicker" while it was refreshing, but it did keep the screen from flickering throughout the rest of the app.
I've recently been having the same problem and tried all sorts of complicated fixes. In the end I found the issue was down to the default styling on the input. I fixed my problem by adding the css input{outline:none}. It's prob just on the focus state so input:focus{outline:none;} should work.

Categories