使用绘图组件想做一个画笔功能,遇到一个奇葩的问题。
参考https://github.com/2youyou2/graphics-tutorials项目里的pencil/step1
onLoad: function () {
this.ctx = this.getComponent(cc.Graphics);
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: this.onTouchBegan.bind(this),
onTouchMoved: this.onTouchMoved.bind(this),
onTouchEnded: this.onTouchEnded.bind(this),
}, this.node);
},
onTouchBegan: function (touch, event) {
var touchLoc = touch.getLocation();
touchLoc = this.node.parent.convertToNodeSpaceAR(touchLoc);
this.points = [touchLoc];
return true;
},
onTouchMoved: function (touch, event) {
var touchLoc = touch.getLocation();
touchLoc = this.node.parent.convertToNodeSpaceAR(touchLoc);
cc.log(touchLoc.x + ' ' + touchLoc.y);
this.points.push(touchLoc);
// this.ctx.clear();
for (let i = 0, l = this.points.length; i < l; i++) {
let p = this.points[i];
if (i === 0) {
this.ctx.moveTo(p.x, p.y);
}
else {
this.ctx.lineTo(p.x, p.y);
}
}
this.ctx.stroke();
},
onTouchEnded: function (touch, event) {
},
注释掉每次绘制前的 this.ctx.clear(); 不能每次只画一笔。。。
在浏览器中运行正常,但在模拟器里运行,一次移动多个点后会出现下面的情况。。。。
移动路径中的点都与起始点有连线。
请问怎么解决这个。。。。。万分感谢

