should I use canvas

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
jurses
Prole
Posts: 5
Joined: Sun May 03, 2015 10:23 am
Location: Canary Islands

should I use canvas

Post by jurses » Thu Dec 07, 2017 2:57 pm

Hi friends, I'm wondering in which situations I should use canvas, it will be easier if you show me some examples in games.
For example, I read in the wiki By drawing things that do not change position often (such as background items) to the Canvas.
Image

In this case, the background could be a canvas and the health bars, but what about the characters, should they be in a canvas too? and the particles like throwing things?

grump
Party member
Posts: 200
Joined: Sat Jul 22, 2017 7:43 pm

Re: should I use canvas

Post by grump » Thu Dec 07, 2017 10:37 pm

Drawing a bitmap on screen is fast. No need to use a canvas. Drawing complex things can be slow. Use a canvas if you need to draw complex things more than once per frame.
jurses wrote:
Thu Dec 07, 2017 2:57 pm
what about the characters, should they be in a canvas too?
Are they slow to render? Do you draw them more than once per frame? If yes, use a canvas. If no, don't.
jurses wrote:
Thu Dec 07, 2017 2:57 pm
and the particles like throwing things?
A particle system is the epitome of a thing that changes often. Just apply the basic logic that is outlined aboved and in the Wiki you mentioned.

Habba
Prole
Posts: 3
Joined: Fri Dec 08, 2017 1:38 pm

Re: should I use canvas

Post by Habba » Fri Dec 08, 2017 2:39 pm

Just a small question for grump.
Could you give an example of what things would be slow to render? I have just started and I'm just using PNG images for my sprites. Are these rendered in constant time or what factors influence the rendering?

I am just starting out and the only thing I am drawing to Canvas at the moment is the tiles that compose the background.

grump
Party member
Posts: 200
Joined: Sat Jul 22, 2017 7:43 pm

Re: should I use canvas

Post by grump » Fri Dec 08, 2017 6:20 pm

Habba wrote:
Fri Dec 08, 2017 2:39 pm
Could you give an example of what things would be slow to render?
Things that require multiple passes to draw, or lots of draw calls. The font generator that is linked in my signature has effect combinations that need to draw each character like 8-10 times with complex shaders to compute distance fields, normal maps, etc. It takes up to several hundred milliseconds to render a complete font; it makes sense to render it one time onto a Canvas and use the resulting image to draw actual text with, which takes almost no time.
I have just started and I'm just using PNG images for my sprites. Are these rendered in constant time or what factors influence the rendering?
GPUs are really good at pushing pixels fast. What takes time is drawing many different things and/or using complex shaders. There is no benefit using a canvas to draw simple sprites, since they're technically the same thing.

User avatar
DiegoG
Prole
Posts: 17
Joined: Sat Apr 29, 2017 2:43 am
Location: Venezuela

Re: should I use canvas

Post by DiegoG » Sat Dec 09, 2017 3:16 am

I would recommend using canvases for things like backgrounds, but, as grump said, mostly only for complex drawings and other slow rendering processes.

For example, in one of my projects I create a tile-based map with a simple table of tile-ids, and so each tile must be drawn at a different position several times, then added the first layer of non-ground tiles (like trees and such), then the second and third, and finally the layer of squares that will be the visible grid. The whole process hits the system with a whopping damage of a fps drop from 60 to 5, so, naturally, I added a reload button and started drawing the whole thing once in a canvas, which kept it at 58fps in the worst cases, and instantaneous most of the time!
I hope this example helps in the decisions of when to make canvases, good luck!
function earth:destroy()
00count=5000
00while count>0 do
0000lg.draw(explosion,math.random(0,600),math.random(0,800))
0000count=count-1
00end
00earth = nil
00print("Earth has been destroyed.")
end

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests