ランダムに40 imgをランダムに生成しましたが、場合によってはこれらのimgの一部が衝突します。JS:多くの要素の衝突をチェックする方法は?
私の質問は、衝突していないことを確認し、衝突した場合に新しいポジションを与える方法でしょうか?
私のコードは以下のとおりですが、明確にするコメントもあります。
$(document).ready(function() {
var viewport = $(window).height();
var pageHeight = $(document).height();
var pageWidth = $(document).width();
var x = 1;
var y = 1;
var itemsPerPage = Math.floor(pageHeight/viewport * 10);
var imgsArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
//Generate random nr for picking one of the images
function generateRandomForArray() {
var num = Math.floor(Math.random() * 9);
return num;
}
//Generate random nr for picking one of the left position (max is page width)
function generateLeftRandom() {
var num = Math.floor(Math.random() * pageWidth);
return num;
}
//Generate random nr for picking one of the top position (max is page height)
function generateTopRandom() {
var num = Math.floor(Math.random() * pageHeight);
return num;
}
//Generate random nr for parallax data-velocity value
function generateVelocityRandom() {
var num = Math.random() * (0.1 - -0.1) + -0.1;;
return num;
}
showLetter();
//Attach img tags with random img src to #toAttachElements container
function showLetter() {
var letter = imgsArray[generateRandomForArray()];
$("#toAttachElements").append("<img class='parallax' data-velocity='" + generateVelocityRandom() + "' src='wp-content/themes/thenewtonstartertheme/img/bg-elements/" + letter + ".png'>");
x++;
if (x < itemsPerPage) {
showLetter();
}
}
//Add random positions to previosly attached imgs
$('#toAttachElements > img').each(function(){
var left = generateLeftRandom();
var top = generateTopRandom();
$(this).css({"position":"absolute","z-index":"1","top": top + "px", "left": left + "px"});
});
});
EDIT!解決策私が最終的に思いついたのは、私のページを行に分割し、すべての行で1つのimgにすることです。以下のコード。
$(document).ready(function() {
if ($(".to-attach-elements")[0]){
var viewport = $(window).height();
var pageHeight = $(document).height();
var pageWidth = $(document).width();
var x = 1;
var y = 1;
var itemsPerPage = Math.floor(pageHeight/viewport * 10);
var topRows = Math.floor(pageHeight/itemsPerPage);
var imgsArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
//Generate random nr for picking one of the images
function generateRandomForArray() {
var num = Math.floor(Math.random() * 9);
return num;
}
//Generate random nr for picking one of the left position (max is page width)
function generateLeftRandom() {
var num = Math.floor(Math.random() * pageWidth);
return num;
}
//Generate random nr for picking one of the top position (max is page height)
function generateTopRandom() {
var num = Math.floor(Math.random() * topRows);
return num;
}
//Generate random nr for parallax data-velocity value
function generateVelocityRandom() {
var num = Math.random() * (0.1 - -0.1) + -0.1;;
return num;
}
var top = generateTopRandom();
showLetter();
//Attach img tags with random img src to #toAttachElements container
function showLetter() {
var letter = imgsArray[generateRandomForArray()];
$(".to-attach-elements").append("<img class='parallax' data-velocity='" + generateVelocityRandom() + "' src='wp-content/themes/thenewtonstartertheme/img/bg-elements/" + letter + ".png'>");
x++;
if (x < itemsPerPage) {
showLetter();
}
}
//Add random positions to previosly attached imgs
$('.to-attach-elements > img').each(function(){
var left = generateLeftRandom();
$(this).css({"position":"absolute","z-index":"1","top": top + "px", "left": left + "px"});
top = top + topRows;
});
}
});
要素の1つに新しい場所がない場合、何が起こりますか? –
ページが大きく、画像が小さいため空き領域が常にあります –
画像のランダムな位置を取得し、ページにすでに追加されている各画像の位置 - 既に追加された画像と位置が交差している間に新しいランダムな位置を生成し、再度テストします。 – Cyclonecode