1
+ import PathHelper from '@markroland/path-helper'
2
+ import * as Utilities from './utils/Utilities.js' ;
3
+
4
+ class SpinMorph {
5
+
6
+ constructor ( env ) {
7
+ this . key = this . constructor . name . toLowerCase ( ) ;
8
+ this . name = 'Spin Morph' ;
9
+ this . env = env ;
10
+
11
+ this . max_r = 0.5 * Math . min (
12
+ ( env . table . x . max - env . table . x . min ) ,
13
+ ( env . table . y . max - env . table . y . min )
14
+ ) ;
15
+
16
+ this . config = {
17
+ "radius" : {
18
+ "name" : "Radius (r)" ,
19
+ "value" : null ,
20
+ "input" : {
21
+ "type" : "createSlider" ,
22
+ "params" : [
23
+ 1 ,
24
+ 0.5 * Math . min ( env . table . x . max , env . table . y . max ) ,
25
+ 0.5 * this . max_r ,
26
+ 1
27
+ ] ,
28
+ "class" : "slider" ,
29
+ "displayValue" : true
30
+ }
31
+ } ,
32
+ "angle" : {
33
+ "name" : "Start Angle (𝜃)" ,
34
+ "value" : null ,
35
+ "input" : {
36
+ "type" : "createSlider" ,
37
+ "params" : [
38
+ 0 ,
39
+ 360 ,
40
+ 0 ,
41
+ 1
42
+ ] ,
43
+ "class" : "slider" ,
44
+ "displayValue" : true
45
+ }
46
+ } ,
47
+ "reverse" : {
48
+ "name" : "Reverse" ,
49
+ "value" : null ,
50
+ "input" : {
51
+ "type" : "createCheckbox" ,
52
+ "attributes" : [ {
53
+ "type" : "checkbox" ,
54
+ "checked" : null
55
+ } ] ,
56
+ "params" : [ 0 , 1 , 0 ] ,
57
+ "displayValue" : false
58
+ }
59
+ }
60
+ } ;
61
+
62
+ this . path = [ ] ;
63
+ }
64
+
65
+ draw ( ) {
66
+
67
+ // Update object
68
+ this . config . radius . value = parseInt ( document . querySelector ( '#pattern-controls > div:nth-child(1) > input' ) . value ) ;
69
+ this . config . angle . value = parseInt ( document . querySelector ( '#pattern-controls > div:nth-child(2) > input' ) . value ) ;
70
+
71
+ // Display selected values
72
+ document . querySelector ( '#pattern-controls > div.pattern-control:nth-child(1) > span' ) . innerHTML = this . config . radius . value ;
73
+ document . querySelector ( '#pattern-controls > div.pattern-control:nth-child(2) > span' ) . innerHTML = this . config . angle . value + '°' ;
74
+
75
+ // Calculate path for Circle at center
76
+ if ( ! this . path . length ) {
77
+ this . path = this . constructPath ( ) ;
78
+ }
79
+
80
+ return this . path ;
81
+ }
82
+
83
+ /**
84
+ * Calculate coordinates for a circle
85
+ **/
86
+ constructPath ( ) {
87
+
88
+ // Start path at far right
89
+ let path = [ ] ;
90
+
91
+ const PathHelp = new PathHelper ( ) ;
92
+
93
+
94
+
95
+ let square = PathHelp . polygon ( 4 , 0.5 * this . max_r ) ;
96
+ square = PathHelp . translatePath ( square , [ 0.5 * this . max_r , 0 ] ) ;
97
+
98
+ let circle = PathHelp . polygon ( 60 , 0.5 * this . max_r ) ;
99
+ circle = PathHelp . translatePath ( circle , [ 0.5 * this . max_r , 0 ] ) ;
100
+
101
+ const i_max = 90 ;
102
+
103
+ for ( let i = 0 ; i < i_max ; i ++ ) {
104
+ let square_i = PathHelp . deepCopy ( square ) ;
105
+ square_i = PathHelp . rotatePath ( square_i , ( 2 * Math . PI ) * ( i / i_max ) ) ;
106
+ path = path . concat ( square_i ) ;
107
+ }
108
+
109
+ for ( let i = 0 ; i < i_max ; i ++ ) {
110
+ let square_i = PathHelp . deepCopy ( square ) ;
111
+ square_i = PathHelp . rotatePath ( square_i , ( 2 * Math . PI ) * ( i / i_max ) ) ;
112
+ square_i = PathHelp . scalePath ( square_i , 1 - i / i_max ) ;
113
+
114
+ let circle_i = PathHelp . deepCopy ( circle ) ;
115
+ circle_i = PathHelp . rotatePath ( circle_i , ( 2 * Math . PI ) * ( i / i_max ) ) ;
116
+ circle_i = PathHelp . scalePath ( circle_i , 1 - i / i_max ) ;
117
+
118
+ let morph = PathHelp . morph ( square_i , circle_i , i / i_max ) ;
119
+ path = path . concat ( morph ) ;
120
+ }
121
+ // path = path.concat(square);
122
+
123
+ // ---
124
+
125
+ path = PathHelp . simplify ( path , 0.33 * this . env . ball . diameter ) ;
126
+
127
+ path = PathHelp . reflectPath ( path , "y" ) ;
128
+
129
+ return path ;
130
+ }
131
+ }
132
+
133
+ export default SpinMorph ;
0 commit comments