toolness / p5.js-widget

A reusable widget for embedding editable p5 sketches in web pages.
https://toolness.github.io/p5.js-widget/
GNU Lesser General Public License v2.1
162 stars 44 forks source link

loading a sketch.js file? #59

Closed luisaph closed 8 years ago

luisaph commented 8 years ago

I would like to load code from an external file, like this:

<script type="text/p5" src="sketch.js"  ></script>

Curious: is this feature supported?

When I try the above, instead of my sketch I see what seems to be a default one:

screen shot 2016-06-30 at 10 45 14 am

Thanks!

toolness commented 8 years ago

Hey, there isn't currently a way to do this, but there totally should be!

How soon do you need the feature?

luisaph commented 8 years ago

I would use for a project that's due in two weeks, but it's not a critical thing :)

toolness commented 8 years ago

Ok, I've implemented basic support for it! It's documented here:

https://toolness.github.io/p5.js-widget/#src

So your example snippet should work now.

I also tried to handle error conditions gracefully, so if the file couldn't be retrieved, you'll see something like this:

404

Another weird situation I've seen beginners run into happens when they've accidentally specified both a src attribute and an inline script, e.g.:

<script type="text/p5" src="static/my-example.js">
function setup() {
  // Do stuff here...
}
</script>

With normal script tags, browsers will use either the src attribute or the inline script (I forget which), which often leaves the user confused when they edit the other one and don't see any changes. So instead, p5.js-widget treats this kind of situation as an error and displays this:

src-and-inline-error

Anyhow, let me know if this works okay for you! Thanks for filing the issue.

luisaph commented 8 years ago

Atul, this is great, thank you! I will let you know how it goes.