File size: 2,246 Bytes
ea35075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
export type IPostMessage = {
    method: string
    type: 'call' | 'return'
    payload: any
}

const iframe = document.getElementById('open-pose-editor') as HTMLIFrameElement

const poseMessage = (message: IPostMessage) => {
    iframe.contentWindow?.postMessage(message)
}

const MessageReturnHandler: Record<string, (arg: any) => void> = {}
const MessageEventHandler: Record<string, (arg: any) => void> = {}

window.addEventListener('message', (event) => {
    const { data } = event
    if (data && data.cmd && data.cmd == 'openpose-3d' && data.method) {
        const method = data.method
        console.log('Method', method, event)
        if (data.type == 'return') {
            MessageReturnHandler[method]?.(data.payload)
        } else if (data.type == 'event') {
            console.log(MessageEventHandler)
            MessageEventHandler[method]?.(data.payload)
        }
    }
})

function InvokeOnlineOpenPose3D(method: string, ...args: any[]) {
    return new Promise((resolve, reject) => {
        const id = setTimeout(() => {
            delete MessageReturnHandler[method]

            reject({
                method,
                status: 'Timeout',
            })
        }, 1000)

        const onReutrn = (arg: any) => {
            clearTimeout(id)
            resolve(arg)
        }
        MessageReturnHandler[method] = onReutrn

        poseMessage({
            method,
            type: 'call',
            payload: args,
        })
    })
}
function CreateClick(name: string, ...args: any[]) {
    const ele = document.getElementById(name)

    ele?.addEventListener('click', async () => {
        console.log(name)
        const value = await InvokeOnlineOpenPose3D(name, ...args)
        console.log('return', value)
    })
}

MessageEventHandler['MakeImages'] = (arg) => {
    console.log('event', arg)
}

CreateClick('GetAPIs')
CreateClick('GetAppVersion')
CreateClick('MakeImages')
CreateClick('Pause')
CreateClick('Resume')
CreateClick('OutputWidth', 512)
CreateClick('OutputHeight', 512)
CreateClick('OnlyHand', true)
CreateClick('MoveMode', true)
CreateClick('GetWidth')
CreateClick('GetHeight')
CreateClick('GetSceneData')
CreateClick('LockView')
CreateClick('UnlockView')
CreateClick('RestoreView')