# Camera and aspect ratio

**URL:** <https://forum.defold.com/t/camera-and-aspect-ratio/9450>\
**Category:** Questions\
**Created:** [June 27, 2017, 7:37am UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450 "2017-06-27T07:37:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [June 27, 2017, 7:37am UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/1 "2017-06-27T07:37:36Z")

</div>

If we use the camera and fix they aspect ratio by script or checkbox in the camera properties panel we are having correct camera’s zoom only if screen.width\>screen.height (0:00-0:20)

[![](https://img.youtube.com/vi/O85dwX7oYUg/maxresdefault.jpg "Defold. Camera and aspect ratio.") ](https://www.youtube.com/watch?v=O85dwX7oYUg)

So we need correct the camera zoom for portrait screens, where screen.width\<screen.height:  
in video: 0:45-1:10

My code for this (see windowevent function):

```lua
local campos = vmath.vector3()
local nearz = .1
local farz = 2000
local initial_z = 680

function init(self)
	msg.post("#camera", "acquire_camera_focus")
	campos=go.get_position()
	campos.z=initial_z
	go.set_position(campos)
	msg.post("#camera", "set_camera", {aspect_ratio = 1.0, fov = 45, near_z = nearz, far_z = farz})
	window.set_listener(windowevent)
	windowevent(self,window.WINDOW_EVENT_RESIZED, {width=render_helper.window_res.x,height=render_helper.window_res.y})
end

function final(self)
	msg.post("#camera", "release_camera_focus")
end

--######################################## Window Event ########################################
function windowevent(self, event, data)
	if event == window.WINDOW_EVENT_RESIZED then
		print("Window resized: ", data.width, data.height)
		msg.post("#camera", "set_camera", {aspect_ratio = data.width/data.height, fov = 45, near_z = nearz, far_z = farz})
		if data.width<data.height then
			campos.z=initial_z*data.height/data.width
			go.set_position(campos)
		else
			campos.z=initial_z
			go.set_position(campos)
		end
	end
end

```

Is it right or I’m missing something in camera initialization, project settings, etc?

---

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [June 27, 2017, 8:10am UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/2 "2017-06-27T08:10:22Z")

</div>

UPD.  
this camera zoom script works nice at any resolution:  
 ![](https://img.youtube.com/vi/ueEkH2HvMG0/maxresdefault.jpg)https://www.youtube.com/embed/ueEkH2HvMG0?feature=oembed&wmode=opaque

but now I got new issue with blink some sprite (z-order of card’s back sprite is -0.1, hidden sprite -shield has 0.0) 😃

p.s perhaps I need to rename this topic to dev diary )

---

<div class="post-metadata">

**Author:** ![pradeep](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/p/f0a364/32.png) [@pradeep](https://forum.defold.com/u/pradeep)\
**Post date:** [December 19, 2017, 8:39pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/3 "2017-12-19T20:39:57Z")

</div>

What camera you are using for ur game? ortho or prespective? How are you zooming and focusing the screen centre on HTML5 build..

---

<div class="post-metadata">

**Author:** ![ddtnfm](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ddtnfm/32/10088_2.png) [@ddtnfm](https://forum.defold.com/u/ddtnfm)\
**Post date:** [August 5, 2018, 1:49pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/4 "2018-08-05T13:49:45Z")

</div>

this is exactly what I’ve been searching for, but I’ve copied this script to my camera’s script and got

```lua
ERROR:SCRIPT: /main/cam/cam.script:13: attempt to index global 'render_helper' (a nil value)

```

I guess I’m missing another step, may I ask for a beginner friendly instruction of using this camera script please?

---

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [August 5, 2018, 5:25pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/5 "2018-08-05T17:25:14Z")

</div>

render\_helper - this module contains game window resolution in table variable “window\_res”. And init script use it for beginning initialisation. You can change it to your own variables.

---

<div class="post-metadata">

**Author:** ![ddtnfm](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ddtnfm/32/10088_2.png) [@ddtnfm](https://forum.defold.com/u/ddtnfm)\
**Post date:** [August 5, 2018, 5:36pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/6 "2018-08-05T17:36:24Z")

</div>

well I’ve just started with defold and I don’t have any idea what should my variable be for this one, should I just define a variable with that name or there’s more to it?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 5, 2018, 7:31pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/7 "2018-08-05T19:31:18Z")

</div>

@ddtnfm Another option would be one of the already available camera solutions on the Asset Portal.

---

<div class="post-metadata">

**Author:** ![ddtnfm](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ddtnfm/32/10088_2.png) [@ddtnfm](https://forum.defold.com/u/ddtnfm)\
**Post date:** [August 6, 2018, 5:53am UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/8 "2018-08-06T05:53:21Z")

</div>

> [@britzl](#):
>
> @ddtnfm Another option would be one of the already available camera solutions on the Asset Portal.

oh, I didn’t knew about the asset portal, thanks 🙂

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 6, 2018, 2:26pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/9 "2018-08-06T14:26:16Z")

</div>

Yeah, we probably need to push for and link to it more @Axel and @samuel.nystedt. Top level links instead of hidden under community.

---

<div class="post-metadata">

**Author:** ![Axel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/axel/32/11723_2.png) [@Axel](https://forum.defold.com/u/Axel)\
**Post date:** [August 6, 2018, 2:46pm UTC](https://forum.defold.com/t/camera-and-aspect-ratio/9450/10 "2018-08-06T14:46:59Z")

</div>

Yep, agreed! We’re booting up post-holidays, but this is one of the things we plan to do soon.
