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

标签 javascript html css angularjs

我有一个 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/

相关文章:

javascript - TinyMCE,允许数据属性

javascript - 如何格式化这个json

ajax - 使用 FormData 上传 JavaScript Blob

jquery - Div 无法识别悬停

html - CSS:在保持纵横比的同时居中和放大或缩小图像

javascript - 使用 javascript 在 IE8 和其他浏览器中获取主体类的标准解决方案是什么

html - 显示产品结果的文本框过滤器

javascript - 在 tumblr 博客上隐藏主页提要

css - 从 flowplayer 中删除嵌入图标 <>

javascript - 超出最大调用堆栈大小避免出现这种情况