javascript - 如何使用 Canvas 居中对齐线条图?

标签 javascript html canvas html5-canvas

问题

当我尝试使用 moveTo(100, 400) 作为 x 轴时,我试图将此线条图放在我的 canvas 的中心,它不会将水平起始位置更改为 100。如果我对 y 轴尝试相同的操作,它会沿 x 轴移动该线。

我还需要帮助沿着 y 轴垂直绘制 y 轴数字 1 - 9,它似乎只能水平对齐。 编辑!:我已经手动抚摸了 y 轴上的每个点,所以我在那里有数字,现在我只想知道如何将图形移动到中心!!

脚本

var c = document.getElementById("myCanvas");
        var ctx = c.getContext("2d");
        
        ctx.linecap = 'round';
        // draw a scale with the numbers on it
        ctx.lineWidth = 2;
            
        ctx.strokeStyle = '#FF9900';
        ctx.fillStyle = 'blue';
        ctx.beginPath();
        ctx.moveTo(100, 400);             
        for (i = 0; i <= 6; i+=1) {
            
             //put a stroke mark
             ctx.lineTo(100*i,400);
             ctx.lineTo(100*i,405); //markers
             ctx.lineTo(100*i,400);
             
             // write the number 10px below
             ctx.strokeStyle = '#000000';
             // default size is 10px
             ctx.strokeText(i, 100*i, 415);
             ctx.strokeStyle = '#FF9900';
        }    
        // draw a vertical scale with lines on it
        ctx.moveTo(0, -100);
        for (b = 0; b <= 9; b+=1) {
            
            //put a stroke mark
            ctx.lineTo(0,44.5*b);
            ctx.lineTo(5,44.5*b);
            ctx.lineTo(0,44.5*b);
            
            // write the number 10px below
            ctx.strokeStyle = '#000000';
            // default size is 10px                  
       }  
       ctx.strokeStyle = '#000000'
       ctx.strokeText(1, 8, 365);
       ctx.strokeText(2, 8, 320.5);
       ctx.strokeText(3, 8, 276);
       ctx.strokeText(4, 8, 231.5);
       ctx.strokeText(5, 8, 187);
       ctx.strokeText(6, 8, 142.5);
       ctx.strokeText(7, 8, 98);
       ctx.strokeText(8, 8, 53.5);
       ctx.strokeText(9, 8, 9);
       ctx.strokeStyle = '#FF9900';
        ctx.stroke();
<!DOCTYPE html>
<html>
   <head>
      <title>Canvas Axis calibration</title>
       <link rel="stylesheet" type="text/css" href="base.css"/> 
        
   </head>
   <body>
    <canvas id="myCanvas" width="1600" height="500"style="border:1px solid #c3c3c3;">
       Canvas is not playing!
    </canvas>
 

</body>
</html>

最佳答案

moveTo() 只是设置线条的起点,它并不是绘制实际的线条。使用 lineTo() 绘制实际线条。所以 moveTo()或者你开始的地方,lineTo()是你的地方。因此 x 轴的起点必须是 moveTo(800, 0)

var c = document.getElementById("myCanvas"),
    ctx = c.getContext("2d"),
    lineWidth = 2,
    xNumber = 6,
    yNumber = 9,
    xCenter = c.width / 2,
    yCenter = 44.5 * yNumber + 44.5

ctx.linecap = 'round';
// draw a scale with the numbers on it
ctx.lineWidth = lineWidth;
ctx.strokeStyle = '#FF9900';
ctx.fillStyle = 'blue';

ctx.beginPath();
ctx.moveTo(xCenter, yCenter);

for (i = 0; i <= xNumber; ++i) {
    //put a stroke mark
    ctx.lineTo((xCenter + (100 * i)), yCenter);
    ctx.lineTo((xCenter + (100 * i)), (yCenter + 5)); //markers
    ctx.lineTo((xCenter + (100 * i)), yCenter);
             
    // write the number 10px below
    ctx.strokeStyle = '#000000';
    // default size is 10px
    ctx.strokeText(i, (xCenter + (100 * i)), (yCenter + 15));
}

ctx.strokeStyle = '#FF9900';
ctx.stroke()

// draw a vertical scale with lines on it
ctx.beginPath()
ctx.moveTo(xCenter, yCenter);

for (b = 0; b <= yNumber; ++b) {
    //put a stroke mark
    if(b === 0) continue;

    ctx.lineTo(xCenter, (yCenter - (44.5 * b)));
    ctx.lineTo((xCenter - 5), (yCenter - (44.5 * b)));
    ctx.lineTo(xCenter, (yCenter - (44.5 * b)));  
    ctx.strokeStyle = '#000000';
    ctx.strokeText(b, (xCenter - 15), (yCenter - (44.5 * b)));
}

ctx.strokeStyle = '#FF9900';
ctx.stroke();
<!DOCTYPE html>
<html>
   <head>
      <title>Canvas Axis calibration</title>
       <link rel="stylesheet" type="text/css" href="base.css"/> 
        
   </head>
   <body>
    <canvas id="myCanvas" width="1600" height="500"style="border:1px solid #c3c3c3;">
       Canvas is not playing!
    </canvas>
 

</body>
</html>

关于javascript - 如何使用 Canvas 居中对齐线条图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55202425/

相关文章:

javascript - Google Maps Api 显示灰色 map

javascript - 来自 Web Worker 的 setTimeout

html - CSS 未通过服务器加载

PHP include语句问题

javascript - 从 html5 <canvas> 创建视频流

javascript - 为什么highcharts桑基图有时会将多个节点名称相同但Id不同的节点合并为一个并隐藏线条?

javascript - 在进入全屏模式后从嵌入式 iframe 中删除 Youtube 品牌、标题文本等

html - 浏览器如何分析没有<table>的<tr> <td>?

javascript - setTimeout() 和 Canvas 未按预期工作

javascript - 是否可以以编程方式检测数据 url 的大小限制?