添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have the following piece of Konva code and I am trying to update the text attribute

var lifePoint = new Konva.Text({
    x: 50,
    y: 50,
    id: 'P1',
    text: 'Player Lifepoint: 100',
    fontSize: 30

After reading the documentation I tried:

// find and change text
const test = layer.find('#P1')[0];
test.setAttr('text', 'changed');

Could anybody possibly tell me what I am doind wrong?

The code looks good. You need to update the layer if you want to see changed. layer.draw(). – lavrton Oct 22, 2018 at 15:43 layer.draw(); // My favorite to forget ! // When the user gets more life points add them on. $('#addlife').on('click', function(e){ // find and change text var text = layer.find('#P1')[0]; pts = pts + 10; text.setAttr('text', 'Player Lifepoint: ' + pts); layer.draw(); // remember to redraw the layer to see the change !
#container
width: 400px;
height: 100px;
border: 1px solid lime;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.4.2/konva.min.js"></script>
<p><button id='addlife'>Add life</button></p>
<div id='container'></div>

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.