I'm creating multiple, smaller copies of an element whenever it is clicked. These clones are sent a random distance and direction. I'm having trouble keeping them within the document boundaries. Here's the Fiddle.
I know that I need to check the position of each new element and compare it to the document boundaries so that any new element it spawns itself will conform, but then I'm worried about users resizing their browsers, and also if the user has a very small window how it will affect the functionality... so I guess I'd also like to set a floor or minimum size that the boundaries can be relegated to.
This all seems somewhat resource-intensive and I don't want it to be too taxing. Is there an efficient way to accomplish what I'm wanting? You can see my pathetic attempts in the Fiddle or below:
var contW = $(document).width();
var contH = $(document).height();
var source = $(this).position();
var posNeg = Math.random() < 0.5 ? -1 : 1;
var newTop = Math.floor(Math.random() * (contH / 2 + (posNeg * $(this).height()))) * posNeg;
var posNeg = Math.random() < 0.5 ? -1 : 1;
var newLeft = Math.floor(Math.random() * (contW / 2 + (posNeg * $(this).width()))) * posNeg;