Difference between revisions of "Canvas:clear"

(Add example.)
m (Added note about performance)
Line 1: Line 1:
 
{{newin|[[0.8.0]]|080|type=function|text=It has been renamed from [[Framebuffer:clear]]}}
 
{{newin|[[0.8.0]]|080|type=function|text=It has been renamed from [[Framebuffer:clear]]}}
Clears content of a [[Canvas]].
+
Clears the contents of a [[Canvas]] to a specific color.
  
When called without arguments, the [[Canvas]] will be cleared with color rgba = {0,0,0,0}, i.e. it will be fully transparent. If called with color parameters (be it numbers or a color table), the alpha component may be omitted in which case it defaults to 255 (fully opaque).
+
Calling this function directly after the Canvas becomes active (via [[love.graphics.setCanvas]] or [[Canvas:renderTo]]) will be more efficient than calling it when the Canvas isn't active, especially on mobile devices.
  
It's possible to clear a section of the canvas with [[love.graphics.setScissor]].
+
[[love.graphics.setScissor]] will restrict the area of the Canvas that this function affects.
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
Line 16: Line 16:
  
 
== Function ==
 
== Function ==
 +
Clear the canvas to transparent black (0, 0, 0, 0).
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 29: Line 30:
  
 
== Function ==
 
== Function ==
 +
Clear the canvas to a specific color.
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 42: Line 44:
 
If the c-key is pressed the canvas will be cleared before drawing a new line on the screen.
 
If the c-key is pressed the canvas will be cleared before drawing a new line on the screen.
 
<source lang="lua">
 
<source lang="lua">
local canvas = love.graphics.newCanvas();
+
local canvas = love.graphics.newCanvas()
local clear;
+
local clear
 
function love.update()
 
function love.update()
 
     -- Use an anonymous function to draw lines on our canvas.
 
     -- Use an anonymous function to draw lines on our canvas.
 
     canvas:renderTo(function()
 
     canvas:renderTo(function()
         if clear then canvas:clear(); end  -- Clear the canvas before drawing lines.
+
         if clear then canvas:clear() end  -- Clear the canvas before drawing lines.
         love.graphics.setColor(love.math.random(255), 0, 0);
+
         love.graphics.setColor(love.math.random(255), 0, 0)
         love.graphics.line(0, 0, love.math.random(0, love.graphics.getWidth()), love.math.random(0, love.graphics.getHeight()));
+
         love.graphics.line(0, 0, love.math.random(0, love.graphics.getWidth()), love.math.random(0, love.graphics.getHeight()))
     end);
+
     end)
 
end
 
end
  
 
function love.draw()
 
function love.draw()
     love.graphics.setColor(255, 255, 255);
+
     love.graphics.setColor(255, 255, 255)
     love.graphics.draw(canvas);
+
     love.graphics.draw(canvas)
 
end
 
end
  
 
function love.keypressed(key)
 
function love.keypressed(key)
     if key == 'c' then clear = not clear; end
+
     if key == "c" then clear = not clear end
 
end
 
end
 
</source>
 
</source>
Line 66: Line 68:
 
* [[parent::Canvas]]
 
* [[parent::Canvas]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Clears content of a Canvas.}}
+
{{#set:Description=Clears the contents of a Canvas to a specific color.}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|Canvas:clear}}
 
{{i18n|Canvas:clear}}

Revision as of 06:04, 26 February 2015

Available since LÖVE 0.8.0
It has been renamed from Framebuffer:clear.

Clears the contents of a Canvas to a specific color.

Calling this function directly after the Canvas becomes active (via love.graphics.setCanvas or Canvas:renderTo) will be more efficient than calling it when the Canvas isn't active, especially on mobile devices.

love.graphics.setScissor will restrict the area of the Canvas that this function affects.

Function

Synopsis

Canvas:clear( )

Arguments

None.

Returns

Nothing.

Function

Clear the canvas to transparent black (0, 0, 0, 0).

Synopsis

Canvas:clear( red, green, blue, alpha )

Arguments

number red
Red component of the clear color (0-255).
number green
Green component of the clear color (0-255).
number blue
Blue component of the clear color (0-255).
number alpha (255)
Alpha component of the clear color (0-255).

Returns

Nothing.

Function

Clear the canvas to a specific color.

Synopsis

Canvas:clear( rgba )

Arguments

table rgba
A sequence with the red, green, blue and alpha values as numbers (alpha may be ommitted).

Returns

Nothing.

Examples

Clear canvas before drawing

If the c-key is pressed the canvas will be cleared before drawing a new line on the screen.

local canvas = love.graphics.newCanvas()
local clear
function love.update()
    -- Use an anonymous function to draw lines on our canvas.
    canvas:renderTo(function()
        if clear then canvas:clear() end   -- Clear the canvas before drawing lines.
        love.graphics.setColor(love.math.random(255), 0, 0)
        love.graphics.line(0, 0, love.math.random(0, love.graphics.getWidth()), love.math.random(0, love.graphics.getHeight()))
    end)
end

function love.draw()
    love.graphics.setColor(255, 255, 255)
    love.graphics.draw(canvas)
end

function love.keypressed(key)
    if key == "c" then clear = not clear end
end

See Also

Other Languages