GarouDan
03/02/2019, 3:44 PMimport kotlinx.interop.wasm.dom.*
import kotlinx.wasm.jsinterop.*
which dependencies should I include in my build.gradle.kts
configuration file?
I’m trying to follow the steps of this project but I didn’t find the correct dependencies yet:
https://github.com/JetBrains/kotlin-native/tree/master/samples/html5CanvasDominaezzz
03/02/2019, 3:45 PMDominaezzz
03/02/2019, 3:45 PMwasm
target.Dominaezzz
03/02/2019, 3:46 PMbuild.gradle
?GarouDan
03/02/2019, 4:29 PMimport org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import java.nio.file.Paths
plugins {
kotlin("multiplatform")
}
repositories {
jcenter()
}
val packageName = "kotlinx.interop.wasm.dom"
val jsinteropKlibFileName = Paths.get(buildDir.toString(), "klib", "$packageName-jsinterop.klib").toString()
kotlin {
wasm32 {
compilations["main"].outputKinds("EXECUTABLE")
compilations["main"].entryPoint("com.company.team.project.application.native_.wasm.main")
}
sourceSets {
val wasm32Main by getting {
kotlin.srcDir("src/main/kotlin")
resources.srcDir("src/main/resources")
dependencies {
implementation(files(jsinteropKlibFileName))
}
}
val wasm32Test by getting {
kotlin.srcDir("src/test/kotlin")
resources.srcDir("src/test/resources")
dependencies {
}
}
}
}
tasks {
withType<KotlinNativeCompile>().configureEach {
dependsOn("jsinterop")
}
}
task("jsinterop") {
val os = org.gradle.internal.os.OperatingSystem.current()!!
val workingDir = projectDir
val ext = if (os.isWindows) ".bat" else ""
val distributionPath = project.properties["org.jetbrains.kotlin.native.home"] as String
val jsinteropCommand = Paths.get(file(distributionPath).path, "bin", "jsinterop$ext").toString()
inputs.property("jsinteropCommand", jsinteropCommand)
inputs.property("jsinteropPackageName", packageName)
outputs.file(jsinteropKlibFileName)
exec {
commandLine(
jsinteropCommand,
"-pkg",
packageName,
"-o",
jsinteropKlibFileName,
"-target",
"wasm32"
)
}
}
Dominaezzz
03/02/2019, 4:31 PMGarouDan
03/02/2019, 4:33 PMExample.kt:7:36: error: unresolved reference: textContent
document.getElementById("target").textContent = "Test"
GarouDan
03/02/2019, 4:34 PMpackage com.company.team.project.application.native_.wasm
import kotlinx.interop.wasm.dom.*
import kotlinx.wasm.jsinterop.*
fun main() {
document.getElementById("target").textContent = "Test"
}
Dominaezzz
03/02/2019, 4:35 PMGarouDan
03/02/2019, 4:36 PMdocument.getElementById("target").textContent
for js, is this exists for wasm? Since my autocompletion is not working I can’t really knowGarouDan
03/02/2019, 4:36 PMdocument.getElementById("myCanvas").asCanvas
but I would like to write just a text for nowDominaezzz
03/02/2019, 4:38 PMGarouDan
03/02/2019, 4:39 PMGarouDan
03/02/2019, 4:47 PMfun main() {
val canvas = document.getElementById("myCanvas").asCanvas
val ctx = canvas.getContext("2d")
val rect = canvas.getBoundingClientRect()
val rectLeft = rect.left
val rectTop = <http://rect.top|rect.top>
var mouseX: Int = 0
var mouseY: Int = 0
var draw: Boolean = false
document.setter("onmousemove") { arguments: ArrayList<JsValue> ->
val event = MouseEvent(arguments[0])
mouseX = event.getInt("clientX") - rectLeft
mouseY = event.getInt("clientY") - rectTop
if (mouseX < 0) mouseX = 0
if (mouseX > 639) mouseX = 639
if (mouseY < 0) mouseY = 0
if (mouseY > 479) mouseY = 479
}
document.setter("onmousedown") {
draw = true
}
document.setter("onmouseup") {
draw = false
}
setInterval(10) {
if (draw) {
ctx.strokeStyle = "#222222"
ctx.lineTo(mouseX, mouseY)
ctx.stroke()
} else {
ctx.moveTo(mouseX, mouseY)
ctx.stroke()
}
}
}