まゆたまガジェット開発逆引き辞典

電子工作やプログラミングのHowtoを逆引き形式で掲載しています。作りたいモノを決めて学んでいくスタイル。プログラマではないので、コードの汚さはお許しを。参照していないものに関しては、コピペ改変まったく問いません

Processingで作った画像をPNGとPDFで書き出す

よく忘れてしまうのでメモです。
redraw(); を使っている場合はredraw();を書いた箇所によってはエラーになったり、何も描画されてないことになったりするので注意です。

こちらprince9.hatenablog.com
のコードでできた画像をPDF書き出ししてみました。
PDFで書き出すと、イラレで編集ができます。
SVGはredraw(); を使うとあまり上手くいかないみたいです。
スペースキーで書き出しができます。

//PDF書き出し用ライブラリ
import processing.pdf.*;


int count;//ファイル名用変数

//最大個数分、または色のパターンの数配列を用意する
  int[] col2 = new int[25];//大さい三角パーツ
  
 //大きい三角の位置
  int triPx2;

 //大きい三角形の数
 int triPcount2;
 
 //色のパターン
 int pt2col;

  //基本の三角形の大きさ
int mosaicSacle;


//色の種類ぶんだけ配列をつくる
 color[] colarray = 
{ 
#ed621c,#e81c44,#abcf2d,#5fb734,#874195,#01449d,#45bcca,#f6c620,#898989,#ffffff
};

void setup() {
  size(600,600);
 noLoop();

 //基本の三角形の大きさ
   mosaicSacle = 20;
}

void draw() {
  background(255);
  
   for(int c2 = 0; c2 < col2.length; c2++){
  //色の乱数生成、大きい三角。ランダムの数値は色の種類の数
  col2[c2] = int(random(10));
}


//三角パーツ大
triBig();
}

void mousePressed() {
  beginRecord(PDF,"pattern"+count+".pdf");
 
      redraw();// ボタンが押されたときだけ再描画
      
}

//スペースキーでPDFとPNG書き出し
void keyPressed() {

  if ( key == ' ' ) {
     save("pattern"+count+".png");
     count++;        
       
        endRecord();
        }
  
  if (key == 'q'){
        exit();
}

  }

//図形の数や位置を設定
void triBig() {
 //三角形の数
    triPcount2 = int(random(1,6));
     //ランダムに算出された数ぶんだけ三角を描写
  for (int i2 = 0; i2 < triPcount2; i2++) {
     //これで1色ずつ色を変える(重複あり)
    for (pt2col = 0; pt2col < triPcount2; pt2col++) {
    //三角形の位置をランダムに
   triPx2 = int(random(1,15))*40;
   //三角形描写
 triBigpt();
}
}
}

//実際の図形の設定
void triBigpt() {
  pushMatrix();
  translate(triPx2,300);
  noStroke();
  
 
  fill(colarray[col2[pt2col]]);
  triangle(mosaicSacle/2, mosaicSacle/2,  0, mosaicSacle, mosaicSacle, mosaicSacle);
  fill(colarray[col2[pt2col+1]]);
  triangle(mosaicSacle, 0,  mosaicSacle/2,mosaicSacle/2 , mosaicSacle, mosaicSacle);
  fill(colarray[col2[pt2col+2]]);
  triangle(mosaicSacle, 0,  mosaicSacle+10, mosaicSacle/2, mosaicSacle, mosaicSacle);
   fill(colarray[col2[pt2col+3]]);
  triangle(mosaicSacle+10,mosaicSacle/2,mosaicSacle,mosaicSacle,mosaicSacle*2,mosaicSacle);

  popMatrix();
}