These are interesting blogs to study.

bitquabit and source code. Look at revision history to make sense of it.