JS Torch: A Guide to using JavaScript for Development

Posted by


In this tutorial, we will cover how to create a simple JS Torch using HTML, CSS, and JavaScript. A torch is a device that emits light, typically in the form of a flame or a beam of light. In our case, we will create a small torch graphic that emits light when the user interacts with it.

Step 1: Set up your HTML file

First, create a new HTML file and name it “index.html”. Inside this file, we will set up the basic structure of our webpage. Add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS Torch Tutorial</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="torch" id="torch"></div>
<script src="script.js"></script>
</body>
</html>

In this code, we have included links to an external CSS file called “styles.css” and an external JavaScript file called “script.js”. We will create these files in the next steps.

Step 2: Create your CSS file

Next, create a new CSS file and name it “styles.css”. In this file, we will style our torch graphic. Add the following code:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}

.torch {
  width: 100px;
  height: 200px;
  background: linear-gradient(to bottom, #f9f9f9, #d3d3d3);
  border-radius: 50px 50px 0 0;
  position: relative;
}

.light {
  width: 80px;
  height: 100px;
  background: radial-gradient(circle, #fff 20%, transparent);
  position: absolute;
  top: 20px;
  left: 10px;
  display: none;
}

In this code, we have styled the torch element with a grey gradient background and rounded edges. We have also styled the light element that will emit light when the torch is activated.

Step 3: Create your JavaScript file

Next, create a new JavaScript file and name it “script.js”. This file will contain the logic for activating the torch and emitting light. Add the following code:

const torch = document.getElementById('torch');
const light = document.createElement('div');
light.classList.add('light');
torch.appendChild(light);

torch.addEventListener('click', () => {
  light.style.display = 'block';
});

torch.addEventListener('mouseleave', () => {
  light.style.display = 'none';
});

In this code, we have selected the torch element using its ID and created a new light element. We have added this light element as a child of the torch element. We have also added event listeners for the “click” and “mouseleave” events on the torch element. When the torch is clicked, the light element will be displayed, and when the mouse leaves the torch element, the light will be hidden.

Step 4: Test your torch

Now that we have set up our HTML, CSS, and JavaScript files, open your “index.html” file in a web browser. You should see a torch graphic on the screen. When you click on the torch, the light will be emitted, and when you move your mouse away from the torch, the light will disappear.

Congratulations! You have successfully created a simple JS Torch using HTML, CSS, and JavaScript. You can further customize the torch and add more features to make it more interactive and engaging. Have fun experimenting with different designs and functionalities!

0 0 votes
Article Rating
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ulrich-tonmoy
1 month ago

JS be like Finally my time has come to rule the AI Devs

@HUEHUEUHEPony
1 month ago

shouldn't this be called TS-Torch

@HUEHUEUHEPony
1 month ago

browser can't access gpu like that yet webgpu remaning…

@eduardoleao5589
1 month ago

Just one last thing:
As you mentioned, the naming was confusing. I changed everything to JS-PyTorch, to match the npm registry!

@yugalkhanal6967
1 month ago

You missed the biggest problem, it's javascript.

@jointtask4047
1 month ago

Anything that can be written in JavaScript, will eventually be written in JavaScript.

@eduardoleao5589
1 month ago

If you want to cover the library again in the future, I’d be thrilled to help.
Feel free if you want to reach out!

@eduardoleao5589
1 month ago

Loved the video!
I’m the developer of the library, and really really appreciate the feedback!
I added GPU support earlier this month, and am working on a new and better documentation!
If you have any questions about the features I would absolutely love to talk about it!

@kamisatoayato9142
1 month ago

Wow