forked from bandochecard/bandochecard.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardMaker.js
More file actions
141 lines (131 loc) · 3.95 KB
/
cardMaker.js
File metadata and controls
141 lines (131 loc) · 3.95 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var images = {
'layout': 'images/card_layout.png',
'icons': 'images/card_icons.png',
'bandoche': ['images/bandoche.jpg','images/bandoche1.jpg','images/bandoche2.jpg','images/bandoche3.jpg'
,'images/bandoche4.jpg','images/bandoche5.jpg','images/bandoche6.jpg','images/bandoche7.jpg','images/bandoche8.jpg']
};
var defaultValues = {
ko:{
title:'단결정 규소',
subtitle:'광물사족/효과',
description:'이 카드를 패에 가지고 있으면 기분이 이상하게 좋아집니다.'
},
en:{
title:'Silicon',
subtitle:'Stone/Effects',
description:'Nobody knows why but it makes you feel better since you have got this card in your deck.'
},
jp:{
title:'シリコーン',
subtitle:'鉱物/効果',
description:'このカードを手に持っていると気分が変によくなります。'
}
};
function hash(v){
var h = 0, len=v.length;
for(var i=0;i<len;i++){
h = 65599 * h + v.charCodeAt(i);
h %= 2147483647;
}
return Math.abs(h ^ (h>>16));
}
function getImage(arg, cb) {
if( typeof arg === 'string') arg = [arg];
for(var i=0;i<arg.length;i++){
var uri = arg[i];
(function(){
var img = new Image();
img.src = uri;
img.addEventListener('load', function () {
cb(img);
});
})();
}
}
function Card(container) {
var lang = document.querySelector('html').getAttribute('lang') || 'ko';
var defaults = defaultValues[lang] || defaultValues['ko'];
var prs = [];
var num = 2 + images.bandoche.length;
this.loaded = false;
this.canvas = document.createElement('canvas');
this.canvas.width = 420;
this.canvas.height = 609;
this.container = container;
this.container.appendChild(this.canvas);
for(var i in defaults){
if( !defaults.hasOwnProperty(i) ) continue;
this[i] = defaults[i];
}
this.bandoche = [];
var that = this;
getImage(images.layout, function (img) {
that.layout = img;
num--;
if (num <= 0) that.loaded = true;
});
getImage(images.icons, function (img) {
that.icons = img;
num--;
if (num <= 0) that.loaded = true;
});
getImage(images.bandoche, function (img) {
that.bandoche.push(img);
num--;
if (num <= 0) that.loaded = true;
});
}
Card.prototype = {
render:function() {
if (!this.loaded) {
setTimeout(this.render.bind(this), 500);
return;
}
var hashed = hash(this.title + this.description + this.subtitle);
var ctx = this.canvas.getContext('2d');
ctx.drawImage(this.bandoche[ hashed % this.bandoche.length ], 40, 110, 340, 340);
ctx.drawImage(this.layout, 0, 0);
// draw title 40,35
ctx.save();
ctx.font = 'bold 27px serif';
ctx.textBaseline = 'top';
ctx.fillStyle = 'black';
ctx.fillText(this.title, 40, 35);
ctx.restore();
// draw type icon 340 40
var type = hashed % 6;
ctx.save();
ctx.drawImage(this.icons, type * 192 + 30, 41, 192, 192, 340, 25, 50, 50);
ctx.restore();
// draw subtitle 42 465
ctx.save();
ctx.font = 'bold 16px serif';
ctx.fillText('[' + this.subtitle + ']', 42, 475);
ctx.restore();
// draw desc 46, 490 / w330,h50
ctx.save();
ctx.textBaseline = 'top';
ctx.fillStyle = 'black';
ctx.font = '14px serif';
var w = 0, cW = 0, line = 0;
for (var ch = 0; ch < this.description.length; ch++) {
cW = ctx.measureText(this.description[ch]).width;
if (cW + w > 330) {
w = 0;
line++;
}
ctx.fillText(this.description[ch], 45 + w, 490 + (line * 16));
w += cW;
}
ctx.restore();
// draw atk 260 555, def 343, 555
var atk = hashed % 10000, def = hashed % 10000;
ctx.save();
ctx.textBaseline = 'top';
ctx.fillStyle = 'black';
ctx.font = 'bold 14px serif';
ctx.fillText(atk, 260, 555);
ctx.fillText(def, 344, 555);
ctx.restore();
}
}