第6讲:增修别人的函数库,以pixi.js库中的Rectangle模块为例。

如果发现别人的函数库缺少了某个功能,希望自己添加,那么可以这么做:
在src/lib目录下新建一个RecUtils.ts文件
内容如下:

import {Rectangle} from "pixi.js";

declare module "pixi.js"{
    class Rectangle{
        containsRect(other: Rectangle): boolean;
    }
}

Rectangle.prototype.containsRect = function(other: Rectangle){
    return (
        other.x >= this.x && other.y >= this.y && other.right <= this.right && other.bottom <= this.bottom
    )
}

在src目录下新建一个AddChangFunction.ts

import {Point, Rectangle} from  "pixi.js";
import './lib/RectUtils.ts'

let rect = new Rectangle(0,1,98,99);
let smallRect = new Rectangle(10,11,20,21);
console.log(rect.x);  // 左上角的x = 0
console.log(rect.y);  // 左上角的y = 1
console.log(rect.right); // 右下角x = 98
console.log(rect.bottom); // 右下角y = 99

let res = rect.containsRect(smallRect);
console.log(res);

修改index.html文件,修改引用为AddChangeFunction.ts文件。

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + TS</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- <script type="module" src="/src/main.ts"></script> -->
    <!-- <script type="module" src="/src/CanvasAndStage.ts"></script> -->
    <script type="module" src="/src/AddChangeFunction.ts"></script>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值