Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /**
*@classdesc Controler of paired items.
* @class
*/
export class MemoryPairsList extends Array {
/**
* Apply visual changes on paired elements.
* @param {HTMLCanvasElement} canvas Set a pair as found.
*/
checkMark(canvas) {
const ctx = canvas.getContext('2d');
const w = canvas.offsetWidth;
const h = canvas.offsetHeight;
const img = this.getImage(canvas);
ctx.clearRect(0, 0, w, h);
ctx.globalAlpha = 0.25;
ctx.drawImage(
img,
0,
0,
parseInt(img.width),
parseInt(img.height),
0,
0,
w,
h
);
ctx.globalAlpha = 1;
ctx.save();
}
/**
*
* @param {HTMLCanvasElement} canvas Canvas element to be checked.
* @return {boolean} If given canvas has already a known pair.
*/
hasPair(canvas) {
const self = this;
let hasPair = false;
self.forEach((c) => {
if (c[0] === canvas) {
hasPair = true;
}
if (c[1] === canvas) {
hasPair = true;
}
});
return hasPair;
}
/**
*
* @param {Array} state Holds info object for both open cards.
* @return {boolean} Are both canvas in given state pair or not.
*/
arePairs(state) {
const one = state[0];
const two = state[1];
const self = this;
let matches = false;
self.forEach((c) => {
if (c[0] === one || c[1] === one) {
if (c[0] === two || c[1] === two) {
matches = true;
this.checkMark(one);
this.checkMark(two);
}
}
});
return matches;
}
/**
* find image of given canvas.
* @param {HTMLCanvasElement} canvas image of the given canvas.
* @return {HTMLImageElement} Image for the given canvas.
*/
getImage(canvas) {
let img;
this.forEach((c) => {
if (c[0] === canvas || c[1] === canvas) {
img = c[2];
}
});
return img;
}
}
|