Calculating shadow values for all Material Design elevations -
in latest material design documentation (https://www.google.com/design/spec/what-is-material/elevation-shadows.html#elevation-shadows-elevation-android-) exhaustive set of ui elements referenced respective elevation (z-index in dp). example switch elevated 1dp, while dialog elevated 24dp. google's list of ui elements uses 10 different elevation levels. since elevation decides shadow of element, we'll need 10 different shadows. , that's i'm lost.
how calculate/deduce right shadow values (color, x-offset, y-offset, blur, spread) each elevation level?
i've found different sources have calculated shadow values 5 different elevations (https://news.layervault.com/stories/42319-calculating-shadow-values-for-material-design). however, 5 elevation steps not enough, nor give explanation how got these respective values.
here's function came gives result, believe:
(javascript)
function getshadow(object, dp) { if (dp <= 0) { panel.style.boxshadow = "none"; return; } panel.style.boxshadow = "0px " + dp + "px " + dp + "px " + "rgba(0, 0, 0, .38)"; }
a jfiddle: https://jsfiddle.net/crkb906z/
basically, distance base layer how far down shadow top is, , use same value blur make fuzzier material gets higher.
i compared result of 3 shadows used polymer , comparable. since can't find consistency between 5 layer shadows people provide, imagine how it's calculated isn't important.
edit
okay, after studying available box-shadows 5 depths (they originated here, google has since removed documentation), i've come more complex formula results in shadows more examples in link:
function applyshadow(element, dp) { if (dp == 0) { element.style.boxshadow = "none"; } else { var shadow = "0px "; var ambienty = dp; var ambientblur = dp == 1 ? 3 : dp * 2; var ambientalpha = (dp + 10 + (dp / 9.38)) / 100; shadow += ambienty + "px " + ambientblur + "px rgba(0, 0, 0, " + ambientalpha + "), 0px "; var directy = (dp < 10 ? (dp % 2 == 0 ? dp - ((dp / 2) - 1) : (dp - ((dp - 1) / 2))) : dp - 4); var directblur = dp == 1 ? 3 : dp * 2; var directalpha = (24 - math.round(dp / 10)) / 100; shadow += directy + "px " + directblur + "px rgba(0, 0, 0, " + directalpha + ")"; element.style.boxshadow = shadow; } }
i update https://jsfiddle.net/crkb906z/1/ show difference.
Comments
Post a Comment