-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgloader.js
118 lines (111 loc) · 3.28 KB
/
imgloader.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
118
// this will load the 4 svg data uris
// it will return a single promise using promise.all
function loadAllSVG() {
// a will be an array of promise objects
let a = [];
a.push(new Promise(function(res,rej){
let img = document.createElement("img");
img.src = clubsData64;
img.onload = function() {
res({t:"clubs", img:img});;
}
img.onerror = function(e) {
rej("could not load img clubs"+e);
}
}));
a.push(new Promise(function(res,rej){
let img = document.createElement("img");
img.src = spadesData64;
img.onload = function() {
res({t:"spades",img:img});
}
img.onerror = function() {
rej("could not load img clubs");
}
}));
a.push(new Promise(function(res,rej){
let img = document.createElement("img");
img.src = diamondsData64;
img.onload = function() {
res({t:"diamonds",img:img});
}
img.onerror = function() {
rej("could not load img clubs");
}
}));
a.push(new Promise(function(res,rej){
let img = document.createElement("img");
img.src = heartsData64;
img.onload = function() {
res({t:"hearts",img:img});
}
img.onerror = function() {
rej("could not load img clubs");
}
}));
return Promise.all(a);
}
function loadMemoryImage(index,rank,suit) {
let c = document.createElement("canvas");
let W = c.width;
let H = c.height;
let ctx = c.getContext("2d");
ctx.clearRect(0,0,W,H);
ctx.font="20px Georgia";
if ((suit==0)|(suit==1)) {
ctx.fill = "black";
roundRect(ctx,0,0,50,70);
} else if ((suit==2)||(suit==3)) {
ctx.fillStyle="red"; // changes font not image
roundRect(ctx,0,0,50,70);
} else if (suit==-1) {
ctx.fillStyle = "blue"; // card back
roundRect(ctx,0,0,50,70,5,"blue");
}
if (rank==BACK) {
} else {
ctx.fillText(rank,5,55);
}
if (suit==-1) {
// nothing yet
} else {
ctx.drawImage(game.allImages[suit].img,10,10);
}
let data = c.toDataURL();
return new Promise(function(res,rej){
let i = document.createElement("img");
i.src=data;
i.onload = function() {
game.gCardImages[index]=i;
res(i);
}
i.onerror = function() {
rej("unable to load image");
}
});
}
// this will generate all the card images used in the game
// and the card back
// sad blue
function generateCardImages() {
let c = document.createElement("canvas");
let W = c.width;
let H = c.height;
let ctx = c.getContext("2d");
let ranks = ["A","2","3","4","5","6","7","8","9",
"T","J","Q","K"];
// will anyone besides me even look at this source code?
// do you get the naked eyes references?
//
let allOfYourPromises = [];
let counter = 0;
for (let j=0;j<4;j++) {
for(let i=0;i<ranks.length;i++) {
allOfYourPromises.push(loadMemoryImage(counter,ranks[i],j));
counter++;
}
}
// hack to get a card back
allOfYourPromises.push(loadMemoryImage(counter,BACK,BACK));
return Promise.all(allOfYourPromises);
}