2021-04-30 22:24:27 +08:00
import { assert } from 'chai' ;
2021-05-05 22:19:24 +08:00
import { addStage , Konva , cloneAndCompareLayer , isBrowser } from './test-utils' ;
2021-04-30 22:24:27 +08:00
describe ( 'Label' , function ( ) {
2017-02-24 22:15:33 +08:00
// ======================================================
2021-04-30 22:24:27 +08:00
it ( 'add label' , function ( ) {
2017-02-24 22:15:33 +08:00
var stage = addStage ( ) ;
var layer = new Konva . Layer ( ) ;
var label = new Konva . Label ( {
x : 100 ,
y : 100 ,
2020-05-08 22:59:35 +08:00
draggable : true ,
2013-09-08 11:55:03 +08:00
} ) ;
2017-02-24 22:15:33 +08:00
// add a tag to the label
label . add (
new Konva . Tag ( {
fill : '#bbb' ,
shadowColor : 'black' ,
shadowBlur : 10 ,
2019-02-20 22:13:39 +08:00
shadowOffset : { x : 10 , y : 10 } ,
2017-02-24 22:15:33 +08:00
shadowOpacity : 0.2 ,
lineJoin : 'round' ,
pointerDirection : 'up' ,
pointerWidth : 20 ,
pointerHeight : 20 ,
2020-05-08 22:59:35 +08:00
cornerRadius : 5 ,
2017-02-24 22:15:33 +08:00
} )
) ;
// add text to the label
label . add (
new Konva . Text ( {
text : '' ,
fontSize : 50 ,
//fontFamily: 'Calibri',
//fontStyle: 'normal',
lineHeight : 1.2 ,
//padding: 10,
2020-05-08 22:59:35 +08:00
fill : 'green' ,
2017-02-24 22:15:33 +08:00
} )
) ;
layer . add ( label ) ;
stage . add ( layer ) ;
2021-04-30 22:24:27 +08:00
label . getText ( ) . fontSize ( 100 ) ;
2017-02-24 22:15:33 +08:00
2021-04-30 22:24:27 +08:00
label . getText ( ) . fontSize ( 50 ) ;
2017-02-24 22:15:33 +08:00
2021-04-30 22:24:27 +08:00
label . getText ( ) . text ( 'Hello big world' ) ;
2017-02-24 22:15:33 +08:00
layer . draw ( ) ;
assert . equal ( label . getType ( ) , 'Group' ) ;
assert . equal ( label . getClassName ( ) , 'Label' ) ;
// use relaxed trace because text can be a slightly different size in different browsers,
// resulting in slightly different tag dimensions
2021-04-30 22:24:27 +08:00
var trace = layer . getContext ( ) . getTrace ( true ) ;
2020-09-18 02:25:46 +08:00
assert . equal (
trace ,
2021-04-30 22:24:27 +08:00
'clearRect();save();lineJoin;transform();shadowColor;shadowBlur;shadowOffsetX;shadowOffsetY;beginPath();moveTo();lineTo();lineTo();lineTo();lineTo();arc();lineTo();arc();lineTo();arc();lineTo();arc();closePath();fillStyle;fill();restore();save();transform();restore();clearRect();save();lineJoin;transform();shadowColor;shadowBlur;shadowOffsetX;shadowOffsetY;beginPath();moveTo();lineTo();lineTo();lineTo();lineTo();arc();lineTo();arc();lineTo();arc();lineTo();arc();closePath();fillStyle;fill();restore();save();transform();font;textBaseline;textAlign;translate();save();fillStyle;fillText();restore();restore();'
2020-09-18 02:25:46 +08:00
) ;
2017-02-24 22:15:33 +08:00
} ) ;
// ======================================================
2021-04-30 22:24:27 +08:00
it ( 'create label from json' , function ( ) {
2017-02-24 22:15:33 +08:00
var stage = addStage ( ) ;
2017-07-29 00:40:07 +08:00
var json =
2020-09-18 02:25:46 +08:00
'{"attrs":{"x":100,"y":100,"draggable":true},"className":"Label","children":[{"attrs":{"fill":"#bbb","shadowColor":"black","shadowBlur":10,"shadowOffsetX":10,"shadowOffsetY":10,"shadowOpacity":0.2,"lineJoin":"round","pointerDirection":"up","pointerWidth":20,"pointerHeight":20,"cornerRadius":5,"x":-151.5,"y":20,"width":303,"height":60},"className":"Tag"},{"attrs":{"text":"Hello big world","fontSize":50,"lineHeight":1.2,"fill":"green","width":"auto","height":"auto","x":-151.5,"y":20},"className":"Text"}]}' ;
2017-02-24 22:15:33 +08:00
var layer = new Konva . Layer ( ) ;
var label = Konva . Node . create ( json ) ;
layer . add ( label ) ;
stage . add ( layer ) ;
2020-09-18 02:25:46 +08:00
2021-05-04 06:09:18 +08:00
var trace = layer . getContext ( ) . getTrace ( false , true ) ;
2021-05-04 10:21:04 +08:00
if ( isBrowser ) {
assert . equal (
trace ,
'clearRect(0,0,578,200);save();lineJoin=round;transform(1,0,0,1,-64,120);shadowColor=rgba(0,0,0,0.2);shadowBlur=10;shadowOffsetX=10;shadowOffsetY=10;beginPath();moveTo(5,0);lineTo(153,0);lineTo(163,-20);lineTo(173,0);lineTo(322,0);arc(322,5,5,4,0,false);lineTo(327,55);arc(322,55,5,0,1,false);lineTo(5,60);arc(5,55,5,1,3,false);lineTo(0,5);arc(5,5,5,3,4,false);closePath();fillStyle=#bbb;fill();restore();save();transform(1,0,0,1,-64,120);font=normal normal 50px Arial;textBaseline=middle;textAlign=left;translate(0,0);save();fillStyle=green;fillText(Hello big world,0,30);restore();restore();'
) ;
} else {
assert . equal (
trace ,
2023-04-06 11:08:48 +08:00
'clearRect(0,0,578,200);save();lineJoin=round;transform(1,0,0,1,-64,120);shadowColor=rgba(0,0,0,0.2);shadowBlur=10;shadowOffsetX=10;shadowOffsetY=10;beginPath();moveTo(5,0);lineTo(153,0);lineTo(163,-20);lineTo(173,0);lineTo(322,0);arc(322,5,5,4,0,false);lineTo(327,55);arc(322,55,5,0,1,false);lineTo(5,60);arc(5,55,5,1,3,false);lineTo(0,5);arc(5,5,5,3,4,false);closePath();fillStyle=#bbb;fill();restore();save();transform(1,0,0,1,-64,120);font=normal normal 50px Arial;textBaseline=middle;textAlign=left;translate(0,0);save();fillStyle=green;fillText(Hello big world,0,30);restore();restore();'
2021-05-04 10:21:04 +08:00
) ;
}
2017-02-24 22:15:33 +08:00
} ) ;
2021-04-30 22:24:27 +08:00
it ( 'find label class' , function ( ) {
2017-02-24 22:15:33 +08:00
var stage = addStage ( ) ;
var layer = new Konva . Layer ( ) ;
var label = new Konva . Label ( {
x : 100 ,
2020-05-08 22:59:35 +08:00
y : 100 ,
2013-09-08 11:55:03 +08:00
} ) ;
2014-04-25 19:17:12 +08:00
2017-02-24 22:15:33 +08:00
// add a tag to the label
label . add (
new Konva . Tag ( {
2020-05-08 22:59:35 +08:00
fill : '#bbb' ,
2017-02-24 22:15:33 +08:00
} )
) ;
// add text to the label
label . add (
new Konva . Text ( {
text : 'Test Label' ,
2020-05-08 22:59:35 +08:00
fill : 'green' ,
2017-02-24 22:15:33 +08:00
} )
) ;
layer . add ( label ) ;
stage . add ( layer ) ;
assert . equal ( stage . find ( 'Label' ) [ 0 ] , label ) ;
} ) ;
2018-05-24 09:59:08 +08:00
// caching doesn't give exactly the same result. WHY?
2021-05-04 06:09:18 +08:00
it ( 'cache label' , function ( ) {
2017-02-24 22:15:33 +08:00
var stage = addStage ( ) ;
var layer = new Konva . Layer ( ) ;
// tooltip
var tooltip = new Konva . Label ( {
x : 170 ,
y : 75 ,
2020-05-08 22:59:35 +08:00
opacity : 0.75 ,
2014-04-25 19:17:12 +08:00
} ) ;
2017-02-24 22:15:33 +08:00
tooltip . add (
new Konva . Tag ( {
fill : 'black' ,
pointerDirection : 'down' ,
pointerWidth : 10 ,
pointerHeight : 10 ,
lineJoin : 'round' ,
shadowColor : 'black' ,
shadowBlur : 10 ,
2019-02-20 22:13:39 +08:00
shadowOffsetX : 10 ,
2020-05-08 22:59:35 +08:00
shadowOpacity : 0.5 ,
2017-02-24 22:15:33 +08:00
} )
) ;
tooltip . add (
new Konva . Text ( {
text : 'Tooltip pointing down' ,
fontFamily : 'Calibri' ,
fontSize : 18 ,
padding : 5 ,
2020-05-08 22:59:35 +08:00
fill : 'white' ,
2017-02-24 22:15:33 +08:00
} )
) ;
var tooltipUp = new Konva . Label ( {
x : 170 ,
y : 75 ,
2020-05-08 22:59:35 +08:00
opacity : 0.75 ,
2017-02-24 22:15:33 +08:00
} ) ;
tooltipUp . add (
new Konva . Tag ( {
fill : 'black' ,
pointerDirection : 'up' ,
pointerWidth : 10 ,
pointerHeight : 10 ,
lineJoin : 'round' ,
shadowColor : 'black' ,
shadowBlur : 10 ,
2019-02-20 22:13:39 +08:00
shadowOffsetX : 10 ,
2020-05-08 22:59:35 +08:00
shadowOpacity : 0.5 ,
2017-02-24 22:15:33 +08:00
} )
) ;
tooltipUp . add (
new Konva . Text ( {
text : 'Tooltip pointing up' ,
fontFamily : 'Calibri' ,
fontSize : 18 ,
padding : 5 ,
2020-05-08 22:59:35 +08:00
fill : 'white' ,
2017-02-24 22:15:33 +08:00
} )
) ;
// label with left pointer
var labelLeft = new Konva . Label ( {
x : 20 ,
y : 130 ,
2020-05-08 22:59:35 +08:00
opacity : 0.75 ,
2017-02-24 22:15:33 +08:00
} ) ;
labelLeft . add (
new Konva . Tag ( {
fill : 'green' ,
pointerDirection : 'left' ,
pointerWidth : 30 ,
pointerHeight : 28 ,
2020-05-08 22:59:35 +08:00
lineJoin : 'round' ,
2017-02-24 22:15:33 +08:00
} )
) ;
labelLeft . add (
new Konva . Text ( {
text : 'Label pointing left' ,
fontFamily : 'Calibri' ,
fontSize : 18 ,
padding : 5 ,
2020-05-08 22:59:35 +08:00
fill : 'white' ,
2017-02-24 22:15:33 +08:00
} )
) ;
// label with left pointer
var labelRight = new Konva . Label ( {
x : 160 ,
y : 170 ,
offsetX : 20 ,
2020-05-08 22:59:35 +08:00
opacity : 0.75 ,
2017-02-24 22:15:33 +08:00
} ) ;
labelRight . add (
new Konva . Tag ( {
fill : 'green' ,
pointerDirection : 'right' ,
pointerWidth : 20 ,
pointerHeight : 28 ,
2020-05-08 22:59:35 +08:00
lineJoin : 'round' ,
2017-02-24 22:15:33 +08:00
} )
) ;
labelRight . add (
new Konva . Text ( {
text : 'Label right' ,
fontFamily : 'Calibri' ,
fontSize : 18 ,
padding : 5 ,
2020-05-08 22:59:35 +08:00
fill : 'white' ,
2017-02-24 22:15:33 +08:00
} )
) ;
// simple label
var simpleLabel = new Konva . Label ( {
x : 180 ,
y : 150 ,
2020-05-08 22:59:35 +08:00
opacity : 0.75 ,
2017-02-24 22:15:33 +08:00
} ) ;
simpleLabel . add (
new Konva . Tag ( {
2020-05-08 22:59:35 +08:00
fill : 'yellow' ,
2017-02-24 22:15:33 +08:00
} )
) ;
simpleLabel . add (
new Konva . Text ( {
text : 'Simple label' ,
fontFamily : 'Calibri' ,
fontSize : 18 ,
padding : 5 ,
2020-05-08 22:59:35 +08:00
fill : 'black' ,
2017-02-24 22:15:33 +08:00
} )
) ;
// add the labels to layer
layer . add ( tooltip , tooltipUp , labelLeft , labelRight , simpleLabel ) ;
2021-04-30 22:24:27 +08:00
layer . children . forEach ( ( child ) = > child . cache ( ) ) ;
2017-02-24 22:15:33 +08:00
stage . add ( layer ) ;
2021-05-04 06:09:18 +08:00
cloneAndCompareLayer ( layer , 250 , 100 ) ;
2017-02-24 22:15:33 +08:00
} ) ;
2021-04-30 22:24:27 +08:00
it ( 'tag should list text size changes' , function ( ) {
2017-02-24 22:15:33 +08:00
var stage = addStage ( ) ;
var layer = new Konva . Layer ( ) ;
stage . add ( layer ) ;
var label = new Konva . Label ( ) ;
var tag = new Konva . Tag ( {
2020-05-08 22:59:35 +08:00
stroke : 'black' ,
2015-02-12 15:10:36 +08:00
} ) ;
2017-02-24 22:15:33 +08:00
label . add ( tag ) ;
2016-10-25 23:42:32 +08:00
2017-02-24 22:15:33 +08:00
var text = new Konva . Text ( {
2020-05-08 22:59:35 +08:00
text : 'hello hello hello hello hello hello hello hello' ,
2017-02-24 22:15:33 +08:00
} ) ;
label . add ( text ) ;
2016-10-25 23:42:32 +08:00
2017-02-24 22:15:33 +08:00
layer . add ( label ) ;
layer . draw ( ) ;
2016-10-25 23:42:32 +08:00
2017-02-24 22:15:33 +08:00
text . width ( 200 ) ;
2016-10-25 23:42:32 +08:00
2017-02-24 22:15:33 +08:00
layer . draw ( ) ;
assert . equal ( tag . width ( ) , text . width ( ) ) ;
2020-11-25 23:19:40 +08:00
text . height ( 200 ) ;
assert . equal ( tag . height ( ) , text . height ( ) ) ;
2017-02-24 22:15:33 +08:00
} ) ;
2020-09-06 19:08:50 +08:00
2021-04-30 22:24:27 +08:00
it ( 'tag cornerRadius' , function ( ) {
2020-09-18 02:25:46 +08:00
var stage = addStage ( ) ;
var layer = new Konva . Layer ( ) ;
var tag = new Konva . Tag ( {
x : 50 ,
y : 50 ,
width : 100 ,
height : 100 ,
fill : 'black' ,
cornerRadius : [ 0 , 10 , 20 , 30 ] ,
} ) ;
layer . add ( tag ) ;
stage . add ( layer ) ;
layer . draw ( ) ;
assert . equal ( tag . cornerRadius ( ) [ 0 ] , 0 ) ;
assert . equal ( tag . cornerRadius ( ) [ 1 ] , 10 ) ;
assert . equal ( tag . cornerRadius ( ) [ 2 ] , 20 ) ;
assert . equal ( tag . cornerRadius ( ) [ 3 ] , 30 ) ;
var trace = layer . getContext ( ) . getTrace ( ) ;
assert . equal (
trace ,
'clearRect(0,0,578,200);save();transform(1,0,0,1,50,50);beginPath();moveTo(0,0);lineTo(90,0);arc(90,10,10,4.712,0,false);lineTo(100,80);arc(80,80,20,0,1.571,false);lineTo(30,100);arc(30,70,30,1.571,3.142,false);lineTo(0,0);arc(0,0,0,3.142,4.712,false);closePath();fillStyle=black;fill();restore();clearRect(0,0,578,200);save();transform(1,0,0,1,50,50);beginPath();moveTo(0,0);lineTo(90,0);arc(90,10,10,4.712,0,false);lineTo(100,80);arc(80,80,20,0,1.571,false);lineTo(30,100);arc(30,70,30,1.571,3.142,false);lineTo(0,0);arc(0,0,0,3.142,4.712,false);closePath();fillStyle=black;fill();restore();'
) ;
2020-09-06 19:08:50 +08:00
} ) ;
2022-08-05 23:21:48 +08:00
2022-08-05 23:26:10 +08:00
it ( 'react to pointer properties' , function ( ) {
2022-08-05 23:21:48 +08:00
var stage = addStage ( ) ;
var layer = new Konva . Layer ( ) ;
stage . add ( layer ) ;
var label = new Konva . Label ( {
x : 100 ,
y : 100 ,
draggable : true ,
} ) ;
var counter = 0 ;
var oldSync = label . _sync ;
label . _sync = ( ) = > {
oldSync . call ( label ) ;
counter += 1 ;
} ;
const tag = new Konva . Tag ( {
fill : '#bbb' ,
shadowColor : 'black' ,
shadowBlur : 10 ,
shadowOffset : { x : 10 , y : 10 } ,
shadowOpacity : 0.2 ,
lineJoin : 'round' ,
pointerDirection : 'up' ,
pointerWidth : 20 ,
pointerHeight : 20 ,
cornerRadius : 5 ,
} ) ;
// add a tag to the label
label . add ( tag ) ;
// add text to the label
label . add (
new Konva . Text ( {
text : 'hello' ,
fontSize : 50 ,
lineHeight : 1.2 ,
fill : 'green' ,
} )
) ;
layer . add ( label ) ;
assert . equal ( counter , 4 ) ;
tag . pointerDirection ( 'bottom' ) ;
assert . equal ( counter , 5 ) ;
tag . pointerWidth ( 30 ) ;
assert . equal ( counter , 6 ) ;
tag . pointerHeight ( 40 ) ;
assert . equal ( counter , 7 ) ;
} ) ;
2015-08-28 11:11:10 +08:00
} ) ;