<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 500vh;
}
body.before-load {
overflow-y: hidden;
}
.sample-canvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
</style>
</head>
<div class="container">
<canvas class="sample-canvas" width="1920" height="1080"></canvas>
</div>
<script>
const canvas = document.querySelector('.sample-canvas');
//context 객체를 이용하여 그림을 그린다
const context = canvas.getContext('2d');
const videoImages = [];
let loadedImagesCount = 0;
let totalImagesCount = 960;
function setImages() {
for (let i = 0; i < totalImagesCount; i++) {
let imgElem = new Image();
imgElem.src = `../video/002/IMG_${7027 + i}.JPG`;
videoImages.push(imgElem);
imgElem.addEventListener('load',function(){
loadedImagesCount++;
if(loadedImagesCount === totalImagesCount){
init();
}
})
}
}
let progress;
let currentFrame;
function init() {
//0번째 index를 그려주고 시작한다
context.drawImage(videoImages[0], 0, 0);
window.addEventListener('scroll', function () {
progress = pageYOffset / (document.body.offsetHeight - window.innerHeight);
if (progress < 0) progress = 0;
if (progress > 1) progress = 1;
requestAnimationFrame(function(){
currentFrame = Math.round((totalImagesCount - 1) * progress);
//context객체에 drawImage 매서드를 이용해 그림을 캔버스판에 그리는 것
//내가 그릴 이미지 객체를 첫번째에 넣고 x좌표, y좌표
context.drawImage(videoImages[currentFrame], 0, 0);
})
});
}
window.addEventListener('load', init);
setImages();
</script>
</body>
</html>
2022. 05. 10. 19:01
혹시 어떻게 해결하셨나요,,,,,,????ㅜㅜㅜ