首页 > 解决方案 > 在 Visual Studio Code 中使用 p5.js 访问文件

问题描述

我是一个新手,刚开始使用 p5.js,刚刚开始使用 Visual Studio Code,并且在使用任何访问文件的函数时遇到问题,无论是图像还是文本。我的目标是加载一个文本文件,以便将其解析为词汇数据。

在参考文档之后,我添加了预加载。您可以看到我也尝试使用图像文件执行此操作,并且产生了相同的结果。

function preload(){
fileContents = loadStrings('defaultvocab.txt');
//img = loadImage('AthensDemocracy.jpg');
}

结果是这组错误消息 错误信息 和预加载中的挂起。

我不确定它是 VSCode 的设置还是我需要在它创建的 JSON 文件中做的事情,我必须添加文件才能让它也能工作,所以它可能是要添加的东西,但我我没有太多运气找到它。目前launch.json是这样的:

{

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "type": "chrome",
        "request": "launch",
        "name": "Launch Chrome against localhost",
        "url": "http://localhost:8080",
        "webRoot": "${workspaceFolder}",
        "file":"index.html"
    }
]

请原谅悲伤的 90 年代网页设计,感谢您的帮助!

标签: visual-studio-codep5.js

解决方案


看起来网页仍在通过本机文件系统路径而不是本地 Web 服务器 (localhost:8080) 加载。

如果您在浏览器中手动打开htts://localhost:8080是否会运行(Web 服务器是否实际运行?)

就个人而言,我使用 Sam Levigne 的p5.vscode 插件

  1. 安装它
  2. 右键单击您的 index.html
  3. 单击使用 Live Server 打开

我发现实时编码(保存时自动重新加载)非常有用,尤其是在原型设计时。


推荐阅读