The goal is to draw random tarot cards from the suit, number of cards is determined by the amount of HTML elements with 'card-container' class on given page.
The deck is structured as follows:
- major arcana (trumps), numbered from 0 to 21
- minor arcana have 4 suits, each numbered 1 to 14.
The result cannot contain duplicates.
After the cards are drawed the script retrieves card description form an external HTML file and replaces img src to according img path.
I also draw background image on canvas as an additional functionality.
The script does what it's supposed to do (at least that's it seems so); I would appreciate any suggestions on its structure, efficiency and how can I generally improve it.
var suits = ['cups', 'disks', 'swords', 'wands', 'trumps'];
var minor_names = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'princess', 'prince', 'queen', 'knight'];
var major_names = ['The Fool', ' The Magician', 'The Priestess', 'The Empress', 'The Emperor', 'The Hierophant', ' The Lovers', 'The Chariot', 'Adjustment', 'The Hermit', 'Fortune', 'Lust', 'The Hanged Man', 'Death', 'Art', 'The Devil', 'The Tower', 'The Star', 'The Moon', 'The Sun', 'The Aeon', 'The Universe'];
var picked = [];
var img_src;
//console.log(suits);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function card_name(suit, card_no) {
var name = minor_names[card_no - 1];
if (suit === 'trumps') {
name = major_names[card_no];
}
}
function draw_cards() {
var suit = suits[Math.round(Math.random() * (suits.length - 1))];
var min = 1;
var max = 14;
if (suit === 'trumps') {
min = 0;
max = 21;
}
var card_no = getRandomInt(min, max);
card_name(suit, card_no);
if (card_no < 10) {
card_no = '0' + card_no;
}
var set_name = suit + '-' + card_no;
picked.push(set_name);
}
function draw(list) {
$('.card-container').each(function() {
draw_cards();
});
for (i = 0; i < picked.length; i++) {
for (j = 0; j < picked.length; j++) {
if (i != j) {
if (picked[i] == picked[j]) {
picked.splice(i, 1);
draw_cards();
}
}
}
}
$('body').addClass('show-cards');
}
function uncover(list) {
var a = 0;
$('.card-container').each(function(i) {
var item = list[a];
$(this).find('article').load( 'content/' + item + '.html');
img_src = 'cards/'+ item + '.jpg';
$(this).find('.front img').attr('src', img_src);
a++;
var li = $(this);
setTimeout(function() {
li.addClass('active');
}, i*500); // delay 500 ms
});
var img = new Image();
img.onload = function() {
var ctx = document.getElementById('ctx').getContext('2d');
ctx.drawImage(img, 0, 0, 400, 400 * img.height / img.width);
};
img.src = img_src;
}
$('.draw').click(function() {
picked.length = 0;
$(this).remove();
$('.show').fadeIn();
draw();
});
$('.show').click(function() {
$(this).remove();
uncover(picked);
});