2013-09-03 11:35:25 +08:00
suite ( 'Text' , function ( ) {
// ======================================================
test ( 'add text with shadows' , function ( ) {
2013-09-09 12:36:54 +08:00
var stage = addStage ( ) ;
2012-11-24 06:54:32 +08:00
var layer = new Kinetic . Layer ( ) ;
2013-01-01 04:45:32 +08:00
var rect = new Kinetic . Rect ( {
2012-11-24 06:54:32 +08:00
x : stage . getWidth ( ) / 2 ,
y : stage . getHeight ( ) / 2 ,
stroke : '#555' ,
strokeWidth : 5 ,
fill : '#ddd' ,
2013-01-01 04:45:32 +08:00
width : 400 ,
height : 100 ,
shadowColor : 'black' ,
shadowBlur : 1 ,
shadowOffset : [ 10 , 10 ] ,
shadowOpacity : 0.2 ,
cornerRadius : 10
} ) ;
var text = new Kinetic . Text ( {
x : stage . getWidth ( ) / 2 ,
y : stage . getHeight ( ) / 2 ,
2012-11-24 06:54:32 +08:00
text : 'Hello World!' ,
fontSize : 50 ,
fontFamily : 'Calibri' ,
fontStyle : 'normal' ,
2013-01-01 04:45:32 +08:00
fill : '#888' ,
stroke : '#333' ,
2012-11-24 06:54:32 +08:00
align : 'right' ,
lineHeight : 1.2 ,
width : 400 ,
height : 100 ,
padding : 10 ,
2013-01-01 04:45:32 +08:00
shadowColor : 'red' ,
2012-12-31 16:45:38 +08:00
shadowBlur : 1 ,
shadowOffset : [ 10 , 10 ] ,
2013-01-01 04:45:32 +08:00
shadowOpacity : 0.2
} ) ;
var group = new Kinetic . Group ( {
2012-11-24 06:54:32 +08:00
draggable : true
} ) ;
// center text box
2014-01-11 14:09:22 +08:00
rect . offset ( text . getWidth ( ) / 2 , text . getHeight ( ) / 2 ) ;
text . offset ( text . getWidth ( ) / 2 , text . getHeight ( ) / 2 ) ;
2012-11-24 06:54:32 +08:00
2013-01-01 04:45:32 +08:00
group . add ( rect ) ;
group . add ( text ) ;
layer . add ( group ) ;
2012-11-24 06:54:32 +08:00
stage . add ( layer ) ;
2013-07-23 13:05:21 +08:00
2013-09-03 11:35:25 +08:00
assert . equal ( text . getClassName ( ) , 'Text' , 'getClassName should be Text' ) ;
} ) ;
// ======================================================
test ( 'text getters and setters' , function ( ) {
2013-09-09 12:36:54 +08:00
var stage = addStage ( ) ;
2012-11-14 13:37:28 +08:00
var layer = new Kinetic . Layer ( ) ;
var text = new Kinetic . Text ( {
x : stage . getWidth ( ) / 2 ,
y : stage . getHeight ( ) / 2 ,
text : 'Hello World!' ,
fontSize : 50 ,
fontFamily : 'Calibri' ,
fontStyle : 'normal' ,
2014-02-26 05:46:16 +08:00
fontVariant : 'normal' ,
2013-01-01 04:45:32 +08:00
fill : '#888' ,
stroke : '#333' ,
2012-11-14 13:37:28 +08:00
align : 'right' ,
lineHeight : 1.2 ,
width : 400 ,
height : 100 ,
padding : 10 ,
2012-12-31 16:45:38 +08:00
shadowColor : 'black' ,
shadowBlur : 1 ,
shadowOffset : [ 10 , 10 ] ,
shadowOpacity : 0.2 ,
2012-11-14 13:37:28 +08:00
draggable : true
} ) ;
// center text box
2014-01-11 14:09:22 +08:00
text . offset ( text . getWidth ( ) / 2 , text . getHeight ( ) / 2 ) ;
2012-11-14 13:37:28 +08:00
layer . add ( text ) ;
stage . add ( layer ) ;
/ *
* test getters and setters
* /
2013-09-03 11:35:25 +08:00
assert . equal ( text . getX ( ) , stage . getWidth ( ) / 2 ) ;
assert . equal ( text . getY ( ) , stage . getHeight ( ) / 2 ) ;
assert . equal ( text . getText ( ) , 'Hello World!' ) ;
assert . equal ( text . getFontSize ( ) , 50 ) ;
assert . equal ( text . getFontFamily ( ) , 'Calibri' ) ;
assert . equal ( text . getFontStyle ( ) , 'normal' ) ;
2014-02-26 05:46:16 +08:00
assert . equal ( text . getFontVariant ( ) , 'normal' ) ;
2013-09-03 11:35:25 +08:00
assert . equal ( text . getFill ( ) , '#888' ) ;
assert . equal ( text . getStroke ( ) , '#333' ) ;
assert . equal ( text . getAlign ( ) , 'right' ) ;
assert . equal ( text . getLineHeight ( ) , 1.2 ) ;
assert . equal ( text . getWidth ( ) , 400 ) ;
assert . equal ( text . getHeight ( ) , 100 ) ;
assert . equal ( text . getPadding ( ) , 10 ) ;
assert . equal ( text . getShadowColor ( ) , 'black' ) ;
assert . equal ( text . getDraggable ( ) , true ) ;
assert . equal ( text . getWidth ( ) , 400 ) ;
assert . equal ( text . getHeight ( ) , 100 ) ;
assert ( text . getTextWidth ( ) > 0 , 'text width should be greater than 0' ) ;
assert ( text . getTextHeight ( ) > 0 , 'text height should be greater than 0' ) ;
2012-11-14 13:37:28 +08:00
text . setX ( 1 ) ;
text . setY ( 2 ) ;
text . setText ( 'bye world!' ) ;
text . setFontSize ( 10 ) ;
text . setFontFamily ( 'Arial' ) ;
text . setFontStyle ( 'bold' ) ;
2014-02-26 05:46:16 +08:00
text . setFontVariant ( 'small-caps' ) ;
2013-01-01 04:45:32 +08:00
text . setFill ( 'green' ) ;
text . setStroke ( 'yellow' ) ;
2012-11-14 13:37:28 +08:00
text . setAlign ( 'left' ) ;
text . setWidth ( 300 ) ;
text . setHeight ( 75 ) ;
text . setPadding ( 20 ) ;
2012-12-31 16:45:38 +08:00
text . setShadowColor ( 'green' ) ;
2012-11-14 13:37:28 +08:00
text . setDraggable ( false ) ;
2013-09-03 11:35:25 +08:00
assert . equal ( text . getX ( ) , 1 ) ;
assert . equal ( text . getY ( ) , 2 ) ;
assert . equal ( text . getText ( ) , 'bye world!' ) ;
assert . equal ( text . getFontSize ( ) , 10 ) ;
assert . equal ( text . getFontFamily ( ) , 'Arial' ) ;
assert . equal ( text . getFontStyle ( ) , 'bold' ) ;
2014-02-26 05:46:16 +08:00
assert . equal ( text . getFontVariant ( ) , 'small-caps' ) ;
2013-09-03 11:35:25 +08:00
assert . equal ( text . getFill ( ) , 'green' ) ;
assert . equal ( text . getStroke ( ) , 'yellow' ) ;
assert . equal ( text . getAlign ( ) , 'left' ) ;
assert . equal ( text . getWidth ( ) , 300 ) ;
assert . equal ( text . getHeight ( ) , 75 ) ;
assert . equal ( text . getPadding ( ) , 20 ) ;
assert . equal ( text . getShadowColor ( ) , 'green' ) ;
assert . equal ( text . getDraggable ( ) , false ) ;
2012-11-14 13:37:28 +08:00
// test set text to integer
text . setText ( 5 ) ;
//document.body.appendChild(layer.bufferCanvas.element)
//layer.setListening(false);
2012-11-19 11:50:50 +08:00
layer . drawHit ( ) ;
2012-11-14 13:37:28 +08:00
2013-09-03 11:35:25 +08:00
} ) ;
// ======================================================
test ( 'text multi line' , function ( ) {
2013-09-09 12:36:54 +08:00
var stage = addStage ( ) ;
2012-11-14 13:37:28 +08:00
var layer = new Kinetic . Layer ( ) ;
2013-07-23 13:05:21 +08:00
2013-03-21 00:02:18 +08:00
var rect = new Kinetic . Rect ( {
x : 10 ,
y : 10 ,
width : 380 ,
height : 300 ,
fill : 'red'
} ) ;
2012-11-14 13:37:28 +08:00
var text = new Kinetic . Text ( {
x : 10 ,
y : 10 ,
2013-04-05 13:22:28 +08:00
text : 'HEADING\n\nAll the world\'s a stage, merely players. They have their exits and their entrances; And one man in his time plays many parts.' ,
2012-11-14 13:37:28 +08:00
//text: 'HEADING\n\nThis is a really cool paragraph. \n And this is a footer.',
2013-03-21 00:02:18 +08:00
fontSize : 24 ,
2012-11-14 13:37:28 +08:00
fontFamily : 'Calibri' ,
fontStyle : 'normal' ,
2013-01-01 04:45:32 +08:00
fill : '#555' ,
2012-11-14 13:37:28 +08:00
//width: 20,
width : 380 ,
//width: 200,
2013-04-05 13:22:28 +08:00
padding : 10 ,
2012-11-14 13:37:28 +08:00
align : 'center' ,
2013-03-21 00:17:21 +08:00
draggable : true ,
2013-03-21 00:26:55 +08:00
wrap : 'WORD'
2012-11-14 13:37:28 +08:00
} ) ;
// center text box
//text.setOffset(text.getBoxWidth() / 2, text.getBoxHeight() / 2);
2013-03-21 00:02:18 +08:00
layer . add ( rect ) . add ( text ) ;
2012-11-14 13:37:28 +08:00
stage . add ( layer ) ;
2013-01-27 11:35:53 +08:00
2013-09-03 11:35:25 +08:00
assert . equal ( text . getLineHeight ( ) , 1 ) ;
2012-11-14 13:37:28 +08:00
2013-03-21 00:02:18 +08:00
/ *
2012-11-14 13:37:28 +08:00
text . transitionTo ( {
2013-03-21 00:02:18 +08:00
width : 50 ,
duration : 20
} ) ;
2013-07-23 13:05:21 +08:00
2013-03-21 00:02:18 +08:00
rect . transitionTo ( {
width : 50 ,
duration : 20
2012-11-14 13:37:28 +08:00
} ) ;
* /
2013-07-23 13:05:21 +08:00
2013-09-03 11:35:25 +08:00
} ) ;
// ======================================================
test ( 'text multi line with shadows' , function ( ) {
2013-09-09 12:36:54 +08:00
var stage = addStage ( ) ;
2012-11-14 13:37:28 +08:00
var layer = new Kinetic . Layer ( ) ;
var text = new Kinetic . Text ( {
x : 10 ,
y : 10 ,
//stroke: '#555',
//strokeWidth: 5,
text : 'HEADING\n\nAll the world\'s a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts.' ,
//text: 'HEADING\n\nThis is a really cool paragraph. \n And this is a footer.',
fontSize : 16 ,
fontFamily : 'Calibri' ,
fontStyle : 'normal' ,
2013-01-01 04:45:32 +08:00
fill : '#555' ,
2012-11-14 13:37:28 +08:00
//width: 20,
width : 380 ,
//width: 200,
padding : 20 ,
align : 'center' ,
2012-12-31 16:45:38 +08:00
shadowColor : 'red' ,
shadowBlur : 1 ,
shadowOffset : [ 10 , 10 ] ,
shadowOpacity : 0.5 ,
2013-01-01 04:45:32 +08:00
draggable : true
2012-11-14 13:37:28 +08:00
} ) ;
layer . add ( text ) ;
stage . add ( layer ) ;
2013-01-27 11:35:53 +08:00
2013-09-03 12:16:26 +08:00
//console.log(layer.getContext().getTrace());
2013-09-03 11:35:25 +08:00
} ) ;
// ======================================================
test ( 'change font size should update text data' , function ( ) {
2013-09-09 12:36:54 +08:00
var stage = addStage ( ) ;
2012-11-14 13:37:28 +08:00
var layer = new Kinetic . Layer ( ) ;
var text = new Kinetic . Text ( {
x : 10 ,
y : 10 ,
text : 'Some awesome text' ,
fontSize : 16 ,
fontFamily : 'Calibri' ,
fontStyle : 'normal' ,
2013-01-01 04:45:32 +08:00
fill : '#555' ,
2012-11-14 13:37:28 +08:00
align : 'center' ,
padding : 5 ,
2013-01-01 04:45:32 +08:00
draggable : true
2012-11-14 13:37:28 +08:00
} ) ;
var width = text . getWidth ( ) ;
var height = text . getHeight ( ) ;
2013-07-23 13:05:21 +08:00
2012-11-14 13:37:28 +08:00
layer . add ( text ) ;
stage . add ( layer ) ;
text . setFontSize ( 30 ) ;
layer . draw ( ) ;
2013-07-23 13:05:21 +08:00
2013-03-15 23:33:05 +08:00
//console.log(text.getHeight() + ',' + height);
2012-11-14 13:37:28 +08:00
2013-09-03 11:35:25 +08:00
assert ( text . getWidth ( ) > width , 'width should have increased' ) ;
assert ( text . getHeight ( ) > height , 'height should have increased' ) ;
2012-11-14 13:37:28 +08:00
2013-09-03 11:35:25 +08:00
} ) ;
2014-02-27 08:19:29 +08:00
test ( 'get text width' , function ( ) {
var stage = addStage ( ) ;
var layer = new Kinetic . Layer ( ) ;
stage . add ( layer ) ;
var text = new Kinetic . Text ( {
text : 'hello asd fasdf asdf asd fasdf asdfasd fa sds helloo' ,
fill : 'black' ,
width : 100
} ) ;
layer . add ( text ) ;
layer . draw ( ) ;
assert . equal ( text . getTextWidth ( ) > 0 && text . getTextWidth ( ) < 100 , true ) ;
} ) ;
2013-09-03 11:35:25 +08:00
} ) ;