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
function _komabako(div, view) { var ctx = null; var quality = view.get_quality(); var canvas = document.createElement("canvas"); canvas.width = _val.KOMABAKO_W * quality; canvas.height = _val.KOMABAKO_H * quality; div.appendChild(canvas); ctx = canvas.getContext("2d"); if (ctx == null) throw new Error(_err.BROWSER_NOT_SUPPORTED); const hako_left = (_val.KOMABAKO_W - 8 * _val.KOMA_L) / 2; const hako_right = (_val.KOMABAKO_W + 8 * _val.KOMA_L) / 2; const hako_top = (_val.KOMABAKO_H - 4 * _val.KOMA_L) / 2; const hako_bottom = (_val.KOMABAKO_H + 4 * _val.KOMA_L) / 2; var hako = [0, 0, 0, 0, 0, 0, 0, 0]; ctx.font = _str.KOMA_FONT; ctx.textAlign = "center"; ctx.textBaseline = "middle"; this.set_hako = function(a) { hako = a; } function set_transform(scale_x, scale_y, x, y) { scale_x *= quality; scale_y *= quality; x *= quality; y *= quality; ctx.setTransform(scale_x, 0, 0, scale_y, x, y); } var pick = view.get_pick(); function draw() { if (ctx == null) return; if (pick.after != -1) { // 駒が動かされた if (pick.after == 82) add_koma(pick.koma); else if (pick.before == 82) sub_koma(pick.koma); pick.before = -1; } var b_hanten = view.get_hanten(); // ビューのクリア set_transform(1, 1, 0, 0); ctx.clearRect(0, 0, _val.KOMABAKO_W, _val.KOMABAKO_H); // 駒箱の描画 ctx.fillStyle = _color.KOMADAI; ctx.fillRect(hako_top, hako_left, 8 * _val.KOMA_L, 4 * _val.KOMA_L); // 駒の描画 var pick_koma = (pick.before != 82) ? -1 : (b_hanten) ? pick.koma ^ 16 : pick.koma; var kama; var moji; var x, y; var sign; ctx.fillStyle = _color.KOMA; for (koma = 0; koma < 32; koma++) { // 味方の歩から相手の龍まで if (hako[koma % 8] == 0) continue; moji = _koma.MOJI[koma % 16]; if (moji == "") continue; // 成れない駒 if (koma % 16 == _koma.Ou) // 玉 if ((hako[_koma.Ou] & ((koma < 16 != b_hanten) ? 0x1 : 0x2)) == 0) continue; x = koma % 8 * _val.KOMA_L + hako_left + _val.KOMA_L / 2; y = Math.floor(koma / 8) * _val.KOMA_L + hako_top + _val.KOMA_L / 2; if (koma == pick_koma) { set_transform(1, 1, x - _val.KOMA_L / 2, y - _val.KOMA_L / 2); ctx.fillStyle = _color.PICK; ctx.fillRect(0, 0, _val.KOMA_L, _val.KOMA_L); ctx.fillStyle = _color.KOMA; } sign = koma < 16 ? 1 : -1; if (moji.length == 2) { // 2文字の駒 set_transform(sign, 0.5 * sign, x, y); ctx.fillText(moji.charAt(0), 0, -0.5 * _val.KOMA_L); ctx.fillText(moji.charAt(1), 0, 0.5 * _val.KOMA_L); } else if (moji.length == 1) { // 1文字の駒 set_transform(sign, sign, x, y); ctx.fillText(moji, 0, 0); } } } this.draw = draw; function add_koma(koma) { if (koma % 8 == _koma.Ou) // 玉 hako[_koma.Ou] |= (koma < 16) ? 0x1 : 0x2; else hako[koma % 8]++; } function sub_koma(koma) { if (koma % 8 == _koma.Ou) // 玉 hako[_koma.Ou] &= (koma < 16) ? 0x2 : 0x1; else hako[koma % 8]--; } _util.touched(canvas, function(x, y) { if (pick.before == -1) { // 駒を掴んでいない koma = get_koma(x, y); if (koma != -1) { pick.koma = koma; pick.before = 82; draw(); } return; } // 駒を掴んでいる if (pick.before == 82) { // 駒箱の駒 koma = get_koma(x, y); if (koma != -1) { pick.koma = koma; pick.before = 82; } draw(); return; } pick.after = 82; view.clean_and_draw(); draw(); }); function get_koma(x, y) { var b_hanten = view.get_hanten(); if (x < hako_left || x >= hako_right || y < hako_top || y >= hako_bottom) return -1; var koma = Math.floor((x - hako_left) / _val.KOMA_L) + 8 * Math.floor((y - hako_top) / _val.KOMA_L); if (hako[koma % 8] == 0) // 駒がない return -1; if (_koma.MOJI[koma % 16] == "") // 成れない駒 return -1; if (b_hanten) koma ^= 16; if (koma % 8 == _koma.Ou) // 玉 if ((hako[_koma.Ou] & (koma < 16 ? 0x1 : 0x2)) == 0) return -1; return koma; } }