Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created February 6, 2012 00:42
Show Gist options
  • Save tatsuro-ueda/1748609 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/1748609 to your computer and use it in GitHub Desktop.
Processing Examples Basic Image
// 「mask」として半透明な画像を使う。
// 2つの画像はPImageのmask()メソッドで合成される。
PImage img;
PImage maskImg;
void setup() {
size(200, 200);
img = loadImage("test.jpg");
maskImg = loadImage("mask.jpg");
img.mask(maskImg);
imageMode(CENTER);
}
void draw() {
background(map(mouseX+mouseY, 0, width+height, 0, 255));
image(img, width/2, height/2);
image(img, mouseX, mouseY);
}
// このサンプルプログラムはProcessingに背景画像を入れるもっとも手軽な方法である
// 背景として画像を使うには、幅と高さが同じでなければならない。
PImage bg;
int a;
void setup()
{
size(200,200);
frameRate(30);
// 背景画像はsize()メソッドに与えたパラメータと同じサイズでなければならない
// "milan_rubbish.jpg"は200 x 200 pixelsである。
bg = loadImage("milan_rubbish.jpg");
}
void draw()
{
background(bg);
a = (a + 1)%(width+32);
stroke(226, 204, 0);
line(0, a, width, a-26);
line(0, a-6, width, a-32);
}
// createImage()関数は手軽に遊ぶことのできる画像を提供する。
// 本サンプルプログラムではグラデーション画像をつくる
PImage img;
void setup()
{
size(200, 200);
// createImage(width, height, format)
// RGB, ARGB, ALPHA (grayscale alpha channel)
img = createImage(120, 120, ARGB);
//img.pixelsは、イメージの全てのピクセルの色を配列に格納する
for(int i=0; i < img.pixels.length; i++) {
// アルファチャンネル値をiの2倍(つまり0~240)として
// 高さの分だけの回数を描いている。
img.pixels[i] = color(0, 90, 102, i%img.width * 2);
}
}
void draw()
{
background(204);
image(img, 33, 33);
// 120x120の画像なので、
// 下のように設定すると正方形の中心にマウスカーソルが来る
image(img, mouseX-60, mouseY-60);
}
// 画像は画像自身のサイズで表示させることも、他のサイズで表示させることもできる
PImage a; // PImage型の変数「a」を宣言する
void setup() {
size(200, 200);
a = loadImage("jelly.jpg"); // 画像をプログラムの中で使う
noLoop(); // Draw()を1回だけ走らせる
}
void draw() {
// 画像をそれ自身のサイズで(0, 0)に表示する
image(a, 0, 0);
// 画像を半分のサイズで(100,0)に表示する
image(a, 100, 0, a.width/2, a.height/2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment