Difference between revisions of "love.graphics.rectangle"

(Arguments)
(See Also)
 
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{newin|[[0.3.2]]|032|type=function}}
 
Draws a rectangle.
 
Draws a rectangle.
 
== Function ==
 
== Function ==
=== Synopsis ===
 
<source lang="lua">
 
love.graphics.rectangle( mode, x, y, width, height )
 
</source>
 
=== Arguments ===
 
{{param|DrawMode|mode|How to draw the rectangle.}}
 
{{param|number|x|The position of top-left corner along the x-axis(The left side = 0).}}
 
{{param|number|y|The position of top-left corner along the y-axis.}}
 
{{param|number|width|Width of the rectangle.}}
 
{{param|number|height|Height of the rectangle.}}
 
 
=== Returns ===
 
Nothing.
 
 
== Function ==
 
{{newin|[[0.10.0]]|100|type=variant}}
 
Draws a rectangle with rounded corners.
 
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 28: Line 12:
 
{{param|number|width|Width of the rectangle.}}
 
{{param|number|width|Width of the rectangle.}}
 
{{param|number|height|Height of the rectangle.}}
 
{{param|number|height|Height of the rectangle.}}
{{param|number|rx|The x-axis radius of each round corner. Cannot be greater than half the rectangle's width.}}
+
{{New feature|0.10.0|
 +
{{param|number|rx (nil)|The x-axis radius of each round corner. Cannot be greater than half the rectangle's width.}}
 
{{param|number|ry (rx)|The y-axis radius of each round corner. Cannot be greater than half the rectangle's height.}}
 
{{param|number|ry (rx)|The y-axis radius of each round corner. Cannot be greater than half the rectangle's height.}}
 
{{param|number|segments (nil)|The number of segments used for drawing the round corners. A default amount will be chosen if no number is given.}}
 
{{param|number|segments (nil)|The number of segments used for drawing the round corners. A default amount will be chosen if no number is given.}}
 +
|100}}
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Notes ==
 +
Custom shaders which use texture coordinates will not work correctly with love.graphics.rectangle (or other shape-drawing functions), since primitive shapes don't have UV texture coordinates associated with the shape's vertex positions.
  
 
== Examples ==  
 
== Examples ==  
=== Draws a rectangle at 20,50 with a width of 60 and a height of 120 ===
+
 
 +
=== Draw a rectangle at 20,50 with a size of 60x120 ===
 +
<source lang="lua">
 +
function love.draw()
 +
love.graphics.rectangle("fill", 20,50, 60,120)
 +
end
 +
</source>
 +
 
 +
=== Draw a rotated rectangle ===
 
<source lang="lua">
 
<source lang="lua">
love.graphics.rectangle("fill", 20, 50, 60, 120 )
+
function drawRotatedRectangle(mode, x, y, width, height, angle)
 +
-- We cannot rotate the rectangle directly, but we
 +
-- can move and rotate the coordinate system.
 +
love.graphics.push()
 +
love.graphics.translate(x, y)
 +
love.graphics.rotate(angle)
 +
love.graphics.rectangle(mode, 0, 0, width, height) -- origin in the top left corner
 +
-- love.graphics.rectangle(mode, -width/2, -height/2, width, height) -- origin in the middle
 +
love.graphics.pop()
 +
end
 +
 
 +
local angle = 0
 +
function love.update(dt)
 +
angle = angle + dt * 1 -- Rotate at one radian per second.
 +
end
 +
 
 +
function love.draw()
 +
drawRotatedRectangle("fill", 150,150, 100,60, angle)
 +
end
 
</source>
 
</source>
  
 
== See Also ==
 
== See Also ==
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
 +
* [[love.graphics.polygon]]
 +
* [[love.graphics.draw]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
[[Sub-Category::Drawing| ]]
 
[[Sub-Category::Drawing| ]]
 
{{#set:Description=Draws a rectangle.}}
 
{{#set:Description=Draws a rectangle.}}
{{#set:Since=000}}
+
 
{{#set:PrettySince=0.3.2}}
 
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.rectangle}}
 
{{i18n|love.graphics.rectangle}}

Latest revision as of 18:39, 6 February 2022

Available since LÖVE 0.3.2
This function is not supported in earlier versions.

Draws a rectangle.

Function

Synopsis

love.graphics.rectangle( mode, x, y, width, height, rx, ry, segments )

Arguments

DrawMode mode
How to draw the rectangle.
number x
The position of top-left corner along the x-axis.
number y
The position of top-left corner along the y-axis.
number width
Width of the rectangle.
number height
Height of the rectangle.
Available since LÖVE 0.10.0
number rx (nil)
The x-axis radius of each round corner. Cannot be greater than half the rectangle's width.
number ry (rx)
The y-axis radius of each round corner. Cannot be greater than half the rectangle's height.
number segments (nil)
The number of segments used for drawing the round corners. A default amount will be chosen if no number is given.

Returns

Nothing.

Notes

Custom shaders which use texture coordinates will not work correctly with love.graphics.rectangle (or other shape-drawing functions), since primitive shapes don't have UV texture coordinates associated with the shape's vertex positions.

Examples

Draw a rectangle at 20,50 with a size of 60x120

function love.draw()
	love.graphics.rectangle("fill", 20,50, 60,120)
end

Draw a rotated rectangle

function drawRotatedRectangle(mode, x, y, width, height, angle)
	-- We cannot rotate the rectangle directly, but we
	-- can move and rotate the coordinate system.
	love.graphics.push()
	love.graphics.translate(x, y)
	love.graphics.rotate(angle)
	love.graphics.rectangle(mode, 0, 0, width, height) -- origin in the top left corner
--	love.graphics.rectangle(mode, -width/2, -height/2, width, height) -- origin in the middle
	love.graphics.pop()
end

local angle = 0
function love.update(dt)
	angle = angle + dt * 1 -- Rotate at one radian per second.
end

function love.draw()
	drawRotatedRectangle("fill", 150,150, 100,60, angle)
end

See Also


Other Languages