抽中卡片增加>30 <40的卡片排列生成规则
* Update defaultTitle for the lottery event * Update style.scss * Update style.scss * feat: set single-time max person count to 40 * fix: support totalCount > 30 in useElementPosition with dynamic rule and add test * chore: ✏️ 处理eslint报错和恢复默认标题 #240 --------- Co-authored-by: heldenti <heldenti@users.noreply> Co-authored-by: LOG1997 <2694233102@qq.com>
This commit is contained in:
19
__test__/ElementPosition.test.ts
Normal file
19
__test__/ElementPosition.test.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { useElementPosition } from '@/hooks/useElement'
|
||||||
|
|
||||||
|
describe('useElementPosition', () => {
|
||||||
|
it('works for totalCount = 40 without throwing', () => {
|
||||||
|
const element = {} as any
|
||||||
|
const count = 10
|
||||||
|
const totalCount = 40
|
||||||
|
const cardSize = { width: 140, height: 200 }
|
||||||
|
const windowSize = { width: 800, height: 600 }
|
||||||
|
const cardIndex = 0
|
||||||
|
|
||||||
|
const result = useElementPosition(element, count, totalCount, cardSize, windowSize, cardIndex)
|
||||||
|
expect(result).toHaveProperty('xTable')
|
||||||
|
expect(result).toHaveProperty('yTable')
|
||||||
|
expect(result).toHaveProperty('scale')
|
||||||
|
expect(typeof result.scale).toBe('number')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1 +1 @@
|
|||||||
export const SINGLE_TIME_MAX_PERSON_COUNT = 30
|
export const SINGLE_TIME_MAX_PERSON_COUNT = 40
|
||||||
@@ -262,6 +262,16 @@ const cardRule: CardRule = {
|
|||||||
/**
|
/**
|
||||||
* @description 设置抽中卡片的位置
|
* @description 设置抽中卡片的位置
|
||||||
*/
|
*/
|
||||||
|
function createRuleForCount(count: number) {
|
||||||
|
// 动态生成规则:行数在 3-5 之间,尽可能均匀分配
|
||||||
|
const len = Math.min(5, Math.max(3, Math.ceil(count / 10)))
|
||||||
|
const base = Math.floor(count / len)
|
||||||
|
let rem = count % len
|
||||||
|
const rule = Array.from({ length: len }).fill(0).map(() => base + (rem > 0 ? (rem--, 1) : 0))
|
||||||
|
const scale = Math.max(0.9, 1.2 - (len - 3) * 0.1)
|
||||||
|
return { maxLine: Math.min(10, Math.ceil(count / len)), scale, rule, length: len }
|
||||||
|
}
|
||||||
|
|
||||||
export function useElementPosition(
|
export function useElementPosition(
|
||||||
element: any,
|
element: any,
|
||||||
count: number,
|
count: number,
|
||||||
@@ -280,7 +290,8 @@ export function useElementPosition(
|
|||||||
x: 0,
|
x: 0,
|
||||||
y: windowSize.height / 2,
|
y: windowSize.height / 2,
|
||||||
}
|
}
|
||||||
const { scale, rule, length } = cardRule[totalCount]
|
const ruleObj = cardRule[totalCount] ?? createRuleForCount(totalCount)
|
||||||
|
const { scale, rule, length } = ruleObj
|
||||||
// 计算缩放后的卡片尺寸
|
// 计算缩放后的卡片尺寸
|
||||||
const scaledCardWidth = cardSize.width * scale
|
const scaledCardWidth = cardSize.width * scale
|
||||||
const scaledCardHeight = cardSize.height * scale
|
const scaledCardHeight = cardSize.height * scale
|
||||||
|
|||||||
@@ -111,4 +111,4 @@
|
|||||||
height: 240px !important;
|
height: 240px !important;
|
||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user