Skip to content

Commit 2a905cb

Browse files
committed
Automatically scale canvas for HiDPI screens
The graph is blurry on HiDPI screens. Scale the canvas to make up for it. See https://www.html5rocks.com/en/tutorials/canvas/hidpi/.
1 parent 5fcc26b commit 2a905cb

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

gitgraph.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ var gitGraph = function (canvas, rawGraphList, config) {
4343

4444
var ctx = canvas.getContext("2d");
4545

46+
var devicePixelRatio = window.devicePixelRatio || 1;
47+
var backingStoreRatio = ctx.webkitBackingStorePixelRatio ||
48+
ctx.mozBackingStorePixelRatio ||
49+
ctx.msBackingStorePixelRatio ||
50+
ctx.oBackingStorePixelRatio ||
51+
ctx.backingStorePixelRatio || 1;
52+
53+
var ratio = devicePixelRatio / backingStoreRatio;
54+
4655
var init = function () {
4756
var maxWidth = 0;
4857
var i;
@@ -60,12 +69,20 @@ var gitGraph = function (canvas, rawGraphList, config) {
6069
graphList.unshift(row);
6170
}
6271

63-
canvas.width = maxWidth * config.unitSize;
64-
canvas.height = graphList.length * config.unitSize;
72+
var width = maxWidth * config.unitSize;
73+
var height = graphList.length * config.unitSize;
74+
75+
canvas.width = width * ratio;
76+
canvas.height = height * ratio;
77+
78+
canvas.style.width = width + 'px';
79+
canvas.style.height = height + 'px';
6580

6681
ctx.lineWidth = config.lineWidth;
6782
ctx.lineJoin = "round";
6883
ctx.lineCap = "round";
84+
85+
ctx.scale(ratio, ratio);
6986
};
7087

7188
var genRandomStr = function () {
@@ -185,7 +202,7 @@ var gitGraph = function (canvas, rawGraphList, config) {
185202
}
186203
}
187204

188-
y = canvas.height - config.unitSize * 0.5;
205+
y = (canvas.height / ratio) - config.unitSize * 0.5;
189206

190207
//iterate
191208
for (i = 0, l = graphList.length; i < l; i++) {

0 commit comments

Comments
 (0)