bouncing ball algorithm issue with JavaScript - javascript

LIVE DEMO
I'm tying to make the ball which is a div bounce to the left once coming from the right but it still come back to the right side, i know there is too many similar questions who gave a solution to this but they all implementing canvas which is not my case , the way I'm doing it seems right to me but like you can see in the live demo the ball still come back to the right , here is the code :
var speed = 15,
directionTop = 1,
directionLeft = 1;
setInterval(function(){
var ballElement = document.getElementsByClassName('ball')[0],
containerElement = document.getElementsByClassName('mainDiv')[0],
playersTestRebounce = document.getElementsByClassName('player')[0];
if (ballElement) {
var boxLeftPos = ballElement.offsetLeft,
boxRightPos = boxLeftPos + ballElement.offsetWidth,
offsetContainer = containerElement.offsetWidth + 40;
offsetContainerTop = containerElement.offsetTop;
if (boxRightPos > offsetContainer) {
directionTop = 1;
directionLeft = -1;
}
if (boxLeftPos < -40) {
directionTop = 1;
directionLeft = 1;
}
if (ballElement.offsetTop > playersTestRebounce.offsetTop && ballElement.offsetTop < (playersTestRebounce.offsetTop + 70) && ballElement.offsetLeft < (playersTestRebounce.offsetLeft + 12)) {
directionTop = 1;
directionLeft = 1;
}
if (ballElement.offsetTop > 390) {
directionTop = -1;
directionLeft = 1;
}
if (ballElement.offsetTop < 0) {
directionTop = 1;
directionLeft = 1;
}
ballElement.style.left = (boxLeftPos + speed * directionLeft) + 'px';
ballElement.style.top = (ballElement.offsetTop + speed * directionTop) + 'px';
/*cordinators = getPos(ballElement);
console.log("ball X : "+ cordinators.y + " ball Y :" +cordinators.x);*/
/*random = Math.floor((Math.random() * 376) + 6);
if (boxLeftPos < -40 || boxRightPos > offsetContainer) {
ballElement.style.top = random + 'px';
} */
}
}, 100);

Basic bouncing can be reduced to this http://jsfiddle.net/52b261e2/1
if (ballElement.offsetTop > 390 ||ballElement.offsetTop <0) {
directionTop *=-1;
}
if (boxLeftPos <0 ||boxRightPos>600) {
directionLeft *=-1;
}
when you reach bottom or top it changes 1 in -1 and -1 in 1
same goes for left to right
*edit forgot to update fiddle

Related

Increase and decrease SVG shape - JS

I would like you to help me for a thing here, for a function to increase and then decrease SVG shape when it hits limit.
It should go from 3 to 6 and then 6 to 3 and so on... but instead it goes from 3 to 6 and then 6 to minus infinite. And I don't understand why.
Here is my code :
var size = 3;
var sizeManager = 1;
function increaseAnimation(el){
var elem = document.getElementById(el);
elem.style.transform = "scale("+size+")";
timer = setTimeout('increaseAnimation(\''+el+'\',3000)');
size=size+0.005*sizeManager;
if(size >= 6){
sizeManager=sizeManager*-1;
}
if (size <= 3){
sizeManager=sizeManager*+1;
}
}
Your weird setTimeout implementation, with bound was broken.
There's also the issue that your sizeManager is not properly reflecting:
function increaseAnimation(id, interval) {
var size = 1;
var velocity = 0.05;
var elem = document.getElementById(id);
function iterate() {
elem.style.transform = "scale(" + size + ")";
size += velocity;
if (size > 2 || size < 1) {
velocity *= -1; // velocity reflected
}
}
var timer = setInterval(iterate, interval);
return function stop() {
clearInterval(timer)
}
}
I also added a stop function which you can call at a later point.
var stopper = increaseAnimation("content", 16);
setTimeout(stopper, 5000);
The error is with the line sizeManager=sizeManager*+1; Multiplying a number by one doesn't change it. You basically want to toggle sizeManager between -1 and +1, and you can do so by multiplying by -1, regardless of whether it is currently negative or positive.
I've tested this code and it seems to work:
var size = 3;
var sizeManager = 1;
function increaseAnimation(el) {
var elem = document.getElementById(el);
elem.style.transform = "scale(" + size + ")";
timer = setTimeout("increaseAnimation('" + el + "', 3000)");
size += 0.005 * sizeManager;
if (size >= 6 || size <= 3) {
sizeManager *= -1;
}
}
Full HTML for a POC demo at: https://pastebin.com/GW0Ncr9A
Holler, if you have questions.
function Scaler(elementId, minScale, maxScale, deltaScale, direction, deltaMsecs) {
var scale = (1 == direction)?minScale:maxScale;
var timer = null;
function incrementScale() {
var s = scale + deltaScale*direction;
if (s < minScale || s > maxScale) direction *= -1;
return scale += deltaScale*direction;
};
function doScale(s) {
document.getElementById(elementId).style.transform = 'scale(' + s + ')';
};
this.getDeltaMsecs = function() {return deltaMsecs;};
this.setTimer = function(t) {timer = t;};
this.run = function() {doScale(incrementScale());};
this.stop = function() {
clearInterval(timer);
this.setTimer(null);
};
};
var scaler = new Scaler('avatar', 3, 6, .05, 1, 50);
function toggleScaler(ref) {
if ('run scaler' == ref.value) {
ref.value = 'stop scaler';
scaler.setTimer(setInterval('scaler.run()', scaler.getDeltaMsecs()));
}
else {
scaler.stop();
ref.value = 'run scaler';
}
};

