Synchronising Sprites and scrolling using ScrollMagic

835 views Asked by At

Overflow community, I was looking for an animation playable by scrolling and I found

This perfect example

I wanted to make something like that with a lack of knowledge about js and js libraries. But even with copy and past all the elements it still doesn't work.

// initialise ScrollMagic controller
var controller = new ScrollMagic.Controller();

// create Tween
var tween = TweenMax.to("#js-animation", 1.0, {
 backgroundPosition: "100% 0",
 ease: SteppedEase.config(480)
})

// build scene
var scene = new ScrollMagic.Scene({duration: 15000})
 .triggerHook("onCenter")
 .setPin("#js-pinned")
 .setTween(tween)
 .addTo(controller);
body {
  padding: 20px;
  text-align: center;
}

.container {
  font-size: 15em;
  min-height: 110vh;
}

.cnc {
  margin: auto;
  width: 50%;
  height: 50%;
  background:  url('http://image.gilawhost.com/16/12/29/9mq5kqgu.png') no-repeat 0 0%;
  background-size: 100%;
}

h1 {
  font-size: 1.2em;
}

p {
  width: 60%;
  margin: auto;
  text-align: left;
}

.p {
  margin-top: 120px;
  font-size: 14px;
  text-align: center;
}
<!DOCTYPE html>
<!--
come from the demo de Tom Bennet
http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock
-->
<html >
<head>
  <meta charset="UTF-8">
  <title>Demo 4: Synchronising Playback with the Scrollbar</title>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

</head>

<body>
  <h1>Demo 4: Synchronising Playback with the Scrollbar</h1>

<p>A responsive sprite animation that is synchronised with the scrollbar, and that remains in a fixed position for its duration of 1500 pixels. Scroll up and down to control playback.</p>

<div class="container" id="js-pinned">
  <div class="cnc" id="js-animation"></div>
</div>

<p class="p">Demo by Tom Bennet. <a href="http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock" target="_blank">See article</a>.</p>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.16.1/TweenMax.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/ScrollMagic.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/plugins/animation.gsap.min.js'></script>

</body>
</html>

Did I miss something or is the sprites file too heavy ?

1

There are 1 answers

0
Jon Uleis On BEST ANSWER

Your sprite PNG is fine - not too heavy. You just needed some more defined sizing rules on the image element.

Here's what that element looks like now:

.cnc {
  margin: auto;
  width: 350px;
  height: 200px;
  background:  url('http://image.gilawhost.com/16/12/29/9mq5kqgu.png') no-repeat 0 0%;
  background-size: auto 100%;
}

Note the width/height dimensions proportional to one frame of your sprite animation, and background-size: auto 100% (which will fit the image to 100% of the height of the container, but let the width grow proportionally).

Working example below - scroll away:

// initialise ScrollMagic controller
var controller = new ScrollMagic.Controller();

// create Tween
var tween = TweenMax.to("#js-animation", 1.0, {
 backgroundPosition: "100% 0",
 ease: SteppedEase.config(479)
})

// build scene
var scene = new ScrollMagic.Scene({duration: 15000})
 .triggerHook("onCenter")
 .setPin("#js-pinned")
 .setTween(tween)
 .addTo(controller);
body {
  padding: 20px;
  text-align: center;
}

.container {
  font-size: 15em;
  min-height: 110vh;
}

.cnc {
  margin: auto;
  width: 350px;
  height: 200px;
  background:  url('http://image.gilawhost.com/16/12/29/9mq5kqgu.png') no-repeat 0 0%;
  background-size: auto 100%;
}

h1 {
  font-size: 1.2em;
}

p {
  width: 60%;
  margin: auto;
  text-align: left;
}

.p {
  margin-top: 120px;
  font-size: 14px;
  text-align: center;
}
<!DOCTYPE html>
<!--
come from the demo de Tom Bennet
http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock
-->
<html >
<head>
  <meta charset="UTF-8">
  <title>Demo 4: Synchronising Playback with the Scrollbar</title>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

</head>

<body>
 

<div class="container" id="js-pinned">
  <div class="cnc" id="js-animation"></div>
</div>

<p class="p">Demo by Tom Bennet. <a href="http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock" target="_blank">See article</a>.</p>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.16.1/TweenMax.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/ScrollMagic.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/plugins/animation.gsap.min.js'></script>

</body>
</html>