From 20d556b7b3ace113c1f3a76d19fa169f9630e13d Mon Sep 17 00:00:00 2001 From: wangzemei Date: Sat, 12 Oct 2024 17:38:38 +0800 Subject: [PATCH] * [task#129807] modify the calculation logic for the calculateCardPosition. --- lib/zin/wg/think3c/js/v1.js | 105 +++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 44 deletions(-) diff --git a/lib/zin/wg/think3c/js/v1.js b/lib/zin/wg/think3c/js/v1.js index 62d60b1863..65de86dfec 100644 --- a/lib/zin/wg/think3c/js/v1.js +++ b/lib/zin/wg/think3c/js/v1.js @@ -103,24 +103,59 @@ function afterFinish() }, 200); }; -/** - * 计算卡片位置。 - * Calculate card position. - * - * @param object region - * @param number index - * @access public - * @return void - */ function calculateCardPosition(region, index) { - const h = 180; // 卡片高度 - const minW = 180; // 最小宽度 - const maxW = 280; // 最大宽度 - const d = 8; // 间距 + const h = 180; + const minW = 140; + const positions = calculateCardPositionsOfScale(region); + const cards = $(`#runResult .card.in_area-${index}`); + if(index == 4) console.log(positions); + if(cards.length <= positions.length) + { + cards.each((i, card) => { + $(card).css({ + left: `${positions[i][0]}px`, + top: `${positions[i][1]}px`, + width: `${positions[i][2]}px`, + height: `${h}px`, + }); + }); + $('.card').removeClass('hidden'); + } + else + { + const scale = 1 / Math.ceil(cards.length / positions.length); + const newPositions = calculateCardPositionsOfScale(region, scale); + cards.each((i, card) => { + $(card).css({ + left: `${newPositions[i][0]}px`, + top: `${newPositions[i][1]}px`, + width: `${minW}px`, + height: `${h}px`, + transform: `scale(${scale})`, + }); + }); + + $('.card').removeClass('hidden'); + } +} + +function calculateCardPositionsOfScale(region, scale = 1) +{ + let h = 180; // 原始卡片高度 + let minW = 140; // 原始卡片宽度 + let maxW = 280; // 最大宽度 + let d = 8; // 间距 const rows = Object.values(region.rows); const rowsCount = rows.length; const positions = []; + if(scale < 1) + { + h = h * scale; + minW = minW * scale; + maxW = maxW * scale; + d = d * scale; + } let r = 0; while (r < rowsCount) @@ -149,7 +184,16 @@ function calculateCardPosition(region, index) if(validStartRow != -1) { let {left, right} = getMaxLeftAndMinRight(rows, validStartRow, h); - right -= 16; + if(scale < 1) + { + left -= 46; + right -= 32; + } + else + { + left += 4; + right -= 20; + } let rowN = Math.floor((right - left + d) / (minW + d)); let w = (right - left + d) / rowN; @@ -168,36 +212,9 @@ function calculateCardPosition(region, index) } } - let isFirst = true; - let firstTop = 0; - positions.forEach((item) => { - if(isFirst) - { - isFirst = false; - firstTop = item[1]; - } - else - { - if(item[1] > firstTop) item[1] += d; - } - }); - const cards = $(`#runResult .card.in_area-${index}`); - if(cards.length <= positions.length) - { - cards.each((i, card) => { - $(card).css({ - left: `${positions[i][0]}px`, - top: `${positions[i][1]}px`, - width: `${positions[i][2]}px`, - height: `${h}px`, - }); - }); - $('.card').removeClass('hidden'); - } - else - { - // TODO: 处理卡片数量超过位置数量的情况 - } + const groupedData = groupPositions(positions, h); + const result = calcPositionsWithOffset(groupedData); + return result; } /**