PAT (Advanced Level) 1007 Maximum Subsequence Sum(最大連續子序和)
阿新 • • 發佈:2021-08-22
寫這個用到了html2canvas.js這個外掛,很簡單大家可以到官網看一下,秒懂的呢種:http://html2canvas.hertzen.com/
就不寫demo介紹了 ,程式碼中註釋還是非常詳細的!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>my拼圖</title> <style> * { box-sizing: border-box; } html, body, #container { width: 100%; height: 100%; margin: 0; padding: 0; background-image: url(./bg.jpg); background-size: 100% auto; overflow: hidden; } .btn { margin-top: 1rem; text-align: center; border: 1px solid #fff; font-size: 0.5rem; } .puzzleBox { position: absolute; top: 4.5rem; left: 0; right: 0; width: 7.2rem; height: 7.2rem; margin: auto; background-image: url(./bg.jpg); background-size: 7.5rem auto; background-position: center -4.5rem; } .puzzleBox .item { position: absolute; top: 0; left: 0; width: 2.4rem; height: 2.4rem; line-height: 2.1rem; border: 1px solid #fff; display: flex; align-items: center; justify-content: center; text-align: center; color: aqua; z-index: 10; background-size: 7.2rem 7.2rem; } .puzzleBox .item:nth-child(1){background-position: 240px 240px;} .puzzleBox .item:nth-child(2){background-position: 0px 0px;} .puzzleBox .item:nth-child(3){background-position: 240px 0px;} .puzzleBox .item:nth-child(4){background-position: 120px 0px;} .puzzleBox .item:nth-child(5){background-position: 0px 240px;} .puzzleBox .item:nth-child(6){background-position: 240px 240px;} .puzzleBox .item:nth-child(7){background-position: 120px 240px;} .puzzleBox .item:nth-child(8){background-position: 0px 120px;} .puzzleBox .item:nth-child(9){background-position: 240px 120px;} .puzzleBox .remove { background-color: #000; display: none; } </style> </head> <body base-width='750' base-height='1334'> <div id="container"> <div class="btn">生成拼圖</div> <div class="puzzleBox"> <div class="item remove"></div> </div> </div> <aside class="loadBox"> <span><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i></span> </aside> <script src="js/jquery.js"></script> <script src="js/html2canvas.min.js"></script> <script src="js/rem.js"></script> <script> var puzzleBox = $('.puzzleBox'); var base64=''; // 九個方塊正確位置陣列 var leftTopArr = [ {"left":'240px',"top":'240px'}, {"left":'0px',"top":'0px'}, {"left":'120px',"top":'0px'}, {"left":'240px',"top":'0px'}, {"left":'0px',"top":'120px'}, {"left":'120px',"top":'120px'}, {"left":'240px',"top":'120px'}, {"left":'0px',"top":'240px'}, {"left":'120px',"top":'240px'} ]; init(); function init() { $('.btn').off().on('click', html2Canvas); } // 使用html2canvas外掛生成截圖 function html2Canvas() { if (!base64) { html2canvas(document.querySelector(".puzzleBox"), { logging: false }).then(function (canvas) { base64 = canvas.toDataURL("image/jpeg", 1); $('.remove').show(); CreateClip(); setTimeout(()=>{ leftTopArrMath();//打亂陣列 },10) }); } } // 迴圈生成九宮格方塊 function CreateClip(){ for(var i=0;i<leftTopArr.length-1;i++){ var itemi = i+1; var itemHtml = '<div class="item" >'+itemi+'</div>'; puzzleBox.append(itemHtml); $('.item').eq(itemi).css({"background-image":'url('+base64+')'}) } // console.log(base64) $('.puzzleBox .item').on('click',itemMove); } // 移動黑方塊 function itemMove(){ // 點選方塊的位置 var thisLeft = $(this).offset().left; var thisTop = $(this).offset().top; // 黑色方塊的位置 var thisLeftBlack = $('.remove').offset().left; var thisTopBlack = $('.remove').offset().top; // 點選方塊與黑色方塊的差值用於下邊判斷 var topdiffer = thisTop - thisTopBlack; var leftdiffer = thisLeft - thisLeftBlack; // 判斷是否挨著黑色方塊 if((thisLeft == thisLeftBlack && (topdiffer== 120 || topdiffer== -120)) || (thisTop == thisTopBlack && (leftdiffer== 120 || leftdiffer==-120))){ var oldLeft = $(this).css('left'); var oldTop = $(this).css('top'); var newLeft = $('.remove').css('left'); var newTop = $('.remove').css('top'); $(this).css({'left':newLeft,'top':newTop}); $('.remove').css({'left':oldLeft,'top':oldTop}); setTimeout(()=>{ success(); },1) } } // 拼圖成功 function success(){ var alertArr = []; var alertNum = 0; for(var i=0;i<leftTopArr.length;i++){ // 通過迴圈獲取到每個方塊的left與top值 並將其新增到陣列中 // 通過判斷拼好的的陣列值與正在拼的陣列值相比較,判斷是否拼成功 var itemLeft = puzzleBox.children('.item').eq(i).css('left'); var itemTop = puzzleBox.children('.item').eq(i).css('top'); alertArr.push({'left':itemLeft,'top':itemTop}); if(alertArr[i].left == leftTopArr[i].left && alertArr[i].top == leftTopArr[i].top){ alertNum++; if(alertNum == 9){//當陣列中九個方塊位置全對 的時候 提示拼圖成功 alert("拼圖成功"); } } } } // 打亂陣列 function leftTopArrMath(){ const leftTopArrMath = leftTopArr.concat([]);//注意,這裡用了深拷貝,如果淺拷貝再打亂陣列的話,原來陣列也會發生改變 for (var i = leftTopArrMath.length - 1; i > 1; i--) { var j = Math.floor(Math.random() * (i + 1)); var tmp = leftTopArrMath[j]; leftTopArrMath[j] = leftTopArrMath[i]; leftTopArrMath[i] = tmp; } // 並把打亂的陣列值傳給方塊 for(var i=0;i<leftTopArrMath.length;i++){ $('.item').eq(i).css({'left':leftTopArrMath[i].left,'top':leftTopArrMath[i].top}); } } </script> </body> </html>