forked from henshmi/Classic-Pool-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas2D.js
executable file
·117 lines (98 loc) · 4.22 KB
/
Canvas2D.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
107
108
109
110
111
112
113
114
115
116
117
"use strict";
function Canvas2D_Singleton() {
this._canvas = null;
this._canvasContext = null;
this._canvasOffset = Vector2.zero;
}
Object.defineProperty(Canvas2D_Singleton.prototype, "offset",
{
get: function () {
return this._canvasOffset;
}
});
Object.defineProperty(Canvas2D_Singleton.prototype, "scale",
{
get: function () {
return new Vector2(this._canvas.width / Game.size.x,
this._canvas.height / Game.size.y);
}
});
Canvas2D_Singleton.prototype.initialize = function (divName, canvasName) {
this._canvas = document.getElementById(canvasName);
this._div = document.getElementById(divName);
if (this._canvas.getContext)
this._canvasContext = this._canvas.getContext('2d');
else {
alert('Your browser is not HTML5 compatible.!');
return;
}
window.onresize = Canvas2D_Singleton.prototype.resize;
this.resize();
};
Canvas2D_Singleton.prototype.clear = function () {
this._canvasContext.clearRect(0, 0, this._canvas.width, this._canvas.height);
};
Canvas2D_Singleton.prototype.resize = function () {
var gameCanvas = Canvas2D._canvas;
var gameArea = Canvas2D._div;
var widthToHeight = Game.size.x / Game.size.y;
var newWidth = window.innerWidth;
var newHeight = window.innerHeight;
var newWidthToHeight = newWidth / newHeight;
if (newWidthToHeight > widthToHeight) {
newWidth = newHeight * widthToHeight;
} else {
newHeight = newWidth / widthToHeight;
}
gameArea.style.width = newWidth + 'px';
gameArea.style.height = newHeight + 'px';
gameArea.style.marginTop = (window.innerHeight - newHeight) / 2 + 'px';
gameArea.style.marginLeft = (window.innerWidth - newWidth) / 2 + 'px';
gameArea.style.marginBottom = (window.innerHeight - newHeight) / 2 + 'px';
gameArea.style.marginRight = (window.innerWidth - newWidth) / 2 + 'px';
gameCanvas.width = newWidth;
gameCanvas.height = newHeight;
var offset = Vector2.zero;
if (gameCanvas.offsetParent) {
do {
offset.x += gameCanvas.offsetLeft;
offset.y += gameCanvas.offsetTop;
} while ((gameCanvas = gameCanvas.offsetParent));
}
Canvas2D._canvasOffset = offset;
};
Canvas2D_Singleton.prototype.drawImage = function (sprite, position, rotation, scale, origin) {
var canvasScale = this.scale;
position = typeof position !== 'undefined' ? position : Vector2.zero;
rotation = typeof rotation !== 'undefined' ? rotation : 0;
scale = typeof scale !== 'undefined' ? scale : 1;
origin = typeof origin !== 'undefined' ? origin : Vector2.zero;
this._canvasContext.save();
this._canvasContext.scale(canvasScale.x, canvasScale.y);
this._canvasContext.translate(position.x, position.y);
this._canvasContext.rotate(rotation);
this._canvasContext.drawImage(sprite, 0, 0,
sprite.width, sprite.height,
-origin.x * scale, -origin.y * scale,
sprite.width * scale, sprite.height * scale);
this._canvasContext.restore();
};
Canvas2D_Singleton.prototype.drawText = function (text, position, origin, color, textAlign, fontname, fontsize) {
var canvasScale = this.scale;
position = typeof position !== 'undefined' ? position : Vector2.zero;
origin = typeof origin !== 'undefined' ? origin : Vector2.zero;
color = typeof color !== 'undefined' ? color : Color.black;
textAlign = typeof textAlign !== 'undefined' ? textAlign : "top";
fontname = typeof fontname !== 'undefined' ? fontname : "Courier New";
fontsize = typeof fontsize !== 'undefined' ? fontsize : "20px";
this._canvasContext.save();
this._canvasContext.scale(canvasScale.x, canvasScale.y);
this._canvasContext.translate(position.x - origin.x, position.y - origin.y);
this._canvasContext.textBaseline = 'top';
this._canvasContext.font = fontsize + " " + fontname;
this._canvasContext.fillStyle = color.toString();
this._canvasContext.textAlign = textAlign;
this._canvasContext.fillText(text, 0, 0);
this._canvasContext.restore();
};
var Canvas2D = new Canvas2D_Singleton();