https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Ray Rahke

03/15/2024, 3:49 PM
I have a Kotlin->JS multiplatform up and running to draw canvas stuff. I attempt to call
requestAnimationFrame()
Copy code
fun main() {
  requestAnimationFrame(::main)
}
but receive
Copy code
Unresolved reference: requestAnimationFrame
I have tried many combinations of imports, and all of them at once.
Copy code
import kotlinx.browser.document
import kotlinx.browser.window
import org.w3c.dom.*
import org.w3c.dom.events.*
import kotlin.browser.window
What do I need to import to get access to this standard JS function?
j

Joffrey

03/15/2024, 3:51 PM
kotlinx.browser.window
is the one you need I believe
r

Ray Rahke

03/15/2024, 3:52 PM
Well, that is in the list of imports I have at the top.. So not sure what's going on
j

Joffrey

03/15/2024, 3:52 PM
You also import
kotlin.browser.window
so I'm not sure how that compiles, you should probably remove that one
r

Ray Rahke

03/15/2024, 3:52 PM
so only
Copy code
import kotlinx.browser.document
import kotlinx.browser.window
import org.w3c.dom.*
?
because this is what I had originally before hitting the error
j

Joffrey

03/15/2024, 3:53 PM
For this function specifically, you only need
import kotlinx.browser.window
(not the 2 others), but yeah maybe you need the others for the rest of your code
Wait, are you calling the function on the
window
object? Because you can't just call it directly. You need:
Copy code
window.requestAnimationFrame { ... }
r

Ray Rahke

03/15/2024, 3:53 PM
nah I was calling it how you do in javascript
just as a globally accessibel function
ahk let me try that
j

Joffrey

03/15/2024, 3:54 PM
Yeah in Kotlin the global scope and the
window
scope are not mixed up
r

Ray Rahke

03/15/2024, 4:16 PM
yeah that worked, thanks
🎉 1
2 Views