2023-02-17 11:06:01 +01:00

41 lines
1.3 KiB
JavaScript

import { getRandom } from "tsparticles-engine";
function randomSquareCoordinate(position, offset) {
return position + offset * (getRandom() - 0.5);
}
export class SquareShape {
randomPosition(position, size, fill) {
if (fill) {
return {
x: randomSquareCoordinate(position.x, size.width),
y: randomSquareCoordinate(position.y, size.height),
};
}
else {
const halfW = size.width / 2, halfH = size.height / 2, side = Math.floor(getRandom() * 4), v = (getRandom() - 0.5) * 2;
switch (side) {
case 0:
return {
x: position.x + v * halfW,
y: position.y - halfH,
};
case 1:
return {
x: position.x - halfW,
y: position.y + v * halfH,
};
case 2:
return {
x: position.x + v * halfW,
y: position.y + halfH,
};
case 3:
default:
return {
x: position.x + halfW,
y: position.y + v * halfH,
};
}
}
}
}