fix:尝试修复余晖效果
This commit is contained in:
		@@ -79,7 +79,6 @@ interface SevenSegmentDisplayProps {
 | 
			
		||||
  size?: number;
 | 
			
		||||
  color?: string;
 | 
			
		||||
  AFTERGLOW_BUFFER_SIZE?: number; // 余晖存储槽大小
 | 
			
		||||
  AFTERGLOW_UPDATE_INTERVAL?: number; // 余晖更新间隔,单位毫秒
 | 
			
		||||
  pins?: {
 | 
			
		||||
    pinId: string;
 | 
			
		||||
    constraint: string;
 | 
			
		||||
@@ -149,17 +148,38 @@ const afterglowBuffers = ref<Record<string, boolean[]>>({
 | 
			
		||||
  dp: [],
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// 更新间隔计时器
 | 
			
		||||
let updateIntervalTimer: number | null = null;
 | 
			
		||||
// 余晖判定阈值(比如持续10帧才算稳定,可根据刷新间隔和人眼余晖调节)
 | 
			
		||||
const STABLE_THRESHOLD = 10;
 | 
			
		||||
 | 
			
		||||
// 判断段是否激活 - 如果当前状态或任一历史状态为true,则视为激活
 | 
			
		||||
// 实际显示的段状态(只有稳定后才改变)
 | 
			
		||||
const stableSegmentStates = ref({
 | 
			
		||||
  a: false,
 | 
			
		||||
  b: false,
 | 
			
		||||
  c: false,
 | 
			
		||||
  d: false,
 | 
			
		||||
  e: false,
 | 
			
		||||
  f: false,
 | 
			
		||||
  g: false,
 | 
			
		||||
  dp: false,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// 每段的稳定计数器
 | 
			
		||||
const segmentStableCounters = ref<Record<string, number>>({
 | 
			
		||||
  a: 0,
 | 
			
		||||
  b: 0,
 | 
			
		||||
  c: 0,
 | 
			
		||||
  d: 0,
 | 
			
		||||
  e: 0,
 | 
			
		||||
  f: 0,
 | 
			
		||||
  g: 0,
 | 
			
		||||
  dp: 0,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// 判断段是否激活(用稳定状态)
 | 
			
		||||
function isSegmentActive(
 | 
			
		||||
  segment: "a" | "b" | "c" | "d" | "e" | "f" | "g" | "dp",
 | 
			
		||||
): boolean {
 | 
			
		||||
  return (
 | 
			
		||||
    segmentStates.value[segment] ||
 | 
			
		||||
    afterglowBuffers.value[segment].some((state) => state)
 | 
			
		||||
  );
 | 
			
		||||
  return stableSegmentStates.value[segment];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 更新引脚状态的函数
 | 
			
		||||
@@ -181,28 +201,38 @@ function updateSegmentStates() {
 | 
			
		||||
    updateAfterglowBuffers();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // 先更新 segmentStates
 | 
			
		||||
  for (const pin of props.pins) {
 | 
			
		||||
    if (["a", "b", "c", "d", "e", "f", "g", "dp"].includes(pin.pinId)) {
 | 
			
		||||
      // 如果constraint为空,则默认为未激活状态
 | 
			
		||||
      if (!pin.constraint) {
 | 
			
		||||
        segmentStates.value[pin.pinId as keyof typeof segmentStates.value] =
 | 
			
		||||
          false;
 | 
			
		||||
        segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = false;
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      const pinState = getConstraintState(pin.constraint);
 | 
			
		||||
      let newState: boolean;
 | 
			
		||||
      if (props.cathodeType === "common") {
 | 
			
		||||
        // 共阴极: 高电平激活段
 | 
			
		||||
        newState = pinState === "high";
 | 
			
		||||
      } else {
 | 
			
		||||
        // 共阳极: 低电平激活段
 | 
			
		||||
        newState = pinState === "low";
 | 
			
		||||
      }
 | 
			
		||||
      segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = newState && comActive;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
      // 更新当前状态
 | 
			
		||||
      segmentStates.value[pin.pinId as keyof typeof segmentStates.value] =
 | 
			
		||||
        newState && comActive;
 | 
			
		||||
  // 余晖判定:只有新状态持续 STABLE_THRESHOLD 次才更新 stableSegmentStates
 | 
			
		||||
  for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
 | 
			
		||||
    const typedSegmentId = segmentId as keyof typeof segmentStates.value;
 | 
			
		||||
    const current = segmentStates.value[typedSegmentId];
 | 
			
		||||
    const stable = stableSegmentStates.value[typedSegmentId];
 | 
			
		||||
 | 
			
		||||
    if (current === stable) {
 | 
			
		||||
      segmentStableCounters.value[segmentId] = 0; // 状态一致,计数器清零
 | 
			
		||||
    } else {
 | 
			
		||||
      segmentStableCounters.value[segmentId]++;
 | 
			
		||||
      if (segmentStableCounters.value[segmentId] >= STABLE_THRESHOLD) {
 | 
			
		||||
        stableSegmentStates.value[typedSegmentId] = current;
 | 
			
		||||
        segmentStableCounters.value[segmentId] = 0;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -225,23 +255,6 @@ function updateAfterglowBuffers() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 开始余晖更新间隔
 | 
			
		||||
function startAfterglowUpdates() {
 | 
			
		||||
  if (updateIntervalTimer) return;
 | 
			
		||||
 | 
			
		||||
  updateIntervalTimer = window.setInterval(() => {
 | 
			
		||||
    updateSegmentStates();
 | 
			
		||||
  }, props.AFTERGLOW_UPDATE_INTERVAL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 停止余晖更新间隔
 | 
			
		||||
function stopAfterglowUpdates() {
 | 
			
		||||
  if (updateIntervalTimer) {
 | 
			
		||||
    window.clearInterval(updateIntervalTimer);
 | 
			
		||||
    updateIntervalTimer = null;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 监听约束状态变化
 | 
			
		||||
function onConstraintChange(constraint: string, level: string) {
 | 
			
		||||
  const affectedPin = props.pins.find((pin) => pin.constraint === constraint);
 | 
			
		||||
@@ -261,12 +274,6 @@ onMounted(() => {
 | 
			
		||||
 | 
			
		||||
  updateSegmentStates();
 | 
			
		||||
  onConstraintStateChange(onConstraintChange);
 | 
			
		||||
  startAfterglowUpdates();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
onUnmounted(() => {
 | 
			
		||||
  // 清理约束状态监听
 | 
			
		||||
  stopAfterglowUpdates();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// 暴露属性和方法
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user