Processing 如何在处理过程中用渐变填充矩形或椭圆?
我试着让我的球拍从白色变为渐变(线性),让球有一个径向渐变。谢谢你的帮助!您可以在void DrawPable中找到拨片的代码 这是我的目标: 这是我的代码: //球 int-ballX=500; int-ballY=350; int ballHeight=35; 整型球宽=35; int-speedX=4; int=4; int方向x=1Processing 如何在处理过程中用渐变填充矩形或椭圆?,processing,gradient,Processing,Gradient,我试着让我的球拍从白色变为渐变(线性),让球有一个径向渐变。谢谢你的帮助!您可以在void DrawPable中找到拨片的代码 这是我的目标: 这是我的代码: //球 int-ballX=500; int-ballY=350; int ballHeight=35; 整型球宽=35; int-speedX=4; int=4; int方向x=1 int方向y=1 //Paddles int player1X = 30; int player2X = 830; int player1Y = 350;
int方向y=1
//Paddles
int player1X = 30;
int player2X = 830;
int player1Y = 350;
int player2Y = 350;
//Healthbars
int bar1X = 100;
int bar1Y = 20;
int player1health = 100;
int bar1colour = #22E515;
int bar2X = 700;
int bar2Y = 20;
int player2health = 100;
int bar2colour = #22E515;
//Movements
boolean upX = false;
boolean downX = false;
boolean upY = false;
boolean downY = false;
void setup() {
size(900, 700);
}
void draw() {
background(55, 68, 120);
drawPaddle();
//EmptySpace**
fill(55, 68, 120);
noStroke();
rect(player1X, player1Y, 40, 140);
rect(player2X, player2Y, 40, 140);
//Healthbars
fill(bar1colour);
rect(bar1X, bar1Y, player1health, 15);
fill(bar2colour);
rect(bar2X, bar2Y, player2health, 15);
//Ball
fill(194, 16, 0);
ellipse(ballX, ballY, ballHeight, ballWidth);
moveCircle();
movePaddle();
moveCollisions();
}
void drawPaddle() {
fill(255);
noStroke();
rect(30, 0, 40, 1000);
rect(830, 0, 40, 1000);
}
void moveCircle() {
ballX = ballX + speedX * 1;
ballY = ballY + speedY * 1;
if (ballX > width- ballWidth +20 || ballX < ballWidth) {
speedX *= -1;
}
if (ballY > height- ballHeight +20 || ballY < ballHeight) {
speedY *= -1;
}
}
void movePaddle() {
//key movements
if (upX == true) {
player1Y = player1Y - 5;
}
if (downX == true) {
player1Y = player1Y + 5;
}
if (upY == true) {
player2Y = player2Y - 5;
}
if (downY == true) {
player2Y = player2Y + 5;
}
//Wrap around
if (player1Y > 700) {
player1Y = 0;
} else if (player1Y + 140 < 0) {
player1Y = 700;
}
if (player2Y > 700) {
player2Y = 0;
} else if (player2Y + 140 < 0) {
player2Y = 700;
}
}
void moveCollisions() {
//Collisions
if ((ballX - ballWidth / 2 < player1X + 40) && ((ballY - ballHeight / 2 > player1Y + 140) || (ballY + ballHeight / 2 < player1Y))) {
if (speedX < 0) {
player1health -= 20;
speedX = -speedX*1;
if (player1health == 20) {
bar1colour = #F51911;
}
}
} else if ((ballX + ballWidth / 2 > player2X) && ((ballY - ballHeight / 2 > player2Y + 140) || (ballY + ballHeight/2 < player2Y))) {
if (speedX > 0) {
player2health -= 20;
bar2X += 20;
speedX = -speedX*1;
if (player2health == 20) {
bar2colour = #F51911;
}
}
}
}
void keyPressed() {
if (key == 'w' || key == 'W') {
upX = true;
} else if (key == 's' || key == 'S') {
downX = true;
} else if (keyCode == UP) {
upY = true;
} else if (keyCode == DOWN) {
downY = true;
}
}
void keyReleased() {
if (key == 'w' || key == 'W') {
upX = false;
} else if (key == 's' || key == 'S') {
downX = false;
} else if (keyCode == UP) {
upY = false;
} else if (keyCode == DOWN) {
downY = false;
}
}
//拨片
int player1X=30;
int player2X=830;
int player1Y=350;
int player2Y=350;
//健康酒吧
int bar1X=100;
int bar1Y=20;
int player1health=100;
int bar1colour=#22E515;
int bar2X=700;
int-bar2Y=20;
int player2health=100;
int bar2colour=#22E515;
//运动
布尔upX=false;
布尔值downX=false;
布尔upY=false;
布尔绒毛=假;
无效设置(){
大小(900700);
}
作废提款(){
背景(55、68、120);
抽桨();
//空的空间**
填充(55、68、120);
仰泳();
rect(player1X,player1Y,40140);
rect(player2X,player2Y,40140);
//健康酒吧
填充(Bar1颜色);
rect(bar1X、bar1Y、player1health、15);
填充物(钡色);
rect(bar2X、bar2Y、player2health、15);
//球
填充(194,16,0);
椭圆(球X、球Y、球高、球宽);
moveCircle();
移动桨();
移动碰撞();
}
无效抽桨(){
填充(255);
仰泳();
rect(30,0,40,1000);
rect(830,0,40,1000);
}
void moveCircle(){
ballX=ballX+speedX*1;
巴利=巴利+斯皮迪*1;
如果(球>宽度-球宽度+20 | |球<球宽度){
speedX*=-1;
}
如果(球>高度-球高+20 | |球<球高){
速度*=-1;
}
}
void moveblade(){
//关键动作
如果(upX==true){
player1Y=player1Y-5;
}
if(downX==true){
player1Y=player1Y+5;
}
如果(upY==true){
player2Y=player2Y-5;
}
if(downY==true){
player2Y=player2Y+5;
}
//环绕
如果(播放器1Y>700){
player1Y=0;
}否则如果(播放器1Y+140<0){
player1Y=700;
}
如果(播放器2Y>700){
player2Y=0;
}否则如果(播放器2Y+140<0){
player2Y=700;
}
}
void moveCollisions(){
//碰撞
如果((球宽/2player1Y+140)| |(球高/2球员2X)和((球-球高/2>球员2Y+140)| |(球+球高/2<球员2Y))){
如果(speedX>0){
玩家2健康-=20;
bar2X+=20;
speedX=-speedX*1;
如果(player2health==20){
bar2colour=#F51911;
}
}
}
}
按下void键(){
如果(键=='w'| |键=='w'){
upX=真;
}else if(key='s'| | key='s'){
downX=真;
}else if(keyCode==UP){
upY=真;
}else if(keyCode==向下){
绒毛=真;
}
}
void keyReleased(){
如果(键=='w'| |键=='w'){
upX=假;
}else if(key='s'| | key='s'){
downX=假;
}else if(keyCode==UP){
upY=假;
}else if(keyCode==向下){
绒毛=假;
}
}
您可以尝试为正在绘制的每个矩形创建一个对象,使用渐变色填充它,然后在drawpable()中使用image(pgraphic,x,y)
而不是使用rect(x1,y1,x2,y2)
以下是如何在处理中使用lerpColor()
创建渐变效果:
- 将梯度的起点设定为
(x1,y1)
,终点设定为(x2,y2)
- 制作一个开始和结束颜色,如
c1
和c2
- 现在,对于点P
(x,y)
,计算起点和P之间的距离,然后除以起点和终点之间的距离。这将是从开始颜色和结束颜色到lerp的“数量”。
float t=dist(x1,y1,x,y)/dist(x1,x2,y1,y2)
- 将此值放入
lerpColor()
作为lerpColor(c1,c2,值)
。这将为您提供点P的颜色
- 对要计算渐变的每个点重复相同的操作
下面是一个例子:
注意:这里,我取t
,它是要lerped的量,即梯度起点之间的距离除以梯度起点和终点之间的距离,因为它始终是一个介于0和1之间的值
PGraphics g = createGraphics(50, 200); // set these values to the size(width, height) of paddle you want
color startColor = color(255, 25, 25); // color of start of the gradient
color endColor = color(25, 25, 255); // color of end of the gradient
g.beginDraw(); //start drawing in this as you would draw in the screen
g.loadPixels();//load pixels, as we are going to set the color of this, pixel-by-pixel
int x1 = g.width/2;
int y1 = 0;
int x2 = g.width/2;
int y2 = g.height;
// (x1, y1) is the start point of gradient
// (x2, y2) is the end point of gradient
// loop through all the pixels
for (int x=0; x<g.width; x++) {
for (int y=0; y<g.height; y++) {
//amout to lerp, the closer this is to (x2, y2) it will get closer to endColor
float t = dist(x1, y1, x, y) / dist(x1, y1, x2, y2);
// you need to convert 2D indices to 1D, as pixels[] is an 1D array
int index = x + y * g.width;
g.pixels[index] = lerpColor(startColor, endColor, t);
}
}
g.updatePixels(); // update the pixels
g.endDraw(); // we are done drawing into this
// Now, you can draw this using image(g, x, y);
PGraphics g=createGraphics(50200);//将这些值设置为所需挡板的大小(宽度、高度)
颜色开始颜色=颜色(255,25,25);//渐变开始的颜色
color endColor=color(25,25,255);//渐变结束的颜色
g、 beginDraw()//像在屏幕上绘制一样,在此开始绘制
g、 loadPixels()//加载像素,因为我们要设置这个的颜色,逐像素
int x1=g.宽度/2;
int y1=0;
int x2=g.宽度/2;
int y2=g.高度;
//(x1,y1)是渐变的起点
//(x2,y2)是梯度的终点
//循环遍历所有像素
对于(int x=0;x我编写了一个库,专门用于这种用途(在处理过程中绘制颜色渐变),名为-从Github发行版下载.jar并将其拖放到草图上。它将1D渐变作为二维光谱渲染到草图或给定的PGraphics
对象中
以下是使用所需渐变绘制线性和径向光谱的示例:
PeasyGradients renderer;
PGraphics rectangle, circle, circleMask;
final Gradient pinkToYellow = new Gradient(color(227, 140, 185), color(255, 241, 166));
void setup() {
size(800, 800);
renderer = new PeasyGradients(this);
rectangle = createGraphics(100, 400);
renderer.setRenderTarget(rectangle); // render into rectangle PGraphics
renderer.linearGradient(pinkToYellow, PI/2); // gradient, angle
circle = createGraphics(400, 400);
renderer.setRenderTarget(circle); // render into circle PGraphics
renderer.radialGradient(pinkToYellow, new PVector(200, 200), 0.5); // gradient, midpoint, zoom
// circle is currently a square image of a radial gradient, so needs masking to be circular
circleMask = createGraphics(400, 400);
circleMask.beginDraw();
circleMask.fill(255); // keep white pixels
circleMask.circle(200, 200, 400);
circleMask.endDraw();
circle.mask(circleMask);
}
void draw() {
background(255);
image(rectangle, 50, 50);
image(circle, 250, 250);
}
好问题,很棒的mycycle和void main的答案已经很好了。
这个看起来很棒
我想提供一个小的解决方法:使用P2D
渲染器通过形状处理渐变:
size(100,100,P2D);// render via OpenGL
noStroke();
// draw the rect by manually setting vertices
beginShape();
// gradient start
fill(#fef1a6);
vertex(0 , 0); // TL
vertex(100, 0); // TR
// gradient end
fill(#e28ab9);
vertex(100,100); // BR
vertex( 0,100); // BL
endShape();
对于更复杂的形状,这可以用于接合
PGraphics gradient;
PGraphics mask;
void setup() {
size(640, 360, P2D);
gradient = getGradientRect(width, height, #fef1a6, #e28ab9);
mask = getMaskRect(width, height);
}
void draw() {
background(#483b6c);
drawPongShapes();
// apply pong shapes mask to gradient
gradient.mask(mask);
// render masked gradient
image(gradient, 0, 0);
}
// draw white shapes (masks) on black background
void drawPongShapes(){
mask.beginDraw();
mask.background(0);
mask.ellipse(width * 0.5, height * 0.5, 60, 60);
mask.rect(width * .25, mouseY, 30, 150);
mask.rect(width * .75, height-mouseY, 30, 150);
mask.endDraw();
}
PGraphics getMaskRect(int w, int h) {
PGraphics layer = createGraphics(w, h, P2D);
layer.beginDraw();
layer.background(0);
layer.noStroke();
layer.fill(255);
layer.ellipseMode(CENTER);
layer.rectMode(CENTER);
layer.endDraw();
return layer;
}
PGraphics getGradientRect(int w, int h, color grad1, color grad2) {
PGraphics layer = createGraphics(w, h, P2D);
layer.beginDraw();
layer.noStroke();
// draw rect as shape quad
layer.beginShape();
// gradient start
layer.fill(grad1);
layer.vertex(0, 0); // TL
layer.vertex(w, 0); // TR
// gradient end
layer.fill(grad2);
layer.vertex(w, h); // BR
layer.vertex(0, h); // BL
layer.endShape();
layer.endDraw();
return layer;
}