Hi, has anyone found a way to play videos on the w...
# multiplatform
e
Hi, has anyone found a way to play videos on the web target yet?? using cmp
p
Just see this lib in some posts here: https://github.com/TheReprator/KVideo-Player
👀 1
c
If you want to use the Web target extensively, it's a good idea to get familiar with JavaScript's core frameworks. You can add a native media element to the DOM and use that directly. This probably seems like a lot of work for just one feature, but you'll likely encounter the same situation in other features as your app grows, so it's worth it to learn to cleanly integrate native web components within your compose app. If you have some specific questions about the integration, I can probably help, but I'll leave it at this until there are specific questions.
e
I tried integrating with DOM, but since im using webmain, there are no libraries available that'd allow the js integration, some support js, the others wasmjs, but for webmain you need libraries that support both
c
You're using webMain because you need both WasmJs and JS support? If not, you'll probably have a smoother experience just picking one of them first... With that said, integrating with the DOM does not require any libraries, so it's one of the things you should have no trouble with even on webMain. Here's an example of injecting a
div
in order to act as the host for compose. The created div can be used for anything - in this case, for hosting the compose canvas - and the same method can be used to add other native DOM components. For many of these common components, their basic functionality is built in to compose. If you're using something more complex, you may need to use some raw JS functions, but I'm not yet sure that you'll need them.
Copy code
import ui.App

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.ComposeViewport
import kotlinx.browser.document
import org.w3c.dom.HTMLDivElement
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    val rootDiv = addDivToBody()

    ComposeViewport(viewportContainerId = rootDiv.id) {
        App()
    }
}

@OptIn(ExperimentalUuidApi::class)
private fun addDivToBody(): HTMLDivElement {
    val elementId: String = Uuid.random().toString()
    val div = (document.createElement("div") as HTMLDivElement).apply {
        id = elementId
        classList.add("fullscreen")
        document.body?.appendChild(this)
    }
    return div
}
p
If you need common dom api from webMain target, add dependency org.jetbrains.kotlinxkotlinx browser0.5.0 to it
e
i tried this, it draws over the other compose components, turns out it renders on a different layer, can anything be done about it??
c
Drawing over other components, in the DOM, is the expected behavior. Compose is rendered in a single Canvas element within the DOM; explaining why other DOM components can't interact directly with a Canvas render is far beyond the scope of a Slack comment, so let's leave that topic for now. The solution is to create a sized blank area within your Compose render and overlay the DOM component on top of it. You'll need to read measurement info from your Composable component and use that to update the CSS style of your overlay element to keep the overlay perfectly synched with the Composable. The end result will look as if you've injected the element into the Compose Canvas, even if the reality is that it's simply an overlay. This is a pretty advanced technique within the realm of UI implementations, so don't worry if it seems intimidating right now! This is intimidating for most engineers, especially if they're new to the framework.