Last active
June 7, 2026 13:05
-
-
Save moshenskyi/a514fd6d81f0950f79004376f7643129 to your computer and use it in GitHub Desktop.
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
| /* | |
| * Copyright 2026 Nazarii Moshenskyi | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| import android.graphics.RuntimeShader | |
| import androidx.compose.foundation.Canvas | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.gestures.detectDragGestures | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.fillMaxWidth | |
| import androidx.compose.foundation.layout.height | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.foundation.layout.width | |
| import androidx.compose.foundation.shape.RoundedCornerShape | |
| import androidx.compose.material3.Text | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.getValue | |
| import androidx.compose.runtime.mutableIntStateOf | |
| import androidx.compose.runtime.remember | |
| import androidx.compose.runtime.setValue | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.draw.clip | |
| import androidx.compose.ui.geometry.Offset | |
| import androidx.compose.ui.graphics.BlendMode | |
| import androidx.compose.ui.graphics.Brush | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.graphics.CompositingStrategy | |
| import androidx.compose.ui.graphics.Path | |
| import androidx.compose.ui.graphics.ShaderBrush | |
| import androidx.compose.ui.graphics.StrokeCap | |
| import androidx.compose.ui.graphics.StrokeJoin | |
| import androidx.compose.ui.graphics.drawscope.Stroke | |
| import androidx.compose.ui.graphics.graphicsLayer | |
| import androidx.compose.ui.input.pointer.pointerInput | |
| import androidx.compose.ui.semantics.clearAndSetSemantics | |
| import androidx.compose.ui.semantics.contentDescription | |
| import androidx.compose.ui.semantics.semantics | |
| import androidx.compose.ui.semantics.stateDescription | |
| import androidx.compose.ui.tooling.preview.Preview | |
| import androidx.compose.ui.unit.dp | |
| import com.moshenskyi.shaderssample.ui.theme.ShadersSampleTheme | |
| import org.intellij.lang.annotations.Language | |
| @Language("AGSL") | |
| private const val GRAIN_SHADER = """ | |
| uniform float2 resolution; | |
| const float2 SEED_VECTOR = float2(12.9897, 78.2323); | |
| const float HASH_SCALE = 43758.5453; | |
| // try to experiment with frequency and amplitude and see how it changes the pattern | |
| const float LINE_FREQUENCY = 1000.0; | |
| const float LINE_AMPLITUDE = 0.03; | |
| float generateGrain(float2 point) { | |
| // simple coordinate hash to convert 2D coord into pseudo-random scalar | |
| // without the seed vector symetric coordinates will produce diagonal stripes | |
| float hashedCoord = dot(point, SEED_VECTOR); | |
| // multiply by a large number to spread the fractional part | |
| float noiseWave = sin(hashedCoord) * HASH_SCALE; | |
| return fract(noiseWave); // get fractional part | |
| } | |
| float generateLine(float coord) { | |
| return sin(coord * LINE_FREQUENCY) * LINE_AMPLITUDE; | |
| } | |
| half4 main(float2 fragCoord) { | |
| float2 uv = fragCoord / resolution; | |
| // grey color | |
| float3 baseColor = float3(0.72, 0.72, 0.70); | |
| float grain = generateGrain(fragCoord); | |
| // I use uv.y to generate horizontal lines | |
| float lines = generateLine(uv.y); | |
| // raw noise is too bright, multiply it by 0.3 | |
| float brightness = grain * 0.3 + lines; | |
| float3 color = baseColor + brightness; | |
| return half4(color, 1.0); | |
| } | |
| """ | |
| @Composable | |
| fun ScratchCard( | |
| modifier: Modifier = Modifier, | |
| cardNumber: String = "4242 4242 4242 4242" | |
| ) { | |
| val cardBackground = remember { | |
| Brush.linearGradient( | |
| colors = listOf( | |
| Color(0xFF222831), | |
| Color(0xFF393E46), | |
| Color(0xFF1B1F24) | |
| ) | |
| ) | |
| } | |
| Box( | |
| modifier = modifier | |
| .clip(RoundedCornerShape(24.dp)) | |
| .background(cardBackground) | |
| .padding(24.dp), | |
| contentAlignment = Alignment.Center | |
| ) { | |
| Text( | |
| text = cardNumber, | |
| modifier = Modifier.clearAndSetSemantics { }, | |
| color = Color.White | |
| ) | |
| ScratchOverlay( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .height(50.dp) | |
| ) | |
| } | |
| } | |
| @Composable | |
| private fun ScratchOverlay( | |
| modifier: Modifier = Modifier | |
| ) { | |
| val shader = remember { RuntimeShader(GRAIN_SHADER) } | |
| val shaderBrush = remember { ShaderBrush(shader) } | |
| val strokeWidth = 30.dp | |
| val scratchPath = remember { Path() } | |
| var pathVersion by remember { mutableIntStateOf(0) } | |
| Canvas( | |
| modifier = modifier | |
| .graphicsLayer { | |
| compositingStrategy = CompositingStrategy.Offscreen | |
| } | |
| .semantics { | |
| contentDescription = "Scratch area" | |
| stateDescription = "Card number hidden" | |
| } | |
| .pointerInput(Unit) { | |
| detectDragGestures( | |
| onDragStart = { offset: Offset -> | |
| scratchPath.moveTo(offset.x, offset.y) | |
| pathVersion++ | |
| }, | |
| onDrag = { change, _ -> | |
| change.consume() | |
| scratchPath.lineTo(change.position.x, change.position.y) | |
| pathVersion++ | |
| } | |
| ) | |
| } | |
| ) { | |
| pathVersion | |
| shader.setFloatUniform("resolution", size.width, size.height) | |
| drawRect(brush = shaderBrush) | |
| drawPath( | |
| path = scratchPath, | |
| color = Color.Transparent, | |
| style = Stroke( | |
| width = strokeWidth.toPx(), | |
| cap = StrokeCap.Round, | |
| join = StrokeJoin.Round, | |
| ), | |
| blendMode = BlendMode.Clear | |
| ) | |
| } | |
| } | |
| @Preview | |
| @Composable | |
| fun ScratchCardPreview() { | |
| ShadersSampleTheme { | |
| Box( | |
| modifier = Modifier.fillMaxSize(), | |
| contentAlignment = Alignment.Center | |
| ) { | |
| ScratchCard( | |
| modifier = Modifier | |
| .width(340.dp) | |
| .height(210.dp) | |
| ) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment