[Bug] Group `getBoundingClientRect` does not take into account all group children when `shallow=true`
Hi there! Firstly, thanks for your work on Two.js. Been building a whiteboard app using it and quite like the abstractions offered by the library.
Describe the bug
My understanding is that getBoundingClientRect(true) called on a Group should return bounding rect of that group, relative to its immediate parent. The behaviour I'm seeing seems to be that it only takes into account dimensions of the last child of that group.
For example,
import Two from 'https://cdn.skypack.dev/two.js@latest';
const two = new Two({
type: Two.Types.canvas,
fullscreen: true,
autostart: true
}).appendTo(document.body);
const rect1 = two.makeRectangle(100, 100, 50, 50);
const rect2 = two.makeRectangle(100, 100, 110, 100);
const group = two.makeGroup()
//group.add(rect1, rect2)
group.add(rect2, rect1)
two.add(group);
console.log(group.getBoundingClientRect(true))
I'd expect the bounding rect above to have width and height of 111 and 101, respectively but the output is
{
"top": 74.5,
"left": 74.5,
"right": 125.5,
"bottom": 125.5,
"width": 51,
"height": 51
}
which looks like rect1 dimensions. If I change the order in which rectangles are added to the group (uncomment first add and comment the second), output changes to
{
"top": 49.5,
"left": 44.5,
"right": 155.5,
"bottom": 150.5,
"width": 111,
"height": 101
}
which looks like rect2 dimensions. My understanding is that it should take the dimensions of all its children into account (please, correct me if I'm wrong).
To Reproduce CodePen with the snippet above: https://codepen.io/Sergei-Gorshechnikov/pen/QwLoMxL
Expected behavior
getBoundingClientRect(true) should take into account all child shapes of a given group
Environment (please select one):
- [x] Code executes in browser (e.g: using script tag to load library)
- [ ] Packaged software (e.g: ES6 imports, react, angular, vue.js)
- [ ] Running headless (usually Node.js)
Desktop (please complete the following information):
- OS: Mac OS Sonoma 14.4
- Browser Chrome
- Version 132.0.6834.110
Thanks for posting and glad you like it!
This is the same bug that was discovered in this issue: https://github.com/jonobr1/two.js/issues/747
I'll be publishing the new version of Two.js this week which has fixes to this. Thanks for your patience.