呼吸灯处理
import { Graphics, Sprite } from 'pixi.js';
import { allPointTexture } from '@/utils/controlUtil/getSocketTextures';
declare interface borderType {
x: number;
y: number;
width: number;
height: number;
code: string;
visible?: boolean;
}
export default class RobotBorder extends Graphics {
borderFlag: boolean;
robotCode: string;
timer: number | null | undefined;
animId: number = 0;
animStartTime: number = 0;
constructor(options: borderType) {
super();
this.visible = true;
this.borderFlag = true;
this.scale.y = this.scale.y * -1;
this.robotCode = options.code;
this.resetSize(options);
}
animloop = (time: number) => {
if (!this.animStartTime) {
this.animStartTime = time;
}
const frame: number = Math.floor((time - this.animStartTime) / 16.6) % 120;
this.alpha = Math.abs(60 - frame) * 0.016;
this.animId = window.requestAnimationFrame(this.animloop);
};
resetSize(options: borderType) {
const { x, y, width, height } = options;
const children = this.children;
window.cancelAnimationFrame(this.animId);
this.clear();
this.removeChild(...children);
const { texture } = allPointTexture.ROBOT_BORDER;
const sprite = new Sprite(texture);
const [realW, realH] = [width * 1.4, height * 1.4];
const size = Math.max(realW, realH);
sprite.width = size;
sprite.height = size;
sprite.position.set(x - size / 2, -y - size / 2);
this.addChild(sprite);
this.animId = window.requestAnimationFrame(this.animloop);
}
}