gpt4 book ai didi

javascript - 如何使用 Angular 防止随机定位的图像重叠

转载 作者:行者123 更新时间:2023-11-28 16:02:49 26 4
gpt4 key购买 nike

我有一个 Angular 应用程序,它使用以下代码将多个图像加载到页面上的随机位置:

HTML:

<div ng-repeat="s in selectedImages" class="container">
<img ng-src="{{s.img}}" class="sImage" ng-style="s.pos"/>
</div>

CSS:

.wordImage {
position:absolute;
width:100px;
}

JS Controller :

function loadImages() {
$scope.myImages = ['img1.png', 'img2.png', 'img3.png', 'img4.png', 'img5.png']
$scope.selectedImages = [];
for (i in $scope.myImages) {
$scope.selectedImages.push(addRandomLocationToImage($scope.myImages[i]));
}
}

function addRandomLocationToImage(image) {
image.pos = {};
var preTop = getRandomHeight(); // get Height for this image
var preLeft = getRandomWidth(); // get Width for this image
image.pos = {top:preTop,
left:preLeft};
return image; // returns the same image object but contains pos{} for ng-style
}

function getRandomHeight() {
var imgHeight = 100;
var winHeight = $(window).height();
var randomH = Math.random() * (winHeight - 100); // subtract 100 for the header. and also footer padding
if (randomH < 150) {
randomH += 150; // add to keep it out of the header
}
if(winHeight - randomH < 100) { // if image will be on bottom edge of page
randomW -= imgHeight; // subtract 100 because that is the height of the images, this will prevent them from being partially off the page
}
return randomH;
}

function getRandomWidth() {
var imgWidth = 100;
var winWidth = $(window).width();
var randomW = Math.random() * winWidth;
if (randomW < 0) { // make sure it is not less than zero, then assign new number until it is greater than zero
while (randomW < 0) {
randomW = Math.random() * winWidth;
}
}
if (winWidth - randomW < 100) { // if image will be on right edge of page
randomW -= imgWidth; // subtract 100 because that is the width of the images, this will prevent them from being partially off the page
}
return randomW;
}

loadImages();

这肯定会在页面上生成随机图像……但它们很容易重叠。 我的问题是,如何防止它们重叠?这是我一直在处理的一些代码。

var newLeft = currentImage.pos.left;
var newTop = currentImage.pos.top;
for (i in $scope.selectedImages) {
var originalLeft = $scope.selectedImages[i].pos.left;
var originalTop = $scope.selectedImages[i].pos.top;
if ((originalLeft - newLeft < 100 && originalLeft - newLeft > -100) && // could overlap horizontally
(originalTop - newTop < 100 && originalTop - newTop > -100)) { // could overlap vertically
//do something to select a new random location.
}
}

最佳答案

基本上,您必须检查每个其他图像的图像位置。您可以为此使用 Array.some:

Considering that you store the image position in the image object as x and y properties

function checkCollision(testImage) {
return $scope.myImages.some(function(img) {
if (img == testImage)
return false;

if (!img.x || !img.y) // Image has no position yet
return false;

return
testImage.x < img.x + imgWidth &&
testImage.x + imgWidth > img.x &&
testImage.y < img.y + imgHeight &&
testImage.y + imgHeight > img.y;
});
}

您必须注意,根据可用空间和图像大小,可能会出现无法为图像找到合适位置的情况。

我做了一个functional example .

关于javascript - 如何使用 Angular 防止随机定位的图像重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39775433/

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