Skip to content

Commit

Permalink
Support touch events on mobile`s browsers (#850)
Browse files Browse the repository at this point in the history
* add support for touchscreen events in js for mobile devices

* add touchevents for wasm

---------

Co-authored-by: dmitriy tukhvatullin <[email protected]>
  • Loading branch information
jershell and dmitriy tukhvatullin authored Jan 18, 2024
1 parent fd9a62d commit 5b5ee68
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
34 changes: 34 additions & 0 deletions skiko/src/jsMain/kotlin/org/jetbrains/skiko/Convertors.js.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jetbrains.skiko

import org.w3c.dom.TouchEvent
import org.w3c.dom.asList
import org.w3c.dom.events.KeyboardEvent
import org.w3c.dom.events.MouseEvent
import org.w3c.dom.events.WheelEvent
Expand Down Expand Up @@ -35,6 +37,38 @@ internal fun toSkikoDragEvent(
)
}

internal fun toSkikoEvent(
event: TouchEvent,
kind: SkikoPointerEventKind,
offsetX: Double,
offsetY: Double
): SkikoPointerEvent {
val touches = event.changedTouches.asList()
val pointers = touches.map { touch ->
val x = touch.clientX.toDouble() - offsetX
val y = touch.clientY.toDouble() - offsetY
val force = touch.asDynamic().force as Double

SkikoPointer(
x = x,
y = y,
pressed = kind in listOf(SkikoPointerEventKind.DOWN, SkikoPointerEventKind.MOVE),
device = SkikoPointerDevice.TOUCH,
id = touch.identifier.toLong(),
pressure = force
)
}

return SkikoPointerEvent(
x = pointers.centroidX,
y = pointers.centroidY,
kind = kind,
timestamp = (currentNanoTime() / 1E6).toLong(),
pointers = pointers,
platform = event
)
}

internal fun toSkikoTypeEvent(
character: String,
event: KeyboardEvent?,
Expand Down
31 changes: 29 additions & 2 deletions skiko/src/jsMain/kotlin/org/jetbrains/skiko/SkiaLayer.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,42 @@ package org.jetbrains.skiko

import kotlinx.browser.window
import org.jetbrains.skiko.w3c.HTMLCanvasElement
import org.w3c.dom.TouchEvent
import org.w3c.dom.events.KeyboardEvent
import org.w3c.dom.events.MouseEvent
import org.w3c.dom.events.WheelEvent
import org.w3c.dom.events.UIEvent

internal actual fun SkiaLayer.bindCanvasEventsToSkikoView(
canvas: HTMLCanvasElement
) {
val htmlCanvas = canvas.unsafeCast<org.w3c.dom.HTMLCanvasElement>()

var offsetX = 0.0
var offsetY = 0.0
htmlCanvas.addEventListener("touchstart", { event ->
event.preventDefault()
event as TouchEvent
htmlCanvas.getBoundingClientRect().apply {
offsetX = left
offsetY = top
}
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.DOWN, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchmove", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.MOVE, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchend", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchcancel", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP, offsetX, offsetY))
})
htmlCanvas.addEventListener("mousedown", { event ->
event as MouseEvent
isPointerPressed = true
Expand Down Expand Up @@ -54,7 +81,7 @@ actual typealias SkikoGesturePlatformEvent = Any
actual typealias SkikoPlatformInputEvent = KeyboardEvent
actual typealias SkikoPlatformKeyboardEvent = KeyboardEvent
// MouseEvent is base class of PointerEvent
actual typealias SkikoPlatformPointerEvent = MouseEvent
actual typealias SkikoPlatformPointerEvent = UIEvent

internal actual fun SkiaLayer.setOnChangeScaleNotifier() {
state?.initCanvas(desiredWidth, desiredHeight, contentScale, this.pixelGeometry)
Expand Down
38 changes: 38 additions & 0 deletions skiko/src/wasmJsMain/kotlin/org/jetbrains/skiko/Convertors.wasm.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
package org.jetbrains.skiko

import org.w3c.dom.TouchEvent
import org.w3c.dom.asList
import org.w3c.dom.events.KeyboardEvent
import org.w3c.dom.events.MouseEvent
import org.w3c.dom.events.WheelEvent

internal external abstract class ExtendedTouchEvent: TouchEvent {
val force: Double
}

internal fun toSkikoEvent(
event: TouchEvent,
kind: SkikoPointerEventKind,
offsetX: Double,
offsetY: Double
): SkikoPointerEvent {
val touches = event.changedTouches.asList()
val pointers = touches.map { touch ->
val x = touch.clientX.toDouble() - offsetX
val y = touch.clientY.toDouble() - offsetY
val force = touch.unsafeCast<ExtendedTouchEvent>().force

SkikoPointer(
x = x,
y = y,
pressed = kind in listOf(SkikoPointerEventKind.DOWN, SkikoPointerEventKind.MOVE),
device = SkikoPointerDevice.TOUCH,
id = touch.identifier.toLong(),
pressure = force
)
}

return SkikoPointerEvent(
x = pointers.centroidX,
y = pointers.centroidY,
kind = kind,
timestamp = (currentNanoTime() / 1E6).toLong(),
pointers = pointers,
platform = event
)
}

internal fun toSkikoEvent(
event: MouseEvent,
kind: SkikoPointerEventKind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,43 @@ package org.jetbrains.skiko

import kotlinx.browser.window
import org.jetbrains.skiko.w3c.HTMLCanvasElement
import org.w3c.dom.TouchEvent

import org.w3c.dom.events.KeyboardEvent
import org.w3c.dom.events.MouseEvent
import org.w3c.dom.events.UIEvent
import org.w3c.dom.events.WheelEvent

internal actual fun SkiaLayer.bindCanvasEventsToSkikoView(
canvas: HTMLCanvasElement
) {
val htmlCanvas = canvas as org.w3c.dom.HTMLCanvasElement

var offsetX = 0.0
var offsetY = 0.0
htmlCanvas.addEventListener("touchstart", { event ->
event.preventDefault()
event as TouchEvent
htmlCanvas.getBoundingClientRect().apply {
offsetX = left
offsetY = top
}
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.DOWN, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchmove", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.MOVE, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchend", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchcancel", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP, offsetX, offsetY))
})
htmlCanvas.addEventListener("mousedown", { event ->
event as MouseEvent
isPointerPressed = true
Expand Down Expand Up @@ -55,7 +82,7 @@ actual typealias SkikoGesturePlatformEvent = Any
actual typealias SkikoPlatformInputEvent = KeyboardEvent
actual typealias SkikoPlatformKeyboardEvent = KeyboardEvent
// MouseEvent is base class of PointerEvent
actual typealias SkikoPlatformPointerEvent = MouseEvent
actual typealias SkikoPlatformPointerEvent = UIEvent

internal actual fun SkiaLayer.setOnChangeScaleNotifier() {
state?.initCanvas(desiredWidth, desiredHeight, contentScale, this.pixelGeometry)
Expand Down

0 comments on commit 5b5ee68

Please sign in to comment.