ここにリンクがあります:http://jsfiddle.net/6VRxE/11/
固定サイズのテキストボックスがあります。テキスト、テキストサイズ、およびパディングを動的に変更して、テキストが長方形の内側に収まり、垂直方向に配置されるようにします。
var messageText = new Kinetic.Text({
x: .25*stage.getWidth(),
y: .25*stage.getHeight(),
width: .5*stage.getWidth(),
height: .5*stage.getHeight(),
fontSize: 10,
fontFamily: 'Lucida Grande',
text: 'Hello, click to continue',
align: 'center',
textFill: '#006400',
stroke: 'gray',
draggable: true,
fill: 'white',
});
次のようなイベントまたは関数でテキストを変更したいのですが、次のようになります。
messageText.on('click',function(){
messageText.setText('hi');
messageText.setFontSize(messageText.getHeight()/2);
messageText.setPadding(messageText.getHeight()/5); //this was one possibility to do some alignment but doesn't work great
stage.draw();
});
長さに応じてテキストサイズを計算するための良い解決策はありますか?