konvajs - Konvajs:如果它等于 fontSize,则文本高度不会出现在 stage.toJSON() 中
问题描述
创建文本节点后:
const text = new Konva.Text({
x: stage.width() / 2,
y: stage.height() / 2,
width: 200,
fontSize: 30,
height: 30,
fill: 'green',
text: 'Simple text'
});
其中高度值等于fontSize值,则函数stage.toJSON()
不返回高度参数:
[object Object] {
attrs: [object Object] { ... },
children: [[object Object] {
attrs: [object Object] { ... },
children: [[object Object] {
attrs: [object Object] {
fill: "green",
fontSize: 30,
text: "Simple text",
width: 200
},
className: "Text"
}],
className: "Layer"
}],
className: "Stage"
}
这是一个 jsbin:http ://jsbin.com/hikobiceji/1/edit?html,js,console
但如果height和fontSize不相等:
const text = new Konva.Text({
x: stage.width() / 2,
y: stage.height() / 2,
width: 200,
fontSize: 30,
height: 30.01,
fill: 'green',
text: 'Simple text'
});
然后存在高度参数:
[object Object] {
attrs: [object Object] { ... },
children: [[object Object] {
attrs: [object Object] { ... },
children: [[object Object] {
attrs: [object Object] {
fill: "green",
fontSize: 30,
height: 30.01,
text: "Simple text",
width: 200
},
className: "Text"
}],
className: "Layer"
}],
className: "Stage"
}
解决方案
这就是Konva
序列化的内部逻辑。Konva
正在尝试使 JSON 尽可能小。所以它不包括属性的默认值。
如果文本放在一行中,则文本的高度将等于其字体大小。在您的情况下,默认 getter 返回30
。由于它等于您使用的值,因此 Konva 将跳过它。
推荐阅读
- javascript - 我无法从 Firestore 中的文档中检索数据,但没有收到任何错误
- shopify - 即使页面刷新,我也只想渲染 shopify 片段
- excel - Excel VBA - 如何在每 12 个月后增加值
- typescript - Typescript 通过同级属性推断类型
- reactjs - 使用 axios 发送退款表单时出错
- python - 如何在 macOS 上使用 python 打开应用程序?
- typescript - 理解 typescript 类型断言
- mysql - 你好。我尝试使用 django + mySQL (api) 执行命令 docker-compose up (build I have done) 项目。但我有例外:
- byte - 区分异步数据流中最重要和最不重要的字节
- amazon-web-services - Terraform AWS,未创建实例