-
-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement] Using <mask> instead of <clipPath> #563
Comments
I need to say that the idea with the eraser needs some work, because of it is necessary to create a new |
Super cool. This is a great idea. The reason I haven't implemented |
@jonobr1 context.globalCompositeOperation = "destination-out"; //"xor" also can be used
context.strokeStyle = "rgba(0, 0, 0, 1.0)"; |
Most likely this formula is used in SVG mask for transparency: const alpha = 0.2126 * red + 0.7152 * green + 0.0722 * blue; with only gray colors this formula becomes more simple: const alpha = red; //same as "alpha = green" and "alpha = blue" links: |
Example of computing alpha with for (let i = 0; i < data.length; i += 4) {
const red = data[i];
const green = data[i + 1];
const blue = data[i + 2];
const alpha = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
data[i + 3] = alpha;
} Perhaps someone can come up with a more fast code, but I don't see this yet. |
Well, I figured out the problem with http://jsfiddle.net/7gkdn6ha/1/ But converting colors to alpha with |
Thanks for exploring this. This is super helpful! I think I can add this once I'm done with the ES6 branch. I'll add this to the milestones |
Workers can be used to process ImageData asynchronously, and WebAssembly can be used for speedup. Some useful links: |
C++ code for the Worker function: void updateAlpha(unsigned char* data, int len) {
for (int i = 0; i < len; i += 4) {
int red = data[i];
int green = data[i + 1];
int blue = data[i + 2];
int alpha = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
data[i + 3] = alpha;
}
} Optimized version: void updateAlpha(unsigned char* data, int len) {
int i = 0;
while (i < len) {
int red = data[i++];
int green = data[i++];
int blue = data[i++];
int alpha = (2126 * red + 7152 * green + 722 * blue) / 10000;
data[i++] = alpha;
}
} Optimized and compact version: void updateAlpha(unsigned char* data, int len) {
int i = 0;
while (i < len) {
data[i++] = (2126 * data[i++] + 7152 * data[i++] + 722 * data[i++]) / 10000;
}
} |
@jonobr1 Frankly, it is worth considering abandoning Canvas 2D altogether, as it is an obsolete technology that slows down progress. Such useful effects as glow, blur and shadows will also work faster in WebGL than in Canvas 2D. |
Thanks for the input and resources. The article about image styling and filters in WebAssembly is particularly helpful! |
Is your feature request related to a problem? Please describe.
<mask>
allows you to do more things than<clipPath>
. It can be used even to create an eraser functionality.Describe the solution you'd like
Change the
.mask
property to.clipPath
..mask
will be used to create<mask>
tag.It will be possible to specify a group or path that will be added to the tag inside of
<defs>
.Describe alternatives you've considered
More complex algorithms can be used to create the eraser, but they will be slower and require more code.
Additional context
Below the parts of code that I have used for SVG.
Properties:
Init:
Reset:
Create the tag
Some checks
Main code:
The text was updated successfully, but these errors were encountered: