How do I make some image invisible in love2d?

Asked

Viewed 141 times

6

How do I set the visibility of an image in Love2d? For example, make it 10%, 25% or 50% visible.

2 answers

5


An alternative to my comment and my previous response is to use the method setColor before drawing the image.

local opacidade = .5; -- de 0 à 1
love.graphics.setColor(0xFF, 0xFF, 0xFF,
    0xFF * opacidade);

Or without fractions:

local opacidade = 255; // de 0 à 255
love.graphics.setColor(0xFF, 0xFF, 0xFF, opacidade);

Defining transparency

In the first use, opacity (transparency) is defined using a number ranging from 0 to 1, which optionally contains decimal places. 0 is 0% visible and 1 is 100% visible. .5 (or 0.5) is 50% visible. This can be more specific even, for example: .55559.

In the second use, opacity is defined using an integer number ranging from 0 to 255. 0 is 0% visible and 255 is 100% visible. 127 is practically 50% visible.

Example:

local Imagem1, opacidade;

opacidade = .5; -- 50%

function love.load()
    Imagem1 = love.graphics.newImage 'test.png';
end

function love.draw()
    -- setColor vem antes...
    love.graphics.setColor(0xFF, 0xFF, 0xFF,
        0xFF * opacidade);
    -- de draw.
    love.graphics.draw(Imagem1);
end

Example repeating the image 3 times:

duplicatas

local imagem1;

function love.load()
    -- Carrega a imagem
    imagem1 = love.graphics.newImage 'test.png';
end

function love.draw()
    -- #Duplicata1 20% visível (No canto.)
    love.graphics.setColor(0xFF, 0xFF, 0xFF, .2 * 0xFF);
    love.graphics.draw(imagem1);

    -- #Duplicata2 50% visível (No meio.)
    love.graphics.setColor(0xFF, 0xFF, 0xFF, .5 * 0xFF);
    love.graphics.draw(imagem1, 150);

    -- #Duplicata3 100% visível (Em cima de todas.)
    love.graphics.setColor(0xFF, 0xFF, 0xFF, 0xFF);
    love.graphics.draw(imagem1, 350);
end
  • how can I use it?

  • @arthurgps2 You refer to defining transparency?

  • dwarf let it go you’ve already edited xD,but one thing,vc wrote this code based on java? I recognized because of ; at the end of each line

  • @arthurgps2 I like to use it, and Nop ! I don’t know deeply Java, but it looks like C#, which are both beautiful languages :d

  • 1

    This well, alias for some reason,

  • calm,now I have another doubt. It is possible to leave two images, one visible completely and the other only partially?

  • @arthurgps2 In this case you will need to reject the setColor method with the last argument 255 (or 0xFF) before drawing the image fully visible (if it is drawn after a transparent image).

  • Can you give me an example? I’m sorry if I’m asking too much of Voce,and I’m still learning LOVE2D...

  • @arthurgps2 I added an example

Show 4 more comments

1

Based on my comment, it is possible to define the visibility of an image by copying its data, mapping its pixels to modify the alpha field, and in the end create another image through the modified data.

You need to do this only once if you need to define visibility.

Demonstration

db

This example should work with common images (BMP, JPEG and PNG):

local imagem2;

function love.load()
    -- Carrega a imagem
    local imagem1 = love.graphics.newImage 'test.png';

    -- Pega seus dados
    local imageData = imagem1:getData();

    --[=[ https://love2d.org/wiki/CompressedImageData
       Adiciona um erro para dados compressos
     * (dados compressos vem de imagens
     * do formato DXT1, DXT5, and BC5 / 3Dc, raramente usados).
     * Eu pessoalmente não sei que dados são e como funcionam.
     * O seu uso mais comum será BMP, JPEG ou PNG, creio eu. ]=]
    assert(not imagem1:isCompressed(),
        'Formato de imagem desconhecido');

    -- A opacidade vai de 0 até 1
    local opacidade = .2;

    -- Muda a opacidade de cada pixel
    imageData:mapPixel(function(x, y, r, g, b, a)
        return r, g, b, a * opacidade;
    end);

    imagem2 = love.graphics.newImage(imageData);
end

function love.draw()
    love.graphics.draw(imagem2);
end
  • I will still leave this answer if solve any problem of setColor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.