简体   繁体   中英

How to vertically center text inside a KineticJS Wedge?

I've got the following code:

var text = new Kinetic.Text({
    text: carNames,
    fontFamily: 'Calibri',
    fontSize: 17,
    fill: 'black',
    align: 'center'
});
text.toImage({
    width: text.getWidth(),
    height: text.getHeight(),
    callback: function (img) {
        var cachedText = new Kinetic.Image({
            image: img,
            x: 180,
            y: 0
        });
        wedge.add(cachedText);
        layer.draw();
    }
});

Which produces wedges and text that look like this:

在此处输入图片说明

But I need the text to be centered inside the wedge, like this:

在此处输入图片说明

Does anyone know of a way to achieve this? I've tried several things but I just can't get the text to align as in the second image.

Thanks in advance for your trouble.

One way to do it is using the text's offset in combination with the text's rotationDeg:

Demo: http://jsfiddle.net/m1erickson/mqsY3/

在此处输入图片说明

Here's code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script>

<style>
body{padding:20px;}
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:350px;
  height:350px;
}
</style>        
<script>
$(function(){

    var stage = new Kinetic.Stage({
        container: 'container',
        width: 350,
        height: 350
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);

    var cx=175;
    var cy=175;
    var wedgeRadius=120;
    var accumAngle=0;

    var center = new Kinetic.Circle({
        x:cx,
        y:cy,
        radius:5,
        fill: 'red'
    });
    layer.add(center);

    for(var i=0;i<12;i++){
        newTextWedge(30,"Element # "+i);
    }

    function newTextWedge(angle,text){

        var wedge = new Kinetic.Wedge({
          x: cx,
          y: cy,
          radius: wedgeRadius,
          angleDeg: angle,
          stroke: 'gray',
          strokeWidth: 1,
          rotationDeg:-accumAngle+angle/2
        });
        layer.add(wedge);

    if(accumAngle>90 && accumAngle<270){
        var offset=[wedgeRadius-10,7];
        var textAngle=accumAngle-180;
    }else{
        var offset=[-50,7];
        var textAngle=accumAngle;
    }

        var text = new Kinetic.Text({
            x:cx,
            y:cy,
            text:text,
            fill: 'red',
            offset:offset,
            rotationDeg:textAngle
        });
        layer.add(text);

        layer.draw();

        accumAngle+=angle;
    }


}); // end $(function(){});

</script>       
</head>

<body>
    <div id="container"></div>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM