script.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /**
  2. Solitaire for HTML5
  3. tepaanan@gmail.com
  4. */
  5. // Mouse pos
  6. var mousex=0, mousey=0;
  7. // Mouse click and card's upper left corner offset
  8. var xOff=0, yOff=0;
  9. // Mouse offset between moves
  10. var moveXAmount=0;
  11. var moveYAmount=0;
  12. // Card size
  13. var cardHeight=80; //110;
  14. var cardWidth=54; //75;
  15. var cardInitVal=0;
  16. // Canvas
  17. var canvas
  18. var ctx;
  19. var tmpCanvas;
  20. var tmpCtx;
  21. // Is mouse down
  22. var mouseDown=false;
  23. // For cards
  24. var activeCard=undefined;
  25. var cardLoadingIndex=0;
  26. var cardArray = new Array();
  27. var cardNameArray = new Array();
  28. var movingCardsDrawed = true;
  29. var generalGraphicsArray = new Array();
  30. var generalGraphicsLoadingIndex = 0;
  31. // Deck types
  32. var DeckType = {
  33. Foundation:1,
  34. Waste:2,
  35. Stock:3,
  36. Target:4
  37. };
  38. // Card lands
  39. var CardLand = {
  40. Club:1,
  41. Diamond:2,
  42. Heart:3,
  43. Spade:4
  44. };
  45. // For decks
  46. var deck1 = new DeckObject(DeckType.Foundation,0,0);
  47. var deck2 = new DeckObject(DeckType.Foundation,0,0);
  48. var deck3 = new DeckObject(DeckType.Foundation,0,0);
  49. var deck4 = new DeckObject(DeckType.Foundation,0,0);
  50. var deck5 = new DeckObject(DeckType.Foundation,0,0);
  51. var deck6 = new DeckObject(DeckType.Foundation,0,0);
  52. var deck7 = new DeckObject(DeckType.Foundation,0,0);
  53. var stock = new DeckObject(DeckType.Stock,0,0);
  54. var waste = new DeckObject(DeckType.Waste,0,0);
  55. var target1 = new DeckObject(DeckType.Target,0,0);
  56. var target2 = new DeckObject(DeckType.Target,0,0);
  57. var target3 = new DeckObject(DeckType.Target,0,0);
  58. var target4 = new DeckObject(DeckType.Target,0,0);
  59. // Background
  60. var background = new Image();
  61. var cardBackground = new Image();
  62. var deckBackground = new Image();
  63. // Loading graphics -message
  64. var message;
  65. // Window resize event
  66. var resizeTimer = undefined;
  67. var documentWidth = 0;
  68. var documentHeight = 0;
  69. /******************************************************************************
  70. * requestAnimFrame
  71. * http://www.html5canvastutorials.com/labs/html5-canvas-ultimate-flash-killer/
  72. */
  73. window.requestAnimFrame = (function(callback){
  74. return window.requestAnimationFrame ||
  75. window.webkitRequestAnimationFrame ||
  76. window.mozRequestAnimationFrame ||
  77. window.oRequestAnimationFrame ||
  78. window.msRequestAnimationFrame ||
  79. function(callback){
  80. window.setTimeout(callback, 1000 / 60);
  81. };
  82. })();
  83. /******************************************************************************
  84. * Card object
  85. * http://www.javascriptkit.com/javatutors/object2.shtml
  86. */
  87. function CardObject(id, x, y, img){
  88. // Properties
  89. this.id = id;
  90. this.land = 0;
  91. this.deckId = 0;
  92. this.isTurned = false;
  93. this.deckObject = undefined;
  94. this.x = x;
  95. this.y = y;
  96. this.oldX = x;
  97. this.oldY = y;
  98. this.img = img;
  99. this.topCard = undefined;
  100. this.isBlack = false;
  101. // Methods
  102. this.cancel = _CardObject_cancel;
  103. this.move = _CardObject_move;
  104. this.storePos = _CardObject_storePos;
  105. this.drawCard = _CardObject_drawCard;
  106. }
  107. function _CardObject_drawCard(ctx,drawAlsoTopCard) {
  108. // Draw this card
  109. if (this.isTurned) {
  110. ctx.drawImage(this.img,this.x,this.y,cardWidth,cardHeight);
  111. } else {
  112. ctx.drawImage(cardBackground,this.x,this.y,cardWidth,cardHeight);
  113. }
  114. // And also its top cards if needed
  115. if (drawAlsoTopCard && this.topCard) {
  116. this.topCard.drawCard(ctx,drawAlsoTopCard);
  117. }
  118. }
  119. function _CardObject_storePos(x,y) {
  120. this.x = x;
  121. this.y = y;
  122. this.oldX = x;
  123. this.oldY = y;
  124. }
  125. function _CardObject_move(x,y) {
  126. if (this.isTurned) {
  127. this.x = x;
  128. this.y = y;
  129. if (this.topCard) {
  130. this.topCard.move(x, y + cardHeight * 0.2);
  131. }
  132. }
  133. }
  134. function _CardObject_cancel() {
  135. this.x = this.oldX;
  136. this.y = this.oldY;
  137. if (this.topCard) {
  138. this.topCard.cancel();
  139. }
  140. }
  141. /******************************************************************************
  142. * Deck object
  143. */
  144. function DeckObject(id, x, y){
  145. // Properties
  146. this.id = id;
  147. this.x = x;
  148. this.y = y;
  149. this.cardArray = new Array();
  150. // Methods
  151. this.setPos = _DeckObject_setPos;
  152. this.addCard = _DeckObject_addCard;
  153. this.cardUnderMouse = _DeckObject_cardUnderMouse;
  154. this.deckUnderMouse = _DeckObject_deckUnderMouse;
  155. this.drawDeck = _DeckObject_drawDeck;
  156. this.removeTopCard = _DeckObject_removeTopCard;
  157. this.removeFromThisCard = _DeckObject_removeFromThisCard;
  158. }
  159. function _DeckObject_drawDeck(ctx) {
  160. for(var i=0 ; i < this.cardArray.length ; i++) {
  161. var card = this.cardArray[i];
  162. // Do not draw active card and its top cards
  163. if (card!=activeCard) {
  164. card.drawCard(ctx,false);
  165. } else {
  166. return;
  167. }
  168. }
  169. }
  170. function _DeckObject_setPos(x,y){
  171. this.x = x;
  172. this.y = y;
  173. }
  174. function _DeckObject_addCard(card){
  175. var yCap = cardHeight * 0.2;
  176. // Set new position for the card
  177. if (this.id == DeckType.Foundation) {
  178. card.storePos(this.x,this.y + yCap * this.cardArray.length);
  179. } else {
  180. card.storePos(this.x,this.y);
  181. }
  182. // Store new deck data into card
  183. card.deckId = this.id;
  184. card.deckObject = this;
  185. // Set new card as top card of previous top
  186. if(this.cardArray.length > 0) {
  187. this.cardArray[this.cardArray.length-1].topCard = card;
  188. }
  189. // Add new card into deck array
  190. this.cardArray.push(card);
  191. // Add top card into this deck
  192. if (card.topCard) {
  193. this.addCard(card.topCard);
  194. }
  195. }
  196. function _DeckObject_removeTopCard() {
  197. // Get top most card
  198. var card = this.cardArray.pop();
  199. // Reset old top card data
  200. if(this.cardArray.length > 0) {
  201. this.cardArray[this.cardArray.length-1].topCard = undefined;
  202. //this.cardArray[this.cardArray.length-1].isTurned = true;
  203. }
  204. return card;
  205. }
  206. function _DeckObject_removeFromThisCard(card) {
  207. // Remove all card from the deck starting from this card
  208. for(var i=0;i<this.cardArray.length;i++) {
  209. if (card != this.cardArray[this.cardArray.length-1]) {
  210. this.cardArray.pop();
  211. i--;
  212. } else if (card == this.cardArray[this.cardArray.length-1]) {
  213. this.cardArray.pop();
  214. if(this.cardArray.length > 0) {
  215. this.cardArray[this.cardArray.length-1].topCard = undefined;
  216. this.cardArray[this.cardArray.length-1].isTurned = true;
  217. }
  218. return;
  219. }
  220. }
  221. }
  222. function _DeckObject_cardUnderMouse(x,y){
  223. for(var i=this.cardArray.length-1;i>=0;i--) {
  224. var card = this.cardArray[i];
  225. if (card.x < x && card.y < y && card.x+cardWidth > x && card.y+cardHeight > y) {
  226. return card;
  227. }
  228. }
  229. return undefined;
  230. }
  231. function _DeckObject_deckUnderMouse(x,y) {
  232. var yCap = cardHeight * 0.2 * this.cardArray.length;
  233. if (this.id != DeckType.Foundation) {
  234. if (this.x < x && this.y < y && this.x+cardWidth > x && this.y+cardHeight > y) {
  235. return this;
  236. }
  237. } else {
  238. if (this.x < x && this.y < y && this.x+cardWidth > x && this.y+cardHeight+yCap > y) {
  239. return this;
  240. }
  241. }
  242. return undefined;
  243. }
  244. /******************************************************************************
  245. * Solitaire game logic
  246. * Check is the move allowed
  247. */
  248. function isAllowedMove(fromDeck, toDeck, card) {
  249. // Get card that will be under this new card
  250. var topOfCard = undefined;
  251. if (toDeck.cardArray.length > 0)
  252. topOfCard = toDeck.cardArray[toDeck.cardArray.length-1];
  253. // Check deck issues
  254. if (topOfCard && topOfCard.isTurned == false)
  255. return false;
  256. if (fromDeck.id != DeckType.Foundation && fromDeck.id != DeckType.Waste)
  257. return false;
  258. if (toDeck.id != DeckType.Foundation && toDeck.id != DeckType.Target)
  259. return false;
  260. // Card's decks must differ
  261. if (fromDeck == toDeck)
  262. return false;
  263. // Check card's issues
  264. if(toDeck.cardArray.length>0) {
  265. if (toDeck.id == DeckType.Foundation) {
  266. // Card can be top of one step greater card and different color
  267. // Card can not be same color
  268. if (card.land == topOfCard.land || topOfCard.id != card.id + 1 || card.isBlack == topOfCard.isBlack)
  269. return false;
  270. } else if (toDeck.id == DeckType.Target) {
  271. // Cards must be in ascending order and same suite in 2 target deck
  272. if (topOfCard.id + 1 != card.id || topOfCard.land != card.land)
  273. return false;
  274. }
  275. } else {
  276. // Moving top of empty deck
  277. // If there is no cards in the deck, then the first one must be King card in source decks 1
  278. if (toDeck.cardArray.length == 0 && card.id != 13 && toDeck.id == DeckType.Foundation)
  279. return false;
  280. // Ace card must be the first card in foundation
  281. if (toDeck.id == DeckType.Target && toDeck.cardArray.length == 0 && card.id != 1)
  282. return false;
  283. }
  284. return true;
  285. }
  286. /******************************************************************************
  287. * Window resize event handling
  288. */
  289. function resizeEvent(event) {
  290. if(resizeTimer) {
  291. clearTimeout(resizeTimer);
  292. resizeTimer = undefined;
  293. }
  294. //resizeTimer = setTimeout("doResize()",300);
  295. }
  296. function doResize() {
  297. resizeTimer = undefined;
  298. window.location.reload();
  299. }
  300. /******************************************************************************
  301. * Draw fast single card
  302. * Draw only dirty background behind the card
  303. * Draw active moving card on top of all others
  304. */
  305. function drawCardFast(cardToDraw, toContext, toBackground) {
  306. // Draw dirty background behind the card
  307. if (toBackground) {
  308. if (cardToDraw.topCard) {
  309. toContext.drawImage(toBackground,
  310. cardToDraw.x-moveXAmount, 0, cardWidth+moveXAmount*2, canvas.height,
  311. cardToDraw.x-moveXAmount, 0, cardWidth+moveXAmount*2, canvas.height);
  312. } else {
  313. var dirtyx = cardToDraw.x-moveXAmount;
  314. if (dirtyx<=0)
  315. dirtyx = 0;
  316. var dirtyy = cardToDraw.y-moveYAmount;
  317. if (dirtyy<=0)
  318. dirtyy = 0;
  319. toContext.drawImage(toBackground,
  320. dirtyx, dirtyy, cardWidth+moveXAmount*2, cardHeight+moveYAmount*2,
  321. dirtyx, dirtyy, cardWidth+moveXAmount*2, cardHeight+moveYAmount*2 );
  322. }
  323. }
  324. // Draw card and its parent cards
  325. cardToDraw.drawCard(toContext,true);
  326. }
  327. /******************************************************************************
  328. * Copy whole canvas and draw it once
  329. * Starts showing copy of the original canvas
  330. */
  331. function copyCanvasAndDraw() {
  332. // Draw target canvas to ready for copying it
  333. ctx.drawImage(background,0,0,documentWidth,documentHeight);
  334. // Target backgrounds
  335. var capX = (canvas.width - (7 * cardWidth)) / 8;
  336. ctx.drawImage(deckBackground,cardWidth*3+capX*4,capX,cardWidth,cardHeight);
  337. ctx.drawImage(deckBackground,cardWidth*4+capX*5,capX,cardWidth,cardHeight);
  338. ctx.drawImage(deckBackground,cardWidth*5+capX*6,capX,cardWidth,cardHeight);
  339. ctx.drawImage(deckBackground,cardWidth*6+capX*7,capX,cardWidth,cardHeight);
  340. // Draw decks
  341. deck1.drawDeck(ctx);
  342. deck2.drawDeck(ctx);
  343. deck3.drawDeck(ctx);
  344. deck4.drawDeck(ctx);
  345. deck5.drawDeck(ctx);
  346. deck6.drawDeck(ctx);
  347. deck7.drawDeck(ctx);
  348. stock.drawDeck(ctx);
  349. waste.drawDeck(ctx);
  350. target1.drawDeck(ctx);
  351. target2.drawDeck(ctx);
  352. target3.drawDeck(ctx);
  353. target4.drawDeck(ctx);
  354. // Draw target canvas to temporary canvas
  355. tmpCtx.drawImage(canvas, 0, 0);
  356. // And active card on that
  357. if (activeCard) {
  358. activeCard.drawCard(tmpCtx,true);
  359. }
  360. // Change temporary canvas to visible
  361. canvas.style.display="none";
  362. tmpCanvas.style.display="block";
  363. }
  364. /******************************************************************************
  365. * Set original canvas back to visible
  366. */
  367. function resetCanvas() {
  368. canvas.style.display="block";
  369. tmpCanvas.style.display="none";
  370. }
  371. /******************************************************************************
  372. * Draw all
  373. */
  374. function drawAll() {
  375. // Background
  376. ctx.drawImage(background,0,0,documentWidth,documentHeight);
  377. // Target backgrounds
  378. var capX = (canvas.width - (7 * cardWidth)) / 8;
  379. ctx.drawImage(deckBackground,cardWidth*3+capX*4,capX,cardWidth,cardHeight);
  380. ctx.drawImage(deckBackground,cardWidth*4+capX*5,capX,cardWidth,cardHeight);
  381. ctx.drawImage(deckBackground,cardWidth*5+capX*6,capX,cardWidth,cardHeight);
  382. ctx.drawImage(deckBackground,cardWidth*6+capX*7,capX,cardWidth,cardHeight);
  383. // Draw decks
  384. deck1.drawDeck(ctx);
  385. deck2.drawDeck(ctx);
  386. deck3.drawDeck(ctx);
  387. deck4.drawDeck(ctx);
  388. deck5.drawDeck(ctx);
  389. deck6.drawDeck(ctx);
  390. deck7.drawDeck(ctx);
  391. stock.drawDeck(ctx);
  392. waste.drawDeck(ctx);
  393. target1.drawDeck(ctx);
  394. target2.drawDeck(ctx);
  395. target3.drawDeck(ctx);
  396. target4.drawDeck(ctx);
  397. // Draw active card lates to get it on top of all rest
  398. if (activeCard) {
  399. activeCard.drawCard(ctx,true);
  400. }
  401. }
  402. /******************************************************************************
  403. * Initialize general graphics
  404. */
  405. function initGeneralGraphics() {
  406. if (generalGraphicsLoadingIndex==0) {
  407. background.onload = function(){
  408. initGeneralGraphics();
  409. };
  410. background.src = generalGraphicsArray[generalGraphicsLoadingIndex];
  411. } else if (generalGraphicsLoadingIndex==1) {
  412. cardBackground.onload = function(){
  413. initGeneralGraphics();
  414. };
  415. cardBackground.src = generalGraphicsArray[generalGraphicsLoadingIndex];
  416. } else if (generalGraphicsLoadingIndex==2) {
  417. deckBackground.onload = function(){
  418. initGeneralGraphics();
  419. };
  420. deckBackground.src = generalGraphicsArray[generalGraphicsLoadingIndex];
  421. } else {
  422. initCards();
  423. return;
  424. }
  425. generalGraphicsLoadingIndex++;
  426. updateMessage('Downloading general graphics...'+generalGraphicsLoadingIndex + " / 3");
  427. }
  428. /******************************************************************************
  429. * Initialize cards and load card graphics
  430. */
  431. function initCards() {
  432. updateMessage('Loading graphics for cards... '+cardLoadingIndex+' / ' + cardNameArray.length);
  433. if (cardNameArray.length == cardLoadingIndex) {
  434. message.style.display='none';
  435. // All loaded
  436. initDecks();
  437. return;
  438. }
  439. // Create card object
  440. var card = new CardObject(1,0,0,undefined);
  441. card.img = new Image();
  442. var landVal = (cardLoadingIndex+1) / 13;
  443. if (landVal<=1) {
  444. card.land = CardLand.Club;
  445. card.isBlack = true;
  446. if (cardInitVal > 12)
  447. cardInitVal = 0;
  448. cardInitVal++;
  449. } else if (landVal>1 && landVal<=2) {
  450. card.land = CardLand.Diamond;
  451. card.isBlack = false;
  452. if (cardInitVal > 12)
  453. cardInitVal = 0;
  454. cardInitVal++;
  455. } else if (landVal>2 && landVal<=3) {
  456. card.land = CardLand.Heart;
  457. card.isBlack = false;
  458. if (cardInitVal > 12)
  459. cardInitVal = 0;
  460. cardInitVal++;
  461. } else {
  462. card.land = CardLand.Spade;
  463. card.isBlack = true;
  464. if (cardInitVal > 12)
  465. cardInitVal = 0;
  466. cardInitVal++;
  467. }
  468. card.id = cardInitVal;
  469. card.img.onload = function(){
  470. initCards();
  471. };
  472. card.img.src = cardNameArray[cardLoadingIndex];
  473. cardArray.push(card);
  474. cardLoadingIndex++;
  475. }
  476. /******************************************************************************
  477. * Create all game decks
  478. */
  479. function initDecks() {
  480. // Mess cards first
  481. for (var i=0;i<300;i++) {
  482. var randNro1=Math.floor(Math.random()*cardArray.length);
  483. var card1 = cardArray[randNro1];
  484. var randNro2=Math.floor(Math.random()*cardArray.length);
  485. var card2 = cardArray[randNro2];
  486. cardArray[randNro1] = card2;
  487. cardArray[randNro2] = card1;
  488. }
  489. // Deal cards into decks
  490. var capX = (canvas.width - (7 * cardWidth)) / 8;
  491. var yPos = capX * 2 + cardHeight;
  492. var index = 0;
  493. deck1.setPos(capX,yPos);
  494. deck1.addCard(cardArray[index++]);
  495. deck1.cardArray[deck1.cardArray.length-1].isTurned = true;
  496. deck2.setPos(cardWidth*1+capX*2,yPos);
  497. deck2.addCard(cardArray[index++]);
  498. deck2.addCard(cardArray[index++]);
  499. deck2.cardArray[deck2.cardArray.length-1].isTurned = true;
  500. deck3.setPos(cardWidth*2+capX*3,yPos);
  501. deck3.addCard(cardArray[index++]);
  502. deck3.addCard(cardArray[index++]);
  503. deck3.addCard(cardArray[index++]);
  504. deck3.cardArray[deck3.cardArray.length-1].isTurned = true;
  505. deck4.setPos(cardWidth*3+capX*4,yPos);
  506. deck4.addCard(cardArray[index++]);
  507. deck4.addCard(cardArray[index++]);
  508. deck4.addCard(cardArray[index++]);
  509. deck4.addCard(cardArray[index++]);
  510. deck4.cardArray[deck4.cardArray.length-1].isTurned = true;
  511. deck5.setPos(cardWidth*4+capX*5,yPos);
  512. deck5.addCard(cardArray[index++]);
  513. deck5.addCard(cardArray[index++]);
  514. deck5.addCard(cardArray[index++]);
  515. deck5.addCard(cardArray[index++]);
  516. deck5.addCard(cardArray[index++]);
  517. deck5.cardArray[deck5.cardArray.length-1].isTurned = true;
  518. deck6.setPos(cardWidth*5+capX*6,yPos);
  519. deck6.addCard(cardArray[index++]);
  520. deck6.addCard(cardArray[index++]);
  521. deck6.addCard(cardArray[index++]);
  522. deck6.addCard(cardArray[index++]);
  523. deck6.addCard(cardArray[index++]);
  524. deck6.addCard(cardArray[index++]);
  525. deck6.cardArray[deck6.cardArray.length-1].isTurned = true;
  526. deck7.setPos(cardWidth*6+capX*7,yPos);
  527. deck7.addCard(cardArray[index++]);
  528. deck7.addCard(cardArray[index++]);
  529. deck7.addCard(cardArray[index++]);
  530. deck7.addCard(cardArray[index++]);
  531. deck7.addCard(cardArray[index++]);
  532. deck7.addCard(cardArray[index++]);
  533. deck7.addCard(cardArray[index++]);
  534. deck7.cardArray[deck7.cardArray.length-1].isTurned = true;
  535. stock.setPos(capX,capX);
  536. while (index < cardArray.length -1) {
  537. stock.addCard(cardArray[index++]);
  538. }
  539. waste.setPos(cardWidth*1+capX*2,capX);
  540. waste.addCard(cardArray[index++]);
  541. waste.cardArray[waste.cardArray.length-1].isTurned = true;
  542. target1.setPos(cardWidth*3+capX*4,capX);
  543. target2.setPos(cardWidth*4+capX*5,capX);
  544. target3.setPos(cardWidth*5+capX*6,capX);
  545. target4.setPos(cardWidth*6+capX*7,capX);
  546. // All created
  547. drawAll();
  548. }
  549. /******************************************************************************
  550. * Update message to the user
  551. */
  552. function updateMessage(text) {
  553. message.innerHTML = text;
  554. }
  555. /******************************************************************************
  556. * Initialize canvas for drawing and load graphics
  557. */
  558. function init() {
  559. // Document size
  560. documentWidth = document.body.offsetWidth;
  561. documentHeight = document.body.offsetHeight;
  562. // Card size
  563. cardHeight=documentWidth / 7;
  564. cardWidth=cardHeight / 1.46;
  565. // Main canvas for drawing surface
  566. canvas = document.getElementById('deskCanvas');
  567. canvas.width = documentWidth - canvas.offsetLeft*2;
  568. canvas.height = documentHeight - canvas.offsetTop;
  569. canvas.style.display="block";
  570. ctx = document.getElementById('deskCanvas').getContext('2d');
  571. // Add the temporary canvas where main canvas is copied/drawed in mousedown
  572. // Temporary canvas is drawed in mousemove
  573. var container = canvas.parentNode;
  574. tmpCanvas = document.createElement('canvas');
  575. tmpCanvas.style.display="none";
  576. tmpCanvas.id = 'tmpDeskCanvas';
  577. tmpCanvas.width = canvas.width;
  578. tmpCanvas.height = canvas.height;
  579. tmpCanvas.offsetLeft = canvas.offsetLeft;
  580. tmpCanvas.offsetRight = canvas.offsetRight;
  581. container.appendChild(tmpCanvas);
  582. tmpCtx = tmpCanvas.getContext('2d');
  583. // Loading graphics message
  584. message = document.getElementById('message');
  585. // Touch and Mouse events
  586. /*
  587. canvas.addEventListener( 'touchstart', ev_mousedown, false);
  588. canvas.addEventListener( 'touchmove', ev_mousemove, false);
  589. canvas.addEventListener( 'touchend', ev_mouseup, false );
  590. canvas.addEventListener('mouseup', ev_mouseup, false);
  591. tmpCanvas.addEventListener( 'touchstart', ev_mousedown, false);
  592. tmpCanvas.addEventListener('touchmove', ev_mousemove, false);
  593. tmpCanvas.addEventListener('touchend', ev_mouseup, false);
  594. tmpCanvas.addEventListener('mouseup', ev_mouseup, false);
  595. */
  596. canvas.addEventListener('mousedown', ev_mousedown, false);
  597. canvas.addEventListener('mousemove', ev_mousemove, false);
  598. canvas.addEventListener('mouseup', ev_mouseup, false);
  599. tmpCanvas.addEventListener('mousedown', ev_mousedown, false);
  600. tmpCanvas.addEventListener('mousemove', ev_mousemove, false);
  601. tmpCanvas.addEventListener('mouseup', ev_mouseup, false);
  602. // Card names
  603. cardLoadingIndex = 0;
  604. cardNameArray.push('gfx/Club_ace.png');
  605. cardNameArray.push('gfx/Club_2.png');
  606. cardNameArray.push('gfx/Club_3.png');
  607. cardNameArray.push('gfx/Club_4.png');
  608. cardNameArray.push('gfx/Club_5.png');
  609. cardNameArray.push('gfx/Club_6.png');
  610. cardNameArray.push('gfx/Club_7.png');
  611. cardNameArray.push('gfx/Club_8.png');
  612. cardNameArray.push('gfx/Club_9.png');
  613. cardNameArray.push('gfx/Club_10.png');
  614. cardNameArray.push('gfx/Club_jack.png');
  615. cardNameArray.push('gfx/Club_queen.png');
  616. cardNameArray.push('gfx/Club_king.png');
  617. cardNameArray.push('gfx/Diamond_ace.png');
  618. cardNameArray.push('gfx/Diamond_2.png');
  619. cardNameArray.push('gfx/Diamond_3.png');
  620. cardNameArray.push('gfx/Diamond_4.png');
  621. cardNameArray.push('gfx/Diamond_5.png');
  622. cardNameArray.push('gfx/Diamond_6.png');
  623. cardNameArray.push('gfx/Diamond_7.png');
  624. cardNameArray.push('gfx/Diamond_8.png');
  625. cardNameArray.push('gfx/Diamond_9.png');
  626. cardNameArray.push('gfx/Diamond_10.png');
  627. cardNameArray.push('gfx/Diamond_jack.png');
  628. cardNameArray.push('gfx/Diamond_queen.png');
  629. cardNameArray.push('gfx/Diamond_king.png');
  630. cardNameArray.push('gfx/Heart_ace.png');
  631. cardNameArray.push('gfx/Heart_2.png');
  632. cardNameArray.push('gfx/Heart_3.png');
  633. cardNameArray.push('gfx/Heart_4.png');
  634. cardNameArray.push('gfx/Heart_5.png');
  635. cardNameArray.push('gfx/Heart_6.png');
  636. cardNameArray.push('gfx/Heart_7.png');
  637. cardNameArray.push('gfx/Heart_8.png');
  638. cardNameArray.push('gfx/Heart_9.png');
  639. cardNameArray.push('gfx/Heart_10.png');
  640. cardNameArray.push('gfx/Heart_jack.png');
  641. cardNameArray.push('gfx/Heart_queen.png');
  642. cardNameArray.push('gfx/Heart_king.png');
  643. cardNameArray.push('gfx/Spade_ace.png');
  644. cardNameArray.push('gfx/Spade_2.png');
  645. cardNameArray.push('gfx/Spade_3.png');
  646. cardNameArray.push('gfx/Spade_4.png');
  647. cardNameArray.push('gfx/Spade_5.png');
  648. cardNameArray.push('gfx/Spade_6.png');
  649. cardNameArray.push('gfx/Spade_7.png');
  650. cardNameArray.push('gfx/Spade_8.png');
  651. cardNameArray.push('gfx/Spade_9.png');
  652. cardNameArray.push('gfx/Spade_10.png');
  653. cardNameArray.push('gfx/Spade_jack.png');
  654. cardNameArray.push('gfx/Spade_queen.png');
  655. cardNameArray.push('gfx/Spade_king.png');
  656. // General graphics
  657. generalGraphicsLoadingIndex = 0;
  658. generalGraphicsArray.push('gfx/background.png');
  659. generalGraphicsArray.push('gfx/card_background.png');
  660. generalGraphicsArray.push('gfx/deck_background.png');
  661. // Start loading general graphics
  662. initGeneralGraphics();
  663. }
  664. /******************************************************************************
  665. * Find card under mouse coordinates
  666. */
  667. function cardUnderMouse(x,y) {
  668. // Find card under user mouse press
  669. var card = undefined;
  670. if(card = deck1.cardUnderMouse(x,y)) return card;
  671. if(card = deck2.cardUnderMouse(x,y)) return card;
  672. if(card = deck3.cardUnderMouse(x,y)) return card;
  673. if(card = deck4.cardUnderMouse(x,y)) return card;
  674. if(card = deck5.cardUnderMouse(x,y)) return card;
  675. if(card = deck6.cardUnderMouse(x,y)) return card;
  676. if(card = deck7.cardUnderMouse(x,y)) return card;
  677. if(card = waste.cardUnderMouse(x,y)) return card;
  678. if(card = stock.cardUnderMouse(x,y)) return card;
  679. return undefined;
  680. }
  681. /******************************************************************************
  682. * Find deck under mouse coordinates
  683. */
  684. function deckUnderMouse(x,y) {
  685. // Find deck under user mouse press
  686. var deck = undefined;
  687. if(deck = deck1.deckUnderMouse(x,y)) return deck;
  688. if(deck = deck2.deckUnderMouse(x,y)) return deck;
  689. if(deck = deck3.deckUnderMouse(x,y)) return deck;
  690. if(deck = deck4.deckUnderMouse(x,y)) return deck;
  691. if(deck = deck5.deckUnderMouse(x,y)) return deck;
  692. if(deck = deck6.deckUnderMouse(x,y)) return deck;
  693. if(deck = deck7.deckUnderMouse(x,y)) return deck;
  694. if(deck = waste.deckUnderMouse(x,y)) return deck;
  695. if(deck = stock.deckUnderMouse(x,y)) return deck;
  696. if(deck = target1.deckUnderMouse(x,y)) return deck;
  697. if(deck = target2.deckUnderMouse(x,y)) return deck;
  698. if(deck = target3.deckUnderMouse(x,y)) return deck;
  699. if(deck = target4.deckUnderMouse(x,y)) return deck;
  700. return undefined;
  701. }
  702. /******************************************************************************
  703. * Mouse down
  704. */
  705. function ev_mousedown (ev) {
  706. if (mouseDown)
  707. return;
  708. //ev.preventDefault();
  709. mouseDown = true;
  710. //message.style.display='block';
  711. //updateMessage('mousedown');
  712. mousex = ev.clientX - canvas.offsetLeft;
  713. mousey = ev.clientY - canvas.offsetTop;
  714. activeCard = cardUnderMouse(mousex,mousey);
  715. var activeDeck = deckUnderMouse(mousex,mousey);
  716. // User wants more cards from stock to waste
  717. if ((activeCard && activeCard.deckId == DeckType.Stock) ||
  718. (activeDeck && activeDeck.id == DeckType.Stock) ) {
  719. if (activeCard && stock.cardArray.length > 0) {
  720. // Do move from stock to waste
  721. stock.removeTopCard();
  722. activeCard.topCard = undefined;
  723. activeCard.isTurned = true;
  724. waste.addCard(activeCard);
  725. } else {
  726. // Stock is empty, copy card back from waste
  727. var card = waste.removeTopCard();
  728. while(card) {
  729. card.topCard = undefined;
  730. card.isTurned = false;
  731. stock.addCard(card);
  732. card = waste.removeTopCard();
  733. }
  734. }
  735. mouseDown = false;
  736. activeCard = undefined;
  737. return;
  738. }
  739. // User starts moving cards
  740. if (activeCard) {
  741. xOff = mousex - activeCard.x;
  742. yOff = mousey - activeCard.y;
  743. // Copy main canvas and start drawing copy of it
  744. copyCanvasAndDraw();
  745. } else {
  746. mouseDown = false;
  747. }
  748. }
  749. /******************************************************************************
  750. * Mouse move
  751. */
  752. function ev_mousemove (ev) {
  753. //message.style.display='block';
  754. //updateMessage('mousemove');
  755. if (!mouseDown)
  756. return;
  757. if (!activeCard) {
  758. return;
  759. }
  760. //ev.preventDefault();
  761. var newX = ev.clientX - tmpCanvas.offsetLeft;
  762. var newY = ev.clientY - tmpCanvas.offsetTop;
  763. /*
  764. // Is mouse position outside of canvas?
  765. var tmpX = newX - xOff;
  766. var tmpY = newY - yOff;
  767. if (tmpX < 1 || tmpX + cardWidth >= canvas.width ||
  768. tmpY < 1 || tmpY + cardHeight >= canvas.height) {
  769. // MouseUp
  770. mouseDown = false;
  771. // Cancel move
  772. activeCard.cancel();
  773. activeCard = undefined;
  774. // Draw whole main canvas
  775. drawAll();
  776. // Start showing again main canvas
  777. resetCanvas();
  778. return;
  779. }
  780. */
  781. // Move card
  782. if (movingCardsDrawed) {
  783. moveXAmount = Math.abs(mousex - newX) +2;
  784. moveYAmount = Math.abs(mousey - newY) +2;
  785. movingCardsDrawed = false;
  786. } else {
  787. moveXAmount += Math.abs(mousex - newX) +2;
  788. moveYAmount += Math.abs(mousey - newY) +2;
  789. }
  790. mousex = newX;
  791. mousey = newY;
  792. activeCard.move(mousex - xOff, mousey - yOff);
  793. // request new frame
  794. requestAnimFrame(function(){
  795. drawCardFast(activeCard,tmpCtx,canvas);
  796. movingCardsDrawed = true;
  797. });
  798. }
  799. /******************************************************************************
  800. * Mouse up
  801. */
  802. function ev_mouseup (ev) {
  803. mouseDown = false;
  804. //message.style.display='block';
  805. //updateMessage('mouseup');
  806. //ev.preventDefault();
  807. var x = mousex;
  808. var y = mousey;
  809. // Change card into different deck
  810. if (activeCard) {
  811. var activeDeck = deckUnderMouse(x,y);
  812. if (activeDeck) {
  813. if (isAllowedMove(activeCard.deckObject, activeDeck, activeCard)) {
  814. // Remove cards from the original deck
  815. activeCard.deckObject.removeFromThisCard(activeCard);
  816. // Add cards into new deck
  817. activeDeck.addCard(activeCard);
  818. } else {
  819. activeCard.cancel();
  820. }
  821. } else {
  822. activeCard.cancel();
  823. }
  824. }
  825. activeCard = undefined;
  826. // Draw whole main canvas
  827. drawAll();
  828. // Start showing again main canvas
  829. resetCanvas();
  830. movingCardsDrawed = true;
  831. }