What steps do I need to take in order to create a 3D viewer utilizing a Spritesheet

While browsing online, I stumbled upon a Wiki dedicated to the game Team Fortress 2. Initially, it seemed like a typical gaming Wiki, but there was one particular feature that captured my attention and left me completely perplexed.

This Wiki showcased all the in-game items such as weapons and cosmetics. However, what stood out to me was the inclusion of a 3D viewer next to each item description. What made it even more intriguing was that it wasn't just any ordinary viewer - at first glance, I thought it was ThreeJS, but after inspecting the developer tools, I realized it was created using a Spritesheet with various gun directions. The technical prowess behind this implementation still baffles me.

If anyone out there can shed some light on how this was achieved, I would greatly appreciate it.

For those interested, here are the relevant links:

Link to the 3D viewer:

Spritesheet used for creating the viewer:

I'm simply fascinated by this technology and eager to learn more about its development process.

Answer №1

To inspect the 3D image, right click on it and select "Inspect Element" (the wording might vary based on your browser). Locate the HTML element responsible for rendering the image, typically a <div> as shown below. Observe the changes as you move the image around.

<div class="viewer-3d-frame" data-id="0" style="height: 243px; background: url(&quot;https://wiki.teamfortress.com/w/images/a/af/Pistol_3D.jpg?20180817142106&quot;) -9403px 0px no-repeat; left: 26px; top: 19px; width: 221px;"></div>

Pay close attention to the style attribute:

height: 243px;
background: url("https://wiki.teamfortress.com/w/images/a/af/Pistol_3D.jpg?20180817142106") -9403px 0px no-repeat;
left: 26px;
top: 19px;
width: 221px;

Note: The image URL actually leads to a sprite sheet containing multiple 2D images, displaying only a portion of it.

When moving the image, observe how the background, left, and width properties change.

  • The background property specifies which image to display and where. By setting a negative x-coordinate, the image shifts off-screen to the left.
  • The left property determines the starting point for drawing the image within its container, shifting it horizontally. Setting left: 0 places the image at the far left.
  • The width property controls how much of the background image is visible. Adjusting this value alters the displayed section.

You can experiment with these values using the inspection tool to see real-time adjustments. Check out the diagram below for a visual representation of these concepts.

https://i.sstatic.net/fl6Zj.png

Behind the scenes, there's likely a lookup mechanism storing coordinates for each sprite's position and size within the sprite sheet, dynamically updating the MouseMove event tracking user input to modify these values in the div.

Here's a hypothetical code snippet:

const spriteset = [
  {
    "x": -9403,
    "left": 26,
    "width": 221
  },
  {
    ...
  },
  ...
];

let currentSpriteIndex = 0;

const div3dViewer = document.querySelector("div.viewer-3d-frame");

div3dViewer.addEventListener('mousemove', e => {
    if (e.buttons === 1) {// Left button is down
        if (e.movementX < 0) {
            // mouse is dragged left
        } else if (e.movementX > 0) {
            // mouse is dragged right
        } else if (e.movementY < 0) {
            // mouse is dragged up
        } else if (e.movementY > 0) {
            // mouse is dragged down
        }
    }
})

Answer №2

An ancient method frequently utilized in various sprite-centric games, numerous tutorials are accessible on YouTube to guide you through the process:

https://www.youtube.com/results?search_query=Animating+a+sprite+sheet+with+javascript
Alternatively, explore the topic of Animating a sprite sheet with javascript for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

issue arising where the table's border is not displaying

I'm confused about why the border of the first tbody tr's td is not visible. https://i.stack.imgur.com/Fwlv7.png I can't see any reason why the border is not showing up. Here's what I've figured out: If the natural height of th ...

What is the best way to create a shaking image animation using React Native?

I am trying to create a shaking image animation in React Native when a TouchableOpacity is pressed. So far, I have implemented an animated image with the following code: const backgroundImage = require('./components/images/baby-sleep.jpg') cla ...

Changes in menu layout in response to window resizing

The menu needs to be centered on the website and adjust to the browser window resizing. Currently, it's positioned in the center and the animation is working fine. However, when I attempt to make the menu responsive so that it stays centered when resi ...

Show the result of file upload, whether it was successful or not, using ReactJS

