如何在 qml canvas 元素中创建弯曲文本? [英] How i can create a curved text in qml canvas element?

查看:36
本文介绍了如何在 qml canvas 元素中创建弯曲文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 qml 中创建一个弯曲的文本,这可能吗?有我可以导入的 javascript 库吗?

i want to create a curved text in qml it's possible? Is there a javascript library that i can import to do it?

我的想法是可能使用 canvas 元素,但我不知道我该怎么做...这是个好主意??

My idea is that it's possible maybe with canvas element but i don't know how i can do it... it's a good idea??

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")


    Canvas{

      anchors.fill: parent

      onPaint: {
          var ctx = getContext("2d");
          ctx.fillStyle = Qt.rgba(1, 0, 0, 1);
          ctx.fillRect(0, 0, width, height);
          //how i can create curved text??? 

      }

    }
}

推荐答案

您好,我尝试将代码插入@folibis 发布的链接中,完美运行!!!这是代码:

Hello I tried the code into the link posted by @folibis , work perfectly!!! This is the code :

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Canvas{

        property string nameFont: webFont.name

        function drawTextAlongArc(context, str, centerX, centerY, radius, angle)
        {

            context.save();
            context.translate(centerX, centerY);
            context.rotate(-1 * angle / 2);
            context.rotate(-1 * (angle / str.length) / 2);
            for (var n = 0; n < str.length; n++) {
                context.rotate(angle / str.length);
                context.save();
                context.translate(0, -1 * radius);
                var char1 = str[n];
                context.fillText(char1, 0, 0);
                context.restore();
            }
            context.restore();

        }


      anchors.fill: parent
      onPaint: {

          var ctx = getContext("2d");
          ctx.fillStyle = Qt.rgba(1, 1, 1, 1);
          ctx.fillRect(0, 0, width, height);


          ctx.font='50px Verdana'

          //ctx.font = '30px Courier New'
          ctx.textAlign = "center";

          var centerX = width / 2;
          var centerY = height/2; //height - 30;
          var angle   = Math.PI * 0.8; // radians
          var radius  = 180;
          ctx.fillStyle="#000000"
          drawTextAlongArc(ctx, "Hello World", centerX, centerY, radius, angle);

      }
    }
} 

这篇关于如何在 qml canvas 元素中创建弯曲文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