FMC2nd_p41_Lines09

p41_Lines09

p41_Lines09


イメージからアプレットを起動

描画され回転する画面と、それをマスクした表現。の再現
マスクの方法はp41_Lines08と同じです。
用意したマスク用外部ファイルはこちら。(真っ白画像。右クリックなどで「リンク先を別名で保存」などでダウンロードしてください。)
しかし、この方法ではrotate()を使って画面を回転させたとき、マスクとなる画像も含めすべて回ってしまうので、レイヤーとしてマスクの下にあるLineだけを回すには、他の処理が必要となった。
その方法は、Lineをオブジェクト化して一本一本にx,yの座標情報を持たせ、そのオブジェクト群の座標をrotate()で更新させていく。さらに一方で「見せかけマスク」となる画像を上書きするときには座標を戻して、常に同じ位置で描画するようpushMatrix()とpopMatrix()を使いました。


p41_Lines09.pde(メイン)
[java]
int leftBounds = 20 -200;
int topBounds = 20 -200;
int x = leftBounds;
int y = topBounds;
int rightBounds = 380 -200;
int bottomBounds = 380 -200;
int baseMax = 20;
int randomMax = 10;
int maxLines = 400;
int counter = 0;
int alpha = 20;

Line[] Lines;
//Ellipse[] Ellipses;

PImage myMask;

void setup(){
size(400, 400);
colorMode(HSB, 360, 100, 100);
background(0, 0, 100);

Lines = new Line[maxLines];
// Ellipses = new Ellipse[maxLines];

myMask = loadImage(“mask.png”);

strokeWeight(1);
smooth();
}

void draw(){
pushMatrix();
background(0,0,100);
if(counter < maxLines){
int maxDistance = baseMax * floor(random(0, 1) * randomMax);
int minDistance = 2;
int direction = random(0, 1) < 0.5 ? -1 : 1;
int xEnd = x + (direction * (minDistance + int( random(0, 1) * maxDistance)) );
direction = random(0, 1) < 0.5 ? -1 : 1;
int yEnd = y + (direction * (minDistance + int( random(0, 1) * maxDistance)) );

if(xEnd < leftBounds) xEnd = leftBounds;
if(xEnd > rightBounds) xEnd = rightBounds;
if(yEnd < topBounds) yEnd = topBounds;
if(yEnd > bottomBounds) yEnd = bottomBounds;

translate(200, 200);
rotate(counter/PI/360);

Lines[counter] = new Line(x, y, xEnd, yEnd);
Lines[counter].setAlpha(alpha);
Lines[counter].setWeight(maxDistance/20);
// Ellipses[counter] = new Ellipse(x, y, maxDistance * .2, maxDistance * .2);

x = xEnd;
y = yEnd;

}
else {
translate(200, 200);
rotate(counter/PI/360);
}
counter++;
// alpha++;
// if(alpha > 100) alpha = 0;

// if(counter >= 10 && counter // else if(counter > maxLines && counter < maxLines+10) {
// Lines[counter-10].disappear();
// }
// else if(counter > maxLines+10) noLoop();

if(counter < maxLines){
for(int i=0; i Lines[i].bdraw();
// Ellipses[i].bdraw();
}
}
else{
for(int i=0; i< maxLines; i++){
Lines[i].bdraw();
// Ellipses[i].bdraw();
}
}
popMatrix();
image(myMask, 0, 0);
}

void keyPressed(){
if(key == ‘r’){
background(255);
counter = 0;
Lines = new Line[maxLines];
// Ellipses = new Ellipse[maxLines];
}
if(key ==’q'){
save(“p41_Lines09.jpg”);
exit();
}

}
[/java]

Line.pde(クラス)
[java]
public class Line{
//member
int x, y, xEnd, yEnd;
color sColor;
int alpha;
int sWeight;
int counter;
//constructor
Line(int x, int y, int xEnd, int yEnd){
this.x = x;
this.y = y;
this.xEnd = xEnd;
this.yEnd = yEnd;
sColor = #006699;
alpha = 100;
sWeight = 10;
}
//method
void setAlpha(int alpha){
this.alpha = alpha;
// bdraw();
}

void setWeight(int sWeight){
this.sWeight = sWeight;
// bdraw();
}

void bdraw(){
stroke(sColor, alpha);
strokeWeight(sWeight);
line(x, y, xEnd, yEnd);
}
void disappear(){
x = y = xEnd = yEnd = 1000;
bdraw();
}
}
[/java]

タグ:

コメントをどうぞ