babylon.js实现文本间距效果

在babylon.js中,如果使用TextBlock时,想给文字加间距,官方没有这个属性去进行间距设置,需要自己手动写js代码去实现这个效果。

一、先来看一下默认间距和设置间距后的效果:

文本默认间距:
在这里插入图片描述
设置完文本间距后的效果:
在这里插入图片描述

这里有两种实现方式:
第一种当然是选择使用空格,但是不好把控精确
第二种是使用代码实现,可以设置精确的间距值,下面贴出实现代码

二、代码实现

封装:

class TextBlockExtra extends BABYLON.GUI.TextBlock{
  _letterSpacing = 0;
  set letterSpacingInPixels(value) {
    this._letterSpacing = value;
    this.markAllAsDirty();
  }
  get letterSpacingInPixels() {
    return this._letterSpacing;
  }

  textWithSpacing(
    context,
    text,
    x,
    y,
    spacing
  ) {
    const total_width =
      context.measureText(text).width + spacing * (text.length - 1);
    const align = context.textAlign;
    context.textAlign = "left";

    switch (align) {
      case "right":
        x -= total_width;
        break;
      case "center":
        x -= total_width / 2;
        break;
    }

    let offset,
      pair_width,
      char_width,
      char_next_width,
      pair_spacing,
      char,
      char_next;

    for (offset = 0; offset < text.length; offset = offset + 1) {
      char = text.charAt(offset);
      pair_spacing = 0;
      if (offset + 1 < text.length) {
        char_next = text.charAt(offset + 1);
        pair_width = context.measureText(char + char_next).width;
        char_width = context.measureText(char).width;
        char_next_width = context.measureText(char_next).width;
        pair_spacing = pair_width - char_width - char_next_width;
      }

      if (this.outlineWidth) {
        context.strokeText(char, x, y);
      }
      context.fillText(char, x, y);
      x = x + char_width + pair_spacing + spacing;
    }

    context.textAlign = align;
  }

_drawText(
    text,
    textWidth,
    y,
    context
  ) {
    var width = this._currentMeasure.width;
    var x = 0;
    switch (this._textHorizontalAlignment) {
      case BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT:
        x = 0;
        break;
      case BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT:
        x = width - textWidth;
        break;
      case BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER:
        x = (width - textWidth) / 2;
        break;
    }

    if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
      context.shadowColor = this.shadowColor;
      context.shadowBlur = this.shadowBlur;
      context.shadowOffsetX = this.shadowOffsetX;
      context.shadowOffsetY = this.shadowOffsetY;
    }

    if (this._letterSpacing !== 0) {
        switch(this._textHorizontalAlignment){
            case BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER:
                x -= text.length * this._letterSpacing * 0.5;
            break;
            case BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT:
                x -= text.length * this._letterSpacing;
            break;
        } 
        this.textWithSpacing(
            context,
            this.text,
            this._currentMeasure.left + x,
            y,
            this._letterSpacing
        )
    }else{
        if(this.outlineWidth){
            context.strokeText(text, this._currentMeasure.left + x, y);
        }
        context.fillText(text, this._currentMeasure.left + x, y);
    }

    if (this.underline) {
      context.beginPath();
      context.lineWidth = Math.round(this.fontSizeInPixels * 0.05);
      context.moveTo(this._currentMeasure.left + x, y + 3);
      context.lineTo(this._currentMeasure.left + x + textWidth, y + 3);
      context.stroke();
      context.closePath();
    }

    if (this.lineThrough) {
      context.beginPath();
      context.lineWidth = Math.round(this.fontSizeInPixels * 0.05);
      context.moveTo(
        this._currentMeasure.left + x,
        y - this.fontSizeInPixels / 3
      );
      context.lineTo(
        this._currentMeasure.left + x + textWidth,
        y - this.fontSizeInPixels / 3
      );
      context.stroke();
      context.closePath();
    }
  }
}

使用:

    var promptText = new TextBlockExtra();
    promptText.text = "码到成功";
    promptText.color = "white";
    promptText.fontSize = 40;
    // 字体间距
    promptText.letterSpacingInPixels =40
    advancedTexture.addControl(promptText);

然后就可以正常使用了~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值