次のような素敵で滑らかなテーパー ブラシを作成する必要があります。
しかし、マウスの速度の動きに問題があります。ブラシのテーパーをマウスの移動速度に依存しないようにする方法。ブラシの先細りを作る必要がありますが、マウスの動きは非常に速くて遅くなります。
速度が異なると、次の結果が得られます。
var el = document.getElementById('c');
var ctx = el.getContext('2d');
var isDrawing, lastPoint;
ctx.lineWidth = 20;
el.onmousedown = function(e) {
isDrawing = true;
lastPoint = { x: e.clientX, y: e.clientY };
ctx.lineWidth = 20;
};
el.onmousemove = function(e) {
if (!isDrawing) return;
ctx.lineJoin = "round";
ctx.lineCap = "butt";
ctx.strokeStyle = "#000000";
ctx.globalAlpha = "1";
ctx.globalCompositeOperation = "source-over";
if(ctx.lineWidth >= 5) {
ctx.lineWidth -= 0.1;
}
var currentPoint = { x: e.clientX, y: e.clientY };
ctx.beginPath();
ctx.moveTo(lastPoint.x, lastPoint.y);
ctx.lineTo(currentPoint.x, currentPoint.y);
ctx.closePath();
ctx.stroke();
lastPoint = currentPoint;
};
el.onmouseup = function() {
isDrawing = false;
};
function clearit() {
ctx.clearRect(0,0, 1000, 1000);
}
canvas { border: 1px solid #ccc }
<canvas id="c" width="500" height="500"></canvas>