1
1
function animate ( ) {
2
2
3
- for ( let elem of pathElements ) {
4
- switch ( elem . type ) {
5
- case 'userInput' :
6
- animateUserInput ( elem ) ;
7
- break ;
8
- case 'lut' :
9
- animateLUT ( elem ) ;
10
- break ;
11
- case 'DFF' :
12
- animateDFF ( elem ) ;
13
- break ;
14
-
15
- default :
16
- break ;
17
- }
18
- }
3
+ for ( let elem of pathElements ) {
4
+ // console.log('animate', elem);
5
+ switch ( elem . type ) {
6
+ case 'userInput' :
7
+ animateUserInput ( elem ) ;
8
+ break ;
9
+ case 'lut' :
10
+ animateLUT ( elem ) ;
11
+ break ;
12
+ case 'DFF' :
13
+ animateDFF ( elem ) ;
14
+ break ;
15
+
16
+ default :
17
+ break ;
18
+ }
19
+ }
19
20
}
20
21
21
22
function animateUserInput ( elem ) {
22
- let elemPos = document . querySelector ( `#userInput-out` ) . getBoundingClientRect ( ) ;
23
- let elemTop = elemPos . top ;
24
- let elemLeft = elemPos . left ;
25
- connections . innerHTML += `<div class="animation" id="animation-${ elem . type } " style="position: absolute; top: ${ convertPxToVh ( elemTop ) } vh; left: ${ convertPxToVw ( elemLeft ) } vw;"></div>` ;
26
-
27
- document . getElementById ( `animation-${ elem . type } ` ) . animate ( [
28
- { transform : `translateX(0vw)` } ,
29
- { transform : `translateX(${ convertPxToVw ( document . querySelector ( `#userInput-out-Wire1` ) . getBoundingClientRect ( ) . right ) } vw)` } ,
30
- { transform : `translateY(${ convertPxToVh ( document . querySelector ( `#userInput-out-Wire2` ) . getBoundingClientRect ( ) . top ) } vh)` } ,
31
- { transform : `translateX(${ convertPxToVw ( document . querySelector ( `#userInput-out-Wire3` ) . getBoundingClientRect ( ) . right ) } vw)` }
32
- ] , {
33
- duration : 1000 ,
34
- iterations : 1
35
- } ) ;
23
+ let elemPos = document . querySelector ( `#userInput-out` ) . getBoundingClientRect ( ) ;
24
+ let elemTop = elemPos . top ;
25
+ let elemLeft = elemPos . left ;
26
+ connections . innerHTML += `<div class="animation" id="animation-${ elem . type } " style="position: absolute; top: ${ convertPxToVh ( elemTop ) } vh; left: ${ convertPxToVw ( elemLeft ) } vw;"></div>` ;
36
27
}
37
28
38
29
function animateLUT ( elem ) {
39
- let elemPos = document . querySelector ( `#lut-${ elem . id } -out` ) . getBoundingClientRect ( ) ;
40
- let elemTop = elemPos . top ;
41
- let elemLeft = elemPos . left ;
42
- connections . innerHTML += `<div class="animation" id="animation-${ elem . type } " style="position: absolute; top: ${ convertPxToVh ( elemTop ) } vh; left: ${ convertPxToVw ( elemLeft ) } vw;"></div>` ;
30
+ let elemPos = document . querySelector ( `#lut-${ elem . id } -out` ) . getBoundingClientRect ( ) ;
31
+ let elemTop = elemPos . top ;
32
+ let elemLeft = elemPos . left ;
33
+ connections . innerHTML += `<div class="animation" id="animation-${ elem . type } - ${ elem . id } " style="position: absolute; top: ${ convertPxToVh ( elemTop ) } vh; left: ${ convertPxToVw ( elemLeft ) } vw;"></div>` ;
43
34
}
44
35
45
36
function animateDFF ( elem ) {
46
- let elemPos = document . querySelector ( `#ff-${ elem . id } -out` ) . getBoundingClientRect ( ) ;
47
- let elemTop = elemPos . top ;
48
- let elemLeft = elemPos . left ;
49
- connections . innerHTML += `<div class="animation" id="animation-${ elem . type } " style="position: absolute; top: ${ convertPxToVh ( elemTop ) } vh; left: ${ convertPxToVw ( elemLeft ) } vw;"></div>` ;
37
+ let elemPos = document . querySelector ( `#ff-${ elem . id } -out` ) . getBoundingClientRect ( ) ;
38
+ let elemTop = elemPos . top ;
39
+ let elemLeft = elemPos . left ;
40
+ connections . innerHTML += `<div class="animation" id="animation-ff-${ elem . id } " style="position: absolute; top: ${ convertPxToVh ( elemTop ) } vh; left: ${ convertPxToVw ( elemLeft ) } vw;"></div>` ;
41
+ }
42
+
43
+
44
+ function move ( element , distances ) {
45
+
46
+ console . log ( "Type of distances:" , typeof distances [ 0 ] . split ( '-' ) [ 0 ] ) ;
47
+ // Create an array of animation steps
48
+ const steps = [
49
+ distances [ 0 ] . split ( '-' ) [ 0 ] === 'right' || distances [ 0 ] . split ( '-' ) [ 0 ] === 'left'
50
+ ? { left : distances [ 0 ] . split ( '-' ) [ 1 ] }
51
+ : { top : distances [ 0 ] . split ( '-' ) [ 1 ] } ,
52
+ distances [ 1 ] . split ( '-' ) [ 0 ] === 'right' || distances [ 1 ] . split ( '-' ) [ 0 ] === 'left'
53
+ ? { left : distances [ 1 ] . split ( '-' ) [ 1 ] }
54
+ : { top : distances [ 1 ] . split ( '-' ) [ 1 ] } ,
55
+ distances [ 2 ] . split ( '-' ) [ 0 ] === 'right' || distances [ 2 ] . split ( '-' ) [ 0 ] === 'left'
56
+ ? { left : distances [ 2 ] . split ( '-' ) [ 1 ] }
57
+ : { top : distances [ 2 ] . split ( '-' ) [ 1 ] }
58
+ ] ;
59
+
60
+ // Add optional steps if they exist
61
+ if ( distances [ 3 ] ) {
62
+ steps . push (
63
+ distances [ 3 ] . split ( '-' ) [ 0 ] === 'right' || distances [ 0 ] . split ( '-' ) [ 0 ] === 'left'
64
+ ? { left : distances [ 3 ] . split ( '-' ) [ 1 ] }
65
+ : { top : distances [ 3 ] . split ( '-' ) [ 1 ] }
66
+ ) ;
67
+ }
68
+
69
+ if ( distances [ 4 ] ) {
70
+ steps . push (
71
+ distances [ 4 ] . split ( '-' ) [ 0 ] === 'right' || distances [ 0 ] . split ( '-' ) [ 0 ] === 'left'
72
+ ? { left : distances [ 4 ] . split ( '-' ) [ 1 ] }
73
+ : { top : distances [ 4 ] . split ( '-' ) [ 1 ] }
74
+ ) ;
75
+ }
76
+
77
+ // Animate each step with timeout between them
78
+ function animateStep ( index ) {
79
+ if ( index >= steps . length ) return ;
80
+
81
+ element . animate ( [ steps [ index ] ] , {
82
+ duration : 200 ,
83
+ fill : 'forwards'
84
+ } ) . onfinish = ( ) => {
85
+ // Apply styles to maintain position
86
+ Object . assign ( element . style , steps [ index ] ) ;
87
+
88
+ // Wait before starting next animation
89
+ setTimeout ( ( ) => animateStep ( index + 1 ) , 200 ) ;
90
+ } ;
91
+ }
92
+
93
+ // Start the animation sequence
94
+ animateStep ( 0 ) ;
95
+ }
96
+
97
+ function strToInt ( value ) {
98
+ if ( value . substring ( value . length - 2 ) === 'vh' ) {
99
+ return Number ( value . replace ( 'vh' , '' ) ) ;
100
+ } else {
101
+ return Number ( value . replace ( 'vw' , '' ) ) ;
102
+ }
50
103
}
104
+
105
+
106
+ forward . addEventListener ( 'click' , function ( ) {
107
+ // let element = 'userInput-out';
108
+ let element = 'ff--1' ;
109
+ let elem = document . getElementById ( `animation-${ element } ` ) ;
110
+ let wire1 = document . getElementById ( `${ element } -Wire1` )
111
+ let wire2 = document . getElementById ( `${ element } -Wire2` )
112
+ let wire3 = document . getElementById ( `${ element } -Wire3` )
113
+ let wire4 = document . getElementById ( `${ element } -Wire4` ) ?? null ;
114
+ let wire5 = document . getElementById ( `${ element } -Wire5` ) ?? null ;
115
+ animateElement ( elem , [ wire1 , wire2 , wire3 , wire4 , wire5 ] ) ;
116
+ } ) ;
117
+
118
+ function animateElement ( elem , wires ) {
119
+ let value1 = strToInt ( wires [ 0 ] . style . marginLeft ) + strToInt ( wires [ 0 ] . style . width ) - .3 + 'vw' ;
120
+ let value2 = elem . style . top > wires [ 1 ] . style . marginTop
121
+ ? strToInt ( wires [ 1 ] . style . marginTop ) - .5 + 'vh'
122
+ : strToInt ( wires [ 1 ] . style . marginTop ) + strToInt ( wires [ 1 ] . style . height ) - 1 + 'vh' ;
123
+ let value3 = elem . style . left > wires [ 2 ] . style . marginLeft
124
+ ? strToInt ( wires [ 2 ] . style . marginLeft ) - .25 + 'vw'
125
+ : strToInt ( wires [ 2 ] . style . marginLeft ) + strToInt ( wires [ 2 ] . style . width ) - .25 + 'vw' ;
126
+ let value4 = wires [ 3 ] != null ? elem . style . top > wires [ 3 ] . style . marginTop
127
+ ? strToInt ( wires [ 3 ] . style . marginTop ) - .5 + 'vh'
128
+ : strToInt ( wires [ 3 ] . style . marginTop ) + strToInt ( wires [ 3 ] . style . height ) - 1.25 + 'vh' : null ;
129
+ let value5 = wires [ 4 ] != null ? strToInt ( wires [ 4 ] . style . marginLeft ) + strToInt ( wires [ 4 ] . style . width ) - .25 + 'vw' : null ;
130
+ let distances = [ `right-${ value1 } ` , `up-${ value2 } ` , `right-${ value3 } ` ] ;
131
+ if ( wires [ 3 ] != null ) distances . push ( `down-${ value4 } ` ) ;
132
+ if ( wires [ 4 ] != null ) distances . push ( `right-${ value5 } ` ) ;
133
+ move ( elem , distances ) ;
134
+ } ;
0 commit comments