-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendlessScroller.js
More file actions
66 lines (52 loc) · 2.07 KB
/
Copy pathendlessScroller.js
File metadata and controls
66 lines (52 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const imgHeightInput = document.getElementById('imgHeight');
const imgWidthInput = document.getElementById('imgWidth');
const viewportWidthInput = document.getElementById('viewportWidth');
const scrollSpeedInput = document.getElementById('scrollSpeed');
const animateButton = document.getElementById('animateButton');
const canvasContainer = document.querySelector('.canvas-container');
animateButton.addEventListener('click', () => {
const imgHeight = imgHeightInput.value;
const imgWidth = imgWidthInput.value;
const viewportWidth = viewportWidthInput.value;
const scrollSpeed = scrollSpeedInput.value;
// remove any existing canvas from the canvas container
while (canvasContainer.firstChild) {
canvasContainer.removeChild(canvasContainer.firstChild);
}
// make a new canvas element and set its dimensions based on inputs
const canvas = document.createElement('canvas');
canvas.width = viewportWidth;
canvas.height = imgHeight;
canvasContainer.appendChild(canvas);
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = URL.createObjectURL(fileInput.files[0]);
img.onload = function() {
// initialize the x coordinates for two images, so that they are seamlessly connected
let x1 = 0;
let x2 = imgWidth;
//console.log(imgWidth);
//console.log(x2);
///draw the images and animate the scrolling effect
function draw() {
ctx.clearRect(0, 0, viewportWidth, imgHeight);
// draw the two images side by side
ctx.drawImage(img, x1, 0, imgWidth, imgHeight);
ctx.drawImage(img, x2, 0, imgWidth, imgHeight);
// update the x coordinates based on the scroll speed
x1 -= scrollSpeed;
x2 -= scrollSpeed;
// Check if the images has scrolled off the viewport, if so reset the x coordinate
if (x1 <= -imgWidth) {
x1 = x2 + imgWidth;
}
if (x2 <= -imgWidth) {
x2 = x1 + imgWidth;
}
//console.log("x1: " + x1)
//console.log("x2: " + x2)
requestAnimationFrame(draw);
}
draw();
}
});