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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions p5.easycam.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,5 @@ export declare namespace Dw {
}
}
export declare function createEasyCam(): Dw.EasyCam;
export declare function setContext(context: p5): void;
export {};
80 changes: 47 additions & 33 deletions p5.easycam.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,47 @@
* and combines new useful features with the great look and feel of its parent.
*
*/



(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?

typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dw = factory());
}(this, (function () {
'use strict';

/** @namespace */
var Dw = (function(ext) {

/** The p5 variable is used to reference the current p5js runtime context. See setContext() to change this. */
var p5 = (typeof window !== 'undefined' && window.p5) ? window.p5 : null;

/** @namespace */
var Dw = (function(ext) {
/**
* Register the EasyCam library with p5js, by adding a createEasyCam() function to its prototype.
*
* p5 is normally available on the global window. But if it's running in instance mode,
* or has been imported as a module with an alias, it could have a different name.
* This function allows the p5 runtime identifier to be overridden when necessary.
*/
const setContext = function(context) {
p5 = context;
p5.prototype.createEasyCam = createEasyCam;
};

/**
* p5.EasyCam creator function.
* Arguments are optional, and equal to the default EasyCam constructor.
* @return {EasyCam} a new EasyCam
*/
const createEasyCam = function(/* p5.RendererGL, {state} */) {
var renderer = this._renderer;
var args = arguments[0];

if(arguments[0] instanceof p5.RendererGL) {
renderer = arguments[0];
args = arguments[1]; // could still be undefined, which is fine
}

return new Dw.EasyCam(renderer, args);
}

/**
* EasyCam Library Info
Expand Down Expand Up @@ -1061,7 +1091,7 @@ class EasyCam {
// 3) set new modelview (identity)
//renderer.resetMatrix(); //behavior changed in p5 v1.6
renderer.uMVMatrix = p5.Matrix.identity();

// 4) set new projection (ortho)
renderer._curCamera.ortho(0, w, -h, 0, -d, +d);
// renderer.ortho();
Expand Down Expand Up @@ -1634,6 +1664,10 @@ Object.freeze(INFO); // and constant

ext = (ext !== undefined) ? ext : {};

/** Make the initializers available to the outside world. */
ext.setContext = setContext;
ext.createEasyCam = createEasyCam;

/**
* @memberof Dw
* @type {EasyCam}
Expand Down Expand Up @@ -1667,31 +1701,11 @@ return ext;

})(Dw);



/**
* @submodule Camera
* @for p5
*/

if(p5){


/**
* p5.EasyCam creator function.
* Arguments are optional, and equal to the default EasyCam constructor.
* @return {EasyCam} a new EasyCam
*/
p5.prototype.createEasyCam = function(/* p5.RendererGL, {state} */){

var renderer = this._renderer;
var args = arguments[0];

if(arguments[0] instanceof p5.RendererGL){
renderer = arguments[0];
args = arguments[1]; // could still be undefined, which is fine
}

return new Dw.EasyCam(renderer, args);
/** Automatically register the EasyCam creator function with p5, assuming p5 is defined in the global namespace. */
if (typeof window !== 'undefined' && window.p5) {
window.Dw = Dw;
window.Dw.setContext(window.p5);
}
}

return Dw;
})));
4 changes: 4 additions & 0 deletions p5.easycam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,10 @@ export function createEasyCam(): Dw.EasyCam {
return;
}

export function setContext(context: any): void {
return;
}

/**
* @submodule Camera
* @for p5
Expand Down