'compare pixel postition via code on javacript
i have a problem. i'm using javascript and i'm always trying to make a faster code.
so i create an app that check the pixel position.
if your pixel pointer(red) is in the target position, the target make an action.. just press (t) to see it down
But when cross the red tile on loop to create a lot of places akk "if" dont work.
I wish he alert everytime the red takes the blue.
have any trigger to make check colision to the first object to another??
how to make a lot of if to be checked automated?
<body>
</body>
<script>
//ok... trying again
document.addEventListener('keydown', function(e) {
if (e.keyCode == 84) {
e.preventDefault();
check();
}
})
var position = 0
let a = document.createElement("div")
a.style.backgroundColor = "red"
a.style.height = '1px'
a.style.width = '1px'
a.style.top = position
a.style.position = "absolute"
document.body.append(a)
for (let index = 0; index < 10; index++) {
let x = index
//b = new b all the time
let b = document.createElement("div")
b.style.backgroundColor = "blue"
b.style.height = '1px'
b.style.width = '1px'
b.style.position = "absolute"
b.style.top = Math.floor(Math.random(10 - 0) * 100)
document.body.append(b)
//remember b = nem b on let all the time
function check(){
a.style.top = position++
if (a.style.top === b.style.top ){
alert("its ok")
}
}
}
</script>
help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
