gpt4 book ai didi

javascript - 使用上一个和下一个按钮在 javascript 中循环浏览图像

转载 作者:行者123 更新时间:2023-11-30 15:21:28 25 4
gpt4 key购买 nike

我正在使用 Javascript 编写一个小型移动应用程序。

我正在调用一个远程 API,该 API 会响应一些 JSON 格式的数据。在该数据中是我需要在我的应用程序中显示的产品图像的 URL。我已经将 JSON 解析为一个 JS 数组,现在有一个包含所有图像 URL 的数组。当我第一次加载我的应用程序时,我有一个 Canvas ,用于显示数组中的第一张图像。我有两个按钮,上一个和下一个。我希望能够通过使用数组中的 URL 在 Canvas 上绘制来使用这些按钮循环浏览图像。

有什么想法吗?

最佳答案

您正在寻找类似于以下内容的内容吗?

  // Image URLs
var imageUrls = ['http://emojipedia-us.s3.amazonaws.com/cache/8b/bd/8bbd3405b0a197214e229428c23dbe60.png', 'http://emojipedia-us.s3.amazonaws.com/cache/05/d1/05d1ba284ee1a3bfe4e0f68988baafb9.png', 'http://emojipedia-us.s3.amazonaws.com/cache/99/4c/994c5997c7a509703cc53ec2000bb258.png', 'http://emojipedia-us.s3.amazonaws.com/cache/59/0c/590c832e73a472c416bf9d8bfdd02a4a.png'];

// Keep track of the index of the image URL in the array above
var imageShownIndex = 0;

// Create a canvas
var canvas = document.createElement('canvas');
var canvasContext = canvas.getContext('2d');
canvas.height = 150;
canvas.width = 150;

// Create a button that will load the previous image on the canvas when clicked
var previousButton = document.createElement('button');
previousButton.innerHTML = 'Previous Image';
previousButton.onclick = function () {
// Show images in a cycle, so when you get to the beginning of the array, you loop back to the end
imageShownIndex = (imageShownIndex==0) ? imageUrls.length-1 : imageShownIndex-1;
updateImage();
};

// Do same for the next button
var nextButton = document.createElement('button');
nextButton.innerHTML = 'Next Image';
nextButton.onclick = function () {
imageShownIndex = (imageShownIndex == imageUrls.length-1) ? 0 : imageShownIndex+1;
updateImage();
};

document.body.appendChild(previousButton);
document.body.appendChild(canvas);
document.body.appendChild(nextButton);

// Show the first image without requiring a click
updateImage();

function updateImage() {

// Create the Image object, using the URL from the array as the source
// You could pre-load all the images and store them in the array, rather than loading each image de novo on a click
var img = new Image();
img.src = imageUrls[imageShownIndex];

// Clear the canvas
canvasContext.clearRect(0, 0, 150, 150);

// After the image has loaded, draw the image on the canvas
img.onload = function() {
canvasContext.drawImage(img, 0, 0);
}

}

编辑:或者如果您已经制作了 HTML 元素,您可以只使用 JavaScript 来控制 Canvas 。例如:

<html>
<body>
<button id="previous_button" onclick="goToPreviousImage()">Previous Image</button>
<canvas id="image_canvas" height=150, width=150></canvas>
<button id="next_button" onclick="goToNextImage()">Next Image</button>
</body>

<script>
var imageUrls = ['http://emojipedia-us.s3.amazonaws.com/cache/8b/bd/8bbd3405b0a197214e229428c23dbe60.png', 'http://emojipedia-us.s3.amazonaws.com/cache/05/d1/05d1ba284ee1a3bfe4e0f68988baafb9.png', 'http://emojipedia-us.s3.amazonaws.com/cache/99/4c/994c5997c7a509703cc53ec2000bb258.png', 'http://emojipedia-us.s3.amazonaws.com/cache/59/0c/590c832e73a472c416bf9d8bfdd02a4a.png'];

var imageShownIndex = 0;

var canvas = document.getElementById('image_canvas');
var canvasContext = canvas.getContext('2d');

function goToPreviousImage() {
imageShownIndex = (imageShownIndex==0) ? imageUrls.length-1 : imageShownIndex-1;
updateImage();
};

function goToNextImage() {
imageShownIndex = (imageShownIndex == imageUrls.length-1) ? 0 : imageShownIndex+1;
updateImage();
};

updateImage();

function updateImage() {

var img = new Image();
img.src = imageUrls[imageShownIndex];

canvasContext.clearRect(0, 0, 150, 150);

img.onload = function() {
canvasContext.drawImage(img, 0, 0);
}

}

</script>
</html>

关于javascript - 使用上一个和下一个按钮在 javascript 中循环浏览图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com