#Dom updates for youse
Explore tagged Tumblr posts
t-baba · 5 years ago
Photo
Tumblr media
How to Build a Tic Tac Toe Game with Svelte
Svelte is a next generation way of building user interfaces.
While frameworks like React, Vue and Angular do the bulk of their work in the browser, Svelte takes it to the next level. It does its work when you build the app and it compiles your Svelte app to efficient vanilla JavaScript. So you get the best of both worlds. You write your code in Svelte which makes it easy to read, re-use and all the other benefits you get when you use a framework, and it makes for a blazing-fast web app as it complies down to vanilla JavaScript so that you don’t have the overhead of the JavaScript framework you’re using.
Svelte allows you to write less code. It also doesn’t use the concept of the Virtual DOM popularized by React. It instead surgically updates the DOM when the state of the app changes so the app starts fast and stays fast.
Prerequisites
For this tutorial, you need a basic knowledge of HTML, CSS and JavaScript.
You must also have installed the latest version of Node.js.
We’ll also be using npx, which comes installed by default with Node.js.
Throughout this tutorial we’ll be using yarn. If you don’t have yarn already installed, install it from here.
To make sure we’re on the same page, these are the versions used in this tutorial:
Node 12.10.0
npx 6.11.3
yarn 1.17.3
Getting Started with Svelte
In this tutorial, we’ll be building a Tic Tac Toe game in Svelte. By the end, you’ll be able to get up and running quickly with Svelte and get started in building your own apps in Svelte.
To get started, we must scaffold our app using degit. degit is more or less the same as git clone, but much quicker. You can learn more about it here.
Go ahead and make a new project by typing the following in the terminal:
$ npx degit sveltejs/template tic-tac-toe-svelte
npx lets you use the degit command without installing it globally.
Before npx, we would have to do the two following steps to achieve the same result:
$ npm install --global degit $ degit sveltejs/template tic-tac-toe-svelte
Thanks to npx, we don’t bloat our global namespace, and we always use the latest version of degit.
degit clones the repo https://github.com/sveltejs/template into a tic-tac-toe-svelte folder.
Go ahead into the tic-tac-toe-svelte directory and install the dependencies by typing the following in the terminal:
$ cd tic-tac-toe-svelte $ yarn
Now run the application by typing the following in the terminal:
$ yarn dev
Now open up the browser and go to http://localhost:5000 and you should see the following:
If you go into the src/ folder, you’ll see two files, App.svelte and main.js. main.js is the entry point of a Svelte app.
Open up the main.js and you should see the following:
import App from './App.svelte'; const app = new App({ target: document.body, props: { name: 'world' } }); export default app;
The above file imports App.svelte and instantiates it using a target element. It puts the component on the DOM’s document.body. It also passes name props to the App component. This prop will be accessed in App.svelte.
Components in Svelte are written using .svelte files which contain HTML, CSS and JavaScript. This will look familiar if youse worked with Vue.
Now open up App.svelte and you should see the following:
<script> export let name; </script> <style> h1 { color: purple; } </style> <h1>Hello {name}!</h1>
Firstly, we have the script tag inside, in which we have a named export called name. This should be similar to the prop mentioned in main.js.
Then we have a style tag that lets us style all the elements in that particular file, which is scoped to that file only so there’s no issue of cascading.
Then, at the bottom, we have an h1 tag, inside which we have Hello {name}!. The name in curly brackets will be replaced by the actual value. This is called value interpolation. That’s why Hello world! is printed on the screen.
Basic Structure of a Svelte Component
All .svelte files will basically have the following structure:
<script> /* Javascript logic */ </script> <style> /* CSS styles */ </style> <!-- HTML markup -->
The HTML markup will have some additional Svelte-specific syntax, but the rest is just plain HTML, CSS and JavaScript.
Making Tic Tac Toe in Svelte
Let’s get started with building our Tic Tac Toe game.
Replace main.js with the following:
import App from './App.svelte' const app = new App({ target: document.body, }) export default app
We’ve basically removed the props property from App component instantiation.
Now replace App.svelte with the following:
<script> const title = "Tic Tac Toe"; </script> <svelte:head> <title>{title}</title> </svelte:head> <h1>{title}</h1>
Here, we initialize a constant variable title with a string Tic Tac Toe.
Then, in the markup below, we use a special Svelte syntax, svelte:head, to set the title property in the head tag.
This is basically similar to doing this:
<head> <title>Tic Tac Toe</title> </head>
But the advantage of using the svelte:head syntax is that the title can be changed at runtime.
We then use the same title property in our h1 tag. It should now look like this:
Now create two other files in the src/ directory named Board.svelte and Square.svelte.
Open Square.svelte and paste in the following:
<script> export let value; </script> <style> .square { flex: 1 0 25%; width: 50px; height: 70px; background-color: whitesmoke; border: 2px solid black; margin: 5px; padding: 5px; font-size: 20px; text-align: center; } .square:hover { border: 2px solid red; } </style> <button class="square">{value}</button>
Basically, we’re creating a button and styling it.
Now open up Board.svelte and paste the following:
<script> import Square from "./Square.svelte"; let squares = [null, null, null, null, null, null, null, null, null]; </script> <style> .board { display: flex; flex-wrap: wrap; width: 300px; } </style> <div class="board"> {#each squares as square, i} <Square value={i} /> {/each} </div>
Here we’ve imported the Square component. We’ve also initialized the squares array, which will contain our X and 0’s data which is currently null.
The post How to Build a Tic Tac Toe Game with Svelte appeared first on SitePoint.
by Akshay Kadam via SitePoint https://ift.tt/2pOPORx
0 notes
sunburnacoustic · 3 months ago
Text
Dom news— a couple of weeks ago, Dom was invited along with his friend Andy Burrows, who is the illustrious drummer with the 00s indie band Razorlight (now reunited with their classic lineup), for an interview with BBC Radio 2’s Edith Bowman, who was sitting in on Jo Whiley's show, obviously to talk about the new Razorlight album Planet Nowhere since that’s just come out. Edith also knows Andy personally, she’s been interviewing the band for the last 20 years, and also is married to Tom Smith from Editors, who is also in a creative project with Andy called Smith and Burrows. So Edith is super casual and comfortable with them, and when Andy showed up for the interview, she mentioned that the original idea was for him to bring his friend and fellow drummer Dom Howard from Muse along: they had been hanging out and watching a film the previous night, and at the end of it, Dom had just gone, you know what, naaaahh; and decided not to come in with Andy the next morning.
I meant to clip that and post it here, since the interview is only available for the next 1 week or so, but my laptop has decided to die so tragically to the point where I could not edit and submit my radio show yesterday (annoying and tragic the way things can suddenly decide they don't want to work when 12 hours ago they were fine). So I am stuck at the moment. Anyway, you lose nothing because there was indeed no Dom present. Or we may have had a question or two about what's going on with Muse right now.
Here's the listen back link for the next few days. If someone else wants to clip the audio the interview starts around the 40-minute mark and ends at 1h10m-ish, they mention Dom around the 44th minute and at the end, where they cracked open a soundtrack vinyl on air and dedicated that to Dom, so there.
15 notes · View notes