![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/03/watataku-hukidashi-light-blue.png)
今回の記事の目的はProcessingの「PGraphics.clear()
」を理解し、自分なりに使ってみること。
この記事を書いた人
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/03/watataku-user-profile-illustrationi-light-blue.png)
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/03/watataku-user-profile-illustrationi-light-blue.png)
- クリエイティブコーディングで制作した作品と、上達のためにやったこと・学習過程を発信
- コンセプトは「クリエイティブコーディング1万時間の歩き方」
- 2024年にProcessingの構文のアウトプットを終え、2025年から作品制作開始(クリエイティブコーディングで作成した作品集)
目次
【Processing】PGraphics.clear()について
PGraphics.clear()について
- 英語の「PGraphics.clear」は「グラフィックスをクリアする」という意味
PGraphics.clear()
は、画面を全部透明にして新しくするためのもの
【Processing】PGraphics.clear()の使い方【画像とコード】
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/11/how-to-use-pgraphics-clear-in-processing-refreshing-the-canvas1.png)
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/11/how-to-use-pgraphics-clear-in-processing-refreshing-the-canvas1.png)
このコードでは、毎フレームで pg.clear()
を使ってキャンバスを透明にリセットし、その後に赤色の円と緑色の四角を描いています。そのため、常に最新の描画内容だけが表示され、前のフレームで描かれた図形は表示されません。
PGraphics pg;
void setup() {
size(400, 200); // メインキャンバスのサイズを指定(幅400、高さ200)
pg = createGraphics(200, 200); // PGraphicsを初期化(サイズ200x200)
}
void draw() {
background(200); // メインキャンバスの背景色をグレーに設定
// PGraphicsに描画を開始
pg.beginDraw();
pg.clear(); // お絵かき用の紙をきれいにする(透明にする)
// 赤色の円を描く
pg.fill(255, 0, 0);
pg.ellipse(100, 100, 150, 150);
// 緑色の四角を描く
pg.fill(0, 255, 0);
pg.rect(75, 75, 50, 50);
pg.endDraw();
// メインキャンバスにPGraphicsを表示
image(pg, 100, 0); // PGraphicsをメインキャンバスに表示(位置: 100, 0)
}
【Processing】PGraphics.clear()を使ってみた感想
PGraphicsを使って、図形などを重ねたいなと思ったときに使います。
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/03/watataku-hukidashi-light-blue.png)
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/03/watataku-hukidashi-light-blue.png)
![](https://watataku-artist-katsuyaku.com/wp-content/uploads/2024/03/watataku-hukidashi-light-blue.png)
それでは今日もレッツワクワクコーディング。