Skip to content

Commit

Permalink
fix relateive touch pos
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Jun 29, 2023
1 parent d7ac0af commit 2e53722
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kaboom",
"description": "kaboom.js is a JavaScript library that helps you make games fast and fun!",
"version": "3000.0.13",
"version": "3000.0.14",
"license": "MIT",
"homepage": "https://kaboomjs.com/",
"repository": "github:replit/kaboom",
Expand Down
44 changes: 36 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,19 @@ export default (opt: {
e.preventDefault()
state.events.onOnce("input", () => {
const touches = [...e.changedTouches]
const box = state.canvas.getBoundingClientRect()
if (opt.touchToMouse !== false) {
state.mousePos = new Vec2(touches[0].clientX, touches[0].clientY)
state.mousePos = new Vec2(
touches[0].clientX - box.x,
touches[0].clientY - box.y,
)
state.mouseState.press("left")
state.events.trigger("mousePress", "left")
}
touches.forEach((t) => {
state.events.trigger(
"touchStart",
new Vec2(t.clientX, t.clientY),
new Vec2(t.clientX - box.x, t.clientY - box.y),
t,
)
})
Expand All @@ -712,40 +716,64 @@ export default (opt: {
e.preventDefault()
state.events.onOnce("input", () => {
const touches = [...e.changedTouches]
const box = state.canvas.getBoundingClientRect()
if (opt.touchToMouse !== false) {
state.mousePos = new Vec2(touches[0].clientX, touches[0].clientY)
state.mousePos = new Vec2(
touches[0].clientX - box.x,
touches[0].clientY - box.y,
)
state.events.trigger("mouseMove")
}
touches.forEach((t) => {
state.events.trigger("touchMove", new Vec2(t.clientX, t.clientY), t)
state.events.trigger(
"touchMove",
new Vec2(t.clientX - box.x, t.clientY - box.y),
t,
)
})
})
}

canvasEvents.touchend = (e) => {
state.events.onOnce("input", () => {
const touches = [...e.changedTouches]
const box = state.canvas.getBoundingClientRect()
if (opt.touchToMouse !== false) {
state.mousePos = new Vec2(touches[0].clientX, touches[0].clientY)
state.mousePos = new Vec2(
touches[0].clientX - box.x,
touches[0].clientY - box.y,
)
state.mouseState.release("left")
state.events.trigger("mouseRelease", "left")
}
touches.forEach((t) => {
state.events.trigger("touchEnd", new Vec2(t.clientX, t.clientY), t)
state.events.trigger(
"touchEnd",
new Vec2(t.clientX - box.x, t.clientY - box.y),
t,
)
})
})
}

canvasEvents.touchcancel = (e) => {
state.events.onOnce("input", () => {
const touches = [...e.changedTouches]
const box = state.canvas.getBoundingClientRect()
if (opt.touchToMouse !== false) {
state.mousePos = new Vec2(touches[0].clientX, touches[0].clientY)
state.mousePos = new Vec2(
touches[0].clientX - box.x,
touches[0].clientY - box.y,
)
state.mouseState.release("left")
state.events.trigger("mouseRelease", "left")
}
touches.forEach((t) => {
state.events.trigger("touchEnd", new Vec2(t.clientX, t.clientY), t)
state.events.trigger(
"touchEnd",
new Vec2(t.clientX - box.x, t.clientY - box.y),
t,
)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/kaboom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const VERSION = "3000.0.13"
const VERSION = "3000.0.14"

import initApp from "./app"

Expand Down

0 comments on commit 2e53722

Please sign in to comment.