m0_49280365 2022-04-29 16:30
浏览 100
已结题

Konvajs:如何解决特殊的“选择和转换:调整文本大小”问题?

1. 背景:

我使用konvajs创建一个“table”组件:

结构:

       Stage
         |     
       Layer       
         |            
   +-----+--------------+    
   |                    |
Group(tableGroup) Group(tableGroup)
                        |
         +--------------+--------+
         |                       |
      Group(cellGroup)     Group(cellGroup)
         |
    +----+----+
    |         |
Shape(Rect)  Shape(Text)

图像:

img

2. 目标:

我希望每个文本形状都具有宽度自适应性。我在konvajs的官方文件中找到了它:

如何使用变换工具更改文本的宽度? https://konvajs.org/docs/select_and_transform/Resize_Text.html

方案 1: 将所有“cellGroup”添加到“transformer”

// My code
tableGroup.on('click', function (e) { // Click the selected table
   tr.nodes([])
   tableGroup.getChildren().map(function(item) {
       tr.nodes(tr.nodes().concat(item)); // Add all cellGroups to transformer
       item.off('transform')
       item.on('transform', (e) => {
           item.setAttrs({
               width: item.width() * item.scaleX(),
               height: item.height() * item.scaleY(),
               scaleX: 1,
               scaleY: 1,
           });
           item.getChildren().map(function(child){
               child.setAttrs({
                    width: item.width() * item.scaleX(),
                    height: item.height() * item.scaleY(),
                    scaleX: 1,
                    scaleY: 1,
               });
           })
       })
    })
});

方案 2: 将“tableGroup”添加到“transformer”

// My code
tableGroup.on('click', function (e) { // Click the selected table
   tr.nodes([tableGroup]) // Add all tableGroup to transformer
   tableGroup.on('transform', (e) => {
       tableGroup.getChildren().map(function(item) {
           item.setAttrs({
               width: item.width() * item.scaleX(),
               height: item.height() * item.scaleY(),
               scaleX: 1,
               scaleY: 1,
           });
           item.getChildren().map(function(child){
               child.setAttrs({
                    width: item.width() * item.scaleX(),
                    height: item.height() * item.scaleY(),
                    scaleX: 1,
                    scaleY: 1,
               });
           })
        })
     })
});

结论:方案1是可行的,但方案2是不可行的。我的要求是将“tableGroup”添加到转换器“transformer”中,并实现文本宽度的自适应。找到解决方案,非常感谢。

3. 其他:

问:为什么必须在“transformer”中添加“tableGroup”?

答:因为当用“transformer”移动“Group”或“Shape”时,“Group”或“Shape”的坐标(x,y)会发生变化。我不想更改“cellGroup”的坐标,我想更改“tableGroup”(x,y)的坐标。或者你有一个更好的解决方案。找到解决方案,非常感谢。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 5月7日
    • 修改了问题 4月29日
    • 修改了问题 4月29日
    • 修改了问题 4月29日
    • 展开全部

    悬赏问题

    • ¥15 汇编语言没有主程序吗?
    • ¥15 这个函数为什么会爆内存
    • ¥15 无法装系统,grub成了顽固拦路虎
    • ¥15 springboot aop 应用启动异常
    • ¥15 matlab有关债券凸性久期的代码
    • ¥15 lvgl v8.2定时器提前到来
    • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
    • ¥15 cam_lidar_calibration报错
    • ¥15 拓扑学,凸集,紧集。。
    • ¥15 如何扩大AIS数据容量