Created
September 22, 2023 19:15
-
-
Save halilozercan/c4a96513879239c4af1e62fe6af3c79f to your computer and use it in GitHub Desktop.
Variable Font Emoji in Compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Download the varimoji font from https://crt-mate.github.io/varimoji/varimojiVF.woff2 | |
// Convert it to TTF and put that under res/font/ | |
@OptIn(ExperimentalTextApi::class) | |
@Composable | |
fun VariableFontEmoji() { | |
var actv by remember { mutableFloatStateOf(50f) } | |
var vlnc by remember { mutableFloatStateOf(50f) } | |
Column( | |
modifier = Modifier.padding(32.dp), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = "A", | |
fontFamily = FontFamily( | |
Font( | |
resId = R.font.varimoji, | |
variationSettings = FontVariation.Settings( | |
FontVariation.Setting("ACTV", actv), | |
FontVariation.Setting("VLNC", vlnc), | |
) | |
) | |
), | |
fontSize = 140.sp | |
) | |
Spacer(modifier = Modifier.height(32.dp)) | |
Box(modifier = Modifier | |
.fillMaxSize() | |
.pointerInput(Unit) { | |
awaitEachGesture { | |
do { | |
val event = awaitPointerEvent() | |
event.changes.filter { it.pressed }.forEach { | |
val coercedPosition = Offset( | |
it.position.x.coerceIn(0f, size.width.toFloat()), | |
it.position.y.coerceIn(0f, size.height.toFloat()), | |
) | |
actv = coercedPosition.x / size.width * 100f | |
vlnc = coercedPosition.y / size.height * 100f | |
it.consume() | |
} | |
} while (event.changes.all { it.pressed }) | |
} | |
} | |
.drawBehind { | |
drawRect( | |
Brush.linearGradient( | |
listOf(Color(0xFFE5E5E5), Color.White), | |
start = Offset(size.width / 2f, size.height / 2f), | |
end = Offset(size.width, 0f) | |
), | |
topLeft = Offset(size.width / 2f, 0f), | |
size = size / 2f | |
) | |
drawRect( | |
Brush.linearGradient( | |
listOf(Color(0xFFE5E5E5), Color.White), | |
start = Offset(size.width / 2f, size.height / 2f), | |
end = Offset(0f, size.height) | |
), | |
topLeft = Offset(0f, size.height / 2f), | |
size = size / 2f | |
) | |
val point = Offset(actv / 100f * size.width, vlnc / 100f * size.height) | |
drawCircle( | |
Brush.radialGradient( | |
listOf(Color.Red.copy(alpha = 0.5f), Color.Transparent), | |
radius = 36.dp.toPx(), | |
center = point | |
), | |
radius = 36.dp.toPx(), | |
center = point | |
) | |
}) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment