-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.js
89 lines (78 loc) · 1.88 KB
/
grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//enterprisbug.github.com/grid.js#v1.1
(function(window, document, undefined) {
function mergeWithDefaultValues(obj) {
for (var i = 1; i < arguments.length; i++) {
var def = arguments[i];
for (var n in def) {
if (obj[n] === undefined) {
obj[n] = def[n];
}
}
}
return obj;
}
var defaults = {
distance : 50,
lineWidth : 1,
gridColor : "#000000",
caption : true,
font: "10px Verdana"
};
/** The constructor */
var Grid = function Grid(o) {
if (!this.draw) return new Grid(o);
this.opts = mergeWithDefaultValues(o || {}, Grid.defaults, defaults);
};
Grid.defaults = {};
mergeWithDefaultValues(Grid.prototype, {
draw: function(target) {
var self = this;
var o = self.opts;
if (target) {
target.save();
target.lineWidth = o.lineWidth;
target.strokeStyle = o.gridColor;
target.font = o.font;
if (target.canvas.width > target.canvas.height) {
until = target.canvas.width;
} else {
until = target.canvas.height;
}
for (var y = 0; y <= until; y += o.distance) {
target.beginPath();
if (o.lineWidth == 1.0) {
target.moveTo(0, y + 0.5);
target.lineTo(target.canvas.width, y + 0.5);
} else {
target.moveTo(0, y);
target.lineTo(target.canvas.width, y);
}
target.closePath();
target.stroke();
if (o.caption)
{
target.fillText(y, y, 10);
}
}
for (var x = 0; x <= until; x += o.distance) {
target.beginPath();
if (o.lineWidth == 1.0) {
target.moveTo(x + 0.5, 0);
target.lineTo(x + 0.5, target.canvas.height);
} else {
target.moveTo(x, 0);
target.lineTo(x, target.canvas.height);
}
target.closePath();
target.stroke();
if (o.caption)
{
target.fillText(x, 0, x);
}
}
target.restore();
}
}
});
window.Grid = Grid;
})(window, document);