使用pixi绘制呼吸灯

5 篇文章 0 订阅
3 篇文章 0 订阅

呼吸灯处理

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;
    }
    // time - this.animStartTime  时间差
    // (time - this.animStartTime) / 16.6  总共有多少帧
    // frame 代表是值范围 保证循环范围在 0-119  帧
    //  Math.abs(60 - frame) * 0.016 保证前 一半是  从暗到名  后一半是由明到暗
    const frame: number = Math.floor((time - this.animStartTime) / 16.6) % 120;
    this.alpha = Math.abs(60 - frame) * 0.016;
    // console.log(this.alpha, frame);
    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);
    // console.log(x, y);
    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);
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值