0

I have the following code, which I have integrated as an iframe. By scrolling, images are loaded one after the other. after the last image, I would like the page to continue scrolling, as I have more content at the bottom. it does that, but I either have to move the mouse a little and scroll or scroll several times. does anyone have a solution for this?

const html = document.documentElement;
const canvas = document.getElementById("3d");
const context = canvas.getContext("2d");

const frameCount = 27;
const currentFrame = index => (
  `https://www.in-visionen.de/Webdesign/Webdesign${index.toString().padStart(4, '0')}.jpg`
)

const preloadImages = () => {
  for (let i = 1; i < frameCount; i++) {
    const img = new Image();
    img.src = currentFrame(i);
  }
};

const img = new Image()
img.src = currentFrame(1);
canvas.width = 3840;
canvas.height = 2160;
img.onload = function() {
  context.drawImage(img, 0, 0);
}

const updateImage = index => {
  img.src = currentFrame(index);
  context.drawImage(img, 0, 0);
}

window.addEventListener('scroll', () => {
  const scrollTop = html.scrollTop;
  const maxScrollTop = html.scrollHeight - window.innerHeight;
  const scrollFraction = scrollTop / maxScrollTop;
  const frameIndex = Math.min(
    frameCount - 1,
    Math.ceil(scrollFraction * frameCount)
  );

  requestAnimationFrame(() => updateImage(frameIndex + 1))
});

preloadImages()
.art {
  height: 100vh;
}

.design {
  height: 200vh;
}

canvas {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: 80vw;
  max-height: 80vh;
}
<div class="art">
  <div class="design">
    <canvas id="3d">
</canvas>
  </div>
</div>

5
  • I made a snippet of your code however note that <script language="javascript" type="text/javascript"> can simply be <script> as all modern browsers default to JavaScript now days. Commented Jun 25, 2024 at 14:33
  • 1
    You may wish to add some content that is scrollable to better illustrate the issue Commented Jun 25, 2024 at 14:34
  • FWIW you can elimiate canvas { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); max-width: 80vw; max-height: 80vh; } entirely if you use the .design styled as a grid and super centered .desgn{display:grid; place-items:center;} Commented Jun 25, 2024 at 14:37
  • I think you can use IntersectionObserver here; I started to put an answer in but will need to come back later as I get time to do so Commented Jun 27, 2024 at 11:51
  • Hey Mark Schultheiss, thnaks... i will wait for the code... I am a beginner and i understand not so much about javescript, but i want to have this design... Commented Jun 27, 2024 at 12:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.