To create a rectangle with rounded corners using a path, you will need to follow these steps:
const createRoundedRectangle = (x1, y1, x2, y2, thickness = 1, radius = 0) => {
const vector1 = new THREE.Vector2(x1, y1);
const vector2 = new THREE.Vector2(x2, y2);
const direction = vector2.clone().sub(vector1).normalize();
const angle = direction.angle();
const perpendicularDirection = direction.clone().rotateAround(new THREE.Vector2(0, 0), Math.PI / 2);
const path = new THREE.Path();
path.moveTo(...vector2.clone().addScaledVector(perpendicularDirection, thickness - radius).toArray());
if(radius > 0) {
path.arc(...direction.clone().multiplyScalar(-radius).toArray(), radius, angle, angle + Math.PI / 2);
}
path.lineTo(
...vector1.clone()
.addScaledVector(perpendicularDirection, thickness)
.addScaledVector(direction, radius)
.toArray()
);
if(radius > 0) {
path.arc(...perpendicularDirection.clone().multiplyScalar(-radius).toArray(), radius, angle + Math.PI / 2, angle + Math.PI);
}
path.lineTo(
...vector1.clone()
.addScaledVector(perpendicularDirection, -(thickness - radius))
.toArray()
);
if(radius > 0) {
path.arc(...direction.clone().multiplyScalar(radius).toArray(), radius, angle + Math.PI, angle + 1.5 * Math.PI);
}
path.lineTo(
...vector2.clone()
.addScaledVector(perpendicularDirection, -(thickness))
.addScaledVector(direction, -radius)
.toArray()
);
if(radius > 0) {
path.arc(...perpendicularDirection.clone().multiplyScalar(radius).toArray(), radius, angle + 1.5 * Math.PI, angle + 2 * Math.PI);
}
path.lineTo(...vector2.clone().addScaledVector(perpendicularDirection, thickness - radius).toArray());
return path;
};
Then, use the function to add a hole in the shape:
var hole1 = createRoundedRectangle(10, 200, 400, 300, 40, 20);
boardShape.holes.push(hole1);