Difference between revisions of "love.graphics.translate"

(add another missing function, been reading the source again)
 
m (Grammar and stuff.)
 
(15 intermediate revisions by 11 users not shown)
Line 1: Line 1:
 
Translates the coordinate system in two dimensions.
 
Translates the coordinate system in two dimensions.
  
When this function is called with two numbers, dx, and dy, all the following drawing operations take effect as if their x and y coordinates were x+dx and y+dy. This change lasts until love.draw() exits.
+
When this function is called with two numbers, dx, and dy, all the following drawing operations take effect as if their x and y coordinates were x+dx and y+dy.  
 +
 
 +
Scale and translate are not commutative operations, therefore, calling them in different orders will change the outcome.
 +
 
 +
This change lasts until the next [[love.draw]] call, or a [[love.graphics.pop]] reverts to a previous [[love.graphics.push]], or [[love.graphics.origin]] is called - whichever comes first.
 +
 
 +
Translating using whole numbers will prevent tearing/blurring of images and fonts draw after translating.
 +
 
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
Line 13: Line 20:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Examples ==
 +
Translate down and to the right by 10 pixels. Remember, the translation is reset at the end of each [[love.draw]].
 +
<source lang="lua">
 +
function love.draw()
 +
  love.graphics.translate(10, 10)
 +
  love.graphics.print("Text", 5, 5)  -- will effectively render at 15x15
 +
end
 +
</source>
 +
 +
 +
Move the coordinate system with the mouse:
 +
<source lang="lua">
 +
tx=0
 +
ty=0
 +
function love.draw()
 +
mx = love.mouse.getX()
 +
my = love.mouse.getY()
 +
if love.mouse.isDown(1) then
 +
if not mouse_pressed then
 +
mouse_pressed = true
 +
dx = tx-mx
 +
dy = ty-my
 +
else
 +
tx = mx+dx
 +
ty = my+dy
 +
end
 +
elseif mouse_pressed then
 +
mouse_pressed = false
 +
end
 +
love.graphics.translate(tx, ty)
 +
 +
-- example graphics:
 +
love.graphics.circle( "line", 0, 0, 400 )
 +
love.graphics.line(-440, 0, 440, 0)
 +
love.graphics.line(0, -440, 0, 440)
 +
end
 +
 +
-- restore position with the right mouse button:
 +
function love.mousepressed(x, y, button, istouch)
 +
  if button == 2 then
 +
      tx = 0
 +
      ty = 0
 +
  end
 +
end
 +
</source>
 +
 +
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
 +
* [[love.graphics.pop]]
 +
* [[love.graphics.push]]
 +
* [[love.graphics.rotate]]
 +
* [[love.graphics.scale]]
 +
* [[love.graphics.shear]]
 +
* [[love.graphics.origin]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Draws a triangle.}}
+
{{#set:Description=Translates the coordinate system in two dimensions.}}
 +
{{#set:Since=000}}
 +
{{#set:Sub-Category=Coordinate System}}
 +
== Other Languages ==
 +
{{i18n|love.graphics.translate}}

Latest revision as of 18:20, 3 June 2022

Translates the coordinate system in two dimensions.

When this function is called with two numbers, dx, and dy, all the following drawing operations take effect as if their x and y coordinates were x+dx and y+dy.

Scale and translate are not commutative operations, therefore, calling them in different orders will change the outcome.

This change lasts until the next love.draw call, or a love.graphics.pop reverts to a previous love.graphics.push, or love.graphics.origin is called - whichever comes first.

Translating using whole numbers will prevent tearing/blurring of images and fonts draw after translating.

Function

Synopsis

love.graphics.translate( dx, dy )

Arguments

number dx
The translation relative to the x-axis.
number dy
The translation relative to the y-axis.

Returns

Nothing.

Examples

Translate down and to the right by 10 pixels. Remember, the translation is reset at the end of each love.draw.

function love.draw()
   love.graphics.translate(10, 10)
   love.graphics.print("Text", 5, 5)   -- will effectively render at 15x15
end


Move the coordinate system with the mouse:

tx=0
ty=0
function love.draw()
	mx = love.mouse.getX()
	my = love.mouse.getY()
	if love.mouse.isDown(1) then
		if not mouse_pressed then
			mouse_pressed = true
			dx = tx-mx
			dy = ty-my
		else
			tx = mx+dx
			ty = my+dy
		end
	elseif mouse_pressed then
		mouse_pressed = false
	end
	love.graphics.translate(tx, ty)
	
	-- example graphics:
	love.graphics.circle( "line", 0, 0, 400 )
	love.graphics.line(-440, 0, 440, 0)
	love.graphics.line(0, -440, 0, 440)
end

-- restore position with the right mouse button:
function love.mousepressed(x, y, button, istouch)
   if button == 2 then
      tx = 0
      ty = 0
   end
end


See Also


Other Languages