0.14.4 • Published 2 months ago

@maximeij/css-brickout v0.14.4

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
2 months ago

css-brickout npm (scoped) image

A timeless favorite...

Demo

CSS Brickout (aka CSS Breakout) exposes a responsive, customizable, themable, extensible implementation of the beloved classic to the web. This 0 dependency library can be used in any ecosystem and framework.

Quick start

No time to chat, here's the copy pastable stuff:

npm i @maximeij/css-brickout
import '@maximeij/css-brickout/css';
import {Game} from '@maximeij/css-brickout';

See demo.ts for a quick example of how to invoke it. Either have a div with id game rendered, or pass a custom parentId to the input object to target anything else.

...with a twist!

Unlike other web based games, this little number renders all its graphics with good old CSS. No canvas were hurt (or used) during the production. While this was initially more of a personal challenge for the love of CSS-art and other related endeavors, it has proven to bring some advantages.

Easy themes 🎨

With CSS Brickout, no need to wonder how hard it'll be to customize the feel and look of your game: it's as simple as tried and true --css-variables. Anything not covered by that can be easily styled with static class names exposed by the lib:

/* Example custom game container */
#custom-game {
  --ball-bg: hsl(98, 18, 89%);
  --paddle-bg: hsl(0, 0, 50%);
}

#custom-game .ball {
  border-style: double;
}

Easy extension 🔨

The game emits custom events as they occur, allowing you to set up handlers that will customize the gameplay endlessly! These events are currently available and include the GameObject emitting it unless stated otherwise:

Game events:

  • 'gamestarted', 'gamepaused', 'gameresumed'
  • 'gamewon', 'gamelost'

Ball events:

  • 'ballcollision' (includes both the Ball and the GameObject it collided with)
  • 'balldestroyed'

Brick events:

  • 'brickdestroyed'

!IMPORTANT By default, the Game will set up listenners to decrease life and increase score on balldestroyed and brickdestroyed respectively. These can be omitted with {options: { skipDefaultRules: true}}

Example use:

// Basic
element.addEventListener('ballcollision', ({detail}) => console.log(detail.ball, 'bonk', detail.object));

/**
 * Advanced (as seen on demo.ts)
 * Basic particle effect for ball destruction using moving particles
 */
element.addEventListener('balldestroyed', ({detail: ball}) =>
  ball.emitParticles(10, ['ball--destroyed-particle'], 300, true).forEach(particle => {
    particle.style.left = `${50 - Math.round(100 * Math.random())}px`;
    particle.style.top = `${0 - Math.round(50 * Math.random())}px`;
    particle.style.opacity = '0';
  }),
);
/** Previous example class */
.particle.ball--destroyed-particle {
  border-radius: 50%;
  background: var(--ball-bg);
  transition: all 300ms ease-out;
  top: 0px;
  left: 0px;
  opacity: 1;
}

Still though... CSS for 60 FPS?

Yep! There's really only thing moving at 60FPS (the balls), and positioning them and other objects in a way consistent with the underlying model is pretty straightforward:

this.element.style.transform = `translateX(calc(${absX}px - 50%)) translateY(calc(${absY}px - 50%))`;

The movement of the ball is smooth but the game does consume more power than it would if using optimized graphics like canvas and svg. I plan to decouple the rendering logic so we can create a more efficient rendering method to compare just exactly how big is the difference with CSS is. In the meantime this is a fun experiment.

In fact, even with non-trivial collision detection (see geometry.ts), we can run it upwards of thousands of times per frame. The performance bottleneck is often first the repainting every frame, which will slow down FPS as the number of elements and the complexity of their styles (transparencies, blurs) increases.

!NOTE The 60 FPS limit is often a result of requestAnimationFrame which, when uncapped by the device, the game can run up to triple the FPS.

Recent changes

Coming soon

  • Bonus Drops + Effect Duration
  • Object properties as CSS variables
0.14.4

2 months ago

0.14.2

2 months ago

0.14.3

2 months ago

0.14.0

2 months ago

0.14.1

2 months ago

0.13.9

2 months ago

0.13.8

3 months ago

0.13.6

4 months ago

0.13.7

4 months ago

0.13.5

4 months ago

0.13.3

4 months ago

0.13.4

4 months ago

0.13.1

5 months ago

0.13.2

5 months ago

0.13.0

5 months ago

0.12.2

5 months ago

0.12.3

5 months ago

0.12.0

5 months ago

0.12.1

5 months ago

0.11.0

7 months ago

0.10.1

8 months ago

0.11.1

7 months ago

0.10.2

8 months ago

0.11.2

7 months ago

0.11.3

7 months ago

0.11.4

7 months ago

0.11.5

7 months ago

0.11.6

6 months ago

0.11.7

6 months ago

0.10.0

8 months ago

0.9.0

10 months ago

0.7.2

10 months ago

0.7.1

10 months ago

0.9.1

9 months ago

0.7.0

10 months ago

0.5.2

10 months ago

0.6.0

10 months ago

0.4.5

12 months ago

0.4.4

1 year ago

0.4.6

12 months ago

0.5.0

12 months ago

0.4.3

1 year ago

0.5.1

11 months ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago