Get the `PGraphicsFX2D` from a `PSurfaceFX`?

Is it possible? Looking at the source code of Processing, it can't be done. In the end I need a GraphicsContext from the PGraphicsFX2D. I know that PGraphicsFX2D has a method getNative(), which returns the context, but the graphics is not visible in PSurfaceFX.

Answers

  • If you show your attempt, then efforts will be focus on getting your attempt working. Right now, efforts are funneled to figure out what you are attempting in the first place.

    Kf

  • edited March 2018

    @kfrajer I was attempting to implement clip() in FX2D for my sketch. Unfortunately, the renderer doesn't support clipping, so I deciced to implement a somewhat flawed but functioning solution on my own.

    I have already figured out that I can get the GraphicsContext from a Canvas object using getGraphicsContext2D(), and this is my solution:

    GraphicsContext ctx;
    Canvas canvas = (Canvas) surface.getNative();
    ctx = (GraphicsContext) canvas.getGraphicsContext2D();
    

    And that's how I've implemented clip() and noClip():

    @Override
    public void clip(float x, float y, float w, float h) {
      ctx.save();
      ctx.beginPath();
      ctx.rect(x, y, w, h);
      ctx.closePath();
      ctx.clip();
    }
    
    @Override
    public void noClip() {
      ctx.restore();
    }
    

    This is not a great solution because GraphicsContext#save saves all the common properties like the fill and stroke color, which will reset my settings to the ones when I call clip(). In my sketch this won't be that big of a deal, but for implementing this solution to Processing itself it needs to be improved.

    Anyways, issue fixed.

    EDIT: That tag in the @Override tags is not my fault, it's the syntax highlighter

    EDIT #2: Now I just understood that this solution is unusable for animation, 'cause it makes the sketch go nuts.

    EDIT #3!: I just removed a clip() call and it works just fine now. Dunno why it did this weird stuff tho

  • Thxs for sharing you solution. You can describe your clip() version in github as a feature request and they could added into their to do things. You can also exchange ideas here in the forum (as you have done) to see if there are any other way to implement it, or maybe find an alternative approach. Regarding the @override tag, it is a known bug in the forum. You can add a space to solve the issue like this: @ Override

    Kf

Sign In or Register to comment.