Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support module bundling, and support dynamic p5 runtime contexts #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jackwh
Copy link

@jackwh jackwh commented Apr 21, 2023

Further to the discussion in #26, this PR does two things:

  1. Wrap the EasyCam code in a UMD module. EasyCam can now be imported and used in modular projects. This allows tools like Webpack or Vite to bundle code which relies on it.
  2. Allows the p5 context to be set dynamically. The code previously expected p5 to be available globally, which won't always be the case if p5 is aliased or bundled.

Example code demonstrating these changes:

import * as mySketch from "p5";
import EasyCam from "p5.easycam";

// ...

mySketch.setup = () => {
  // Tell EasyCam to use the "mySketch" context:
  EasyCam.setContext(mySketch);

  // Create a camera
  cam = mySketch.createEasyCam();
};

// ...

The traditional <script src="..." /> approach still works as before. I've checked all the examples, and everything seems to be backwards-compatible.

The only bit I'm unsure about is whether the TypeScript definitions need changing, now the code's wrapped in a module. I understand the types are a work-in-progress so perhaps someone can advise on that.




(function (global, factory) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This self-calling function makes the library accessible as a module, whilst still running itself automatically in the global context.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted not to indent everything within the module wrapper, just to keep the diff/history clean... FYI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Were you able to import this modified version successfully into your Yarn/TypeScript project?

@jwdunn1
Copy link
Member

jwdunn1 commented Apr 22, 2023

Good work here! I copied the modified p5.easycam.js file from this PR and tested a dozen or more of my EasyCam sketches on OpenProcessing and editor.p5js.org. They all worked perfectly, both global and multi-instance sketches. I'll look into the TypeScript definitions tomorrow when I find time. Were you able to import this modified version successfully into your Yarn/TypeScript project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants