Skip to content

Commit

Permalink
Added support for sharp grid lines.
Browse files Browse the repository at this point in the history
The HTML5 canvas uses sub-pixel rendering, meaning that 1px wide lines often end up being blurred across two physical pixels. Considering a vertical line, its 'x' position specifies the center of the line. Therefore it spans +/-0.5 pixels from that point. Bam. Blurriness.

The solution is to subtract (adding would also work) 0.5 to the calculated x/y positions for vertical/horizontal lines respectively.

This patch does not change the default behaviour.

This problem is visible before this patch too, where the topmost and bottommost lines are 1px thick, while others are 2px thick.
  • Loading branch information
drewnoakes committed Feb 23, 2013
1 parent 2ea83e3 commit f1a0a88
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion smoothie.js
Expand Up @@ -78,7 +78,7 @@ TimeSeries.prototype.append = function(timestamp, value) {
function SmoothieChart(options) {
// Defaults
options = options || {};
options.grid = options.grid || { fillStyle:'#000000', strokeStyle: '#777777', lineWidth: 1, millisPerLine: 1000, verticalSections: 2 };
options.grid = options.grid || { fillStyle:'#000000', strokeStyle: '#777777', lineWidth: 1, sharpLines: false, millisPerLine: 1000, verticalSections: 2 };
options.millisPerPixel = options.millisPerPixel || 20;
options.maxValueScale = options.maxValueScale || 1;
// NOTE there are no default values for 'minValue' and 'maxValue'
Expand Down Expand Up @@ -207,6 +207,8 @@ SmoothieChart.prototype.render = function(canvas, time) {
for (var t = time - (time % options.grid.millisPerLine); t >= time - (dimensions.width * options.millisPerPixel); t -= options.grid.millisPerLine) {
canvasContext.beginPath();
var gx = Math.round(dimensions.width - ((time - t) / options.millisPerPixel));
if (options.grid.sharpLines)
gx -= 0.5;
canvasContext.moveTo(gx, 0);
canvasContext.lineTo(gx, dimensions.height);
canvasContext.stroke();
Expand All @@ -231,6 +233,8 @@ SmoothieChart.prototype.render = function(canvas, time) {
// Horizontal (value) dividers.
for (var v = 1; v < options.grid.verticalSections; v++) {
var gy = Math.round(v * dimensions.height / options.grid.verticalSections);
if (options.grid.sharpLines)
gy -= 0.5;
canvasContext.beginPath();
canvasContext.moveTo(0, gy);
canvasContext.lineTo(dimensions.width, gy);
Expand Down

0 comments on commit f1a0a88

Please sign in to comment.