Add this component to an empty entity and it will create a plane, and an empty image with the resolution you desire as that plane’s material map. It’s really simple stuff, but it helps out when you want to add text that follows a character, etc. You can probably even make an entire game like this.
Just add the component, and call this in the draw function
yourCanvas:draw(function() -- do your drawing here end)
canvas = class()
function canvas:init(entity,w,h,c)
self.entity=entity
entity.model = craft.model.plane(vec2(40,20))
entity.material = craft.material(asset.builtin.Materials.Basic)
entity.eulerAngles=vec3(-90,0,-180)
entity.material.blendMode = NORMAL
entity.w=w or 500
entity.h=h or 200
entity.w2=entity.w/2
entity.h2=entity.h/2
entity.bg=c
entity.image=image(entity.w,entity.h)
entity.material.map = entity.image
self.entity.fill=function(entity)
setContext(entity.image)
background(entity.bg)
fill(0, 0)
stroke(0, 0)
rectMode(CORNER)
strokeWidth(8)
rect(0,0,entity.h)
setContext()
end
self.entity.draw=function(entity,cb)
entity:fill()
if cb~=nil then
setContext(entity.image)
cb()
setContext()
end
end
self.entity.obj=function(entity,obj)
setContext(Canvas.image)
obj:draw()
setContext()
end
end
In order to do the drawing, I usually just reuse some very simple functions.
--quick 2D
function drawRnd(x,y,w,h,c)
pushStyle()
w = w or 1
h = h or 1
c = c or color(255)
fill(c)
stroke(c)
ellipse(x,y,w,h)
popStyle()
end
function drawRect(x,y,w,h,c)
pushStyle()
w = w or 1
h = h or 1
c = c or color(255)
fill(c)
stroke(c)
rect(x,y,w,h)
popStyle()
end
function drawLine(x,y,w,h,c)
pushStyle()
w = w or nil
h = h or nil
c = c or color(255, 0, 0)
-- fill(c)
stroke(c)
strokeWidth(1)
line(x,y,w,h)
popStyle()
end
function drawSprite(x,y,a)
pushStyle()
local x=x or WIDTH/2
local y=y or HEIGHT/2
sprite(a,x,y)
popStyle()
end
function drawTextUI(x,y,words,c,s,fo)
pushStyle()
fo = fo or "SourceSansPro-Bold"
font()
s = s or 60
fontSize(s)
a = a or CENTER
textAlign(a)
c = c or color(255)
fill(c)
text(words, x, y)
popStyle()
end
Anyways, have a good one! If you make any changes, please share them! i’d love to improve upon this functionality.