Welcome, Guest

Author Topic: Create awesome crosshair with Coherent UI!  (Read 1414 times)

Billy

  • Administrator
  • *****
  • Posts: 41
    • View Profile
Create awesome crosshair with Coherent UI!
« on: November 06, 2014, 01:00:42 AM »
Hey guys, in this short tutorial we will show you how to create crosshair for you game ui, using HTML and CSS. The crosshair position will be always in the center of your game view.

Adding a simple crosshair to the page

In most of the cases we need to put crosshair in the center of the screen without any other elements around it. In order to do that, you should first choose a picture of crosshair. After that, we will show you the HTML markup of the page. We chose the following picture for the demo:


Credits

Code: [Select]
<img src="img/crosshair_small.png" class="crosshair" />

In this case, we need to put only one img tag with class crosshair in our body tag.

C++

Code: [Select]
.crosshair {
  position: absolute;
  top: 50%;
  left: 50%;
  display: block;
  transform: translateY(-50%) translateX(-50%);
}

In order to center the crosshair you have to set the top and left position to 50%. The property transform should be set to translateY(-50%) and translateX(-50%). The CSS property transform is usually used for rotating and scaling elements, but through the translateY and translateX function we can vertically and horizontal align elements.

Adding a crosshair with dynamic content

For the purposes of the example, let us accept that we would like to put some content, attached right after the crosshair. In the particular example we will attach ammo type and we will put the text for the ammo counter and progress bar.

Code: [Select]
<div class="crosshair-wrapper">
  <img src="img/crosshair_small.png" class="crosshair" />
  <div class="progress-outer">
    <div class="progress-inner">
      <div class="progress"></div>
    </div>
    <span class="progress-value">36</span>
  </div>
</div>

Again after body tag we have to put the following HTML snippet.

Code: [Select]
body {
  background: #DCD3CD;
}
 
.crosshair-wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
 
  height: 200px; // important for the centering of the crosshair image
  transform: translateY(-50%) translateX(-50%);
}
 
.crosshair {
  display: block;
}
 
.progress-outer {
  position: relative;
  margin: 10px auto;
  padding: 3px;
  height: 14px;
  width: 126px;
  background: #000;
  -webkit-border-radius: 5px;
}
 
.progress-inner {
  width: 100px;
  background: #333;
  -webkit-border-radius: 2px;
}
 
.progress {
  width: 36px;
  height: 14px;
  background: #EE2524 url(../img/red-bullet.png);
}
 
.progress-value {
  position: absolute;
  top: 3px;
  right: 5px;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  font-family: monospace;
}

In this case we have to set the position and transform properties on our parent crosshair-wrapper element. Note that crosshair-wrapper must have a height property, which must be the same height as of our crosshair image. In such a way you will fix the vertical centering of crosshair icon relative to its height but, not to the height of the crosshair-wrapper. The final result will be:



Check out the live demo here

Download the source code here
 

Tags: