[英]How do I implement multi-touch interaction with paper.js?
So paper.js is a fantastic library. 所以paper.js是一个很棒的库。 I'm currently attempting to utilize it in order to build a multi-touch whiteboard/drawing webapp that will eventually be collaborative over a server.
我目前正在尝试利用它来构建多点触控白板/绘图Web应用程序,最终将通过服务器进行协作。
How do I implement multi-touch with paper though? 但是,如何实现纸张多点触摸?
My roadblock lies here. 我的障碍就在这里。 I have been unable to find a way to allow paper.js to create multiple paths for each touch.
我一直无法找到一种方法来允许paper.js为每次触摸创建多个路径。
So far I have done quite a bit of research through Google, although my research could be faulty, I have attempted to use browser touchevents with paper.js, and I have attempted to handle each event by creating a new canvas to separate them. 到目前为止,尽管我的研究可能有误,但我已经通过Google进行了很多研究,我尝试将浏览器touchevents与paper.js一起使用,并且尝试通过创建新的画布将它们分开来处理每个事件。
If there is anyone here that has successfully implemented multi-touch with paper.js, or has a better recommendation than paper for this app, I would be more than happy to hear. 如果这里有人成功地通过paper.js实现了多点触控,或者对此应用程序的建议比纸张更好,我将非常高兴听到。
I've done this using hammer.js library with paperJS. 我已经使用了带有paperJS的Hammer.js库来完成此操作。
I create one path for each possible fingers. 我为每个可能的手指创建一条路径。 When the touch event is raised, I enumerate all touches and I add points for each path corresponding.
引发触摸事件时,我会枚举所有触摸,并为每个对应的路径添加点。 However I use paperJS with Javascript and not in Paperscript's tag, moreover I use touch-emulator of Hammer.js for testing in my browser.
但是我将paperJS与Javascript一起使用,而不是在Paperscript的标签中使用,此外,我使用Hammer.js的触摸模拟器在浏览器中进行测试。
I can give you an example with HammerJS : 我可以举一个HammerJS的例子:
SEE LIVE EXAMPLE HERE 观看现场示例
Import javascript files 导入JavaScript文件
<script src="js/libs/jquery/jquery.js"></script>
<script src="js/libs/hammer.js/touch-emulator.js"></script>
<script> TouchEmulator(); </script>
<script src="js/libs/hammer.js/hammer.js"></script>
<script src="js/libs/hammer.js/jquery.hammer.js"></script>
<script src="js/libs/paper/paper-full.js"></script>
Touch-emulator is an emulation of multitouch. 触摸仿真器是多点触摸的仿真。 It emulate two finger's touch when you press Shift .
当您按Shift时,它会模仿两根手指的触摸。 This emulator is provided by Hammer.js.
该模拟器由Hammer.js提供。 ( Important : You must execute TouchEmulator() function after import).
( 重要 :导入后必须执行TouchEmulator()函数)。
Canvas 帆布
<canvas id="myCanvas" width="1500" height="1500" style="background-color: #000;">
PaperJS Installation PaperJS安装
var canvas;
var myPaths = [];
//PaperJS Installation
paper.install(window);
window.onload = function() {
//Setup PaperJS
canvas = document.getElementById('myCanvas');
paper.setup(this.canvas);
//Define array of paths (I've choose 12 because my multitouch table accept 12 touch max)
for (var i = 0; i < 12; i++)
{
myPath = new paper.Path();
myPath.strokeColor = '#00ff00';
myPath.strokeWidth = 2;
myPaths.push(myPath);
}
};
var myCanvas = document.getElementById('myCanvas');
The installation of paperJS is necessary when you use it out of Paperscript's tag. 当你使用它 Paperscript的标签paperJS的安装是必要的。 Documentation Here I initialize my paths.
文档在这里,我初始化我的路径。
Listen emulator's events 聆听模拟器的事件
//Listen multitouch event for simultation
document.body.addEventListener('touchstart', touchStart, false);
document.body.addEventListener('touchmove', touchmove, false);
document.body.addEventListener('touchend', touchEnd, false);
Listening the events of touch-emulator 聆听触摸模拟器的事件
Trace each paths for each fingers touch 跟踪每个手指触摸的每个路径
var touch = false;
function touchStart()
{
touch = true;
}
function touchEnd()
{
touch = false;
//Finish all paths
myPaths = [];
for (var i = 0; i < 12; i++)
{
myPath = new paper.Path();
myPath.strokeColor = '#00ff00';
myPath.strokeWidth = 2;
myPaths.push(myPath);
}
}
function touchmove(ev) {
if (!touch)
return;
//Draw path for each touch
for (var i = 0; i < ev.changedTouches.length; i++)
{
var x1, y1;
x1 = ev.changedTouches[i].pageX;
y1 = ev.changedTouches[i].pageY;
myPaths[i].add(new Point(x1, y1));
paper.view.draw();
}
console.log(ev);
}
Finally, adding points of each path corresponding to each touch. 最后,添加与每个触摸相对应的每个路径的点。
Warning : See above, this example use touch-emulator. 警告:请参见上文,此示例使用触摸仿真器。 The event object "ev" of touch-emulator, is bit different to the event object of hammer.js.
触摸模拟器的事件对象“ ev”与Hammer.js的事件对象有些不同。
For hammer.js, is something like : 对于Hammer.js,类似于:
$('#myCanvas').hammer().on("drag", function(ev) {
for (var i = 0; i < ev.gesture.touches.length; i++)
{
var x1, y1;
x1 = ev.gesture.touches[i].pageX;
y1 = ev.gesture.touches[i].pageY;
myPaths[i].add(new Point(x1, y1));
paper.view.draw();
}
});
I doing my best for writing a correct english, I hope that it's understandable. 我会尽力编写正确的英语,希望这是可以理解的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.