【问题标题】:How to trace / track user input on touch screen to match image on screen如何在触摸屏上跟踪/跟踪用户输入以匹配屏幕上的图像
【发布时间】:2023-03-10 01:40:01
【问题描述】:
【问题讨论】:
标签:
javascript
svg
touch-event
touchscreen
【解决方案1】:
var xmlns = "http://www.w3.org/2000/svg";
function setMouseCoordinates(event) {
// contains the size of element having id 'image svg' and its position relative to the viewport, svg width/height equal to image height and width
var boundary = document.getElementById('<id_of_image_svg>').getBoundingClientRect();
// sets the x position of the mouse co-ordinate
auth_mouseX = event.clientX - boundary.left;
// sets the y position of the mouse co-ordinate
auth_mouseY = event.clientY - boundary.top;
return [auth_mouseX, auth_mouseY];
}
// add this in mousedown or respective touch event
function drawPath(event) {
var {
auth_mouseX,
auth_mouseY
} = setMouseCoordinates(event);
// Creates an element with the specified namespace URI and qualified name.
scribble = document.createElementNS(xmlns, 'path');
// sets the stroke width and color of the drawing drawn by scribble drawing tool
scribble.style.stroke = 'red';
// sets the stroke width of the scribble drawing
scribble.style.strokeWidth = '2';
scribble.style.fill = 'none';
scribble.setAttributeNS(null, 'd', 'M' + auth_mouseX + ' ' + auth_mouseY);
}
现在您可以在 touchdown 事件中添加此功能并相应地创建逻辑。您必须检查用户是否已通过变量开始或停止绘图并相应地更改其值。
要评估任务是否在给定区域内,您必须使用一些形状检测 api 或 AI(您可以在谷歌上搜索,其中有很多,例如 AWS rekognition,还有很多我不记得名字了)
希望它无论如何都会有所帮助。 :)