Skip to content

Commit

Permalink
[FEAT/#28] customShadow ๊ตฌํ˜„
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyobeen-Park committed Jul 9, 2024
1 parent 39cbd47 commit 3f45ad5
Showing 1 changed file with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.terning.core.designsystem.component.modifier

import android.graphics.BlurMaskFilter
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.terning.core.designsystem.theme.Grey200

@Composable
fun Modifier.customShadow(
color: Color = Grey200,
shadowRadius: Dp = 0.dp,
shadowWidth: Dp = 2.dp,
offsetX: Dp = 0.dp,
offsetY: Dp = 0.dp,
) = composed {
val paint: Paint = remember { Paint() }
val density = LocalDensity.current
val radius = shadowRadius + shadowWidth
val blurRadiusPx = with(density) { radius.toPx() }
val maskFilter = remember {
BlurMaskFilter(blurRadiusPx, BlurMaskFilter.Blur.NORMAL)
}
drawBehind {
drawIntoCanvas { canvas ->
val frameworkPaint = paint.asFrameworkPaint()
if (radius != 0.dp) {
frameworkPaint.maskFilter = maskFilter
}
frameworkPaint.color = color.toArgb()

val leftPixel = offsetX.toPx()
val topPixel = offsetY.toPx()
val rightPixel = size.width + leftPixel
val bottomPixel = size.height + topPixel

if (radius > 0.dp) {
val radiusPx = radius.toPx()
canvas.drawRoundRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
radiusX = radiusPx,
radiusY = radiusPx,
paint = paint,
)
} else {
canvas.drawRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
paint = paint,
)
}
}
}
}

0 comments on commit 3f45ad5

Please sign in to comment.