Draconar
Draconar

Reputation: 1195

How to draw a glowing halo around elements using Processing 2.0 (Java)?

If I want to create a 'glowing halo effect' using Processing 2.0 (Java) how could I do that?

Is there any built-in method/transformation that I can apply to my object in order to them to glow?

If not, is there any visual trick that would accomplish the same effect?

Upvotes: 1

Views: 4741

Answers (1)

kevinsa5
kevinsa5

Reputation: 3411

I do not know of a built-in way, at least generically (you don't mention how your code is organized). But, here's a quick demo for a DIY approach. The idea is that you draw a series of progressively larger and dimmer ellipses behind your object. You'll probably want to tweak the hard-coded numbers to your liking, and maybe have it go transparent non-linearly (be dimmer for longer, perhaps?).

Thing thing1;
Thing thing2;

void setup(){
  size(300,300);
  smooth();
  thing1 = new Thing(1*width/3,height/2,false);
  thing2 = new Thing(2*width/3,height/2,true);
}
void draw(){
  background(100);
  stroke(0);
  line(100,100,250,250);
  line(150,100,300,250);
  thing1.display();
  thing2.display();
}

class Thing
{
  PVector loc;
  boolean glowing;
  Thing(int x, int y, boolean glow){
    loc = new PVector(x,y);
    glowing = glow;
  }
  void display(){
    if(glowing){
      //float glowRadius = 100.0;
      float glowRadius = 100.0 + 15 * sin(frameCount/(3*frameRate)*TWO_PI); 
      strokeWeight(2);
      fill(255,0);
      for(int i = 0; i < glowRadius; i++){
        stroke(255,255.0*(1-i/glowRadius));
        ellipse(loc.x,loc.y,i,i);
      }
    }
    //irrelevant
    stroke(0);
    fill(0);
    ellipseMode(CENTER);
    ellipse(loc.x,loc.y,40,40);
    stroke(0,255,0);
    line(loc.x,loc.y+30,loc.x,loc.y-30);
    line(loc.x+30,loc.y,loc.x-30,loc.y);
  }
}

enter image description here

Additionally, if your object isn't circularly symmetric, you can do something like the following. It looks at the object's pixels and draws an almost transparent ellipse anywhere it finds a non-blank pixel. It's a rather... messy solution, though. It might be better to follow the edge of the object, or perhaps some other method. I hope this gives you some ideas!

Thing thing1;

void setup(){
  size(300,300);
  background(0);
  thing1 = new Thing();
  thing1.display();
}
void draw(){}

class Thing
{
  PGraphics pg;
  Thing(){
    pg = createGraphics(200,200);
  }
  void display(){
    pg.beginDraw();
    pg.background(0,0);
    pg.strokeWeight(10);
    pg.stroke(255,100,0);
    pg.line(100,50,100,150);
    pg.line(75,50,125,50);
    pg.line(75,150,125,150);
    pg.line(30,100,170,100);
    pg.endDraw();
    PGraphics glow = createGraphics(200,200);
    glow.beginDraw();
    glow.image(pg,0,0);
    glow.loadPixels();
    glow.fill(255,3);
    glow.noStroke();
    //change the +=2 for different effects
    for(int x = 0; x < glow.width; x+=2){
      for(int y = 0; y < glow.height; y+=2){
        if(alpha(glow.pixels[y*glow.width+x]) != 0) glow.ellipse(x,y,40,40);
      }
    }

    glow.endDraw();
    //draw the glow:
    image(glow,50,50);
    //draw the sprite:
    image(pg,50,50);
  }
}

enter image description here

Upvotes: 6

Related Questions