How to prevent randomly generated images from overlapping using Javascript

I'm trying to spawn randomly generated targets on the screen using a function to copy an invisible image of the target to be spawned at a random place within the screen using window object properties.
For this to happen I think the image have to have position set to absolute, but then several images may be spawned in a way that they will overlap, how can I prevent this from happening?
The div element where I copy my first image and also store the other copies.
<div id="targetsDiv">
<img src="target2.png" alt="target_shot" class="target" />
</div>
Inside the script:
var x_pixels = window.innerWidth - 180;
var y_pixels = window.innerHeight - 180;
var x_midScreen = window.innerWidth / 2;
var y_midScreen = window.innerHeight / 2;
var xRandom = Math.floor(Math.random()*x_pixels) +1;
var yRandom = Math.floor(Math.random()*y_pixels) +1;
var targetCollection = document.getElementById("targetsDiv");
var targetCopy = targetCollection.innerHTML
var targetList = document.getElementsByClassName("target");
targetList[0].style.display = "none";
var targetNr = 1;
var numberOfSpawns = 3;
function spawnTargets () {
while (targetNr <= numberOfSpawns) {
if ((xRandom < x_midScreen - 126 || xRandom > x_midScreen + 26) || (yRandom < y_midScreen - 126 || yRandom > y_midScreen + 26)) {
targetCollection.innerHTML += targetCopy;
targetList[targetNr].style.left = xRandom + "px";
targetList[targetNr].style.top = yRandom + "px";
targetNr++;
}
xRandom = Math.floor(Math.random()*x_pixels) +1;
yRandom = Math.floor(Math.random()*y_pixels) +1;
}
}
PS: I appreciate all help, tips and tweaks that you guys provide:) Thanks for helping
The area you want to exclude in the centre of the screen is rather large, and on my little laptop, there isn't even any room to still show the random positioned images.
But anyway, this piece of code should do the job -- I added some infinite loop protection which on my PC was a life-saver:
HTML (added style attribute)
<div id="targetsDiv">
<img src="target2.png" alt="target_shot" class="target"
style="visibility:hidden"/>
</div>
JavaScript:
window.addEventListener('load', function () {
var targetCollection = document.getElementById("targetsDiv");
var template = document.getElementsByClassName("target")[0];
var targetWidth = template.offsetWidth;
var targetHeight = template.offsetHeight;
var x_pixels = window.innerWidth - targetWidth;
var y_pixels = window.innerHeight - targetHeight;
var x_midScreen = window.innerWidth / 2;
var y_midScreen = window.innerHeight / 2;
function spawnTargets (numberOfSpawns) {
var targets = [];
var count = 0; // infinite recursion protection
for (var i = 0; i < numberOfSpawns; i++) {
do {
do {
var x = Math.floor(Math.random()*x_pixels);
var y = Math.floor(Math.random()*y_pixels);
if (count++ > 200) {
console.log('give up');
return;
}
} while ((x >= x_midScreen-450 && x <= x_midScreen+300) && (y >= y_midScreen-350 || y <= y_midScreen+200));
for (var j = 0; j < i; j++) {
if (x >= targets[j].x - targetWidth && x <= targets[j].x + targetWidth &&
y >= targets[j].y - targetHeight && y <= targets[j].y + targetHeight) break; // not ok!
}
} while (j < i);
targets.push({x, y});
img = document.createElement('img');
img.src = template.src;
img.setAttribute('width', targetWidth + 'px');
img.setAttribute('height', targetHeight + 'px');
img.className = template.className;
targetCollection.appendChild(img);
img.style.left = x + "px";
img.style.top = y + "px";
}
}
spawnTargets(3);
});

