HumanBreak/src/module/render/components/misc.tsx

42 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { DefaultProps, ElementLocator, Sprite } from '@/core/render';
import { defineComponent, ref, watch } from 'vue';
import { SetupComponentOptions } from './types';
import { MotaOffscreenCanvas2D } from '@/core/fx/canvas2d';
interface ProgressProps extends DefaultProps {
/** 进度条的位置 */
loc: ElementLocator;
/** 进度条的进度1表示完成0表示未完成 */
progress: number;
/** 已完成部分的样式默认为绿色green */
success?: CanvasStyle;
/** 未完成部分的样式默认为灰色gray */
background?: CanvasStyle;
}
const progressProps = {
props: ['loc', 'progress', 'success', 'background']
} satisfies SetupComponentOptions<ProgressProps>;
export const Progress = defineComponent<ProgressProps>(props => {
const element = ref<Sprite>();
const render = (canvas: MotaOffscreenCanvas2D) => {
const { ctx } = canvas;
const width = props.loc[2] ?? 200;
const height = props.loc[3] ?? 200;
ctx.fillStyle = props.background ?? 'gray';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = props.success ?? 'green';
ctx.fillRect(0, 0, width * props.progress, height);
};
watch(props, () => {
element.value?.update();
});
return () => {
return <sprite ref={element} loc={props.loc} render={render}></sprite>;
};
}, progressProps);