File size: 4,957 Bytes
a637d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const POSES = new Map();

function notify(str, type) {
    if (type === undefined) type = 'success';

    switch (type) {
        case 'success': console.log(str); break;
        case 'info': console.log(str); break;
        case 'warn': console.warn(str); break;
        case 'error': console.error(str); break;
    }

    const p = document.createElement('p');
    p.textContent = str;
    p.classList.add('item', type);
    const cont = document.querySelector('#notifications');
    cont.appendChild(p);
    setTimeout(() => cont.removeChild(p), 3000);
}

async function save_pose(obj) {
    const res = await fetch('/pose/save', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(obj),
    });
    const result = await res.json();
    if (result.ok) reload_poses();
    return result;
}

async function delete_pose(name) {
    const res = await fetch('/pose/delete', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ name }),
    });
    const result = await res.json();
    notify(result.result, result.ok ? 'success' : 'error');
    if (result.ok) reload_poses();
    return result;
}

async function reload_poses() {
    POSES.clear();

    const res = await fetch('/pose/all');
    const cont = document.querySelector('#saved_poses');
    cont.innerHTML = '';
    const df = document.createDocumentFragment();
    for (let data of await res.json()) {
        POSES.set(data.name, data);

        const fig = document.createElement('figure')
        const img = document.createElement('img');
        const cap = document.createElement('figcaption');
        const clo = document.createElement('div');
        const clo2 = document.createElement('span');
        fig.dataset.poseName = data.name;
        cap.textContent = data.name;
        clo.textContent = 'x';
        clo.classList.add('close');
        clo2.classList.add('close2');
        clo2.textContent = 'delete';
        clo.appendChild(clo2);

        img.src = 'data:image/png;base64,' + data.image;
        img.title = data.name;
        fig.append(clo, img, cap);

        df.appendChild(fig);
    }
    cont.appendChild(df);
}

document.addEventListener('DOMContentLoaded', async () => {

    const ui = {
        container: document.querySelector('#cont'),
        canvas: document.querySelector('#main_canvas'),
        notation: document.querySelector('#notation'),
        indicator1: document.querySelector('#body_indicator1'),
        indicator2: document.querySelector('#body_indicator2'),
        all_reset: document.querySelector('#all_reset'),
        reset_camera: document.querySelector('#reset_camera'),
        reset_pose: document.querySelector('#reset_pose'),
        fixed_roll: document.querySelector('#fixed_roll'),
        add_body: document.querySelector('#add_body'),
        remove_body: document.querySelector('#remove_body'),
        canvas_width: document.querySelector('#canvas_width'),
        canvas_height: document.querySelector('#canvas_height'),
        bg: document.querySelector('#bg_file'),
        reset_bg: document.querySelector('#reset_bg'),
        elliptic_limbs: document.querySelector('#elliptic_limbs'),
        //joint_radius: document.querySelector('#joint_radius'),
        limb_width: document.querySelector('#limb_width'),
        low_fps: document.querySelector('#low_fps'),
        save: document.querySelector('#save_button'),
        copy: document.querySelector('#copy_button'),
        save_pose: document.querySelector('#save_pose'),
        save_pose_callback: save_pose,
        notify: notify,
    };

    document.addEventListener('poseload', e => {
        const obj = POSES.get(e.detail.name);
        if (obj) ui.loadPose(obj);
    }, false);

    const { init, init_3d } = await import('posex');

    init(ui);
    const animate = init_3d(ui);
    animate();

    await reload_poses();

}, false);

document.addEventListener('DOMContentLoaded', () => {
    const get_name = ele => {
        while (ele && ele !== document) {
            if (ele.dataset && ele.dataset.poseName !== undefined)
                return ele.dataset.poseName;
            ele = ele.parentNode;
        }
        return '';
    };

    document.querySelector('#saved_poses').addEventListener('click', e => {
        let target = e.target;
        if (target.tagName === 'IMG') target = target.parentNode;
        if (target.classList.contains('close2')) target = target.parentNode;
        if (target.tagName === 'FIGURE') {
            const name = get_name(target);
            const ev = new CustomEvent('poseload', { bubbles: true, detail: { name } });
            target.dispatchEvent(ev);
        } else if (target.classList.contains('close')) {
            const name = get_name(target);
            if (name.length != 0) {
                delete_pose(name);
            }
        }
    }, false);
}, false);