2022-02-05 17:44:23 +01:00
|
|
|
---
|
|
|
|
title: Reporting a percentage option value in millimeter
|
|
|
|
---
|
|
|
|
|
|
|
|
Percentage options are great for parametric desing, but not always
|
2022-02-19 08:04:25 +01:00
|
|
|
very intuitive for the user. For example: Setting the `chestEase`
|
2022-02-05 17:44:23 +01:00
|
|
|
option to `9%` is not very meaningful unless you happen to know
|
|
|
|
what that percentage is based on.
|
|
|
|
|
|
|
|
To address this common grievance, FreeSewing allows you to add a
|
|
|
|
`toAbs` method that should return the value of the option in
|
|
|
|
millimeter.
|
|
|
|
|
|
|
|
## Structure
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
The `toAbs` property should hold a function with the following
|
2022-02-05 17:44:23 +01:00
|
|
|
signature:
|
|
|
|
|
|
|
|
```js
|
|
|
|
function toAbs(percentage, settings) {
|
|
|
|
// return value in millimeter here
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The first parameter is the percentage value provided by the user (for example
|
|
|
|
`0.5` for `50%`).
|
2022-02-19 08:04:25 +01:00
|
|
|
The second parameter is the pattern's run-time configuration
|
2022-02-05 17:44:23 +01:00
|
|
|
or [settings](/reference/api/settings) which holds -- among other things -- the
|
|
|
|
measurements provided by the user.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
In our example above, let's say that the `chestEase` option is
|
|
|
|
a simple percentage of the `chest` measurement. Our option
|
|
|
|
configuration could like like this:
|
|
|
|
|
|
|
|
```js
|
|
|
|
chestEase: {
|
|
|
|
pct: 8,
|
|
|
|
min: 0,
|
|
|
|
max: 20,
|
|
|
|
toAbs: function(value, settings) {
|
|
|
|
return settings.measurements.chest * value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
With object destructuring and fat-arrow notation,
|
2022-02-05 17:44:23 +01:00
|
|
|
you can write it a bit terser like this:
|
|
|
|
|
|
|
|
```js
|
|
|
|
toAbs: (val, { measurements }) => measurements.chest * val
|
|
|
|
```
|
|
|
|
|
|
|
|
## Using pctBasedOn for simple measurement fractions
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
Many percentage options represent a simple fraction of a measurement
|
2022-02-05 17:44:23 +01:00
|
|
|
(chest circumference in the example above).
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
As this scenario is so common, `@freesewing/core` exports a `pctBasedOn` method
|
2022-02-05 17:44:23 +01:00
|
|
|
that will do the work for you:
|
|
|
|
|
|
|
|
```js
|
|
|
|
// First import the method
|
|
|
|
import { pctBasedOn } from '@freesewing/core'
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
// ...
|
|
|
|
options: {
|
|
|
|
chestEase: {
|
|
|
|
pct: 8,
|
|
|
|
min: 0,
|
|
|
|
max: 20,
|
|
|
|
// Pass the measurement name as parameter
|
|
|
|
// and spread the return value into your option
|
|
|
|
...pctBasedOn('chest')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This will not only add an `toAbs()` method to your option -- one that will return
|
2022-02-19 08:04:25 +01:00
|
|
|
the value in millimeter of whatever percentage the option is set to -- it will
|
2022-02-05 17:44:23 +01:00
|
|
|
also add a `fromAbs()` method that does the inverse: return the percentage of
|
2022-02-19 08:04:25 +01:00
|
|
|
any millimeter value passed into it. See [Setting a value in millimeter as a
|
2022-02-05 17:44:23 +01:00
|
|
|
percentage option](/api/config/options/pct/fromabs) for details.
|