processing.org

I figure it’s faster to translate the PGraphics than copying the RShape and then translating. The reason the texture jumps around is because the center point of my shape is different at any point of the rotation.

PImage createBlendTexture(RShape userShape, PImage userTexture, int userMode) {
  int w = ceil(userShape.getWidth());
  int h = ceil(userShape.getHeight());
  PGraphics blendGraphics = createGraphics(w, h);
  
  RPoint graphicsCenter = new RPoint(w/2.0, h/2.0);
  RPoint shapeCenter = userShape.getCenter();
  graphicsCenter.sub(shapeCenter);

  blendGraphics.beginDraw();
  blendGraphics.hint(ENABLE_STROKE_PURE);
  blendGraphics.translate(graphicsCenter.x, graphicsCenter.y);
  userShape.draw(blendGraphics);
  blendGraphics.resetMatrix();  // blendMode() does some funky stuff without this
  blendGraphics.blendMode(userMode);
  blendGraphics.image(userTexture, 0, 0);
  blendGraphics.endDraw();

  return blendGraphics.get();
}