I need to implement an Ajax call for uploading a file to the server. I want to display success and failure messages based on the outcome of the file upload process. $.ajax({ url: "https:my_url", data: {my data: my_data}, method : "POST", success: ...

Performing multiplication and calculating the final sum of an array object in Node JS

I am looking to calculate the total based on an array of objects sum of each row = sum of costs * quantity total = sum of (sum of each row) Below is the object array that I am working with const newArray = [ { 'LOA Qty': '2000', ' ...

The non-null assertion operator seems to be failing to function properly

Error during runtime: Issue with reading property 'id' of an undefined element if (!this.havingMultipleProjects) {//checking for only one project or none at all if (this.authenticationProvider.member.projects != null) this.projectProvider ...

javascript issue with setting the id attribute on a select option

I can't seem to set the id attribute in my select element using JavaScript. When I inspect it, the id attribute isn't showing up... Here's the first method using JS: var selectorElem = document.getElementById('selector') var ...

Having trouble with jQuery scrollTop not working after loading content with ajax?

When the html content is loaded via ajax, I need to scroll to a specific element. This element has the attribute data-event-id="". However, there are instances when the variable $('.timelineToolPanel[data-event-id="'+id+'"]').offset().t ...

Remove the 5th element from the given array, starting with the element at index 2

My array consists of 3 groups, with each group containing 5 elements. var tempArray1 = ['prodn', 'PP1', 'UK1', 'Exp', 'India2', 'prodn', 'PP2', 'france1', 'Imp', &ap ...

Is there a way to produce random colors using this SCSS function?

The current function is returning an output in the color #ff0063, but my goal is to create a variety of colorful pixel dots on the screen. Could someone provide an explanation of what this code is doing? @function multiple-box-shadow ($n) { $value: &apos ...

File upload failed with the Easy Upload Adapter in ckeditor

I am experiencing an issue when trying to upload an image. Despite researching the error, I have not been able to find a solution that works. My code is written in Express.js with ejs and specifically relates to the addPost.ejs file. <!DOCTYPE html& ...

Error encountered with Passport js Local Strategy: The LocalStrategy necessitates a verify callback function

Encountering Passport JS Local Strategy Error with Node & Express I have successfully implemented OAuth 2 for Google authentication using Passport in my project. However, I am facing an error with the Local Strategy of Passport and I can't seem to fi ...

Guide to securely authenticate on Google Drive API asynchronously with the help of AngularJS and HTML

Is it possible for someone to help me figure out how to authenticate asynchronously with the Google Drive API using AngularJS and HTML? I seem to be stuck on the gapi.auth.authorize call because the callback function is not being called. Below is a snippet ...

Retrieve PDF from Controller using jQuery AJAX request

While searching for solutions on how to create a PDF using a controller in EvoPDF, I noticed that none of the examples addressed the scenario where the controller is called via jQuery AJAX. In my application, I have a simple jQuery function that sends dat ...

Is there a way to automatically scroll to a sidebar item when clicking on a marker?

Is it possible to make the sidebar scroll to the item clicked on the marker? I saw this functionality on a website like this one. Any ideas on how to achieve this would be appreciated. Thanks! This div contains map id & side_bar id. <div style="wi ...

Arrange the divs in numerical order based on the number of downloads

I wrote a basic code to organize parent divs based on their titles. However, when I attempted to do the same for download counts, nothing happened - no errors or actions from the code. There are two functions in total, both of which are triggered by butto ...

Using JavaScript to display a hidden element when clicked within an overflow-auto container

I have a collection of items grouped within a container with overflow-auto applied. When clicked, I want to scroll/place an item all the way to the right if possible. This will trigger the other items to auto-scroll to the left, positioning the clicked it ...

Middleware that handles form post requests quickly became overwhelmed and resulted in a RangeError

Currently, I am working with a form using ejs templates to collect data. When accessing the "/" route, my application renders the "main" view and utilizes a middleware to handle the form data. However, I encountered an error stating "RangeError: Maximum ca ...

Which is better for handling events - jQuery delegation or function method?

Which approach is quicker and has broader browser support? 1. Utilizing a JavaScript function such as: function updateText(newtext) { $('div#id').text(newtext); } and incorporating it into an element's onclick event: <button onc ...

What is the jquery alternative to the PHP 'if IP equals' condition?

I am curious to know if the following PHP 'if' conditions can be achieved in jquery or JavaScript, with a preference for jquery. if ($_SERVER['REMOTE_ADDR'] != '123.99.55.616') { // public doingness } if ($ ...