Conways Game of Life: Animation

Adding some animation to Conways Game of Life

April 24, 2016 - 6 minute read -
code kata

A while back I completed an excercise to implement Conways Game of Life using Javascript. I have recently found a play by play on Pluralsight where Lea Verou did the same thing. I was especially interested, because Lea also chose to represent her grid using checkboxes. Although Lea’s implementation is undoubtably better I was, overall, happy with my own attempt, but one thing she did looked really good to me. Lea added a simple animation when the checked property changed to represent “birth”, so I’ve decided to shamelessly borrow this idea and to extend it to include an animation for “death” too.

The main change to the css is to add the keyframes declarations for “birth” and “death” and to add them as animations to the checkboxes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@-webkit-keyframes birth {
    0% {
        box-shadow: 0 0 0 99px white inset;
    }
    50% {
        box-shadow: 0 0 0 99px green inset;
    }
}
@keyframes birth {
    0% {
        box-shadow: 0 0 0 99px white inset;
    }
    50% {
        box-shadow: 0 0 0 99px green inset;
    }
}
.conway-game input[type="checkbox"]:checked {
    box-shadow: 0 0 0 99px black inset;
    -webkit-animation: birth 500ms;
    animation: birth 500ms;
}
@-webkit-keyframes death {
    0% {
        box-shadow: 0 0 0 99px black inset;
    }
    50% {
        box-shadow: 0 0 0 99px red inset;
    }
}
@keyframes death {
    0% {
        box-shadow: 0 0 0 99px black inset;
    }
    50% {
        box-shadow: 0 0 0 99px red inset;
    }
}
.conway-game input[type="checkbox"] {
    box-shadow: 0 0 0 99px white inset;
    -webkit-animation: death 500ms;
    animation: death 500ms;
}

And here is the updated game…