Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support Compose Multiplatform #142

Open
egorikftp opened this issue Feb 10, 2024 · 3 comments
Open

[Feature] Support Compose Multiplatform #142

egorikftp opened this issue Feb 10, 2024 · 3 comments

Comments

@egorikftp
Copy link

Due to migration to new Modifier API, please consider to add support Compose Multiplatform 馃檪

@Darkrai9x
Copy link

Darkrai9x commented Feb 24, 2024

@PatilShreyas In Compose Multiplatform you can try this for jvm and ios

val pictureRecorder = PictureRecorder()
CacheDrawModifierNode {
            val width = this.size.width
            val height = this.size.height
            onDrawWithContent {
                val pictureCanvas =
                    pictureRecorder.beginRecording(Rect.makeWH(width, height))
                        .asComposeCanvas()
                draw(this, this.layoutDirection, pictureCanvas, this.size) {
                    this@onDrawWithContent.drawContent()
                }
                val picture = pictureRecorder.finishRecordingAsPicture()
                drawIntoCanvas { canvas -> canvas.nativeCanvas.drawPicture(picture) }
            }
        },

And convert picture to ImageBitmap

            val bitmap = org.jetbrains.skia.Bitmap()
            val ci = ColorInfo(
                ColorType.BGRA_8888, ColorAlphaType.OPAQUE, ColorSpace.sRGB,
            )
            bitmap.setImageInfo(ImageInfo(ci, width.toInt(), height.toInt()))
            bitmap.allocN32Pixels(width.toInt(), height.toInt())
            val canvas = org.jetbrains.skia.Canvas(bitmap)
            canvas.drawPicture(picture)
            bitmap.setImmutable()
            bitmap.asComposeImageBitmap()

@PatilShreyas
Copy link
Owner

Thanks for the insights @Darkrai9x, will try this out. Also, will need to explore this at least for Desktop and iOS platforms and as a low priority for the web as a target.

@egorikftp
Copy link
Author

egorikftp commented Apr 23, 2024

Note: In Compose 1.7+ the API changed
https://developer.android.com/develop/ui/compose/graphics/draw/modifiers#composable-to-bitmap

Also looks like new API KMP compatible 馃コ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants