-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicsDevice.js
106 lines (88 loc) · 2.83 KB
/
graphicsDevice.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function GraphicsDevice(canvas, refWidth, refHeight) {
var self = this;
var ctx = canvas.getContext("2d");
this.context = ctx;
canvas.width = document.width;
canvas.height = document.height;
var offsetX = 0;
var offsetY = 0;
ajustCanvasSize();
var ajustX = canvas.width / refWidth;
var ajustY = canvas.height / refHeight;
utils.assert(ajustX != 0 && ajustY != 0, "Banaloba mikasso!");
this.viewport = {
borderLeft: 0,
borderTop: 0,
width: refWidth,
height: refHeight
};
this.strokeRect = function(x, y, width, height, color, lineWidth) {
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.strokeRect(ajustX * x, ajustY * y, ajustX * width, ajustY * height);
};
this.fillRect = function(x, y, width, height, color) {
ctx.fillStyle = color;
ctx.fillRect(ajustX * x, ajustY * y, ajustX * width, ajustY * height);
};
this.fillCircle = function(x, y, radius, color) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(ajustX * x, ajustY * y, ajustX * radius, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
};
this.fillText = function(text, x, y, fontSize, fontName, color)
{
var oldFont = ctx.font;
ctx.font = "" + ajustX * fontSize + "pt " + fontName;
ctx.fillStyle = color;
ctx.fillText(text, ajustX * x, ajustY * y);
ctx.font = oldFont;
};
this.drawImage = function(img, x, y, width, height) {
ctx.drawImage(img, ajustX * x, ajustY * y, ajustX * width, ajustY * height);
};
this.clearRect = function(x, y, width, height)
{
ctx.clearRect(ajustX * x, ajustY * y, ajustX * width, ajustY * height);
};
this.convertClientToCanvasPosition = function(clientX, clientY) {
return {
x: (clientX - offsetX) / ajustX,
y: (clientY - offsetY) / ajustY
};
};
this.convertVirtualToRealPosition = function(x, y) {
return {
x: x / ajustX,
y: y / ajustY
};
};
this.getCanvasRealSize = function(){
return {
width: canvas.width,
height: canvas.height
}
}
function ajustCanvasSize() {
var refRatio = refWidth / refHeight;
var defaultWidth = canvas.width;
var defaultHeight = canvas.height;
var screenRatio = defaultWidth / defaultHeight;
// Resize canvas
if(screenRatio < refRatio)
{
canvas.height = canvas.width / refRatio;
}
else
{
canvas.width = canvas.height * refRatio;
}
// Replace canvas
offsetX = (defaultWidth - canvas.width) / 2;
offsetY = (defaultHeight - canvas.height);
canvas.style.marginLeft = offsetX + "px";
canvas.style.marginTop = offsetY + "px";
}
}