Skip to content

Instantly share code, notes, and snippets.

@jobsturm
Created January 22, 2016 15:46
Show Gist options
  • Save jobsturm/33dccbc8de57b2bf8c08 to your computer and use it in GitHub Desktop.
Save jobsturm/33dccbc8de57b2bf8c08 to your computer and use it in GitHub Desktop.
These two scripts combined create CSS only triangles.
@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;
}
@function fact($number) {
$value: 1;
@if $number > 0 {
@for $i from 1 through $number {
$value: $value * $i;
}
}
@return $value;
}
@function pi() {
@return 3.14159265359;
}
@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;
}
@function sinus($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;
}
@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;
}
@function tan($angle) {
@return sin($angle) / cos($angle);
}
/*
Calculations will be based on the following triangle
^
/|\
C / | \ B
/ | \
---------
A
Where B and C are always the same.
*/
// calculating function
@function calculate_lenght_C($width, $angle)
$A_angle: 90 - $angle
$B_angle: 90deg
$C_angle: 180 - ($A_angle + $B_angle)
$A_length: $width
$B_length: $A_length * sinus($B_angle) / sinus($A_angle)
$C_length: $B_length * sinus($C_angle) / sinus($A_angle)
@debug $C_angle
@debug $C_length
@return $C_length
@mixin calculated_dimensions_triangle($width, $angle)
width: 0
height: 0
border-left: $width solid transparent
border-right: $width solid transparent
border-bottom: calculate_lenght_C($width, $angle) solid
// how to use:
// @include calculated_dimensions_triangle(1920px, 90 - 45deg)
// border-bottom-color: red
// angles
$angle-white-triangle-left : 4.623deg
$angle-white-triangle-right : 9.672deg
.white-triangle-holder
height: 118px
width: 100%
position: absolute
bottom: 0px
&:before, &:after
background-color: white
position: absolute
bottom: 0px
&:before
content: " "
bottom: 0px
@include calculated_dimensions_triangle(1920px, $angle-white-triangle-left)
&:after
content: " "
bottom: 0px
&.bottom
&:after
&.top
&:before
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment