I have multiple pixi graphics rectangles that I want to rotate around a single point. I solved this by creating a pixi container and adding all objects to is. When rotating the container, all objects rotate accordingly. After the rotation is done, I want to remove the object from the container and put them back on the canvas.
I've done this with a normal pixi stage and container and it works just fine. However, my project requires I make use of the pixi viewport librarie for working with mouse zoom etc. when I try to remove the objects from the container and put them back on the viewport, the coordinates doesn't seem to match the correct location. I've tried many translations like toGlobal, toLocal, toWorld and toScreen for both the pixi graphics objects, containers and the viewport. None seem to give the correct results.
function startRotation(angle) {
// add all objects (in my case panels) to the container
for (let selectedPanel of selectedPanels) {
// create a container
if (container === undefined) {
container = new PIXI.Container();
viewport.addChild(container);
}
viewport.removeChild(selectedPanel.g);
container.addChild(selectedPanel.g);
}
// rotate the container
let por = pointOfRotation; // calculated somewhere else
container.position.x = por.x;
container.position.y = por.y;
container.pivot.x = por.x;
container.pivot.y = por.y;
container.rotation = Math.Radians(angle - startingRotation);
}
function finishRotation() {
if (container !== undefined) {
for (let selectedPanel of selectedPanels) {
// attempt to find the global position of the panels
// translate the panel position from container to global
let viewportX = selectedPanel.g.toGlobal(container).x;
let viewportY = selectedPanel.g.toGlobal(container).y;
let globalX = viewport.toWorld(viewportX, viewportY).x;
let globalY = viewport.toWorld(viewportX, viewportY).y;
container.removeChild(selectedPanel.g);
viewport.addChild(selectedPanel.g);
// set the new position of the panels
selectedPanel.g.x = globalX;
selectedPanel.g.y = globalY;
// set the new rotation of the panels
let panelRotation = selectedPanel.getRotation() + Math.Degrees(container.rotation);
selectedPanel.setRotation(panelRotation);
}
viewport.removeChild(container);
container = undefined;
}
}
After I remove the panels from the container and add them to the viewport, I want to set the correct position of the panels on the viewport, but the code used for this does not work.