Doubled lines with SVG export

Hi!

I have a problem with exporting SVGs.
The code underneath is a adaptation of the star example .
I added SVG export but have the problem theat the resulting lines are doubled on top of each other.
this might be invisible but problematic if one wants to use a SVG for pen plotting/ laser cutting etc.

I would have blamed my code, but if I remove beginnRecord(); and endRecord(); and export with
size(800, 600, SVG, "stars.svg") the resulting SVG looks just fine - no doubled lines.
…but this is no solution since I do want processing to open a window.

Is this user error or a bug in the library?

UPDATE: The pdf library behaves in same way…:thinking:

import processing.svg.*;

void setup(){
  size(800, 600);
  //size(800, 600, SVG, "stars.svg"); This works - no double lines
  noLoop();
  noFill();
  background(255);
}

void draw(){
  beginRecord(SVG, year()+month()+day()+hour()+second()+"stars.svg");
  
  for(int i=0; i<12; i++){
    pushMatrix();
    translate(random(100,width-100), random(100,height-100));
    rotate(radians(random(360)));
    float innen = random(60);
    float aussen = random(innen, 140);
    star(0, 0, innen, aussen, int(random(3,12))); 
    popMatrix();  
  }
  
  endRecord();
}


void star(float x, float y, float radius1, float radius2, int npoints) {
  float angle = PI / npoints;
  float halfAngle = angle/2.0;
  beginShape();
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius2;
    float sy = y + sin(a) * radius2;
    vertex(sx, sy);
    sx = x + cos(a+halfAngle) * radius1;
    sy = y + sin(a+halfAngle) * radius1;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}

Not sure that I can help, but just for clarification I’m not sure I understand the problem. If you want to see both the screen image and the svg image which is shown in a browser you need to move the beginRecord() call up into the setup(). It’s described in this reference in case you haven’t seen it:
https://processing.org/reference/libraries/svg/index.html It’s the technique called SVG Export (With Screen Display)

screen output is shown below:

SVG output is shown here:

The window output is clearly different from the svg output; is that a problem?

1 Like

Hello @okelk ,

The noFill() is between beginRecord() and end Record() in my example.
I also simplified (only 2 stars) so I could see changes in SVG file more easily.

This will not create double lines in the SVG file and the sketch window will match the SVG generated:

import processing.svg.*;

void setup()
  {
  size(400, 400);
  noLoop();
  //noFill();
  //background(255);
  }

PShape sh;
String s;

void draw()
  {
  background(224);
   
  s = str(frameCount) + "stars.svg";
  println(s); 
  
  beginRecord(SVG, s);
  noFill();
  //noStroke();
  
  for(int i=0; i<2; i++){
    pushMatrix();
    translate(i*70+180, i*70+180);
    rotate(random(PI));
       
    float innen = 50;
    float aussen = 100;
    star(0, 0, innen, aussen, 2); 
    popMatrix();  
  }
  endRecord();
  }


void star(float x, float y, float radius1, float radius2, int npoints) {
  float angle = PI / npoints;
  float halfAngle = angle/2.0;
  beginShape();
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius2;
    float sy = y + sin(a) * radius2;
    vertex(sx, sy);
    sx = x + cos(a+halfAngle) * radius1;
    sy = y + sin(a+halfAngle) * radius1;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}

void mouseClicked()
  {
  redraw();
  }

Related issue (Processing 3.5.4):

:)

1 Like

Thanks! this is great! moving the noFill() inside the area where beginRecord() is active did the trick.

But I still think that there is some buggy behaviour here.
I would expect things like noFill() outside of the beginRecord() ‘endRecord()’ area to be ignored and not to add another shape to the file with no defined fill and no outline.

and I keep having other small issues with SVGs out of processing.

  • some programs can’t open them until i open and save them with inkscape or manualy replace the header in a text editor.
  • inkscape just ignores line-endings and and stroke-width. (and i suspect this is not inkscapes fault)
  • it always draws a rectangle around the whole canvas (even if background() is outside of beginRecord() and endRecord() )
  • the formating in the file is strange and hard to read - why are the end-tags > /> at the beginning of the next line?!

…unfortunately I’m not able to fi those things myself…but I might do some further testing.

This is a svg-file exported using a very minimalist test sketch. You’ll find the test-sketch underneath:

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" width="500" height="500" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
  /><g
  ><g style="stroke-linecap:round; fill:white; stroke:white;"
    ><rect x="0" width="500" height="500" y="0" style="stroke:none;"
    /></g
    ><g style="stroke-linecap:round; stroke-width:10;"
    ><rect x="200" width="100" height="100" y="200" style="fill:none;"
    /></g
  ></g
></svg
>
import processing.svg.*;

void setup(){
  size(500, 500);
  noLoop();
}

void draw(){
  background(255);
  beginRecord(SVG, "exportnofill.svg");
  noFill();
  rectMode(CENTER);
  strokeWeight(10);
  rect(width/2, height/2, 100, 100);
  endRecord();
}

beginRecord() seems to work just as well in the draw() section…the reason I put it there is so that i can use mousePressed() { redraw(); } to redraw and export the whole thing with clicking

This is the output I get with your code with Processing 4.3 on W10 and there is no rectangle around canvas:

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" width="500" height="500" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
  /><g
  ><g style="stroke-linecap:round; stroke-width:10;"
    ><rect x="200" width="100" height="100" y="200" style="fill:none;"
    /></g
  ></g
></svg
>

Agreed!

Formatted with SVG Formatter and Beautifier online :

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" width="500" height="500" xmlns="http://www.w3.org/2000/svg">
  <!--Generated by the Batik Graphics2D SVG Generator-->
  <defs id="genericDefs" />
  <g>
    <g style="stroke-linecap:round; stroke-width:10;">
      <rect x="200" width="100" height="100" y="200" style="fill:none;" />
    </g>
  </g>
</svg>

The source code here:
https://github.com/benfry/processing4/tree/main/java/libraries/svg

States that it is:

A very simple SVG exporter based on the PDF Export library that ships with Processing. Uses Batik’s SVG Generator.

I have made the same observations as you and it is what it is at the moment.
I can’t offer any more insights.

This project uses the Processing SVG exporter with a plotter:

:)