I know this Q/A may be related but using the solutions there I can't clear my canvas completely.
After I set the globalCompositeOperation
of my context, my canvas still had some layer after calling clearRect()
.
The canvas.width = canvas.width
solution will do the trick, but there says it is not supported in all browsers and also a bad practice.
How do we properly clear a canvas after globalComposite operation?
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const img = new Image();
const reader = new FileReader();
const filterRed = document.getElementById("red");
const addText = document.getElementById("addText");
const upload = document.getElementById("upload");
upload.addEventListener("change", function(evnt) {
const file = evnt.target.files[0];
reader.onload = function(e) {
img.src = e.target.result;
img.onload = function() {
ctx.drawImage(img, 0, 0, canvas.clientWidth, canvas.height);
};
};
reader.readAsDataURL(file);
});
filterRed.addEventListener("click", function(e) {
// redraw image again to prevent double clicking
// behave not normaly
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
ctx.fillStyle = "red";
ctx.filter = "contrast(0.6) brightness(100%)";
ctx.globalCompositeOperation = "multiply";
ctx.fillRect(0, 0, canvas.width, canvas.height);
});
addText.addEventListener("click", function(e) {
// redraw all things again
// but here not behave normaly
canvas.toBlob(function(blob) {
img.src = URL.createObjectURL(blob);
img.onload = function() {
// using canvas.width = canvas.width solve the problem
// but it is not the right way
canvas.width = canvas.width;
ctx.drawImage(img, 0, 0, canvas.width, canvas.width);
ctx.font = "50px serif";
ctx.fillStyle = "#00ff00";
ctx.fillText("Hello world", 50, 90);
};
});
});
<p>
Upload Image First
</p>
<label for="upload">Click to select file</label>
<input type="file" accept="image/*|video/*" id="upload" hidden>
<canvas id="canvas"></canvas>
<button id="red">
red
</button>
<button id="addText">
redraw with text
</button>
Here the reproduction codesandbox
The image should not have a white layer on top of it, the text should be white not the image.