如何将 p5.js 导入 TypeScript?

How can i import p5.js into TypeScript?

我正在尝试用打字稿开始一个项目,并想使用 p5.js 库。有什么方法可以将库“导入”到我的 index.ts 文件中,这样我就不需要将 <script src="p5.js"></script> 放入 HTML 中。我是模块的新手。提前致谢。

您可以添加:

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>, like in here:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js">
  </head>
  <body>
    <script src="your_code.js"></script>
  </body>
</html>


很抱歉,但这是唯一的方法,除非您使用像 PlayCode.io, if it is the case, you could go to the left sidebar and type p5.js in LIBS +. And to make the editor TypeScript, go to the up right corner of the editor —not the page— and click the button that says [JS] 这样的在线编辑器,然后 select 您想要的语言。



希望我的回答对您有所帮助。

我认为 npm 上有软件包 p5 可用,您可以使用:

安装

npm i p5 @types/p5

// Or yarn

yarn install p5 @types/p5

然后导入你要使用的东西:

import { Camera } from 'p5';