提问人:pava 提问时间:11/6/2023 最后编辑:starballpava 更新时间:11/8/2023 访问量:44
在实例模式下使用 p5js 时使用 Intellisense
Use Intellisense when using p5js in instance mode
问:
我正在尝试用 expressjs 和 p5js 编写一个小网站,但我在 VSCode 中使用 p5js 和 intellisense 遇到了一个不舒服的问题。我的问题是:
我在实例模式下使用 p5js,因为我希望页面上有多个画布。为此,您可以定义如下内容
const mySketch = (sketch) => {
let x = 100;
let y = 100;
sketch.setup = () => {
sketch.createCanvas(500, 400);
};
sketch.draw = () => {
sketch.background(0);
sketch.fill(255);
sketch.rect(x, y, 50, 50);
};
};
new p5(mySketch); // invoke p5
为了在 VSCode 中将 Intellisense 与 p5js 一起使用,我安装了 @types/p5 并将其添加到 jsconfig.json 中
{
"compilerOptions": {
"target": "es6"
},
"include": [
"*.js",
"**/*.js",
"node_modules/@types/p5/**/*"
]
}
这允许我获得方法/函数的定义,参数,ecc..,当直接调用时。
因此,如果调用并且我将鼠标放在函数上,VSCode 会告诉我有关源自 @types/p5/ 文件夹中的 global.d.ts 文件的特定命令的文档。circle(10, 10, 5)
在实例模式下使用 p5js 时,您不直接调用方法本身,而是从您在开始时传递的 (sketch) 参数调用它们(例如)。sketch.circle(10, 10, 5)
现在:当我调用它们时,如何让 @types/p5 文件夹中已经存在的文档正常工作?sketch.method()
我像这样尝试过 JSDoc
/**
*
* @param{p5} sketch
*/
const mySketch = (sketch) => {
s.setup = () => { }
[...]
但它没有用。
现在,如果我将鼠标悬停在鼠标上,我会得到:circle()
Draws a circle to the canvas. A circle is a round shape. Every point on the edge of a circle is the same distance from its center. By default, the first two parameters set the location of the center of the circle. The third parameter sets the shape's width and height (diameter). The origin may be changed with the ellipseMode() function.
@param x — x-coordinate of the center of the circle.
@param y — y-coordinate of the center of the circle.
@param d — diameter of the circle.
@chainable
如果我做同样的事情,我会得到.sketch.circle()
any
这可能是由于我对 VSCode 的无知造成的,但我无法理解它。
答:
你对 JSDoc 所做的工作很好。你也可以写.或者,如果您使用的是 TypeScript,则 .@param {p5} sketch
const mySketch = (/**@type {p5}*/sketch) => {
const mySketch = (sketch: p5) => {
你缺少的是类似于模块导入的东西,如果它适用于你对 p5 的使用。前任。 在文件顶部。如果您不想进行实际的模块导入,请尝试在文件中的某个位置添加。import p5 from "p5";
/** @typedef {import("p5")} p5 */
您添加到 tsconfig 的属性是无用的。"node_modules/@types/p5/**/*"
includes
评论