Processing 字体变形

在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循环则是实现字体变形的关键,将图片像素化以后比对字体颜色和背景颜色就可以将字体从背景中“抠”出来,然后想捏扁还是捏圆就看各人的喜好了~

字体变形效果

爱好编程、喜欢计算机、也喜欢美食~
原文地址:https://www.cnblogs.com/x5115x/p/3901196.html