Add expression for Math.abs

This commit is contained in:
Andreas Hocevar
2021-08-03 23:51:24 +02:00
parent 05eac3e384
commit f0cac76718
2 changed files with 18 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ import {asArray, isStringColor} from '../color.js';
* * `['clamp', value, low, high]` clamps `value` between `low` and `high`
* * `['%', value1, value2]` returns the result of `value1 % value2` (modulo)
* * `['^', value1, value2]` returns the value of `value1` raised to the `value2` power
* * `['abs', value1]` returns the absolute value of `value1`
*
* * Transform operators:
* * `['case', condition1, output1, ...conditionN, outputN, fallback]` selects the first output whose corresponding
@@ -551,6 +552,17 @@ Operators['^'] = {
},
};
Operators['abs'] = {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function (context, args) {
assertArgsCount(args, 1);
assertNumbers(args);
return `abs(${expressionToGlsl(context, args[0])})`;
},
};
Operators['>'] = {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;

View File

@@ -235,6 +235,12 @@ describe('ol.style.expressions', function () {
expect(expressionToGlsl(context, ['^', ['%', ['time'], 10], 2])).to.eql(
'pow(mod(u_time, 10.0), 2.0)'
);
expect(
expressionToGlsl(context, [
'abs',
['-', ['get', 'attr3'], ['get', 'attr2']],
])
).to.eql('abs((a_attr3 - a_attr2))');
expect(expressionToGlsl(context, ['>', 10, ['get', 'attr4']])).to.eql(
'(10.0 > a_attr4)'
);