kikito / anim8

An animation library for LÖVE
MIT License
730 stars 88 forks source link

Possible to crop frame of animation? #31

Closed jakebesworth closed 6 years ago

jakebesworth commented 6 years ago

You can have a grid, and an animation. The grid seems to be defined as just an image, and spritesheet metadata such as width, height, frame width height...

Let's say we have a single image, single frame. If we're drawing with the simple '1-1' animation, is there a way to edit the grid / animation to reduce the height or width of an image, to basically take a 100% image, reduce it's height by 1% every dt, such that it shrinks to disappearing?

This is useful for say a healthbar image, where it needs to constantly increase, decrease.

The most obvious solution is just reducing the frame height by 1, and increase y by 1 so the "health bar" (as an example) stays in place, but reduces the actual image being printed.

I'm wondering if this is possible with anim8 and if how, I've looked into the docs quite a bit, but can't seem to find an obvious solution.

Thanks @kikito !

jakebesworth commented 6 years ago

I found that each frame is just a quad, so the following code will decrease the top of the image from 100% to 0% until it has completely disappeared:

local currentFrame = animation.frames[animation.position]
 x, y, w, h = currentFrame:getViewport()

if(h > 0) then
    currentFrame:setViewport(x, y + 1, w, h - 1)
    self.y = self.y + 1 -- These are positions for animation:draw
end

This code doesn't take into account more than 1 frame, but that shouldn't be too hard to add.

Now that we can deal with quads directly, it makes doing any sort of more advanced work on the animations much easier