Walkthrough: Sailing
Sailing - Walkthrough
Steps
- Add a button to
index.htmlwith an id of#sailbutton. - Style and position the button using CSS.
- Modify the
Controllerconstructor so it takes a parameter ofship. Setthis.shiptoshipand modify any other references toshipinControllerto come fromthis.ship, not method parameters. - Modify your constructor call in
index.htmlso it also takes a ship. - Add a
clickevent listener to the button inside theControllerconstructor and set the callback function to an arrow function that callsthis.setSail. - Use the index of the next port in the ship's itinerary to find the corresponding DOM element
- Set the ship's left position to the next port's
offsetLeft- do so gradually usingsetInterval, adding 1px every n (you choose) milliseconds, so the boat moves along gradually. Be sure to useclearIntervalwhen the ship reaches the port, and be sure to callship.setSailandship.dockso our instance ofShip` is updated. - Alert the user that they're at the end of their cruise if they attempt to go further than the itinerary.
Add a button to index.html with an id of #sailbutton.
I've chosen to place my button within the #viewport element, but you can place yours wherever you please.
Style and position the button using CSS.
Let's make our button look super retro by using an old-school font.
Head on over to Google Fonts and find a font you like the look of:

Click the + icon and instructions on how to embed the font should come up somewhere on the screen:

Embed the specified HTML inside your head tag and keep the tab open as you'll need to add the CSS rule to your button. I've gone with the Press Start 2P font.
Now head over to your stylesheet. I've used the below styling:
Notice how the CSS from Google Fonts has been added to #sailbutton. It also falls back to the cursive font if there's an error downloading the font.
We can also add styling for when the button is clicked using :focus:
Modify the Controller constructor so it takes a parameter of ship. Set this.ship to ship and modify any other references to ship in Controller to come from this.ship, not method parameters.
And remove the ship parameter from renderShip and set it to this.ship instead.
Modify your constructor call in index.html so it also takes a ship.
Note the order has been shifted around here.
Add a click event listener to the button inside the Controller constructor and set the callback function to an arrow function that calls this.setSail.
The reason we don't just do this:
is because the this context when calling this.setSail would refer to information about the event, rather than our properties/methods on the Controller instance. Instead we pass an arrow function, which takes its this context from it's scope (the constructor) so we ensure that it does refer to our object.
Add a setSail method to the Controller.prototype.
Use the index of the next port in the ship's itinerary to find the corresponding DOM element
Exactly the same as what we did in renderShip but now we get the next index which returns the next port element.
Set the ship's left position to the next port's offsetLeft - do so gradually using setInterval, adding 1px every n (you choose) milliseconds, so the boat moves along gradually. Be sure to use clearInterval when the ship reaches the port, and be sure to call ship.setSail and ship.dock so our instance of Ship is updated.
Firstly we get hold of our #ship element with document.querySelector.
Then we set an interval to run a function every 20 milliseconds:
Inside this function, we find the ship's current CSS property left value as an integer:
Then we say that if the ship element's current left position is that of the next port (minus 32 pixels so the ship shows left of the port) then we'll clear the interval (stop running our function) and we'll call setSail and dock on our ship to update it's data:
Lastly, we just add to the ship's left CSS property:
Alert the user that they're at the end of their cruise if they attempt to go further than the itinerary.
After the declaration of nextPortElement, we add an if statement to check it exists. If it doesn't then we use alert to show a message to the user in their browser window.