Giter Site home page Giter Site logo

Coordinating draw with scroll about vivus HOT 4 CLOSED

benklocek avatar benklocek commented on July 30, 2024
Coordinating draw with scroll

from vivus.

Comments (4)

maxwellito avatar maxwellito commented on July 30, 2024

So here is what I made:

var vivus = new Vivus('Layer_1', {
  duration: 10000000,
  type: 'sync',
  start: 'manual'
});

window.addEventListener('wheel', scrolling);

var progress = 0;
var dh = jQuery(document).height(); // document height
var wh = jQuery(window).height(); // window height
var eh = jQuery('#Layer_1').height(); // element height

function scrolling(e) {
  var elementoffsettop = jQuery("#Layer_1").offset().top;
  var windowsscrollpostion = jQuery(window).scrollTop();
  
  // Define where the scroll starts and end
  var scrollStart = elementoffsettop - wh;
  var scrollEnd = elementoffsettop + eh - wh/2; // I made it stop while at half screen
  var scrollDistance = scrollEnd - scrollStart;
  
  // Calcultate the progress
  var animProgress = (windowsscrollpostion - scrollStart)/scrollDistance
  animProgress = Math.min(1, Math.max(0, animProgress));

  // Little hack to ask for a rendering at frame request. 
  // Otherwise you trigger a re-draw at every scroll event. 
  // It should make the scroll/animation smoother
  requestVivusUpdate(animProgress); 
}


// The logic of all this is to request an animation frame
// and wait for keep the value updated until the frame
// is executed.
var requestVivusUpdateRAF = null;
var requestVivusUpdateVal = 0;
function requestVivusUpdate (progress) {
	requestVivusUpdateVal = progress;
  if (requestVivusUpdateRAF) {
  		return;
  }
  requestVivusUpdateRAF = window.requestAnimationFrame(() => {
  	console.log(requestVivusUpdateVal);
    requestVivusUpdateRAF = null;
    vivus.setFrameProgress(requestVivusUpdateVal);
  });
}

// Ok there might be some useless calls with value 0 and 1 while outside of the desired scroll
// Feel free to fix this ;-)

I dunno if this helps

from vivus.

benklocek avatar benklocek commented on July 30, 2024

Yes! This is brilliant! Much less of a hit on the page, and stops just where it needs to.

Hero!!

from vivus.

maxwellito avatar maxwellito commented on July 30, 2024

😉 you're welcome!

from vivus.

benklocek avatar benklocek commented on July 30, 2024

Hi there,

I'm working on writing this so I can trigger different animations while scrolling.

I have it working when I only have one SVG, but when I add another, both don't show. I'm certain vivus supports multiples on a page or am I missing something?

http://jsfiddle.net/benklocek/7aog23cj/

from vivus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.