logo
积分中心
鸿蒙开发实战——使用Canvas绘制环形文字
头像 蓝亭书序 2026-11-18 13:23:00    发布
0 浏览 0 收藏

1、背 景

我们在之前的文章

图片

  • 鸿蒙开发实战——抽奖转盘

2、使用Canvas实现环形文字


👉🏻 实现思路

由于我们需要绘制的文字是按照圆的周长进行均匀分布,因此,我们第一步就是需要确定每个文字占用的“扇形”区域弧度是多少。

另外,由于文字是围绕一个圆形做分布,因此,我们在绘制每个文字时,都先将文字按照分配的弧度进行旋转,然后进行文字绘制。

由于旋转的中心点默认是(0,0),也就是最左上角,为了保证旋转是在画布的中央,我们只需要将画布做一个平移即可,将画布移动到圆的中心点。


有了思路后,我们开始实现(文末有完整代码)。

👉🏻 step1:定义Canvas并且初始化对应的Context。

Canvas有一个onReady()事件,onReady设置的callback将会在Canvas准备完毕后调用。

代码如下(这是一个基本的Canvas开发模板):


@Entry
@Component
@Preview
export struct Index {
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
  
  private drawCircleText(text: string) {
    // todo 绘制文本逻辑
  }

  build() {
    Row() {
      Column() {
        Canvas(this.context)
          .width(300)
          .height(300)
          .borderColor('green')
          .borderWidth(1)
          .onReady(() => this.drawCircleText('欢迎加入Harmony自习室,微信公众号【Harmony自习室】'))
      }
      .width('100%')
    }
    .height('100%');
  }
}


效果如下(由于我们还未在画布上绘制内容,因此显示是空白):

图片

👉🏻 step2:确定一些绘制参数

我们需要确定圆心位置、圆心半径、每个文字所占用的度数。代码如下:


private drawCircleText(text: string) {
    // 获取画布的尺寸信息
    const width = this.context.width, height = this.context.height;
    const fontSize = 18; // 字体大小
    const r = width / 2 - fontSize; // 半径
    const charRadius = 360 / text.length; // 计算每个文字占用的度数
    // 圆形位置: (width/2, height/2)
}



👉🏻 step3:偏移画布到圆心并且开始绘制每个文字

代码如下:我们在第10行代码将画布偏移到圆心,并且接着就是一个循环,分别绘制每个文字,在每个文字绘制完毕后,对画布进行旋转。


private drawCircleText(text: string) {
    const width = this.context.width, height = this.context.height;
    const fontSize = 18;
    const r = width / 2 - fontSize;
    this.context.clearRect(0, 0, width, height); // 清空画布
    this.context.save();
    this.context.font = `${fontSize}vp`
    this.context.textBaseline = 'top';
    const charRadius = 360 / text.length; // 一个字符所占的弧度
    this.context.translate(width / 2, height / 2);
    for (let i = 0; i < text.length; i++) {
      this.context.fillText(text.charAt(i), 0, -r);
      this.context.rotate(this.getAngle(charRadius));
    }

    this.context.restore();
  }



一个完整的Demo如下:


@Entry
@Component
@Preview
export struct Index {
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);

  // 转换为弧度值
  private getAngle(n: number) {
    return Math.PI / 180 * n;
  }

  private drawCircleText(text: string) {
    const width = this.context.width, height = this.context.height;
    const fontSize = 18;
    const r = width / 2 - fontSize;
    this.context.clearRect(0, 0, width, height); // 清空画布
    this.context.save();
    this.context.font = `${fontSize}vp`
    this.context.textBaseline = 'top';
    const charRadius = 360 / text.length; // 一个字符所占的弧度
    this.context.translate(width / 2, height / 2);
    for (let i = 0; i < text.length; i++) {
      this.context.fillText(text.charAt(i), 0, -r);
      this.context.rotate(this.getAngle(charRadius));
    }
    this.context.restore();
  }

  build() {
    Row() {
      Column() {
        Canvas(this.context)
          .width(300)
          .height(300)
          .borderColor('green')
          .borderWidth(1)
          .onReady(() => this.drawCircleText('欢迎加入Harmony自习室,微信公众号【Harmony自习室】'))
      }
      .width('100%')
    }
    .height('100%');
  }
}


效果如下:

图片

3、one more thing..

另外,为了方便查看,我将两个实现方案都放置到一起,效果如下:

图片


代码地址如下:


https://gitee.com/lantingshuxu/harmony-class-room-demos/tree/feat%2FcircleText/




©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
HarmonyOS

暂无评论数据

加载中...

发布

地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除

京ICP备:2022009079号-2

京公网安备:11010502051901号

ICP证:京B2-20230255