1
0
mirror of https://github.com/locomotivemtl/locomotive-boilerplate.git synced 2026-01-15 00:55:08 +08:00

Add SCSS Math functions

This commit is contained in:
Lucas Vallenet
2022-05-20 11:42:06 +02:00
parent 32ba1c0eeb
commit c2db2e1922
2 changed files with 124 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
// Tools
// ==========================================================================
@import "tools/maths";
@import "tools/functions";
@import "tools/mixins";
@import "tools/fonts";

View File

@@ -0,0 +1,123 @@
// ==========================================================================
// Tools / Maths
// ==========================================================================
// Remove units from the given number
// @param {number} $number The number with units
// @return {number}
@function strip-units($number) {
@return $number / ($number * 0 + 1);
}
// Square root function
//
// @param {string} $number The number
// @return {string} The sqrt number
@function sqrt($number) {
$x: 1;
$value: $x;
@for $i from 1 through 10 {
$value: $x - ($x * $x - abs($number)) / (2 * $x);
$x: $value;
}
@return $value;
}
// Power function
//
// @param {number} $number Number to apply power
// @param {number} $exp The exponant for the power
// @return {number} The powered number
@function pow($number, $exp) {
$value: 1;
@if $exp > 0 {
@for $i from 1 through $exp {
$value: $value * $number;
}
} @else if $exp < 0 {
@for $i from 1 through -$exp {
$value: $value / $number;
}
}
@return $value;
}
// Factorial function
//
// @param {number} $number The number to factorize
// @return {number} The factorised number
@function fact($number) {
$value: 1;
@if $number > 0 {
@for $i from 1 through $number {
$value: $value * $i;
}
}
@return $value;
}
// Pi reference
//
// @return {number} Retrun PI with 11 decimals
@function pi() {
@return 3.14159265359;
}
// Convert deg to rad
//
// @param {string} $angle The angle to convert
// @return {number} The unitless angle converted to rad
@function rad($angle) {
$unit: unit($angle);
$unitless: $angle / ($angle * 0 + 1);
// If the angle has 'deg' as unit, convert to radians.
@if $unit == deg {
$unitless: $unitless / 180 * pi();
}
@return $unitless;
}
// Calculate the sinus of an angle
//
// @param {string} $angle The angle to compute
// @return {number} The sinus of the given angle
@function sin($angle) {
$sin: 0;
$angle: rad($angle);
// Iterate a bunch of times.
@for $i from 0 through 10 {
$sin: $sin + pow(-1, $i) * pow($angle, (2 * $i + 1)) / fact(2 * $i + 1);
}
@return $sin;
}
// Calculate the cosinus of an angle
//
// @param {string} $angle The angle to compute
// @return {number} The cosinus of the given angle
@function cos($angle) {
$cos: 0;
$angle: rad($angle);
// Iterate a bunch of times.
@for $i from 0 through 10 {
$cos: $cos + pow(-1, $i) * pow($angle, 2 * $i) / fact(2 * $i);
}
@return $cos;
}
// Calculate the tangent of an angle
//
// @param {string} $angle The angle to compute
// @return {number} The tangent of the given angle
@function tan($angle) {
@return sin($angle) / cos($angle);
}