I apologize for the code, it's in a state I'm actively playing with.
In Konva JS, I'm creating labels. Which is basically a group, it has a shape and text element. The shapes are drawn dynamically from a database, and each db row has the coordinates, the shape (square, circle etc), size and rotation. These are multiplied by a scale depending on screen size. I had a working solution, which I've been using for weeks, but I also have a function that changes the shape of a shape. So I press a button, it updates the database (e.g changes square to circle) and then redraws the canvas. Because going from a square to a circle changes from a shape that's drawn from the top left to one that's drawn from center, it messes up the coordinates. I decided to switch to drawing rectangles with an offset of width/2 so that rectangles are drawn from the center too.
I have subsequently kept breaking it further and I now can't get text centered at all. I need text centered regardless of shape, size or rotation.
current code
//Gets table array from SQL
tables = callPhpFunction('getTablesInRoom', room);
tables = JSON.parse(tables);
numberOfTables = tables.length;
index = 0;
tableLayer = new Konva.Layer();
tableLayer.add(transformer);
//For every table
while(index < numberOfTables){
tableNumber = tables[index]['tablenumber'];
offset_x = 0;
offset_y = 0;
pos_x = parseInt(tables[index]['posx']) * scale;
pos_y = parseInt(tables[index]['posy']) * scale;
shape = tables[index]['shape'];
tableWidth = parseInt(tables[index]['width']) * scale;
tableHeight = parseInt(tables[index]['height']) * scale;
rotation = parseInt(tables[index]['rotation']);
fillColor = "gray";
strokeColor = "black";
var table = new Konva.Label({
x: pos_x,
y: pos_y,
draggable:canDrag
});
pos_x = 0;
pos_y = 0 ;
//If the shape is a square, rectangle, or undefined
if(shape != "circle" && shape != "ellipse" && shape != "longellipse"){
offset_x = tableWidth/2
offset_y = tableHeight/2
table.add(new Konva.Rect({
width: tableWidth,
height: tableHeight,
offsetX: offset_y,
offsetY: offset_y,
height: tableHeight,
fill: fillColor,
stroke: strokeColor,
strokeWidth: 4
}));
//If shape is a circle or ellipse
} else {
table.add(new Konva.Ellipse({
radiusX: tableWidth/2,
radiusY: tableHeight/2,
fill: fillColor,
stroke: strokeColor,
strokeWidth: 4
}));
//Can't remember why this is here, but it seems to work/ Think if these are reused when the text is drawn it centers them.
pos_x = -tableWidth/2;
pos_y = -tableHeight/2;
}
//Gets the first shape (ellipse or rectangle) in the label. I think is equivalent to getTag()
innerShape = getInnerShape(table);
//Rotates the inner shape, while leaving text unRotated.
if(rotation != 0){
canvasX = pos_x + (tableWidth/2)
canvasY = pos_y + (tableHeight/2)
rotateAroundPoint(innerShape, rotation, {x:canvasX, y:canvasY });
}
//Currently approach is to get center based on the client rectangle. It's not working
box = table.getClientRect();
box_x = box.x + box.width / 2;
box_y = box.y + box.height / 2;
table.add(new Konva.Text({
width: tableWidth,
height: tableHeight,
x: box_x,
y: box_y,
text: tableNumber,
verticalAlign: 'middle',
align: 'center',
fontSize: 24,
fontFamily: 'Calibri',
fill: 'black',
listening: false
}))