Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript water ripple effect slowdown in framerate

I have written a super simple javascript to create a water ripple effect for a website, and the illusion if fairly effective, but I am having some issue with my timer lagging from numerous instances. Originally I attempted it using some CSS3 effects to acheive, but the computer rendering the elements would freeze the screen after a while, so I rewrote it to alter a single PNG repeatedly, and this helped quite a bit, but still gets bogged down. I am not sure how to improve this issue, if using some preload might help (doubt that) or if there is some other optimization which could fix what is a very promising start.

here is my code:

body{
    background-color:#444;
    position:absolute;
    overflow: hidden;
    z-index:0;}
.circle{
    position:absolute;
    background-image:url(circle5.png);
    background-size:cover;
    background-repeat:no-repeat;
    overflow:hidden;
    z-index:2;} 

and the javascript

var locX = '';
var checkX = '';
var locY = '';
var checkY = '';
var eventTimer = setInterval(function(){changeCs()}, 1);//'FRAMERATE' of state changes in milliseconds

//Listen for user mouse movement, fetch current location of cursor, initiate droplets
document.onmousemove = function(e){
    var event = e || window.event;
    locX = event.clientX;
    locY = event.clientY;
    createC();}

//We create divs with the circular image stretched to the otherwise blank div   
function createC(){
//Check to see if the X and Y are the same as in previous cycle, if so movement has stopped.
  if(parseInt(locX) != parseInt(checkX) && parseInt(locY) != parseInt(checkY)){
    var n_Circle = document.createElement('div');
    n_Circle.setAttribute('class','circle');
    n_Circle.setAttribute('style','width:10px;height:3px;opacity:.4;');
    document.body.appendChild(n_Circle);
//We obtain the coordinates of the cursor, and use the same X as the user.
//However with the Y we are shifting downward from the top to maintain illusion of water surface -*THINK EYE LEVEL*
    n_Circle.style.top = parseInt(.75 * window.innerHeight) + parseInt(locY * .4) + 'px';
    n_Circle.style.left = parseInt(locX) + 'px';
    window.setTimeout(function(){createC()},parseInt(Math.random() * 200 + 100));
  }else{return;}//EXIT Function loop
  //ASSIGN new check values before end ot the cycle
  checkX = locX;
  checkY = locY;

  }
//We manipulate the droplet images by this quantity every 'frame'
function changeCs(){
    var arrayCs = document.getElementsByClassName('circle');
    for(i=0; i < arrayCs.length; i++){
        arrayCs[i].style.width = parseInt(arrayCs[i].style.width) + 100 + 'px';
        arrayCs[i].style.left = parseInt(arrayCs[i].style.left) - 50 + 'px';
//The ratio of expansion does not match to the images aspect ratio.
//This helps to create the illusion of depth to the surface deformation
//by shearing a portion of the ellipse away, conveying a sense of depth and angularity
        arrayCs[i].style.height = parseInt(arrayCs[i].style.height) + 26 + 'px'; 
        arrayCs[i].style.top = parseInt(arrayCs[i].style.top) - 14 + 'px';
        arrayCs[i].style.opacity = arrayCs[i].style.opacity - .01;
        if(arrayCs[i].style.opacity <= .00){
            document.body.removeChild(arrayCs[i]);}}}

Will get a JSFiddle up pronto. Any ideas are much appreciated.

like image 508
Thomas Cheney Avatar asked Feb 03 '26 04:02

Thomas Cheney


1 Answers

See if this makes a difference, to me it seems faster but its hard to tell as I'm not as nuanced to what your expectations are. fiddler: http://jsfiddle.net/QNf4K/4/

Instead of creating a div element everytime I just created it once, then cloned it. And I cached the length.

    var locX = '';
    var checkX = '';
    var locY = '';
    var checkY = '';
    var eventTimer = setInterval(function(){changeCs()}, 1);//'FRAMERATE' of state changes in milliseconds
    var ln_Circle = document.createElement('div');
        ln_Circle.setAttribute('class','circle');
        ln_Circle.setAttribute('style','width:10px;height:3px;opacity:.4;');

  //  then just cloned it here:
    var n_Circle = ln_Circle.cloneNode(true);

 //   I also cached the length here:


            var l = arrayCs.length, i = 0;
            for(; i < l; i++){

// Then added a test for existence of arrayCs[i]

          if( arrayCs[i]){
            arrayCs[i].style.width = parseInt(arrayCs[i].style.width) + 100 + 'px';
like image 128
james emanon Avatar answered Feb 04 '26 17:02

james emanon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!