javascript - Why is my custom psuedo-animation out of sync with jQuery's native animate function? -
javascript - Why is my custom psuedo-animation out of sync with jQuery's native animate function? -
so turns out while can animate background-color
of element via jquery ui, cannot same elements utilize background: linear-gradient()
function rendered image - can set linear gradients using jquery's .css()
method.
therefore simple solution problem create pseudo animation rapidly sets linear-gradient via css method on 10-30 milliseconds each particular duration. this, can create linear gradient animations. here's example.
i have done this, i'm encountering problem. @ same time i'm animating linear gradients, i'm animating background-color same start , end values via native jquery animate()
method, , these animations not executing @ same time, despite specifying duration equal both.
the linear gradient animation beingness started , completed visibly after native jquery animation, despite calling slightly before in code (about 4 lines).
// phone call custom linear gradient animation animategradient(rgbstartcolor, rgbstopcolor, 300); ... // native jquery animation $('#stage').animate({ backgroundcolor: rgbstopcolor }, 300, 'linear');
i wondered if easing problem disguised time problem, when specifying easing on jquery anim, it's still visible.
here's code animategradient()
:
animategradient = function(startstring, stopstring, duration) { // convert rgb() string array: [r, g, b] rgbstringtoarray = function(string) { homecoming string.substring(4, string.length-1).replace(/ /g, '').split(',') } // looping function magic happens animationloop = function() { diff = [parseint(start[0]) + (diffstep[0] * i), parseint(start[1]) + (diffstep[1] * i), parseint(start[2]) + (diffstep[2] * i)]; var diffstring = 'rgb(' + math.round(diff[0]) + ',' + math.round(diff[1]) + ',' + math.round(diff[2]) + ')'; $('#serratedtop').css({ background: 'linear-gradient(-45deg, ' + diffstring + ' 12px, transparent 0), linear-gradient(45deg, ' + diffstring + ' 12px, transparent 0)' }); settimeout(function() { if (i <= icount) { animationloop(); } i++; }, 30); } var start = rgbstringtoarray(startstring); var stop = rgbstringtoarray(stopstring); var diff = [stop[0] - start[0], stop[1] - start[1], stop[2] - start[2]]; var = 0; var icount = parseint(duration / 30); // 30 milliseconds should plenty render high plenty framerate (~ 33fps). var diffstep = [diff[0] / icount, diff[1] / icount, diff[2] / icount]; // phone call magic animationloop(); }
yet, when function runs, works, it's visibly out of sync native jquery anim. ideas why is?
jsfiddle demonstrating problem
to avoid problem of different systems calculating changes differently (or @ different speeds), can simplify whole thing using step
callback of animate, "stealing" value animating colour:
$('#stage').animate({ backgroundcolor: 'rgb(230,250,250)' }, { duration: 1000, step: function () { var col = $('#stage').css('backgroundcolor'); $('#serratedtop').css({ background: 'linear-gradient(-45deg, ' + col + ' 12px, transparent 0), linear-gradient(45deg, ' + col + ' 12px, transparent 0)' }); } });
this way in sync steps called each time value changes.
jsfiddle: http://jsfiddle.net/bjgzlkuf/5/
javascript jquery css animation
Comments
Post a Comment