Ok so I am working on a drawing application and I would like to make it so the user can change the opacity of their brush.
(好的,所以我正在开发一个绘图应用程序,我想制作它,以便用户可以更改画笔的不透明度。)
I have gotten the opacity to change by changing the alpha value, but when I draw a line with the lowered alpha value, the line has many dots of different transparency in it.(我已经通过更改Alpha值来更改不透明度,但是当我绘制一条具有较低Alpha值的线时,该线中有许多透明度不同的点。)
How can I make the semi transparent line draw very clean with occasional transparency changes only at overlaps?(如何使半透明线绘制得很干净,并且仅在重叠时偶尔更改透明度?)
Image 1 is what happens when I run my code
(图片1是我运行代码时发生的情况)
Image 2 is what I would like to achieve if possible
(图片2是我想尽可能实现的)
Here is my javascript code for my canvas
(这是我的画布的JavaScript代码)
const canvas = document.querySelector("#canvas");
const ctx = canvas.getContext('2d');
window.addEventListener('load', function() {
//Resizing
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
//Variables
let painting = false;
function startPosition(e) {
painting = true;
draw(e);
}
function finishedPosition() {
painting = false;
ctx.beginPath();
}
function draw(e) {
if(!painting) return;
ctx.lineWidth = 10;
ctx.lineCap = "round";
ctx.lineTo(e.clientX, e.clientY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(e.clientX, e.clientY);
}
//EventListeners
canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', finishedPosition);
canvas.addEventListener('mousemove', draw);
});
ask by Hoyt Felton translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…