如何使文本以逆时针方向写入

时间:2012-09-07 06:19:57

标签: javascript jquery canvas html5-canvas

如何使文字逆时针方向写入?

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();
};

我希望文字在逆时针方向显示为enter image description here

1 个答案:

答案 0 :(得分:4)

不确定你在问什么,但是如果你想以逆时针方向向后写文字,你只需更改这一行:

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

最后一个参数更改为-angle

正如你所料,文本将会倒退。