0
I’m creating a Snake Game in Javascript and to perform animations, collision detection and others, I use the function getBoundingClientRect()
, to get the position left
and top
of a game element.
I just completed almost the whole game and everything works perfectly, however when adding a border in my CSS file for div <div id="game"></div>
which is where my game runs, my little hedge started to move diagonally.
#game{
background: rgb(80, 80, 80);
border: 2px solid #000;
margin-top: 30px;
width: 450px;
height: 450px;
}
Below is the code of my method that gets the position of the game elements and the method that moves the cover and sets a position for the elements:
getPositionOf: function(element){
var game_rect = this.game.getBoundingClientRect();
var element_rect = element.getBoundingClientRect();
return {left: element_rect.left - game_rect.left, top: element_rect.top - game_rect.top};
},
move: function(){
var head = this.body[0]; // Obtém a cabeça da cobra
var {left: head_left, top: head_top} = this.getPositionOf(head);
// this.direction.speed == [0, -10]
head_left += this.direction.speed[0];
head_top += this.direction.speed[1];
this.setPositionOf(head, head_left, head_top);
// ...
},
setPositionOf: function(element, left, top){
element.style.left = left + "px";
element.style.top = top + "px";
}
To make sure that the problem was in this method, or rather in the function getBoundingClientRect
, i have placed within the method the following code to verify which position of the cover the function returns ( keep in mind that the snake must move upward at a speed of 10px ):
if (element.className == "snake"){
console.log(element_rect.left, element_rect.top);
}
The result of this in console when executing the application without the edge:
448.5 258 snake.js:42
448.5 248 snake.js:42
448.5 248 snake.js:42
448.5 238 snake.js:42
The result when executing the application with the border:
448.5 260 snake.js:42
450.5 252 snake.js:42
450.5 252 snake.js:42
452.5 244 snake.js:42
My question is, why the return of getBoundingClientRect()
changes when adding a border for the div game
and what I can do to solve the problem ?
For those who voted negative, it is possible to give a suggestion of what I can improve the question ?
– JeanExtreme002
First of all, I wasn’t the one who said no. On the question, the border is part of the 'css box model', so it changes the size of the elements and this may be the reason for your bug. But just looking at your code, it’s hard to confirm that. You will need to debug this, place breakpoints and compare the expected value of the variables with the value they are effectively assuming until you find the source of the error.
– user142154
In the element that has the border it puts in its CSS the property box-Sizing: border-box that will make the width of the border not add the total width of the element, or change the border for a box-shadow of 1px, because the shadow does not interfere in the box-model
– hugocsl
@hugocsl I tried to do what you said and it still didn’t work. Put in the CSS file the
box-sizing: border-box
made no difference and thebox-shadow
did not create a border on the 4 sides, it just created edge on the bottom and on the right.– JeanExtreme002
Give a studied in the property of Box-Shadow that you will learn to create "edge" of the 4 sides...
– hugocsl