program flow animating in javascript

Hello I am having errors with my code:
https://jsfiddle.net/wzhm2whj/
<script>
//Initial Global variables
var mainloop_frame_time = 34;
var top = 0;
var rootMenu = document.getElementById('menu');
var rootMenuDivs = rootMenu.getElementsByTagName('div')[0];
var rootListDivs = rootMenuDivs.getElementsByTagName('ul')[0];
var childDivs = rootListDivs.getElementsByTagName('div');
var childDiv = childDivs[0];
var childDiv_counter = 0;
var child_change_flag = true;
var child_index_increment = 0;
var child_index_amount = childDivs.length;
//var child_animation_keyframe = 0;
var frame = 0;
var childDiv_tmp1_position = 0;
//finding the web browsers viewport size.
var elem = (document.compatMode === "CSS1Compat") ? document.documentElement : document.body;
var client_height = elem.clientHeight;
var process_array = new Array();
//Initial styling
for (var i = 0; i < childDivs.length; i++) {
var childDiv = childDivs[0];
childDiv.style.backgroundColor = "antiquewhite";
}
var childDiv = childDivs[0];
//rotate function variables
var rotate_div;
var rotate_passed_deg;
var rotate_deg_stop;
var rotate_results;
var rotate_current_deg = 0;
var speed_modifier = 1;
var tmp1_speed = 0;
//case flags
case2_flag = -1;
case3_flag = -1;
//This may not be needed >>> If not, put all code in mainloop.
var processes_logic = function() {
switch (frame) {
case 0:
process_array.push(menu_child0);
break;
//this case is when the previous case is 80% done
case 28:
rootMenu.style.transformOrigin = "top left";
process_array.push(menu_slant);
break;
case 35:
//Added the ability for paramaters, all push paramaters here are: function, menu_index, position, speed, tmp as flag for switching to next menu,
//process_index used to give the process index as refrence to delete..
window.alert(process_array.length);
process_array.push(new Array(menu_div_slide_out, child_index_amount - 1, 0, 0, 0, process_array.length-1));
break;
}
}
var initiate_all_processes = function() {
for (var i = 0; i < process_array.length; i++) {
//Added the ability for paramaters, considerer removing as its not used atm, or revising.
if (process_array[i] != undefined && process_array[i] != null && process_array[i] != "") {
if (process_array[i].length < 6) {
process_array[i]();
} else {
process_array[i][0](process_array[i][5]);
}
}
}
}
function menu_div_slide_out(process_index) {
/*process_array[process_index][
0 = function,
1 = current menu item (index length working backwards)
2 = position,
3 = speed,
4 = tmp,
5 = refrence to this process in array] */
//for debuging purposes to see if a ChildDiv is not devined, what process index is being pointed to.
//window.alert('Process index ' + process_index);
//!!!!!!!! You are probably mixing up how you are setting process index! try +1
process_array[process_index][2] += 3.5 + (process_array[process_index][3] * 1.7);
process_array[process_index][3] += (speed_modifier * .3);
childDivs[process_array[process_index][1]].style.left = process_array[process_index][2] + 'px';
if (process_array[process_index][2] > 100 && process_array[process_index][4] && process_array[process_index][1] > 0) {
// window.alert('CCC');
process_array[process_index][4] = true;
//Add another process at ever 100pxs
process_array.push(new Array(menu_div_slide_out, process_array[process_index][1] - 1, 0, 0, false, process_array.length-1));
//debugger;
} else
if (process_array[process_index][2] >= (900 - (process_array[process_index][2] / 20))) {
childDivs[process_array[process_index][1]].remove();
//process_array.splice(process_array[process_index][5], 1);
}
}
function menu_slant() {
rotate_return = rotate(rootMenu, .1 + (tmp1_speed), 27);
tmp1_speed += (speed_modifier * .5);
if (rotate_return === true) {
/////////////This can be unremoved because there is more animation, perhaps. or can be done in another key frame.
tmp1_speed = 0;
rotate_current_deg = 0;
remove_process(menu_slant);
} else {
if (rotate_return / 27 * 100 >= 60 && case3_flag < 0) {
case2_flag = frame;
}
}
}
var menu_child0 = function() {
childDiv_tmp1_position += 3 + (tmp1_speed * 1.7);
childDiv.style.top = childDiv_tmp1_position + 'px';
rotate(childDiv, .2 + (tmp1_speed), 170);
tmp1_speed += (speed_modifier * .7);
if (childDiv_tmp1_position / client_height * 100 >= 80 && case2_flag < 0) {
case2_flag = frame;
}
if (childDiv_tmp1_position >= client_height) {
childDiv.style.visibility = 'hidden';
tmp1_speed = 0;
childDiv_tmp1_position = 0;
rotate_current_deg = 0;
//may be bloated >>
remove_process(menu_child0);
}
}
function remove_process(index) {
var index_tmp = process_array.indexOf(index);
if (index_tmp >= 0) {
process_array.splice(index_tmp, 1);
}
}
function rotate(rotate_div, rotate_passed_deg, rotate_passed_deg_stop) {
rotate_current_deg += rotate_passed_deg;
rotate_deg = rotate_current_deg < rotate_passed_deg_stop ? rotate_current_deg : rotate_passed_deg_stop;
rotate_div.style.webkitTransform = 'rotate(' + rotate_deg + 'deg)';
rotate_div.style.mozTransform = 'rotate(' + rotate_deg + 'deg)';
rotate_div.style.msTransform = 'rotate(' + rotate_deg + 'deg)';
rotate_div.style.oTransform = 'rotate(' + rotate_deg + 'deg)';
rotate_div.style.transform = 'rotate(' + rotate_deg + 'deg)';
if (rotate_current_deg >= rotate_passed_deg_stop) {
return true;
} else {
return rotate_current_deg;
}
}
//main loop for the animation
var mainloop = function() {
processes_logic();
initiate_all_processes();
frame++;
}
var loop_interval = setInterval(mainloop, mainloop_frame_time);
</script>
I am trying to animate my website falling apart but I am having a hard time articulation this into code. I thought of running the animation in a loop, creating events at specific frames and reusing some codes as functions. I have a rotate function which works to rotate several things.
THE PROBLEM:
The problem I am having is sliding my menu items one at a time to the right. I want one to slide a bit and the next to start sliding after. I wrote a function to slide an item and then in that function it adds another process to an array for the next menu item to be called and run the same function (with passed interval of who is calling). I do not know how many menu items there will be, thats why I am trying to make it dynamic.
I can get it so that the first mwnu item falls, the menu falls by rotating it (some times if there is an error in the code then it wont rotate, but when there are no errors it works better).
The issue is sliding each menu item.
my website is here: http://clearlove.ca/89-404-error
Can any one help me with why this isnt working, and if there is a better way to do what I am trying to do?

