javascript - 同心圆中心的 Canvas 文本

标签 javascript html asp.net canvas

我正在尝试将我的文本放置在 5 个同心圆的中心!谁能帮帮我吗!下面是我的代码。建议我应该做哪些改变才能使其位于圆心。谢谢。

       function circle(e, color, x, y, radius, startAngle, endAngle, clockwise) {
          if (arguments.length < 9) return alert("Not enough arguments.\nThe function \"circle\" requires 9 arguments\nYou provided only " + arguments.length + ".");
           e.beginPath();
           e.arc(x, y, radius, startAngle, endAngle, clockwise);
           e.strokeStyle = color;
           e.stroke();

       }

       function draw(e) {
           var deg360 = Math.PI * 2; 
           circle(e, "#00688B", 120, 120, 90, deg360, 0, deg360, true);
           e.fillStyle = '#00688B';
           e.fill();
           circle(e, "#0099CC", 120, 120, 80, deg360, 0, deg360, true);
           e.fillStyle = '#0099CC';
           e.fill();
           circle(e, "#63D1F4", 120, 120, 70, deg360, 0, deg360, true);

           e.fillStyle = '#63D1F4';
           e.fill();
           circle(e, "#05EDFF", 120, 120, 60, deg360, 0, deg360, true);
           e.fillStyle = '#05EDFF';
           e.fill();
           circle(e, "#BFEFFF", 120, 120, 50, deg360, 0, deg360, true);
           e.fillStyle = '#BFEFFF';
           e.fill();
           circle(e, "#E6E8FA", 120, 120, 40, deg360, 0, deg360, true);
           e.fillStyle = '#E6E8FA';
           e.fill();
          e.fill();
           e.fillStyle = "black"; // font color to write the text with
           e.font = radius * 0.15 + "px arial";
           e.textBaseline = "middle";
           e.textAlign = "center";
           e.fillText(num, radius, e);

           for (num = 1; num < 25; num++) {
               ang = num * Math.PI / 12;
               e.rotate(ang);
               e.translate(0, radius * 1.85);
               e.rotate(-ang);
               e.fillText(num.toString(), 0, 0);
               e.rotate(ang);
               e.translate(0, -radius * 1.85);
               e.rotate(-ang);}}

最佳答案

enter image description here

示例代码和演示:

  1. 移至同心中心点
  2. 从中心点旋转
  3. 向右移动 x 到半径
  4. 取消旋转(因此字母是直立的);
  5. 画出数字。
  6. 将所有变换重置为默认值

var canvas=document.getElementById("canvas");
var e=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

draw(e,40);

function circle(e, color, x, y, radius, startAngle, endAngle, clockwise) {
  if (arguments.length < 9) return alert("Not enough arguments.\nThe function \"circle\" requires 9 arguments\nYou provided only " + arguments.length + ".");
  e.beginPath();
  e.arc(x, y, radius, startAngle, endAngle, clockwise);
  e.strokeStyle = color;
  e.stroke();
}

function draw(e,radius) {
  var deg360 = Math.PI * 2; 
  circle(e, "#00688B", 120, 120, 90, deg360, 0, deg360, true);
  e.fillStyle = '#00688B';
  e.fill();
  circle(e, "#0099CC", 120, 120, 80, deg360, 0, deg360, true);
  e.fillStyle = '#0099CC';
  e.fill();
  circle(e, "#63D1F4", 120, 120, 70, deg360, 0, deg360, true);
  e.fillStyle = '#63D1F4';
  e.fill();
  circle(e, "#05EDFF", 120, 120, 60, deg360, 0, deg360, true);
  e.fillStyle = '#05EDFF';
  e.fill();
  circle(e, "#BFEFFF", 120, 120, 50, deg360, 0, deg360, true);
  e.fillStyle = '#BFEFFF';
  e.fill();
  circle(e, "#E6E8FA", 120, 120, 40, deg360, 0, deg360, true);
  e.fillStyle = '#E6E8FA';
  e.fill();
  e.fill();
  e.fillStyle = "black"; // font color to write the text with
  e.font = radius * 0.15 + "px arial";
  e.textBaseline = "middle";
  e.textAlign = "center";
  e.fillText(0, radius, e);
  for (num = 1; num < 25; num++) {
    ang = num * Math.PI / 12;
    // translate to concentric centerpoint
    e.translate(120,120);
    // rotate from the centerpoint
    e.rotate(ang);
    // move x-right to the radius
    e.translate(radius,0);
    // unrotate (so letters are upright);
    e.rotate(-ang);
    e.fillText(num.toString(), 0, 0);
    // reset all transforms to default
    e.setTransform(1,0,0,1,0,0);
  }
}
<canvas id="canvas" width=300 height=300></canvas>

关于javascript - 同心圆中心的 Canvas 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35351176/

相关文章:

javascript - 为什么使用 "!= null"测试参数是否传递不好

html - Bootstrap 3 页脚问题

jquery - 表格列与 IE 中的固定标题不对齐

c# - 阻止从同一用户 ID 到 Web 方法的多个请求 C#

javascript - 在 Chrome document.styleSheets 上,当我添加样式表时不会更新

javascript - 我是否必须取消 View 中的 dom 元素?

javascript - 在textextjs插件中添加额外数据

html - Rails 使用通知消息重定向到子域

Javascript统一回发

asp.net - 无法获取asp.net中只读文本框的值