๐Ÿ“ฆ zeyap / teddy

๐Ÿ“„ canvas.js ยท 197 lines
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197const canvas = (()=>{
    var clientRect = webglCanvas.getBoundingClientRect();
    var mouseDown = false;
    var mode = 'create'//paint, extrude, cut
    var color = vec4.fromValues(1.0,0.6,0.1,1.0);
    var stroke = [];// trajectory in NDC
    var showWireframe = false
    var originalObjectPath = [];
    
    function onClear(){
        scene.clearObject();
        scene.resetCamera();
        originalObjectPath = [];
        mouseDown = false;
        setMode('create');
    }

    function getStroke(){
        return stroke;
    }

    function getColor(){
        return color;
    }

    function getMode(){
        return mode;
    }

    function onColorChange(evt){
        const hex = evt.target.value;
        const r = parseInt(hex[1]+hex[2],16)/255,
        g = parseInt(hex[3]+hex[4],16)/255,
        b = parseInt(hex[5]+hex[6],16)/255;
        color = vec4.fromValues(r,g,b,1.0);
    }
    
    function onMouseDown(){
        mouseDown = true;
    }

    function onMouseMove(e){
        if(!mouseDown){
            return;
        }

        const [x,y] = scene.getNDCxy(e.clientX,e.clientY,clientRect);

        stroke = stroke.concat([x,y,0])

    }

    function onMouseUp(e){
        mouseDown = false;

        if(mode==='create'){
            // detect intersection
            if(algorithm.selfIntersect(stroke)){
                console.log('self intersects')
                stroke = [];
                return;
            }

            var path = []; // stroke projected onto image plane
            for(let i=0;i<stroke.length;i+=3){
                const x = stroke[3*i],y = stroke[3*i+1];
                
                const ray = scene.getRay(x,y)

                const imgPlane = scene.getImagePlane();
                const planeNormal = imgPlane.n;
                const planePoint = imgPlane.p;//near
                const intersectp = scene.rayPlaneIntersect(ray, planeNormal, planePoint).p
                path.push(intersectp)
            }

            const equalizedPath = [];
            algorithm.Equalize(equalizedPath,path,0.05, true)
            for(let i=0;i<equalizedPath.length;i++){
                originalObjectPath.push(equalizedPath[i])
            }
            
            scene.buildObject(equalizedPath);
            setMode('paint');
        }
        stroke = []
    }

    function setMode(newMode){
        mode = newMode
    }

    function onScroll(event){
        const rotateRadY = event.deltaX
        const rotateRadX = event.deltaY
        scene.rotateCamera(rotateRadX/1000,rotateRadY/1000)
        
        event.preventDefault();
    }

    function onResize(){
        clientRect = webglCanvas.getBoundingClientRect();
        scene.setRatio(clientRect.width/clientRect.height);
    }

    function getReferenceDotPositions(){
        const dotPositions = []
        const pixstep=50
        
        const stepx = 2/(clientRect.height/pixstep)
        const stepy = 2/(clientRect.width/pixstep)

        const z = 0.9
        
        for(let c=-stepx/2;c>-1;c-=stepx){
            for(let r=stepy/2;r<1;r+=stepy){
                dotPositions.push([r,c,z])
            }
            for(let r=-stepy/2;r>-1;r-=stepy){
                dotPositions.push([r,c,z])
            }
        }

        for(let c=stepx/2;c<1;c+=stepx){
            for(let r=stepy/2;r<1;r+=stepy){
                dotPositions.push([r,c,z])
            }
            for(let r=-stepy/2;r>-1;r-=stepy){
                dotPositions.push([r,c,z])
            }
        }
        
        return dotPositions
        
    }

    function getReferenceDotScales(){
        const scalex = 300/clientRect.width
        const scaley = 300/clientRect.height
        return [scalex,scaley]
    }

    function onSwitchWireframe(){
        showWireframe = !showWireframe;
        const elem = document.getElementById('wireframeButtonIcon')
        if(showWireframe){
            elem.src = "images/border-all-solid.svg"
        }else{
            elem.src = "images/th-large-solid.svg"
        }
    }
    function getIfShowWireframe(){
        return showWireframe
    }

    function openInNewTab(url){
        return ()=>{
            window.open(url,'_blank');
        }
    }

    function initializeMouseEvents(){
        var webglCanvas = document.getElementById("webglCanvas");
        webglCanvas.addEventListener('mousedown', onMouseDown)
        webglCanvas.addEventListener('mousemove', onMouseMove)
        webglCanvas.addEventListener('mouseup', onMouseUp)
        webglCanvas.onwheel = onScroll
        
        var clearButton = document.getElementById("clearButton");
        clearButton.addEventListener('click',onClear)

        var colorPicker = document.getElementById("colorPicker");
        colorPicker.addEventListener('change',onColorChange)

        var wireframeButton = document.getElementById("wireframeButton");
        wireframeButton.addEventListener('click',onSwitchWireframe);

        var personalWebLink = document.getElementById("personalweblink");
        personalWebLink.addEventListener('click',openInNewTab('http://zeyapeng.com/'));
        var repositoryLink = document.getElementById("repositorylink");
        repositoryLink.addEventListener('click',openInNewTab('https://github.com/zeyap/teddy'));
        var researchLink = document.getElementById("researchlink");
        researchLink.addEventListener('click',openInNewTab('https://www-ui.is.s.u-tokyo.ac.jp/~takeo/teddy/teddy.htm'));

        window.onresize = onResize
    }

    return {
        initializeMouseEvents,
        getStroke,
        getColor,
        getMode,
        getIfShowWireframe,
        getReferenceDotPositions,
        getReferenceDotScales,
    };
})()