While surfing the internet, I came across a Wiki for a game called Team Fortress 2. At first, I thought it was a typical Wiki of a game, but there was something that caught my attention and I am truly puzzled as to how it can be done.
In short, the Wiki shows all the items from the game like, weapons, cosmetics, etc. But when I went to see an item, next to the text explaining about the item had a 3D viewer, only it was not a normal viewer, At first I thought it was ThreeJS but when I looked at the devtools it was something completely different, the programmer of the site used a Spritesheet with all the gun directions. I am still unsure how it was accomplished.
Can anyone there help me know how I can do this?
Here are the links:
Link to 3D viewer: https://wiki.teamfortress.com/wiki/Pistol
the Spritesheet used to make the viewer: https://wiki.teamfortress.com/w/images/a/af/Pistol_3D.jpg?20180817142106
Nothing at all, i’m just truly curious on how it can be made.
2
Answers
Right click on the image and click "Inspect Element" (the wording may be slightly different depending on your browser.) Use the tool to find the HTML element that renders the 3D image, which is this
<div>
element shown below. Pay attention to what happens when you drag the image around.Specifically, pay attention to what’s in the
style
attribute:Aside: As you discovered in the original post, by navigating to the image URL, it’s actually a sprite sheet of several 2D images and you are only seeing part of the full image.
When you drag the image around, the
background
,left
, andwidth
properties changed.left: 0
moves the image all the way to the left in the div.A width of 0 will draw nothing, and a width of 9999px will draw the entire sprite set, even though it will go out of bounds from the parent div.
You can manually change these parameter values in the inspection tool to see the changes in real time. I created the image below to show you how all of these properties come together.
As for how it all works, there is likely an "internal dictionary" of sorts that contains lookup values for the x-coordinate, left, and width properties of each sprite in the sprite set, that propagates to the div style. I also expect that the
MouseMove
event is being used to trigger sprite changes on user mouse input and propagate these lookup values to the div.Here is some pseudocode:
This is a very old technique used in many sprite based games. There are plenty of tutorials for this on YouTube:
https://www.youtube.com/results?search_query=Animating+a+sprite+sheet+with+javascript
Or search for Animating a sprite sheet with javascript in general