• Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    JavaScript
  • Created about 10 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An old-school arcade-style 3D shoot-em-up rendered entirely with CSS 3D transforms

CSS Space Shooter

Screen shot

Play The Game

This is an experiment I made to investigate the capabilities of CSS 3D transforms. Having played about with this technology a little (see this or this, and having seen some very impressive demos (CSS FPS, CSS X-Wing, I wanted to explore the idea of making a simple 3D game with only DOM and CSS.

Everything in CSS? Cool!

CSS transforms allow us to position and rotate DOM elements in 3D space. The big advantage of this over, say, using canvas or webGL is that we do not need to worry about any of the complex maths involved in projecting a 3D object onto the screen. The browser's rendering engine (with the help of your GPU) will take care of all that. You just need to specify the x, y, z coordinates as well as the rotation along any axis. This makes it really simple to map your JavaScript objects onto the screen, by just keeping track of these simple coordinate and rotation values.

Having previously played with pseudo-3D in canvas, I have some idea of the massive amount of calculation involved in plotting all the lines and vertices of each object manually. In this regard, the simple, declarative nature of CSS allows some really powerful 3D effects with astonishingly little code.

...or not so cool.

That convenience comes at a cost, however. For one, in CSS it is really really hard to create any shape other than a rectangle or an ellipse. Triangles, for example, are only possible through dirty hacks with the border property.

Secondly, performance. Despite hardware acceleration for these 3D transforms, I quickly ran into performance issues when scaling up the number of objects interacting on screen simultaneously. Certain CSS operations are also very expensive, such as transitioning box-shadow values or gradient backgrounds.

I'm sure my code can be optimized and this performance ceiling can be raised considerably. However, I wouldn't recommend using CSS and DOM for a serious 3D game.

Browser Compatibility

  • Right now this works properly in the latest version of Chrome.
  • In my tests with Firefox it is very jerky and then usually grinds to a complete halt after a minute or so.
  • Internet Explorer has a couple of fatal issues - it does not yet support a key CSS property - transform-style: preserve3d - which is essential to this method of building up 3D objects and 3D scenes which all share the same perspective. Additionally, IE does not currently support the Web Audio API, which I use for the sound effects and music. The game currently won't even load for this latter reason.
  • I've not tested in any other browsers, but feedback is welcome.

Credits

Inspiration and implementation details:

  • Keith Clark - seriously, check out his stuff. It's amazing. Used his advice on positioning the DOM elements in the center of the viewport and moving them only with transforms, which works well.
  • html5Rocks - Some really helpful tutorials here and here on how to use the Web Audio API.
  • Dive Into HTML5 article on the localStorage API, which I use to store high scores.

Sound effects

I got all my sounds effects from https://www.freesound.org.

Music

Ludwig van Beethoven - Symphony No.7 in A major op.92 - II, Allegretto

Developing

npm install
npm run watch // dev mode 
npm run compile // production build

License

MIT

More Repositories

1

angularUtils

A place where I will collect useful re-usable Angular components that I make
JavaScript
1,999
star
2

ngx-pagination

Pagination for Angular
TypeScript
1,213
star
3

soundcloud-visualizer

Audio visualization with web audio API, canvas & the SoundCloud API
HTML
449
star
4

chromata

A generative art tool.
JavaScript
376
star
5

angular-es6

An experiment in using ES6 features with AngularJS 1.x
JavaScript
370
star
6

angular-wordpress-seed

A bare-bones AngularJS blog app designed to work with the Wordpress JSON REST API.
JavaScript
310
star
7

horizonal

Turn your static HTML document into a programmable slide show
JavaScript
205
star
8

skqw

JavaScript Audio Visualizer
TypeScript
120
star
9

angular-social-demo

A demo of how to allow social media crawlers (Facebook, Twitter, Pinterest etc) to read your AngularJS app
HTML
110
star
10

angularUtils-pagination

Simple pagination for AngularJS
JavaScript
42
star
11

drawACatApp

A canvas line drawing experiment
JavaScript
29
star
12

heavenly-glory

A kung-fu movie starring you. Web RTC & Web Audio experiment.
JavaScript
23
star
13

css-experiments

Some experiments involving CSS
CSS
14
star
14

michaelbromley.co.uk

The source code for my personal website and blog
JavaScript
13
star
15

headlesscommerceplatforms

A feature comparison of various headless e-commerce solutions
TypeScript
10
star
16

shader-playground

Some snippets of code for fragment shaders
GLSL
9
star
17

cmpg

A super simple component generator for Angular 2
JavaScript
9
star
18

breezejs-odata4-adapter

An experimental adapter to allow BreezeJS to work with an OData v4 server.
JavaScript
7
star
19

skqw-library

Visualization library for SKQW
JavaScript
5
star
20

angularUtils-uiBreadcrumbs

Automatic breadcrumbs directive for use with angular-ui-router
JavaScript
3
star
21

vendure-plugin-example

Handlebars
3
star
22

canvas_experiments

Simple examples of HTML canvas
JavaScript
3
star
23

vendure-demo

TypeScript
2
star
24

angular-issue-18478

Reproduction of Angular issue 18478
TypeScript
1
star
25

pad

A new way to work with text.
TypeScript
1
star
26

angularUtils-disqus

A directive for including Disqus comments in an AngularJS app
JavaScript
1
star
27

gulp-es6-seed

A minimal seed to kick start an ES6 project
JavaScript
1
star