Javascript - no repeat image

I have this code in JavaScript and this images 1-6 are still repeating. But I want when it is on 6 it stop and no repeat. How can i do it?
<script>
function displayNextImage2() {
y = (y === images2.length - 1) ? 0 : y + 1;
document.getElementById("img2").src = images2[y];
}
function displayPreviousImage2() {
y = (y <= 0) ? images2.length - 1 : y - 1;
document.getElementById("img2").src = images2[y];
}
function startTimer2() {
setInterval(displayNextImage2, 5000);
}
var images2 = [], y = 0;
images2[0] = "images/one.PNG";
images2[1] = "images/two.PNG";
images2[2] = "images/three.PNG";
images2[3] = "images/four.PNG";
images2[4] = "images/five.PNG";
images2[5] = "images/six.PNG";
</script>
Thank for every answer.
change the displayNextImage2 method to
function displayNextImage2()
{
if ( y <= images2.length - 1 )
{
document.getElementById("img2").src = images2[y];
y++;
clearInterval(timer2);
}
}
ensure that you clear the interval as well, so you need to keep a reference of it
var timer2 = "";
function startTimer2()
{
timer2 = setInterval(displayNextImage2, 5000);
}
it won't repeat after it reaches the last image

Smooth and fast sprite animation (CSS)

