Hello, here is a simple example:
I've created this simple image, 30x30 pixels:

Download it and place it in an images
folder or anywhere it fits you.
And this is a simple code example:
import "CoreLibs/graphics"
import "CoreLibs/nineslice"
local pd_nineslice <const> = playdate.graphics.nineSlice
-- import the nineslice image from images folder (or anywhere from your projects folder structure)
local nineslice <const> = pd_nineslice.new("images/nineslice", 10, 10, 10, 10)
local renderWidth <const>, renderHeight <const> = 10, 10
-- local renderWidth <const>, renderHeight <const> = 100, 100
-- local renderWidth <const>, renderHeight <const> = 200, 100
function playdate.update()
nineslice:drawInRect(0, 0, renderWidth, renderHeight)
end
You can uncomment the commented renderWidth
and renderHeight
variables so you can see, how the nineslice is rendered based on these values. Based on these values, the image will be "stretched" to fit the given width and height.
If you'd like to use the nineslice with sprites, you can overload the sprite's draw
method. Something like this:
import "CoreLibs/graphics"
import "CoreLibs/nineslice"
local pd <const> = playdate
local pd_graphics <const> = pd.graphics
local pd_sprite <const> = pd_graphics.sprite
local pd_nineslice <const> = pd_graphics.nineSlice
local nineslice <const> = pd_nineslice.new("images/nineslice", 10, 10, 10, 10)
local renderWidth <const>, renderHeight <const> = 200, 100
local sprite <const> = pd_sprite.new()
sprite:moveTo(0, 0)
sprite:setCenter(0, 0)
sprite:setSize(renderWidth, renderHeight)
function sprite:draw()
nineslice:drawInRect(0, 0, self.width, self.height)
end
sprite:add()
function pd.update()
pd_sprite.update()
end