Chapter 9 - Draw from your smartphone¶
In the previous chapter, you were able to set environment variables to access the backend on different URLs.
In this chapter, you'll configure your frontend to access the backend that is running on your computer from your smartphone.
Warning
Please be aware both your computer and your smartphone must be on the same network for this to work.
Steps¶
Support TouchEvent in the Sketch component¶
The Sketch component is currently only supporting mouse events. You'll need to add support for touch events as well. This will allow you to draw on your smartphone.
- Add the
onTouchStart
event handler to theStage
component. - Add the
onTouchMove
event handler to theStage
component. - Add the
onTouchEnd
event handler to theStage
component.
Update the CSS¶
On mobile, while drawing on your screen, you may notice the page is scrolling up and down and might refresh as well. This is because the default behavior of the browser is to scroll the page when you're touching the screen.
Update the global CSS to avoid these issues on mobile.
frontend/src/styles/globals.css | |
---|---|
- Disable the default pull-to-refresh behavior on mobile.
Get your computer IP¶
An IP address is like a home address. Each device connected to a network has an IP address that designate the device.
Your computer has an IP address we'll use to access your drawing application that is running on your computer from your smartphone.
Getting your computer IP address varies from operating systems to another.
- Open a terminal.
- Type
hostname -I | awk '{print $1}'
and pressEnter
. - Note the IP address that follows.
From the Apple menu, select System Preferences....
- In System Preferences, select Wi-Fi.
- In the right of the "Wi-Fi" window, click on the "Details" button of the network you're actually connected to.
- Under TCP/IP, note your current IP address. It should be something like 192.168.1.x, 10.x.x.x, 172.x.x.x.
- Open a command prompt.
- Type
ipconfig
and pressEnter
. - Your IP address will be the IPv4 address.
Set your IP address as the backend URL¶
In your frontend environment variables, set the backend URL as your computer IP with the port. Replace my IP 10.11.12.13
with the one from your computer.
Update the dev container configuration¶
In your dev container configuration, open the ports 3000
and 4000
to be able to access the frontend and the backend from any device of the network, for example your smartphone.
After editing the file, open the Visual Studio Code command Palette with View > Command palette... and type the Dev Containers: Rebuild and Reopen in Container command and select it. This will rebuild your devcontainer.
Access your drawing application from your smartphone¶
Start both applications.
On your smartphone, open a Web browser. Access your computer IP and port. Using the previous example, that would be http://10.11.12.13:3000.
You should now be able to draw from your smartphone! Open a window on your computer using the same address and you should be able to see both players drawing at the same time!
Summary¶
Congrats! You can now draw collaboratively on different devices that are on the same network! The next step in to allow to use your application from anywhere.