Loading...
Logo
Processing Forum
Hi!

I'm working on dynamic image masking with processing.js .
In particular I'm using a triangle as a mask, and sometimes it is upward sometimes it is downward.
Anyway, I'm storing a Triangle class with the coordinates in PVector so i can easily draw the mask in a PGraphics object.

See the note at the end: this program in processing 1.5 IDE works without problem,with the correct masking, but in processing.js not working.
I'm using processing.js v1.4.1

Here is the code:

Copy code
  1. PGraphics triangleMask;
  2. PImage _background;

  3. //------------------------------------------ Triangle
  4. class Triangle {
  5.   PVector a,b,c;
  6.   PVector aabb1, aabb2;

  7.   int x,y, id;

  8.   Triangle(int id, PVector a, PVector b, PVector c) {
  9.     this.id = id;
  10.     this.a = a;    this.b = b;    this.c = c;
  11.     computCentroid();
  12.     computeBoundingBox();
  13.   }

  14.   void computeBoundingBox() {
  15.       // ...
  16.   }

  17.   PVector computCentroid() {
  18.       // ....
  19.   }

  20.   void draw() {
  21.     
  22.     //--------------------------------------------------------------------------- CREATE MASK HERE
  23.     triangleMask = createGraphics(90, 90, JAVA2D); // I have tried also with P3D
  24.     triangleMask.beginDraw();
  25.     triangleMask.background(0);
  26.     
  27.     triangleMask.fill(255);
  28.     triangleMask.stroke(255);

  29.     triangleMask.beginShape();
  30.     triangleMask.vertex(a.x-aabb1.x, a.y-aabb1.y); //aabb1.x, aabb1.y, 
  31.     triangleMask.vertex(b.x-aabb1.x, b.y-aabb1.y);
  32.     triangleMask.vertex(c.x-aabb1.x, c.y-aabb1.y);
  33.     triangleMask.endShape(CLOSE);

  34.     triangleMask.endDraw();

  35.     PImage texture = createImage(90, 90, RGB);
  36.     texture.copy(
  37.       _background,
  38.       (int)aabb1.x, (int)aabb1.y, 90, 90, //aabb2.x, aabb2.y,
  39.       0,0, 90, 90);

  40.     //--------------------------------------------------------------------------- USE MASK HERE
  41.     texture.mask(triangleMask);    
  42.     image(texture, aabb1.x, aabb1.y);

  43.     noFill();
  44.     stroke(255,0,0);
  45.     beginShape();
  46.     vertex(a.x, a.y);
  47.     vertex(b.x, b.y);
  48.     vertex(c.x, c.y);
  49.     endShape(CLOSE);
  50.   }
  51. }
NOTE: This code in Processing.org works as a charm. When i put it in an html page via processing the mask part is ignored: the part of _background copied is not a Triangle but a rectangle (the rect i copy without the mask).

Replies(5)

anyway, i searched a little bit in the forum and this example:


seems to be broken!




any workaround? maybe with css?
The current workaround I have found is to convert the mask from PGraphic to PImage and use a PImage as the actual mask.

I don't know how the mask engine works under the hood in processing.js (and i would like to know it) but for now it is ok.

The conversion PGraphics to PImage is straightworard, using .get method, from here:

If you can, do post the issue to the PJS Bug Tracker.  You also might find some insight from the PJS GoogleGroup which is where a lot of the people who actually maintain the code hangout.   It's unfortunate that there are two separate forums, but it's important that bugs like this are known to the developers since the goal is to make PJS a close to JP as possible.


Cool that you found a workaround!
yeah i reported your example!
the workaround seems to work till now!
if someone get some better methods.. post it!