A
A
Aleksey Levickyj2017-09-26 13:28:25
JavaScript
Aleksey Levickyj, 2017-09-26 13:28:25

How to arrange elements randomly in canvas, taking into account the borders of the canvas?

How to arrange elements randomly in canvas, taking into account the borders of the canvas (so that the elements do not go beyond), considering the center of the canvas, let's say the size is 100px X 100px, so that the elements do not fall into the center either.
PS Randomly rendered elements. Confusion with the definition of the center and boundaries.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Perov, 2017-09-26
@SnaIP

for x and y use the Math.random() function and pass a range there

P
Pavel Kornilov, 2017-09-26
@KorniloFF

if(x>0 && x<canvas.width - el.width && Math.abs(canvas.width/2 - x) > el.width/2) {
// рисуем элемент
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question