我正在尝试在画布的中心绘制一个条形图。我希望条形图水平居中,但它只是稍微向左填充。怎么了?
CSS:
#my-canvas {
border: 1px solid #ffffff;
height: 100px;
width: 100px;
}
HTML:
<canvas id="my-canvas"></canvas>
JavaScript:
var bar = document.getElementById("my-canvas");
var barContext = rejectedBar.getContext("2d");
var canvasWidth = bar.scrollWidth;
var canvasHorizontalCenter = canvasWidth / 2;
var canvasHeight = bar.scrollHeight;
var barWidth = 50;
var barHorizontalCenter = barWidth / 2;
barContext.fillStyle = "#781108";
barContext.fillRect(canvasHorizontalCenter - barHorizontalCenter, 0, barWidth, 100);