4

如何使文本逆时针书写?

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 char = str[n];
        context.fillText(char, 0, 0);
        context.restore();
    }
    context.restore();
}

window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");

    context.font = "30pt Calibri";
    context.textAlign = "center";
    context.fillStyle = "blue";
    context.strokeStyle = "blue";
    context.lineWidth = 4;

    var centerX = canvas.width / 2;
    var centerY = canvas.height - 30;
    var angle = Math.PI * 0.8; // radians
    var radius = 150;
    drawTextAlongArc(context, "Text along arc path", centerX, centerY, radius, angle);

    // draw circle underneath text
    context.arc(centerX, centerY, radius - 10, 0, 2 * Math.PI, false);
    context.stroke();
};

在此处输入图像描述 我希望文本以逆时针方向显示为这样

4

1 回答 1

4

不知道你在问什么,但如果你想以逆时针方向向后写你的文字,你只需改变这一行:

drawTextAlongArc(context, "Text along arc path", centerX, centerY, radius, -angle);

最后一个参数更改为-angle

正如您所期望的那样,文本将会倒退。

于 2012-09-07T06:25:24.610 回答