1

我需要像计时器一样更新 KineticJS 文本的文本属性。但我下面的代码没有按预期工作,我做错了什么:

var dateTimeText = new Kinetic.Text({
            x: 40,
            y: 400,
            text: "Sample",
            fontSize: 18,
            width: 700,
            fontFamily: 'Calibri',
            fill: 'black',
            width: 700,
            padding: 10,
            align: 'right',
            draggable: true
        });
setInterval(function () { onUpdateTime() }, 1000);
        function onUpdateTime() {
            var date = new Date();
            dateTimeText.setText(date.toLocaleTimeString());
        }

在 onUpdateTime() 中,我应该对 dateTimeText 进行操作吗?在 dateTimeText() 上添加可拖动属性并单击它时,我至少可以看到更新的值:)

4

1 回答 1

6

试试这个:

    function onUpdateTime() {
        var date = new Date();
        dateTimeText.setText(date.toLocaleTimeString());
        // layer.draw(); // either this, or whatever the layer is called
        // dateTimeText.getParent().draw(); // or this, auto get parent layer
        dateTimeText.getLayer().draw(); // better solution suggested by Eric Rowell
    }
于 2013-03-04T14:17:32.357 回答