KineticJS setText方法像Timer一样更新

时间:2013-03-04 09:38:56

标签: html5 text canvas timer kineticjs

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

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()上添加draggable属性并单击它时,我至少可以看到更新的值:)

1 个答案:

答案 0 :(得分: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
    }