var _Project_Country = ""; // ========================================================== // 購物後遊戲跳窗 // ========================================================== ;(function($) { $('body').on('click', '.game-model, .game-model-closer', function(e) { if(e.currentTarget === e.target) { $('.game-model').fadeOut(400) } }) })($) // ========================================================== // 抽紅包跳窗 // ========================================================== ;(function(jQuery, $, window, document) { var resArray function startGame(){ $.ajax({ url: _Project_Country + '/game/scratch/data/ajax_get_gift-p.php', dataType: 'json', async: false, method: 'POST', data: { Act_Game_ID: $("#Act_Game_ID").val() } }).done(function(res) { //console.log(res) resArray = res switch(res.status){ case "OK": $(".scratch-canva").css('background-image', 'url("'+res.path+'")'); if(res.DataLayer){ eval(res.DataLayer) } break; case "TODAY_PLAYED": alert(_jsLang.今天已玩過遊戲了); $(".scratch-canva").css('background-image', 'url("/game/scratch/images/gift/played.png")'); $(".game-model-closer").trigger("click"); break; case "ACT_PLAYED": alert(_jsLang.已參加過該活動); $(".game-model-closer").trigger("click"); break; case "MEMBER_NOT_LOGIN": alert(_jsLang.須登入會員才可遊戲); window.location = _Project_Country + '/member-login/'; break; case "ORDER_NOT_FULL": alert(_jsLang.訂單金額不符合遊戲條件); $(".game-model-closer").trigger("click"); break; case "GAME_OVER": case "ERROR": alert(_jsLang.遊戲已結束); $(".game-model-closer").trigger("click"); break; } }).fail(function(x, y, z) { //console.log(x) //console.log(y) //console.log(z) }) } let modelStep = 0 $(window).on("load resize", function(){ $(document).off("mousedown mousemove mouseup touchstart touchmove touchend") var canva = $(".scratch-canva"), ctx = canva[0].getContext("2d") var canvaOffset = { "offsetX":canva.offset().left, "offsetY":canva.offset().top } var drawwing = false //不設定固定寬高會有問題 canva.prop("width",canva.width()) canva.prop("height",canva.height()) ctx.drawImage($(".scratch-cover")[0],0,0,canva.width(),canva.height()) ctx.globalCompositeOperation = "destination-out" ctx.lineWidth = ($(window).width()> 767)?"50":"60" ctx.lineCap = "round" ctx.beginPath() $(document).on("mousedown touchstart", ".scratch-main", function(){ if($(".scratch-tutorial").css('display') != 'none'){ startGame(); } $(".scratch-tutorial").fadeOut(); }) $(document).on("mousedown", ".scratch-canva", function(e){ drawwing = true ctx.beginPath() ctx.moveTo(e.offsetX, e.offsetY) }) $(document).on("mousemove", ".scratch-canva", function(e){ if(drawwing){ ctx.lineTo(e.offsetX, e.offsetY) ctx.stroke() } }) $(document).on("mouseup", ".scratch-canva", function(e){ drawwing = false checkCanvaScratched(ctx,canva,resArray) }) $(document).on("touchstart", ".scratch-canva", function(e){ drawwing = true ctx.moveTo(e.originalEvent.touches[0].pageX - canvaOffset.offsetX, e.originalEvent.touches[0].pageY - canvaOffset.offsetY) }) $(document).on("touchmove", ".scratch-canva", function(e){ if(drawwing){ ctx.lineTo(e.originalEvent.touches[0].pageX - canvaOffset.offsetX, e.originalEvent.touches[0].pageY - canvaOffset.offsetY) ctx.stroke() } }) $(document).on("touchend", ".scratch-canva", function(e){ drawwing = false checkCanvaScratched(ctx,canva,resArray) }) }) $('body').on('click', '.game-model-closer', function(e) { modelStep = 0 modelCheck(modelStep) $(this).parents('.gmodel-wrapper').remove() }) })(jQuery, $, window, document) function checkCanvaScratched(ctx,canva,resArray){ let imageData = ctx.getImageData(0,0,canva.width(),canva.height()).data let conter = 0 for(let i = 0; i < imageData.length; i++){ if(imageData[i] == 0){ conter++ } } //檢查被刮除的量大於45%執行 if(conter >= imageData.length * 0.45){ if(resArray.status == "TODAY_PLAYED"){ resArray.url = "/member-shopgold/" resArray.path = "/game/scratch/images/gift/played.png" } $('body').append( `
` ) } } function modelCheck(step) { if(step) { $('body, #game-wrapper').addClass('theme-stock') } else { $('body, #game-wrapper').removeClass('theme-stock') } }