I draw on a canvas that doesnโt clean and makes it so that the canvas either fades to solid color over time or fades to alpha, revealing the layer behind.
My first instinct was to simply fill in the rectangle above the low alpha pattern with each frame so that the fill color builds up, gradually reducing the picture.
But I found some strange behavior (at least for me, I'm sure there is a reason). The fill color never fully accumulates. And the results change depending on the color of the paint and the fill, lighter / darker than each other.
I found this question when someone did the same thing as me: do the lines disappear after drawing the canvas?
The top answer looks good, and it's the same as me. BUT it only works with black and white. Here is another version of the same violin with different colors, you will see that the picture never disappears, it leaves a ghost: http://jsfiddle.net/R4V97/92/
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
painting = false,
lastX = 0,
lastY = 0;
canvas.width = canvas.height = 600;
canvas.onmousedown = function (e) {
if (!painting) {
painting = true;
} else {
painting = false;
}
lastX = e.pageX - this.offsetLeft;
lastY = e.pageY - this.offsetTop;
};
canvas.onmousemove = function (e) {
if (painting) {
mouseX = e.pageX - this.offsetLeft;
mouseY = e.pageY - this.offsetTop;
ctx.strokeStyle = "rgba(255,255,255,1)";
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(mouseX, mouseY);
ctx.stroke();
lastX = mouseX;
lastY = mouseY;
}
}
function fadeOut() {
ctx.fillStyle = "rgba(60,30,50,0.2)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
setTimeout(fadeOut,100);
}
fadeOut();
Also, if you change the opacity of the fill to 0.01, and the time to something like 20 ms, it will not even fill the correct color, leaving it gray.
Other things I've tried suffer from the same root problem. I tried to jump between two canvases, taking canvas A and drawing it with reduced alpha to canvas B, before drawing canvas B back onto the canvas. A - the same problem, there is a threshold where it does not disappear.
, - 0,95, . - , - ( - 10):
if (alpha<25) {
alpha = 0;
}
, imageData , .
- , !