Making a "fat" eraser
in
Programming Questions
•
1 year ago
Hi all, I'm building a drawing sketch that uses motion tracking to generate drawings. Because I want the video to appear onscreen behind the drawing and because I don't want the video image saved with the drawing, I'm building a separate PGraphics context with the lines and a transparent background. Here's a screenshot:
The problem I'm running into is with erasing: without the video image, I would just draw shapes with the background color... easy. But since I want to clear areas of the PGraphics array, I'm using the following code from another forum post:
This is great, and works very fast. However, if the cursor moves quickly the erased marks appear as isolated dots, not smooth washes.
I've been trying various methods to clear larger areas, but simple methods like drawing a rectangle with a fill of (0,0) doesn't work, as it is just clear and the lines below show through. Another version, which works ok but is VERY laggy, is this:
So, my question is two-part:
The problem I'm running into is with erasing: without the video image, I would just draw shapes with the background color... easy. But since I want to clear areas of the PGraphics array, I'm using the following code from another forum post:
- drawing.loadPixels();
- for (int x=0; x<drawing.width; x++) {
- for (int y=0; y<drawing.height; y++) {
- float distance = dist(x, y, tipX, tipY);
- if (distance <= eraserSize/2) {
- drawing.pixels[x+y*drawing.width] = color(0, 0);
- }
- }
- }
- drawing.updatePixels();
This is great, and works very fast. However, if the cursor moves quickly the erased marks appear as isolated dots, not smooth washes.
I've been trying various methods to clear larger areas, but simple methods like drawing a rectangle with a fill of (0,0) doesn't work, as it is just clear and the lines below show through. Another version, which works ok but is VERY laggy, is this:
- drawing.loadPixels();
- // get distance between start/end, map to a reasonable value
- // (ie: less steps for shorter distances)
- int stepSize = int(dist(tipX, prevX, tipY, prevY));
- stepSize = int(map(stepSize, 0, width, 1, 10));
- // iterate through all steps, erasing a circle at each one
- for (int p = 0; p<stepSize; p++) {
- float tempX = lerp(tipX, prevX, p);
- float tempY = lerp(tipY, prevY, p);
- for (int x=0; x<drawing.width; x++) {
- for (int y=0; y<drawing.height; y++) {
- float distance = dist(x, y, tempX, tempY);
- if (distance <= eraserSize/2) {
- drawing.pixels[x+y*drawing.width] = color(0, 0);
- }
- }
- }
- }
- drawing.updatePixels();
So, my question is two-part:
- Any thoughts on an algorithm to write a rectangle of pixels, but at an angle? Doing it horiz/vert is rather easy, but figuring out how to do this at an angle has me stumped (push/popMatrix don't seem to work properly, at least in my code)
- Any other ideas of a method for erasing that would be faster?
1