line(mouseX, mouseY, pmouseX, pmouseY); //point start and end
2.當滑鼠按下時才畫線
加上判別式 if(mousePressed)
更改背景顏色 background(color)
3.更改筆刷顏色
藉由滑鼠位置判斷選擇顏色進一步更改筆刷顏色
if(mouseX<100 && mouseY<100) {stroke(0,255,0);}
else if(mouseX<100 && mouseY<200){stroke(0,0,255);}
else if(mouseX<100 && mouseY<300){stroke(255,255,0);}
4.更改筆刷大小
當滑鼠移到右邊時,按下滑鼠時,Y愈大,筆刷愈粗
if(mouseX>700) myBrushSize = mouseY/30;
void setup(){
size(800,600);
background(255);
stroke(#FF081C); //line color
}
float myBrushSize = 1;
color myStroke = color(0);
void draw(){
stroke(0); strokeWeight(1);
fill(0,255,0); rect(0,0,100,100); //pointX,Y,w,h
fill(0,0,255); rect(0,100,100,100);
fill(255,255,0); rect(0,200,100,100);
stroke(myStroke); strokeWeight(myBrushSize);
}
void mouseDragged(){ //mouse move
line(mouseX, mouseY, pmouseX, pmouseY); //point start and end
}
void mousePressed(){ //mouse down
if(mouseX<100 && mouseY<100) {myStroke = color(0,255,0);}
else if(mouseX<100 && mouseY<200){myStroke = color(0,0,255);}
else if(mouseX<100 && mouseY<300){myStroke = color(255,255,0);}
if(mouseX>700) myBrushSize = mouseY/30;
}
5.刮畫
版本1 缺點 : 筆刷較大時會發現顏色出現斷層
PImage imgBG;
PImage imgBlack;
void setup() {
size(800, 600);
imgBG = loadImage("color.jpg");
imgBlack = createImage(800, 600, ARGB);
imgBlack.loadPixels();
for (int i = 0; i<imgBlack.pixels.length; i++)
imgBlack.pixels[i] = color(0, 0, 0, 255);
imgBlack.updatePixels();
}
void draw() {
image(imgBG, 0, 0, width, height);
image(imgBlack, 0, 0, width, height);
}
void mouseDragged() {
imgBlack.loadPixels();
imgBlack.pixels[mouseX+mouseY*width] = color(0, 0, 0, 0);
imgBlack.updatePixels();
}
版本 3
PImage imgBG;
PGraphics imgBlack;
void setup() {
size(800, 600);
imgBG = loadImage("color.jpg");
imgBG.resize(800,600);
imgBlack = createGraphics(800,600);
imgBlack.beginDraw();
imgBlack.background(0);
imgBlack.strokeWeight(5);
imgBlack.stroke(0,0,0,28);
imgBlack.endDraw();
}
void draw() {
background(0);
imgBG.mask(imgBlack);
if(keyPressed) image(imgBlack,0,0);
else image(imgBG,0,0,width,height);
}
void mouseDragged() {
imgBlack.beginDraw();
imgBlack.strokeWeight(5);imgBlack.stroke(255);
imgBlack.line(mouseX,mouseY,pmouseX,pmouseY);
imgBlack.endDraw();
}
沒有留言:
張貼留言