I'm trying to make a game of a Space Invaders using JavaScript/jQuery. Now my problem is I cannot make the spaceship move smoothly on the screen.
I am using a keydown event to change sprite's CSS position by n pixels. When I give it a high value, the sprite moves fast but not smoothly. When I choose to move the sprite by 1px it looks smoother, but is very slow. Any ideas how to approach it?
The live demo is here. Use WSAD keys to move the sprite.
PS I'm not asking for animation libraries. I'd like to achieve it using JavaScript/jQuery only.
$(document).ready(function() {
var xPos, yPos;
var height = $(document).height();
var width = $(document).width();
function move(dir) {
console.log(height + "," + width);
xPos = parseInt($('#sprite').css('left'));
yPos = parseInt($('#sprite').css('top'));
switch (dir) {
case 'up':
if (yPos > 10) { $('#sprite').css('top', yPos - 10 + "px"); }
break;
case 'down':
if (yPos < height-140) { $('#sprite').css('top', yPos + 10 + "px"); }
break;
case 'left':
if (xPos > 10) { $('#sprite').css('left', xPos - 10 + "px"); }
break;
case 'right':
if (xPos < width-140) { $('#sprite').css('left', xPos + 10 + "px"); }
break;
}
}
$(document).keydown(function(event) {
var mykey = String.fromCharCode(event.which);
if (mykey == "W" || mykey == "w") { move('up'); }
if (mykey == "S" || mykey == "s") { move('down') }
if (mykey == "A" || mykey == "a") { move('left') }
if (mykey == "D" || mykey == "d") { move('right') }
});
});
Just add CSS3 transition to your ship :
#sprite{
-webkit-transition : all 0.4s ease-in-out;
-moz-transition : all 0.4s ease-in-out;
-o-transition : all 0.4s ease-in-out;
transition : all 0.4s ease-in-out;
}
However, animating with translate is better than position top/left (article by Paul Irish).
Besides, optimize your code by caching $('#sprite') with
var $sprite = $('#sprite');
function move(dir) {
xPos = parseInt($sprite.css('left'));
yPos = parseInt($sprite.css('top'));
...
If you don't do this (as the code is currently), you force jQuery to go all over the whole DOM many times, every time a key is pressed. If you do, the ship is cached so jQuery doesn't have to look for it every time. I suggest you to have a look at jQache for easy caching.
I give you a pure Javascript snippet that is as simple as possible, so you easily replace the divs with images or animated sprites.
Also the positions you control with Javascript exactly as you want.
For space invader this is ideal, because all ships will move smoothly from one position to the other. It is a misstake from old tween experiences to believe it get slow with Javascript, but the intermediate pixels is not needed to tween. The GPU takes care of it by CSS transitions.
Here is 100 moving divs ...
let d = document, html = "", css3 = "", x, y, z, t = 10, n = 100
for (let i = 0; i < n; i++) {
css3 += "#sprite" + i + " {display:inline-box; position:absolute; width:10px; height:10px; background-color:red; transition:top " + t + "s, left " + t + "s;}"
html += "<div id='sprite" + i + "'></div>"
}
var e = d.createElement('style')
e.innerHTML = css3
d.head.appendChild(e)
var e = d.createElement('div')
e.innerHTML = html
d.body.appendChild(e)
move();
setTimeout(move, 0)
setInterval(move, 1000*t)
function move() {
for (let i = 0; i < n; i++) {
x = 600*Math.random()
y = 200*Math.random()
z = d.getElementById("sprite" + i)
if (z) {
z.style.top = y + "px"
z.style.left = x + "px"
}
}
}
This is more Space Invader like but the code got cluttered ...
let d = document, html = "", css3 = "", t = 2, n = 300
let x, y, z, xx = 40, yy = 40, xxx = 30, yyy = 30
document.write(n + " space invaders, " + t + "s moves by " + xxx + "px")
for (let i = 0; i < n; i++) {
css3 += "body {background:black; color:pink;} #sprite" + i + " {display:inline-box; position:absolute; width:30px; height:30px; background-color:blue; transition:top 1s, left " + t + "s;}"
html += "<img id='sprite" + i + "' src='https://i.gifer.com/PCTg.gif'></img>"
}
var e = d.createElement('style')
e.innerHTML = css3
d.head.appendChild(e)
var e = d.createElement('div')
e.innerHTML = html
setTimeout(function(){d.body.appendChild(e)}, 100)
setInterval(move, 1000*t)
function move() {
if (xxx > 0? xx > 100: xx < 40) {
xx += xxx
xxx = -xxx
yy += 10
}
xx += xxx
for (let i = 0; i < n; i++) {
x = 30*(i % 30) + xx
y = 30*parseInt(i / 30) + yy
z = d.getElementById("sprite" + i)
if (z) {
z.style.top = y + "px"
z.style.left = x + "px"
}
}
}

Categories

Resources