文字像画布中的垂直圆弧

时间:2018-06-04 04:27:11

标签: jquery html canvas html5-canvas

有没有办法用画布创建弧形文本?我按照我们的回答:

How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)

我得到的最好的东西是从屋顶或者底部的曲线。我想要像这样的弧:

enter image description here

1 个答案:

答案 0 :(得分:0)

这是画布代码:

https://codepen.io/creativedev/pen/oybGMQ

function drawTextArc(context, str, centerX, centerY, radius, angle) {
  var len = str.length,
    s;
  context.save();
  context.translate(centerX, centerY);
  context.rotate(-1 * angle / 2);
  context.rotate(-1 * (angle / len) / 2);
  for (var n = 0; n < len; n++) {
    context.rotate(angle / len);
    context.save();
    context.translate(0, -1 * radius);
    s = str[n];
    context.fillText(s, 0, 0);
    context.restore();
  }
  context.restore();
}
var canvas = document.getElementById('myCanvas'),
  context = canvas.getContext('2d'),
  centerX = canvas.width / 2,
  centerY = canvas.height - 30,
  angle = Math.PI * 0.8,
  radius = 150;

context.font = '30pt Calibri';
context.textAlign = 'center';
context.fillStyle = 'green';
context.lineWidth = 4;
drawTextArc(context, 'Vertical Arc', centerX, centerY, radius, angle);

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