HumanBreak/src/components/boxAnimate.vue

57 lines
1.2 KiB
Vue
Raw Normal View History

2022-11-16 23:01:23 +08:00
<template>
<canvas
ref="canvas"
:width="width ?? 32"
:height="height ?? 32"
id="canvas"
></canvas>
</template>
<script lang="tsx" setup>
import { onMounted, onUnmounted, ref } from 'vue';
import { addAnimate, removeAnimate } from '../plugin/animateController';
const props = defineProps<{
id: string;
width?: number;
height?: number;
}>();
const canvas = ref<HTMLCanvasElement>();
onMounted(() => {
const c = canvas.value!;
const ctx = c.getContext('2d')!;
const cls = core.getClsFromId(props.id);
const frames = core.getAnimateFrames(cls);
const scale = window.devicePixelRatio;
c.style.width = `${c.width}px`;
c.style.height = `${c.height}px`;
c.width *= scale;
c.height *= scale;
ctx.scale(scale, scale);
2022-11-19 11:30:14 +08:00
const fn = () => {
2022-11-16 23:01:23 +08:00
core.clearMap(ctx);
const frame = core.status.globalAnimateStatus % frames;
core.drawIcon(ctx, props.id, 0, 0, props.width, props.height, frame);
};
2022-11-19 11:30:14 +08:00
fn();
2022-11-16 23:01:23 +08:00
addAnimate(fn);
onUnmounted(() => {
removeAnimate(fn);
});
});
</script>
<style lang="less" scoped>
#canvas {
border: 1.5px solid #ddd;
background-color: #222;
}
</style>