If you want to reduce the opacity of all the parts of your rounded rectangle at the same time, and assuming you are drawing it frequently, it makes sense to create an image of the rounded rectangle, then you only have to draw one image each time, and the opacity will apply to everything on the image.
I’ve written you a function which does this, creating a rounded rectangle using Simeon’s code, and putting it into a mesh. Then some code in draw fades it smoothly into the background colour if you adjust the transparency parameter.
I suggest that if you want to do this stuff, then you need to learn about meshes and all the stuff in this code, before you go much further.
function setup()
--draw image of rounded rectangle
w,h,r,c=250,100,8,color(255,255,255)
roundedMesh=drawRoundRect(w,h,r,c)
parameter.integer("Transparency",0,255,255)
end
function drawRoundRect(w,h,r,c)
local img=image(w,h)
setContext(img)
roundRect(0,0,w,h,r,c)
setContext()
m=mesh()
--mesh will draw the image centred on whatever x,y you translate to
--if you prefer to have x,y be the bottom left corner
--use the commented line instead
--m:addRect(-w/2,-h/2,w,h)
m:addRect(0,0,w,h)
m:setRectTex(0,0,1,1)
m.texture=img
return m
end
function roundRect(x, y, w, h, r,c)
pushStyle()
stroke(c)
fill(c)
insetPos = vec2(x+r,y+r)
insetSize = vec2(w-2*r,h-2*r)
rectMode(CORNER)
rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
if r > 0 then
smooth()
lineCapMode(ROUND)
strokeWidth(r*2)
line(insetPos.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y)
line(insetPos.x, insetPos.y,
insetPos.x, insetPos.y + insetSize.y)
line(insetPos.x, insetPos.y + insetSize.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
line(insetPos.x + insetSize.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
end
popStyle()
end
function draw()
backColour=color(220,150,100) --needed so we can fade towards this colour
background(backColour)
pushMatrix()
translate(300,300) --go to where we want to draw rectangle
--fade towards the background colour, if required
if Transparency<255 then
local trans=Transparency/255
local transr=backColour.r*(1-trans)+255*trans
local transg=backColour.g*(1-trans)+255*trans
local transb=backColour.b*(1-trans)+255*trans
local transa=backColour.a*(1-trans)+255*trans
m:setColors(transr,transg,transb,transa)
end
m:draw()
popMatrix()
end