Saving SVG: I only want to save the canvas, not lines drawn outside

I am trying to make some SVGs for my pen plotter using math to draw lines. I define the canvas size in setup, but some points that the math designates are outside of this. Here is example code that I simplified where this happens by hardcoding the start point of the line at (-50,0):

import processing.svg.*;
import java.util.*;
boolean bExportSVG = false;

void setup() {
  size(200, 200);
}

void draw () {
  background(255);

  if (bExportSVG) {
    beginRecord(SVG, "drawing.svg");
  }
  size(200, 200);
  rect(0, 0, width, height);
  line(-50, 0, 50, 100);
  if (bExportSVG)
  {
    endRecord();
    bExportSVG = false;
  }
}

void keyPressed()
{
  if (key == 'e')
  {
    bExportSVG = true;
  }
}

When I press “e” the file saves. The code works great. However, when I open the SVG in Inkscape, I get the bounding box rectangle and a line that intersects it and starts at (-50,0) outside of the (200,200) canvas. This is problematic because when I go to plot, I only want to plot what is inside the bounding box or canvas. How can I get the file to save the canvas only?

I would hope it would save only the bounding box and a line from (0,50) to (50,100).

This is a simplified example. In what I am really trying to do there are hundreds of lines landing outside.

(I repeated the size() under beginRecord because I thought that might solve the problem but it didn’t—I started learning Processing a couple weeks ago so very new at this.)

Duplicate of this thread: https://discourse.processing.org/t/creating-svg-limited-to-canvas-size/38426/9