お世話になっています。
現在、HTML5 canvasとjavascript mouseEventにてお絵かきアプリを開発しています。線を描くところまでは実装できていますが、線がカクカクで困っております。
なめらかにするために
ctx.lineJoin = "round";
ctx.lineCap = 'round';
や、2次ベジェ曲線の実装を行いました。
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (var i = 1; i < points.length - 2; i++) { var c = (points[i].x + points[i + 1].x) / 2; var d = (points[i].y + points[i + 1].y) / 2; ctx.quadraticCurveTo(points[i].x, points[i].y, c, d); } ctx.quadraticCurveTo( points[i].x, points[i].y, points[i + 1].x, points[i + 1].y ); ctx.stroke();
しかし、2次ベジェ曲線でも多少なめらかになりますが、思い通りの線にはなりませんでした。
他に方法はないか色々調べたところCatmull-Romスプライン曲線というものの存在を知りました。今度はこちらを実装しようと思い、さらに調べたところ下記に
https://github.com/epistemex/cardinal-spline-js
.jaを見つけ実装方法らしき質問も見つけました。
http://stackoverflow.com/questions/17281889/canvas-sometimes-a-stroke-with-lineto-produces-edges
さっそく実装に取り掛かりましたが思い通りに線が描けなくて困っています。どなたか線が描けるところまで見ていただけないでしょうか?また、なめらかに描ける方法があれば教えていただけないでしょうか?
html
//
//
<canvas id="myCanvas"></canvas>
//
javascript
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var points = [];
ctx.lineJoin = "round";
ctx.lineCap = 'round';
//
canvas.addEventListener("touchstart", function (event) {
}
canvas.addEventListener("touchmove", function (event) {
var pos = getPosT(event);
points.push({ x: pos.x, y: pos.y }); ctx.beginPath(); ctx.moveTo(points[0].x, points[0].y); ctx.curve(points); ctx.stroke();
}
canvas.addEventListener("touchend", function () {
points = [];
}
function getPosT(event) {
var mouseX = event.touches[0].clientX - $(canvas).position().left + scrollX();
var mouseY = event.touches[0].clientY - $(canvas).position().top + scrollY();
return {x: mouseX, y: mouseY};
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。