# Tutorial:Cameras (简体中文)

BlackBulletIV写了个"Cameras in Love2D"教程，分成了三块，你可以在下面点击相应链接。也可以看我的在此页的翻译。

# 第一部分：基础篇|创建摄像头的基本原理

```Starting:
|-----------|
| Default   |
|-----------|

love.graphics.push:
|-----------|
| Default   |       ^ pushed up
|-----------|
|-----------|
| New       |       <- pushed under
|-----------|

love.graphics.pop:
|-----------|         |-----------|
| Default   |         | New       |  -> discarded
|-----------|         |-----------|```

## The camera Module

```camera = {}
camera.x = 0
camera.y = 0
camera.scaleX = 1 --scale比例
camera.scaleY = 1
camera.rotation = 0  --rotation旋转

function camera:set()
love.graphics.push()
love.graphics.rotate(-self.rotation)
love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
love.graphics.translate(-self.x, -self.y)
end

function camera:unset()
love.graphics.pop()
end

function camera:move(dx, dy)
self.x = self.x + (dx or 0)
self.y = self.y + (dy or 0)
end

function camera:rotate(dr)
self.rotation = self.rotation + dr
end

function camera:scale(sx, sy)
sx = sx or 1
self.scaleX = self.scaleX * sx
self.scaleY = self.scaleY * (sy or sx)
end

function camera:setPosition(x, y)
self.x = x or self.x
self.y = y or self.y
end

function camera:setScale(sx, sy)
self.scaleX = sx or self.scaleX
self.scaleY = sy or self.scaleY
end```

camera模块的核心函数是setunsetset可以简单的创建变换，unset移除最后一个变换（相当于pop）

```function love.draw()
camera:set()
-- draw stuff
camera:unset()
end```

```camera.x = 50
camera:scale(3) -- 放大3倍```

## The Mouse

```function camera:mousePosition()
return love.mouse.getX() * self.scaleX + self.x, love.mouse.getY() * self.scaleY + self.y
end```