找出以像素为单位的UIElement大小的正确方法



根据文档,UIElement有一个getSize方法,该方法应返回其以像素为单位的大小

文档链接

但是当我调用这个方法时,我没有得到我期望的

当我记录el.getSize()时,我会得到这样的东西:{x: 5, y: 0},但当我记录整个对象时,我看到的大小字段的值是正确的:

图像

我做错了什么?

const {lightningChart, UIElementBuilders} = lcjs;
const dashboard = lightningChart().Dashboard({
numberOfColumns: 1,
numberOfRows: 2,
});
const topPanel = dashboard.createUIPanel({
columnIndex: 0,
rowIndex: 0,
});
const el1 = topPanel.addUIElement(UIElementBuilders.CheckBox)
el1.setText('some text')
console.log(el1.getSize())
console.log('checkbox: ', el1)
<script src="http://unpkg.com/@arction/lcjs@3.3.2/dist/lcjs.iife.js"></script>

可能是在调用getSize()时,尚未计算元素的大小。

你可以尝试在超时后获得大小

const el1 = topPanel.addUIElement(UIElementBuilders.CheckBox)
el1.setText('some text')
requestAnimationFrame(() => {
console.log(el1.getSize())
})

相关内容

最新更新