• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Game development
  1. MDN
  2. Game development
  3. Techniques for game development
  4. Crisp pixel art look with image-rendering

Crisp pixel art look with image-rendering

In This Article
  1. The concept
  2. A CSS-based solution
  3. An example

This article discusses a useful technique for giving your canvas/WebGL games a crisp pixel art look, even on high definition monitors.

The concept

Retro pixel art aesthetics are getting popular, especially in indie games or game jam entries. But since today's screens render content at high resolutions, there is a problem with making sure the pixel art does not look blurry. Developers have been manually scaling up graphics so they are shown with blocks that represent pixels. Two downsides to this method are larger file sizes and compression artifacts.

original size 4x size 4x size (scaled with an image editor)
none vendor's algorithm nearest-neighbor algorithm

A CSS-based solution

The good news is that you can use CSS to automatically do the up-scaling, which not only solves the blur problem, but also allows you to use the images in their original, smaller size, thus saving download time. Also, some game techniques require algorithms that analyse images, which also benefit from working with smaller images.

The CSS property to achieve this scaling is image-rendering. It is still experimental, but there is partial support in most browsers. The steps to achieve this effect are:

  • Create a <canvas> element and set its width and height attributes to the original, smaller resolution.
  • Set its CSS width and height properties to be 2x or 4x the value of the HTML width and height. If the canvas was created with a 128 pixel width, for example, we would set the CSS width to 512px if we wanted a 4x scale.
  • Set the <canvas> element's image-rendering CSS property to some value that does not make the image blurry. Either crisp-edges or pixelated will work. Check out the image-rendering article for more information on the differences between these values, and which prefixes to use depending on the browser.

An example

Let's have a look at an example. The original image we want to upscale looks like this:

Here's some HTML to create a simple canvas:

<canvas id="game" width="128" height="128"></canvas>

CSS to size the canvas and render a crisp image:

canvas {  
  width: 512px;
  height: 512px;
  image-rendering: -moz-crisp-edges;
  image-rendering: -webkit-crisp-edges;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

And some JavaScript to set up the canvas and load the image:

// get canvas context
var ctx = document.getElementById('game').getContext('2d');
// load image
var image = new Image();
image.onload = function () {
    // draw the image into the canvas
    ctx.drawImage(image, 0, 0);
}
image.src = 'https://mdn.mozillademos.org/files/12640/cat.png';

This code used together produces the following result:

Note: You can check out the original code on Github (and a live example.)

Document Tags and Contributors

Tags: 
  • 2D
  • 3D
  • Canvas
  • CSS
  • Games
  • image-rendering
  • JavaScript
  • pixel
  • WebGL
 Contributors to this page: 247flashgames, chrisdavidmills, ladybenko
 Last updated by: 247flashgames, Jun 18, 2016, 11:13:24 AM
  1. Introduction
    1. Introduction to game development for the Web
    2. Anatomy of a video game
    3. Examples
  2. APIs for game development
    1. Canvas
    2. CSS
    3. Full Screen
    4. Gamepad
    5. IndexedDB
    6. JavaScript
    7. Pointer Lock
    8. SVG
    9. Typed Arrays
    10. Web Audio
    11. WebGL
    12. WebRTC
    13. Web Sockets
    14. WebVR
    15. Web Workers
    16. XmlHttpRequest
  3. Techniques
    1. Using async scripts for asm.js
    2. Optimizing startup performance
    3. Using WebRTC peer-to-peer data channels
    4. Efficient animation for web games
    5. 3D games on the Web
      1. 3D games on the Web overview
      2. Explaining basic 3D theory
      3. Building up a basic demo with A-Frame
      4. Building up a basic demo with Babylon.js
      5. Building up a basic demo with PlayCanvas
      6. Building up a basic demo with Three.js
      7. Building up a basic demo with Whitestorm.js
      8. WebVR

    6. Audio for Web Games
    7. 2D collision detection
    8. 3D collision detection
      1. 3D collision detection overview
      2. Bounding volume collision detection with THREE.js
    9. Tiles and tilemaps
      1. Tiles and tilemaps overview
      2. Static maps
      3. Scrolling maps
    10. Implementing game control mechanisms
      1. Game control mechanisms overview
      2. Mobile touch controls
      3. Desktop mouse and keyboard controls
      4. Desktop gamepad controls
      5. Unconventional controls
  4. Tutorials
    1. 2D breakout game using pure JavaScript
    2. 2D breakout game using Phaser
    3. 2D maze game with device orientation
    4. 2D platform game using Phaser
  5. Publishing games
    1. Publishing games overview
    2. Game distribution
    3. Game promotion
    4. Game monetization