webarkit / ARnft

A small javascript library for WebAR with NFT
GNU Lesser General Public License v3.0
219 stars 53 forks source link

Custom events improves: adding targets inherited to window or global object #220

Closed kalwalt closed 2 years ago

kalwalt commented 2 years ago

Improving Events

This is the continue of issue #213 and PR #208. Instead to add a arNFT property inherited to the Window or Global object, i created a new target property inside every class, for example in the ARnft class:

export default class ARnft {
    // ...other properties
    private target: EventTarget;

and inside the constructor i assigned the window || global to the target property:

constructor(width: number, height: number, configUrl: string) {
        this.width = width;
        this.height = height;
        this.configUrl = configUrl;
        this.target = window || global;
        this.uuid = uuidv4();
        this.version = version;
        console.log("ARnft ", this.version);
    }

Finallly we can use it in the class:

const initEvent = new CustomEvent("initARnft");
this.target.dispatchEvent(initEvent);

Example to test

Test for now the arNT_event_example.html, but note that until i not change the code in the ARnft-threejs library won't works. But you can see that the other events are working.

Check list

kalwalt commented 2 years ago

I tested with the new ARnft-threejs lib with the new events. The marker can be detected and tracked, i think that now this PR is ready to be merged.