How can I get lines to draw only on black?

edited November 2016 in Questions about Code
PFont f;

void setup() {
  background(255);
  size(700, 700);
  f=createFont("HelveticaLTStd-BoldCond.otf", 300);
  smooth();
  fill(0);
  textFont(f);
  text("D", 270, 400);
}

void draw() {
  stroke(random(255), random(255), random(255), random(255));
  line(290, random(180, 400), random(350, 430), random(180, 400));
  if (mousePressed) {
    stop();
  }
}

This is the code to the application, I'm drawing a letter D and i want to draw lines above it but only on the letter itself, not on the white inside and outside of it. Is there any way I can do this?

Tagged:

Answers

  • At pixel level, probably. Otherwise, probably not. Unless,the line is of the same color as the background, which is not the case.

  • sure there is a way

  • and what is that @Chrisir ?

  • @Krashan --

    The concept you are looking for is a mask. In Processing, this is implemented through PImage.mask().

    https://processing.org/reference/PImage_mask_.html

    1. draw lines in a PGraphics image
    2. draw text in another PGraphics.
    3. apply the text as a mask() to the lines, creating an image of lines "cut out" in the shape of the text.
    4. draw that image with image().
  • edited November 2016 Answer ✓

    Maybe there is a better way (Edited ** I am referring to my post). Here is my attempt:

    Kf

    PFont f;
    PGraphics pg;
    PGraphics amask;
    
    PGraphics lines;
    
    void setup() {
      background(255);
      size(700, 700);
      f=createFont("Arial", 300);
      smooth();
      fill(0);
      textFont(f);
    
      pg = createGraphics(width, height);
      pg.beginDraw();
      //pg.background(255);
      pg.fill(0);
      pg.textFont(f);
      pg.text("D", 270, 400);
      pg.endDraw();
    
      amask = createGraphics(width, height);
      amask.beginDraw();
      amask.background(255);
      amask.fill(0);
      amask.textFont(f);
      amask.text("D", 270, 400);
      amask.endDraw();
      amask.filter(INVERT);
    
      lines = createGraphics(width, height);
    }
    
    void draw() {
    
      lines.beginDraw();
      lines.stroke(random(255), random(255), random(255), random(255));
      lines.line(290, random(180, 400), random(350, 430), random(180, 400));
      lines.endDraw();
      lines.mask(amask);
    
      image(pg, 0, 0);
      image(lines,0,0);
      pg.copy(0,0,width,height,0,0,width,height);
    
      if (mousePressed) {
        stop();
      }
    }
    
  • I did not know that, thanks for the answer.

  • Thank you so much!

Sign In or Register to comment.