You may refer to the dagger.js version here: https://codepen.io/dagger8224/pen/XWYBKjJ, which should be very easy to understand.