This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
78 lines (62 loc) · 1.46 KB
/
canvas.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
/*
* GameIndus - A free online platform to imagine, create and publish your game with ease!
*
* GameIndus old 2d engine
* Copyright (c) 2015-2016 Maxime Malgorn (Utarwyn)
* <https://github.com/GameIndus/engine-2d-old>
*
*/
function Canvas(){
this.canvas;
this.ctx;
this.size = {w: 0, h: 0};
}
Canvas.prototype = {
load: function(id){
if(typeof id === "string")
this.canvas = document.getElementById(id);
else
this.canvas = id;
this.ctx = this.canvas.getContext('2d');
},
getCanvas: function(){
return this.canvas;
},
getContext: function(){
return this.ctx;
},
setSize: function(width, height){
if(this.canvas == null) return false;
if(width == "100%") width = window.innerWidth;
if(height == "100%") height = window.innerHeight-4;
this.size.w = width;
this.size.h = height;
this.canvas.width = this.size.w;
this.canvas.height = this.size.h;
if(window.devicePixelRatio == 2){
this.size.w = width * 2;
this.size.h = height * 2;
this.canvas.width = this.size.w;
this.canvas.height = this.size.h;
}
this.canvas.style.width = this.canvas.width + "px";
this.canvas.style.height = this.canvas.height + "px";
},
getSize: function(){
var size = this.size;
var getSizeObj = {
w: size.w,
h: size.h,
getWidth: function(){
return this.w;
},
getHeight: function(){
return this.h;
}
};
return getSizeObj;
},
clear: function(){
this.ctx.clearRect(0, 0, this.size.w, this.size.h);
}
};