ClippingNode 裁剪图片
local drawNode = cc.DrawNode:create()
drawNode:drawDot(cc.p(0, 0), 100, cc.c4b(255,255,255,255))
local clippingnode = cc.ClippingNode:create(drawNode)
clippingnode:setAnchorPoint(cc.p(0.5,0.5))
clippingnode:setPosition(cc.p(960, 540))
clippingnode:setInverted(true)
clippingnode:setAlphaThreshold(0)
local sprite = cc.Sprite:create("test.png")
clippingnode:addChild(sprite)
self:addChild(clippingnode)
圆角矩形(转)
function cornerRadius(rect, radius, segments)
assert(rect and #rect == 2, string.format("cornerRadius - rect length is %d, was expecting 2 ", #rect))
local segments = segments or 100
local origin = cc.p(rect.x, rect.y)
local destination = cc.p(rect.x + rect.width, rect.y + rect.height)
if not radius then
return {origin, cc.p(origin.x, destination.y), destination, cc.p(origin.y, destination.x)}
end
local coef = math.pi / 2 / segments
local vertices = {}
for i=0, segments do
local rads = (segments - i) * coef
local x = radius * math.sin(rads)
local y = radius * math.cos(rads)
table.insert(vertices, cc.p(x, y))
end
local tagCenter = cc.p(0, 0)
local minX = math.min(origin.x, destination.x)
local maxX = math.max(origin.x, destination.x)
local minY = math.min(origin.y, destination.y)
local maxY = math.max(origin.y, destination.y)
local pPolygonPtArr = {}
tagCenter.x = minX + radius;
tagCenter.y = maxY - radius;
for i=0, segments do
local x = tagCenter.x - vertices[i + 1].x
local y = tagCenter.y + vertices[i + 1].y
table.insert(pPolygonPtArr, cc.p(x, y))
end
tagCenter.x = maxX - radius;
tagCenter.y = maxY - radius;
for i=0, segments do
local x = tagCenter.x + vertices[#vertices - i].x
local y = tagCenter.y + vertices[#vertices - i].y
table.insert(pPolygonPtArr, cc.p(x, y))
end
tagCenter.x = maxX - radius;
tagCenter.y = minY + radius;
for i=0, segments do
local x = tagCenter.x + vertices[i + 1].x
local y = tagCenter.y - vertices[i + 1].y
table.insert(pPolygonPtArr, cc.p(x, y))
end
tagCenter.x = minX + radius;
tagCenter.y = minY + radius;
for i=0, segments do
local x = tagCenter.x - vertices[#vertices - i].x
local y = tagCenter.y - vertices[#vertices - i].y
table.insert(pPolygonPtArr, cc.p(x, y))
end
return pPolygonPtArr
end
local pPolygonPtArr = cornerRadius({x = 400, y = 400, width = 200, height = 200}, 20, 100)
local drawNode = cc.DrawNode:create()
drawNode:drawPolygon(pPolygonPtArr, #pPolygonPtArr,cc.c4f(1,0,0,1), 1, cc.c4f(0,1,0,1))
self:addChild(drawNode)
保存纹理到本地
local render = cc.RenderTexture:create(texture:getContentSize().width, texture:getContentSize().height)
render:beginWithClear(0, 0, 0, 0)
local sp = cc.Sprite:createWithTexture(texture)
sp:setPosition(texture:getContentSize().width/2, texture:getContentSize().height/2)
sp:visit()
render:endToLua()
local savepath = "res/texture.png"
render:saveToFile(savepath, cc.IMAGE_FORMAT_PNG, true)