How to make a Prey on Animation

Lab | October 22, 2016 | 3 Comments



First, you need to know the total size and half size of the drawing canvas. _sw, _sh is the stage size variable, and _cx, _cy is the half stage size variable. >> is a bit shift operator. X >> 1 is the same as Math.floor (x / 2) but faster.


I’m using quadraticCurveTo to draw the mouth animation. You can change the Y points (start point, end point, control point) while the animation is in progress, and use points to create animations that can close or open your mouth. I added an _guide object variable and added a tween to easily control the animation. Therefore, the _guide is changed from 0 to 1, 0 is the open mouse state, and 1 is the closed mouse state.
Each Y point is changed from the _open position to the _close position, so the getCurrent function is used to obtain the correct value. X = getCurrent (A, 10, 200); That is, when A changes from 0 to 1, X changes from 10 to 200. In this case, you can set the twin directly to the _points variable.


Draw a triangle for the tooth shape, and add grey color shadow area (1/4 width) to improve the detail.


To draw a tooth, you need to know the position of the tooth on the curve. The getPointOnQuad function can be used to get the position, and the last p parameter can be set from 0 to 1. 0 is the first point, 1 is the last point, so 0.5 is the midpoint.
The important thing of the tooth drawing is that the total number should be even. We will draw both the upper teeth and the lower teeth, and the the upper teeth mesh with the lower teeth.
The drawing starts with 1, not 0, and draws up and down alternately. Please refer to the below figure for better understanding.
When i is odd, draw the upper teeth with variables y1 and c1, and the lower teeth with variables y2 and c2 when i is even.
i / _teethTotal is used to get the point of the tooth on the curve.
Drawing two-dimensional objects on the canvas shows quite good performance on devices. You do not even need to use WebGL. This animation works well on both desktop and mobile devices. You can download all the source code from all my my git.


  • TweenLite by GreenSock – for Open and Close animation
  • js-signals by Miller Medeiros – for the controller events dispatch


Copyright (c) 2016 Jongmin Kim
Licensed under the MIT license.


  • +MON


Bhakti  October 26, 2016 at 4:45 AM

Great writing, can’t wait to read your new book. Hope it will be available in english :)

luoyu  October 26, 2016 at 5:10 PM

always wondering how could u make such amazing animation, this artical helps a lot, thanks!

Anzi  November 1, 2016 at 7:33 AM

This article is really helpful to me. Thank you so much!

Leave a Reply