標籤:style blog http color java os for 2014
在Processing中做字型變形通常需要有以下基礎知識:
1、PGraphics對象
2、圖片像素化
製作過程也不複雜,代碼如下:
1 color ELLIPSE_COLOR = color(0); 2 color LINE_COLOR = color(0, 125); 3 color PGRAPHICS_COLOR = color(0); 4 int LINE_LENGTH = 25; 5 boolean reverseDrawing = false; 6 PGraphics pg; 7 PFont f = createFont("宋體", 42); 8 void setup() { 9 size(1280, 720,P2D);10 pg = createGraphics(width, height, JAVA2D);11 pg.beginDraw();12 pg.textFont(f);13 pg.textSize(300);14 pg.textAlign(CENTER, CENTER);15 pg.fill(PGRAPHICS_COLOR);16 pg.text("麥塔威", pg.width/2, pg.height/2);17 pg.endDraw();18 }19 void draw() {20 int gridH = (int) map(mouseX, 0, width, 30, 100);21 int gridV = (int) map(mouseY, 0, height, 15, 100);22 float w = width/gridH;23 float h = height/gridV;24 float r = min(w, h);25 26 background(255);27 strokeWeight(1);28 for (int y=0; y<gridV; y++) {29 for (int x=0; x<gridH; x++) {30 float _x = x*w;31 float _y = y*h;32 color c = pg.get(int(_x), int(_y));33 boolean textDraw = (c == PGRAPHICS_COLOR);34 if (textDraw) {35 noStroke();36 fill(ELLIPSE_COLOR);37 ellipse(_x, _y, r, r);38 } else {39 stroke(LINE_COLOR);40 line(_x, _y, _x+LINE_LENGTH, _y+LINE_LENGTH);41 }42 }43 }44 }
其中,setup部分的pg操作都是在PGraphics對象上的操作,這個對象就類似於畫布上的畫布,而draw裡面兩個for迴圈則是實現字型變形的關鍵,將圖片像素化以後比對字型顏色和背景顏色就可以將字型從背景中“摳”出來,然後想捏扁還是捏圓就看各人的喜好了~