4
4
Calculate fabric pattern sizes for rectangular bottom stuff sack
5
5
*/
6
6
7
- function roundToEigthInch ( number ) {
8
- var number = ( Math . round ( number * 8 ) / 8 ) ;
9
- return number ;
10
- }
11
-
12
- function patternSize ( units , bL , bW , h ) {
13
- if ( units == 1 ) { // METRIC
14
- var csA = 4 ; // cord channel height (4cm)
15
- var hemA = 1 ; // single fold hem allowance on each end of channel (1 cm)
16
- var sA = 1 ; // general seam allowance (1 cm)
17
- }
18
- else { // IMPERIAL
19
- var csA = 1.5 ; // cord channel height (1.5")
20
- var hemA = 0.5 ; // single fold hem allowance on each end of channel (0.5")
21
- var sA = 0.5 ; // general seam allowance (0.5")
22
- }
23
-
24
-
25
- // body fabric panel
26
- var fabricL = 2 * bL + ( 2 * bW ) + ( 2 * sA ) ;
27
-
28
- if ( bW > ( 2.5 * csA ) ) { // add material so top will close when bW is much wider than channel
29
- var fabricH = h + bW - csA + ( 2 * sA ) ; // adds (1/2bW - csA) to top of fabricH to make up additional width
30
- }
31
- else {
32
- var fabricH = h + ( 0.5 * bW ) + ( 2 * sA ) ; // 1/2bW is the corner cut off bottom to make rectangular
33
- }
34
-
35
- // cord channel
36
- var channelL = 2 * bL + ( 2 * bW ) + ( 2 * hemA ) ; //hem on each end
37
- var channelH = 2 * csA + ( 2 * sA ) ; // SA on top and bottom
38
-
39
- // rounding dimensions for output
40
- if ( units == 1 ) {
41
- var fabricL = fabricL . toFixed ( 1 ) ; //trimming to closest mm
42
- var fabricH = fabricH . toFixed ( 1 ) ;
43
- var channelL = channelL . toFixed ( 1 ) ;
44
- var channelH = channelH . toFixed ( 1 ) ;
45
- }
46
- else {
47
- var fabricL = roundToEigthInch ( fabricL ) ; //rounding to nearest 1/8" increment
48
- var fabricH = roundToEigthInch ( fabricH ) ;
49
- var channelL = roundToEigthInch ( channelL ) ;
50
- var channelH = roundToEigthInch ( channelH ) ;
51
- }
52
-
53
- return [ fabricL , fabricH , channelL , channelH , sA ] ;
54
-
55
- } ;
56
-
57
-
58
-
59
- $ ( 'document' ) . ready ( function ( ) {
60
-
61
- var version = "v0.1" ; // initial version
62
- $ ( '.version' ) . html ( version ) ;
63
-
64
-
65
- $ ( '.dimension' ) . change ( function ( ) { //when any .dimension changes (input loses focus), function runs
66
-
67
- var units = $ ( "input[type=radio][name=units]:checked" ) . val ( ) ; // inches (val=0) or cm (val=1)
68
-
69
- // assign variables based on input id values from html form.
70
- //Ensure object is number otherwise arithmetic is screwy
71
- var bottomLength = Number ( $ ( '#bottomLength' ) . val ( ) ) ;
72
- var bottomWidth = Number ( $ ( '#bottomWidth' ) . val ( ) ) ;
73
- var sackHeight = Number ( $ ( '#height' ) . val ( ) ) ;
74
-
75
- var halfBottomWidth = bottomWidth / 2 ;
76
-
77
- if ( bottomLength > 1 && bottomWidth > 0 && sackHeight > 1 ) { // html input min=1, crappy validation but works
78
- var scrap = patternSize ( units , bottomLength , bottomWidth , sackHeight ) ;
79
- var fabricL = scrap [ 0 ] ;
80
- var fabricH = scrap [ 1 ] ;
81
- var channelL = scrap [ 2 ] ;
82
- var channelH = scrap [ 3 ] ;
83
- if ( units == 1 ) {
84
- var unitText = " cm" ;
85
- var sA = scrap [ 4 ] ;
86
- }
87
- else {
88
- var unitText = " in" ;
89
- var sA = scrap [ 4 ] ;
90
- }
91
-
92
- // html id returns
93
- $ ( '.patternLength' ) . html ( fabricL + unitText ) ;
94
- $ ( '.patternHeight' ) . html ( fabricH + unitText ) ;
95
- $ ( '.channelLength' ) . html ( channelL + unitText ) ;
96
- $ ( '.channelHeight' ) . html ( channelH + unitText ) ;
97
- $ ( '.sA' ) . html ( sA + unitText ) ;
98
- $ ( '.bottomWidth' ) . html ( bottomWidth + unitText ) ;
99
- $ ( '.halfBottomWidth' ) . html ( halfBottomWidth + unitText ) ;
100
- $ ( '.bottomLength' ) . html ( bottomLength + unitText ) ;
101
-
102
- }
103
- } ) ;
104
- } ) ;
7
+ calculatorSetup ( function ( ) {
8
+ let isMetric = getIsMetric ( ) ;
9
+ let fields = [ '#bottomLength' , '#bottomWidth' , '#height' ] . map ( getNumberFromField ) ;
10
+
11
+ if ( fields . some ( field => field <= 0 ) ) {
12
+ return ;
13
+ }
14
+ let [ bottomLength , bottomWidth , height ] = fields ;
15
+
16
+ const { cordChannelHeight, hem, sA } = isMetric ?
17
+ { cordChannelHeight : 4 , hem : 1 , sA : 1 } :
18
+ { cordChannelHeight : 1.5 , hem : 0.5 , sA : 0.5 } ;
19
+
20
+ let patternHeight ;
21
+
22
+ // add material so top will close when bW is much wider than channel
23
+ if ( bottomWidth > ( 2.5 * cordChannelHeight ) ) {
24
+ // adds (1/2bW - cordChannelHeight) to top of patternHeight to make up additional width
25
+ patternHeight = height + bottomWidth - cordChannelHeight + ( 2 * sA ) ;
26
+ } else {
27
+ // 1/2bW is the corner cut off bottom to make rectangular
28
+ patternHeight = height + ( 0.5 * bottomWidth ) + ( 2 * sA ) ;
29
+ }
30
+
31
+ let outputFields = {
32
+ patternLength : ( 2 * bottomLength ) + ( 2 * bottomWidth ) + ( 2 * sA ) ,
33
+ patternHeight,
34
+ channelLength : ( 2 * bottomLength ) + ( 2 * bottomWidth ) + ( 2 * hem ) , //hem on each end
35
+ channelHeight : ( 2 * cordChannelHeight ) + ( 2 * sA ) , // SA on top and bottom
36
+ sA,
37
+ bottomWidth,
38
+ halfBottomWidth : bottomWidth / 2 ,
39
+ bottomLength,
40
+ } ;
41
+
42
+ setCalculatedValues ( isMetric , outputFields ) ;
43
+ } , 'v0.1' ) ;
0 commit comments