I have a HTMLCanvasElement which already has stuff drawn on it, and I need to make areas of it transparent. I do not know coordinates of that area, so I cannot use paths, but I know the color I want to get rid of.
Specifically, I have a html element containing text stoked as advised here: https://css-tricks.com/adding-stroke-to-web-text/ and I draw it on a canvas element, as advised here: Rendering HTML elements to <canvas>.
The problem is, that I want to make the white core of my text transparent on the resulting canvas element before using it further. So that only the text's stroke would remain.
Why don't I stroke text directly on the canvas? The reason is that I need to adjust letter spacing, and only CSS styling can do that.
The code snippet is a bit unreadable because in reality it's generated by compiling C# with Bridge.Net, but should give the general idea what I'm trying to do:
ConstructLabel$1: function (toWrite, font, letterSpacing, labelConstructed) {
if (toWrite == null || Bridge.referenceEquals(toWrite, "")) {
return null;
}
var canvas = document.createElement("canvas");
var textElement = document.createElement("div");
textElement.style.width = "auto";
textElement.style.position = "absolute";
textElement.style.whiteSpace = "nowrap";
textElement.style.letterSpacing = letterSpacing;
textElement.style.font = font;
//make the storking of text
textElement.style.color = "white";
textElement.style.textShadow = "-1px -1px 0 #000,1px -1px 0 #000,-1px 1px 0 #000,1px 1px 0 #000";
textElement.textContent = toWrite;
//ToCanvas is my wrapper that takes a HTML element and returns a new canvas that
//will draw a 1:1 representation of that element on it. It executes
//the callback after it being drawn.
return Utility.ToCanvas(textElement, function (drawnCanvas) {
var ctx = drawnCanvas.getContext("2d");
//ctx.ClearColor("white"); <----- how to do this?
labelConstructed(drawnCanvas);
});
}