This question is about using the p5.js Editor tool.
In the index.html file there are statements in the head section to link to p5.js e.g. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
Now I want to link to a js file on my website like this <script src="http://lagers.org.uk/libs/_files/math/complex.js"></script>
but it doesn’t work. The editor doesn’t give any error/warning message about the file it simply states
[poly.js, line 229] "Complex" is not defined in the current scope. If you have defined it in your code, you should check its scope, spelling, and letter-casing (JavaScript is case-sensitive).
The Complex class is defined in this file.
This is the sketch. I have tried it in Firefox and Safari with the same result.
I’ve removed the http: part and then it loaded successfully: <script src=//lagers.org.uk/libs/_files/math/complex.js></script>
We shouldn’t load files using protocol http: while our page is https:!
I tried this with "" around the source file also with and without the // and unfortunately this didn’t solve my problem. The error message is "Complex is not defined"
It could still be that a protocol / security issue but I would expect the editor to report it as a problem
Imo you need to make your class available in the current/global context when loading from remote.
Put that to the bottom of your remote complex.js window.Complex = Complex;
and try again …
Ok! In principle it should work like it is as the code is a simple JS code which should automatically assigned to the global context. Maybe open web developer console and look what that say …
Unfortunately I can’t test now on my mobile. Will have a look tomorrow if the issue is persistent…
When I 1st edited out http: from “Complex.js” link it just worked for me.
But strangely I can’t get it working again!
Anyways, I don’t like https://editor.p5js.org at all; and don’t even have an account there.
It’s always seemed to me it does too many things behind-the-scene!
I don’t use the p5.js editor or any web editor for development, that includes OpenPprocessing. I use Visual Studio Code with TypeScript for anything Javascript and Eclipse for anything Java.
I have used OpenProcessing since 2009 to share my efforts with others. In the beginning I posted Java Applets but now I post p5.js and/or Javascript sketches. I am considering using the p5.js Editor as a secondary outlet for my work.
p5.js editor seems to parse the index.html file and processes all the script tags in fileReducer.js and I suspect that it is ignoring request script requests that are not local.
OpenProcessing, Glitch and many others are meant to host ready-to-deploy code.
While editor.p5js.org is meant for creating code online besides hosting them.
IMO, that’s not the best usage for it. Other sites are much better for sharing.
Try out Glitch.com or GitHub.com.
Second is most probably to also allow cross origin access.
Access to script at ‘https://xxx/complex.js’ from origin ‘https://preview.p5js.org’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
if you use apache server check if headers module are enabled
apachectl -M | grep headers_module
if not install and restart
sudo a2enmod headers
Afterwards make a .htaccess in the directory of your script. should be not on root (do ie js/complex.js) and set it in the js directory with content.
<FilesMatch "\.(js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
and restart apache
sudo systemctl restart apache2
Afterwards it should work! Tested it from my server and it runs in the p5js webeditor successfully …
Ahh! One thing more. load the scripts in the correct order in the head section of the html…
Thanks for your time investigating this issue, unfortunately I am unable to test your fix but since you have got it to work I will mark it as a solution