Is there a way to display SVG in Canvas and changi...
# compose
h
Is there a way to display SVG in Canvas and changing the SVG properties ?
d
Yes, there's an SVG composable iirc.
Vector to be more specific
h
@Dominaezzz Vector Assets ? my problem is i am fetching SVG from the server, and i need w way to show it and manipulate it.
d
Oh no, you'll need to parse it and then render it using the vector composable API. I think it's called
Group
and
Path
.
h
Yeah, that’s what i am doing now, i thought maybe there is someone who worked on it before, or some library to help. But I guess I have to draw it and manipulate it manually. Thanks 👍
v
There is a great tool to convert svg to compose vector code: https://github.com/DevSrSouza/svg-to-compose It can be hooked to gradle build process very easily. Here is an idea
Copy code
// generate_icons.gradle.kts
import br.com.devsrsouza.svg2compose.Svg2Compose
import br.com.devsrsouza.svg2compose.VectorType

buildscript {
    repositories {
        google()
        mavenCentral()
        maven { url = uri("<https://jitpack.io>") }
    }
    dependencies {
        classpath("com.github.DevSrSouza:svg-to-compose:0.7.0")
    }
}

val inputDir = file("src/main/svg")
val outputDir = file("build/generated/icons_gen")

tasks.register("generateIcons") {
    inputs.dir(inputDir)
    outputs.dir(outputDir)
    doLast {
        Svg2Compose.parse(
            applicationIconPackage = ...,
            accessorName = ...,
            vectorsDirectory = inputDir,
            outputSourceDirectory = outputDir,
            type = VectorType.SVG,
            allAssetsPropertyName = "AllIcons"
        )
    }
}

// build.gradle
apply from: 'generate_icons.gradle.kts'

android {
    ...
    sourceSets {
        main {
            java.srcDirs += file("build/generated/icons_gen")
        }
    }
}

preBuild.dependsOn 'generateIcons'
👍 1
h
thanks @Vsevolod Ganin I already look at it, what i am trying to achieve is : 1- fetch SVG from the backend, and then display it in the application. 2- give the user the ability to drag, rotate, scale the SVG element, beside changing its color, stroke color / width ..... Guess I have to build it from scratch
v
Ah, all at runtime, I see. Then yeah, no libraries for that afaik
n
I recommend parsing the SVG into an
ImageVector
and drawing that by passing it to a
VectorPainter
h
@Nader Jawad I tried playing with VectorPainter but AS gave me this error :
Copy code
Cannot access '<init>': it is internal in 'VectorPainter'
n
You want the function constructor which is rememberVectorPainter
❤️ 1
h
Thanks @Nader Jawad it worked
👍 1