无法理解为什么Konva文字不会隐藏

时间:2018-11-05 17:13:06

标签: html konvajs

我要在拖动矩形时隐藏并显示Konva.text->显示文本并拖动结束以隐藏文本,但是我不明白为什么在拖动Konva.text时不隐藏。

var stage = new Konva.Stage({ container: 'container', width: 400, height: 250 });
var layer2 = new Konva.Layer(); var groupe = new Konva.Group({})

var lineV = new Konva.Rect({ x: 50, y: 50, width: 70, height: 50, draggable: true, stroke: 'black'}); groupe.add(lineV)

var info = new Konva.Text({
    text: 'info', 
    visible: false,
    stroke: '#fff', fontSize: 35, fontFamily: 'Calibri', fill: '#000',  align: 'center', name: "infoBulle"});
    layer2.add(info);

    groupe.on('dragstart', function () {
        console.log("drag")
        info.visible(true);
    });
      
    groupe.on('dragend', function () {
        console.log("drop");
        info.visible(false)
        console.log(info.isVisible());
    });

    stage.add(layer2.add(groupe))
    stage.draw();
<script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>
<div id="container" width="400" height="250" style="border: 2px solid red;"></div> 

1 个答案:

答案 0 :(得分:1)

每次更新某个图层时,都需要重绘该图层。第一次显示文本更新是可见的,因为Konva会根据dragmove动作自动更新图层。

var stage = new Konva.Stage({ container: 'container', width: 400, height: 250 });
var layer2 = new Konva.Layer(); var groupe = new Konva.Group({})

var lineV = new Konva.Rect({ x: 50, y: 50, width: 70, height: 50, draggable: true, stroke: 'black'}); groupe.add(lineV)

var info = new Konva.Text({
    text: 'info', 
    visible: false,
    stroke: '#fff', fontSize: 35, fontFamily: 'Calibri', fill: '#000',  align: 'center', name: "infoBulle"});
    layer2.add(info);

    groupe.on('dragstart', function () {
        console.log("drag")
        info.visible(true);
        layer2.batchDraw();
    });
      
    groupe.on('dragend', function () {
        console.log("drop");
        info.visible(false)
        layer2.batchDraw();
        console.log(info.isVisible());
    });

    stage.add(layer2.add(groupe))
    stage.draw();
<script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>
<div id="container" width="400" height="250" style="border: 2px solid red;"></div>