I’m trying to make some clickable sprite, but I’m having trouble understanding which is the clickable area. Is it a rectangle? If so, how it is centered?
Here I post an image with the sprites I created
and the functions I used to create the sprites
function createCanvasTexture(color, shape) {
const size = 64; // Texture size
const canvas = document.createElement(‘canvas’);
canvas.width = size;
canvas.height = size;
const context = canvas.getContext(‘2d’);// Center of the canvas
const centerX = size / 2;
const centerY = size / 2;
const margin = 10; // Margin to ensure shape is smaller than the canvas
const shapeSize = size - margin * 2; // Adjusted size for the shapecontext.fillStyle = color;
switch (shape) {
case ‘circle’:
context.beginPath();
context.arc(centerX, centerY, size / 2 - 1, 0, 2 * Math.PI); // Circle within the canvas
context.fill();
break;
case ‘square’:
const squareSize = size / 2;
context.fillRect(centerX - squareSize / 2, centerY - squareSize / 2, squareSize, squareSize);
break;
case ‘triangle’:
const sideLength = size / 2;
context.beginPath();
context.moveTo(centerX, centerY - sideLength / 2);
context.lineTo(centerX + sideLength / 2, centerY + sideLength / 2);
context.lineTo(centerX - sideLength / 2, centerY + sideLength / 2);
context.closePath();
context.fill();
break;
case ‘rhombus’:
const rhombusSize = size / 2;
context.beginPath();
context.moveTo(centerX, centerY - rhombusSize / 2);
context.lineTo(centerX + rhombusSize / 2, centerY);
context.lineTo(centerX, centerY + rhombusSize / 2);
context.lineTo(centerX - rhombusSize / 2, centerY);
context.closePath();
context.fill();
break;}
return new THREE.CanvasTexture(canvas);
}
export function createSprite(color, shape) {
const texture = createCanvasTexture2(color, shape);
const material = new THREE.SpriteMaterial({ map: texture, transparent: true });
const sprite = new THREE.Sprite(material);
return sprite;
}
Can someone draw the rectangles representing te clickable areas?