这是indexloc提供的服务,不要输入任何密码
Skip to content

EventDispatcher cannot be invoked without 'new'" #54

@laoLiDeHao

Description

@laoLiDeHao
import {  EventDispatcher, Raycaster, Vector2 } from "three";

export class EventManage extends EventDispatcher{
    mouse = new Vector2()
    raycaster = new Raycaster()
    dom
    camera
    scene
    last = null
    /**
     * 
     *  @param {HTMLElement} dom
     * @param {Camera} camera
     * @param {Scene} scene
     */
    constructor(
        dom, camera, scene
    ) {
        super({})
        this.dom = dom
        this.camera = camera
        this.scene = scene

        const mouse = this.mouse
        const raycaster = this.raycaster

        let last = null

        dom.addEventListener('mousemove', (e) => {
            mouse.x = e.offsetX / dom.offsetWidth * 2 - 1
            mouse.y = -e.offsetY * 2 / dom.offsetHeight + 1
            raycaster.setFromCamera(mouse, camera);
            const intersection = raycaster.intersectObjects(this.scene.children)
            if (intersection.length > 0) {
                // console.log(intersection[0]);
                let object = intersection[0].object
                object.dispatchEvent({
                    type: 'mousemove',
                    point: intersection[0].point
                })
                if (last && last.uuid == object.uuid) {
                } else {
                    if (last)
                        last.dispatchEvent({
                            type: 'mouseout',
                            point: intersection[0].point
                        })
                    object.dispatchEvent({
                        type: 'mousein',
                        point: intersection[0].point
                    })
                }
                last = object
            }

        })
        // dom.addEventListener('mouseup', (e) => {
        //     raycaster.setFromCamera(mouse, camera);
        //     const intersection = raycaster.intersectObjects(this.scene.children)
        //     if (intersection.length > 0) {
        //         // console.log(intersection[0]);
        //         intersection[0].object.dispatchEvent({
        //             type: 'mouseup',
        //             point: intersection[0].point
        //         })
        //     }
        // })
        // dom.addEventListener('mousedown', (e) => {
        //     raycaster.setFromCamera(mouse, camera);
        //     const intersection = raycaster.intersectObjects(this.scene.children)
        //     if (intersection.length > 0) {
        //         // console.log(intersection[0]);
        //         intersection[0].object.dispatchEvent({
        //             type: 'mousedown',
        //             point: intersection[0].point
        //         })
        //     }
        // })
        dom.addEventListener('click', (e) => {
            raycaster.setFromCamera(mouse, camera);
            const intersection = raycaster.intersectObjects(this.scene.children)
            
            if (intersection.length > 0) {
                // console.log(intersection[0]);
                intersection[0].object.dispatchEvent({
                    type: 'click',
                    point: intersection[0].point
                })
            }
        })

        console.log(this);
    }
}
permission.js:29 TypeError: Class constructor EventDispatcher cannot be invoked without 'new'
    at new EventManage (index.js:18:1)
    at PredictionManage.GeologyMesh (index.js:43:1)
    at new PredictionManage (index.js:4:1)
    at VueComponent.mounted (selector.js?type=script&index=0!./src/views/GeologicalPredictionAndPrediction/components/geologyPredictionManage/index.vue:24:40)
    at invokeWithErrorHandling (vue.esm.js:3793:61)
    at callHook$1 (vue.esm.js:3194:13)
    at Object.insert (vue.esm.js:4981:13)
    at invokeInsertHook (vue.esm.js:7011:38)
    at VueComponent.patch [as __patch__] (vue.esm.js:7222:9)
    at Vue._update (vue.esm.js:2932:25)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions