Skip to content

Instantly share code, notes, and snippets.

@drcmda
Last active July 20, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drcmda/1af98965801a853814636172319d1225 to your computer and use it in GitHub Desktop.
Save drcmda/1af98965801a853814636172319d1225 to your computer and use it in GitHub Desktop.
plugin archtitecture
// import and create canvs
import Canvas from 'awv3';
const canvas = new Canvas({ dom: '#view' });
// import plugins, they should be separate from the awv3-repo
import Sketcher from 'awv3-sketcher';
import Extrusion from 'awv3-extrusion';
import Measuring from 'awv3-measuring';
// create measure layer, "use" it Node style
const measuring = new Measuring();
canvas.use(measuring);
// same for extrusion
const extrusion = new Extrusion();
canvas.use(extrusion);
// create sketcher, add options, in this case events
const sketcher = new Sketcher({
[Sketcher.Events.Enabled](args) {
document.querySelector('#sketcherGUI').classList.add('visible');
},
[Sketcher.Events.Disabled](args) {
document.querySelector('#sketcherGUI').classList.remove('visible');
}
[Sketcher.Events.Draw](args) {
// set default condition
sketcher.setCondition({ angle: 45, x: 10, distance: 100 });
// show specific dom overlay
let mask = document.querySelector('#sketcherGUI > .conditionMask');
mask.classList.add('visible');
// move it along with the mouse
sketcher.on('move', (event) => mask.style.transform = `transform3d(${event.offsetX}, ${event.offsetY}, 0)`);
},
[Sketcher.Events.DrawFinished](args) {
// remove dom overlay
document.querySelector('#sketcherGUI > .conditionMask').classList.remove('visible');
// remove listener
sketcher.removeEventListener('move');
}
...
});
canvas.use(sketcher);
// plugins can be toggles/enabled
sketcher.toggle(true);
measure.toggle(true);
// plugins have their specific API
let result = measure.distance({ from: new THREE.Vector3(0, 0, 0), to: new THREE.Vector3(10, 10, 10) });
// plugs extend from a offical base class
import Plugin from 'awv3/core/plugin';
class MyPlugin extends Plugin {
constructor() {
super();
}
api1() {
}
api2() {
}
}
// plugs can define their events statically
MyPlugin.Events = {
Event1: 'Event1',
Event2: 'Event2',
...
}
// awv3/core/plugin.js: needs to be defined
class Plugin extends EventEmitter {
...
}
Copy link

ghost commented Jul 20, 2016

  • Probably it's usefull that Plugins not only emits event but also could consume events (like a selection).

@drcmda
Copy link
Author

drcmda commented Jul 20, 2016

  • plugins should be careful defining interaction for objects living in canvas because performance can suffer drastically. if possible plugins should remove interaction and clean up in general when disabled. core/interaction layer should be clever enough to exclude an object from raycasting if no listeners are active.
  • cleanup in general should be encouraged to prevent any left-overs in canvas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment