Skip to content

Commit 8c7e30b

Browse files
committedMar 15, 2017
adding basic touch handling
1 parent 3aaabae commit 8c7e30b

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed
 

‎boardplane.js

+60-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ function TilePlane(tileSource, tileRenderer, tileSize, canvas, overlayCanvas) {
44
var ctx = canvas.getContext("2d");
55
var octx = overlayCanvas.getContext("2d");
66
var mouse_down_point, mouse_move_point, dragging = false;
7+
var touches = [];
78
var selected = undefined;
89
var offset = new Vec(0, 0);
910

11+
overlayCanvas.addEventListener("touchstart", handleTouchstart);
12+
overlayCanvas.addEventListener("touchend", handleTouchend);
13+
overlayCanvas.addEventListener("touchmove", handleTouchmove);
1014
overlayCanvas.addEventListener("mousemove", handle_mouse_move);
1115
overlayCanvas.addEventListener("mousedown", handle_mouse_down);
1216
overlayCanvas.addEventListener("mouseup", handle_mouse_drag_stop);
@@ -32,7 +36,7 @@ function TilePlane(tileSource, tileRenderer, tileSize, canvas, overlayCanvas) {
3236
for (tileOffset.x = 0; tileOffset.x < width_in_chunks; tileOffset.x++)
3337
for (tileOffset.y = 0; tileOffset.y < height_in_chunks; tileOffset.y++)
3438
renderTile(firstTile.x + tileOffset.x, firstTile.y + tileOffset.y);
35-
39+
3640
var gridLineWidth = tileSize / 150;
3741
octx.clearRect(0, 0, cw, ch);
3842
octx.lineWidth = 1;
@@ -43,7 +47,7 @@ function TilePlane(tileSource, tileRenderer, tileSize, canvas, overlayCanvas) {
4347
octx.lineTo(i * tileSize + firstTilePosOnScreen.x, ch + tileSize);
4448
octx.stroke();
4549
}
46-
50+
4751
for (var i = 0; i < height_in_chunks; i++) {
4852
octx.beginPath();
4953
octx.moveTo(firstTilePosOnScreen.x, i * tileSize + firstTilePosOnScreen.y);
@@ -55,7 +59,7 @@ function TilePlane(tileSource, tileRenderer, tileSize, canvas, overlayCanvas) {
5559
function renderTile(x, y) {
5660
var tile = tileSource.getTile(x, y);
5761
var rect = screenRect(x, y);
58-
tileRenderer.render(ctx, rect, tile);
62+
tileRenderer.render(ctx, rect, tile);
5963
}
6064

6165
var tileFromScreen = (screenPos) => new Vec(tileXFromScreen(screenPos.x), tileYFromScreen(screenPos.y));
@@ -65,6 +69,52 @@ function TilePlane(tileSource, tileRenderer, tileSize, canvas, overlayCanvas) {
6569
var tileXFromScreen = (screenX) => Math.floor((screenX + offset.x) / tileSize);
6670
var tileYFromScreen = (screenY) => Math.floor((screenY + offset.y) / tileSize);
6771

72+
function forEachTouch(touchList, callback) {
73+
for (var i = 0; i < touchList.length; i++)
74+
callback(touchList.item(i));
75+
}
76+
77+
function touchPos(te) {
78+
return new Vec(te.clientX, te.clientY);
79+
}
80+
81+
function handleTouchstart(evt) {
82+
forEachTouch(evt.changedTouches, te =>
83+
touches.push({
84+
lastPos: new Vec(te.clientX, te.clientY),
85+
event: te
86+
})
87+
);
88+
}
89+
90+
function handleTouchmove(evt) {
91+
if (touches.length === 1) {
92+
var currentPos = new Vec(evt.changedTouches[0].clientX, evt.changedTouches[0].clientY);
93+
var dxy = currentPos.vector_to(touches[0].lastPos);
94+
touches[0].lastPos = currentPos;
95+
offset.move(dxy);
96+
render();
97+
} else if (touches.length === 2) {
98+
//TODO: add pinch/zoom
99+
// var startDist = Vec.dist(touches[0].lastPos, touches[1].lastPos);
100+
// var currentDist = Vec.dist(touchPos(evt.changedTouches[0]), touchPos(evt.changedTouches[1]));
101+
// var distDiff = currentDist - startDist;
102+
// distDiff = distDiff * 2 / canvas.clientWidth;
103+
// if (Math.abs(distDiff) >= 1) {
104+
// touches[0].lastPos = touchPos(evt.changedTouches[0]);
105+
// touches[1].lastPos = touchPos(evt.changedTouches[1]);
106+
// var tileSizeChange = ((distDiff < 0) ? tileSize : -tileSize / 2);
107+
// zoom(tileSizeChange);
108+
// }
109+
}
110+
}
111+
112+
function handleTouchend(evt) {
113+
forEachTouch(evt.changedTouches, te =>
114+
touches = touches.filter(tc => tc.event.identifier !== te.identifier)
115+
);
116+
}
117+
68118
function handle_mouse_down(event) {
69119
if (event.button === 2) {
70120
event.preventDefault();
@@ -100,9 +150,14 @@ function TilePlane(tileSource, tileRenderer, tileSize, canvas, overlayCanvas) {
100150
render();
101151
}
102152

103-
function handle_mouse_wheel(event) {
153+
function handle_mouse_wheel(event) {
154+
var tileSizeChange = ((event.deltaY < 0) ? tileSize : -tileSize / 2);
155+
zoom(tileSizeChange);
156+
}
157+
158+
function zoom(tileSizeChange) {
104159
let oldTileSize = tileSize;
105-
tileSize *= ((event.deltaY < 0) ? 2 : 0.5 );
160+
tileSize += tileSizeChange;
106161
if (tileSize < 10)
107162
tileSize = 10;
108163
var cw = canvas.clientWidth;

‎geom.js

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Vec.prototype.inverse = function () {
3333
return this;
3434
};
3535

36+
Vec.dist = function(a, b) {
37+
var dx = b.x - a.x;
38+
var dy = b.y - a.y;
39+
40+
return Math.sqrt(dx * dx + dy * dy);
41+
};
42+
3643
Vec.from_event = function (event) {
3744
return new Vec(event.offsetX, event.offsetY);
3845
};

‎index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<head>
33
<title>tiles-js</title>
44
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
66
<style>
77
* {border: 0px; width: 100%; height: 100%; margin: 0px;}
88
canvas {position: absolute; background: transparent;}

0 commit comments

Comments
 (0)
Please sign in to comment.