-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasCoords.js
68 lines (59 loc) · 1.77 KB
/
canvasCoords.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
// Set the standard ratio to full HD.
const DefaultWidth = 1900;
const DefaultHeight = 1000;
var ratio = DefaultWidth / DefaultHeight;
// Create a canvas
var canvas = document.createElement("canvas");
if (window.innerHeight * ratio < window.innerWidth) {
canvas.width = window.innerHeight * ratio;
canvas.height = window.innerHeight;
}
else {
canvas.height = window.innerWidth / ratio;
canvas.width = window.innerWidth;
}
// Scales the images accordingly.
function GetCoords(x, y) {
//var width = window.innerWidth;
//var height = window.innerHeight;
//if(height*ratio < width)
//{
// width = height * ratio;
//}
//else
//{
// height = width / ratio;
//}
var out = new Object();
out.x = (canvas.width * (x / DefaultWidth));
out.y = (canvas.height * (y / DefaultHeight));
return out;
}
function GetCoordsBox(box) {
var out = GetCoords(box.x, box.y);
var tmp = GetCoords(box.w, box.h);
out.w = tmp.x;
out.h = tmp.y;
return out;
}
// Draw an image without clickability
function DrawScaledPos(context, img, box) {
var coord = GetCoordsBox(box);
context.drawImage(img, coord.x, coord.y, coord.w, coord.h);
}
function DrawScaledText(context, text, x, y, size, align) {
// 0.8 is a fix to make it look good.
var coord = GetCoords(x, y+size);
var coordSize = GetCoords(size,0);
context.font = "" + (coordSize.x).toFixed(0) + "px Arial";
context.textAlign = align;
var subst = text;
var n = subst.indexOf("\n");
while (n > -1) {
context.fillText(subst.substr(0, n), coord.x, coord.y);
subst = subst.substr(n+1, subst.length-1);
coord.y += coordSize.x*1.2;
n = subst.indexOf("\n");
}
context.fillText(subst, coord.x, coord.y);
}