Search code examples
textscalekineticjs

how to scale my textlayer


i wish to scale a text layer by button click.

var messageLayer = new Kinetic.Layer();

function writeMessage(message, xx, yy) {
var context = messageLayer.getContext();
context.font = '12pt Calibri';
context.fillStyle = 'white';
wrapText(context, message, xx, yy, seat_size, seat_size);
}

stage.add(messageLayer);
writeMessage(text, xx, yy);

document.getElementById('zoomInBtn').onclick = function zoomInStage(){  
stage.setScale(2);
stage.draw();
}

I'm not sure why but this seems not working..

Thanks for helping.


Hi thanks for pointing out my mistake. but after i add the y value and this is still not working ....

below are my code:

enter <!DOCTYPE HTML>
<html>
<body>
<div id="container"></div>
 <button id="scaleme" style="margin-left:10px;">Scale</button>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.0-beta2.js"></script>
<script>
  function writeMessage(messageLayer, message) {
    var context = messageLayer.getContext();
    messageLayer.clear();
    context.font = '18pt Calibri';
    context.fillStyle = 'black';
    context.fillText(message, 10, 25);
  }
  var stage = new Kinetic.Stage({
    container: 'container',
    width: 578,
    height: 200
  });

  var messageLayer = new Kinetic.Layer();

  stage.add(messageLayer);
  writeMessage(messageLayer, 'text text text');

  document.getElementById('scaleme').onclick = function scale(){ 
     messageLayer.setScale(2,2);
  }

</script>
</body>
</html>

please helppp....thanks!


Solution

  • From the API docs:
    this is the format:

     setScale(x, y) 
    

    So you are missing the y parameter it seems.

    If you do

      stage.setScale(2,2); 
    

    then the whole stage will be scaled, but if you want just the text layer to be scaled:

      messageLayer.setScale(2,2);
    

    lastly, if you want to be fancy, you can do a transition to zoom to the text:

    messageLayer.on('click', function(){
        messageLayer.transitionTo({
                    scale: {
                        x: 2,
                        y: 2
                    },
                duration: 1,
                    easing: 'elastic-ease-out' // this one is not really necessary, just for show
            });
    });
    

    EDIT: Since you are using canvas primitives, do this: http://jsfiddle.net/mmqXK/

     var context = messageLayer.getContext();
     context.scale(1, 0.5);
     messageLayer.draw();
     writeMessage(messageLayer, 'text text text');