Custom 2D Drawing in Android and Html5 Canvas


Recently, I attended an online MOOC course about html5 canvas drawing. I was surprised how simple of its drawing API, which has the power to create awesome UI effect.
I summarize the 2D drawing API as two fields: regular shape drawing API and transformation API. As for 2D shape drawing in html5 Canvas there are following limited properties and functions: beginPath, endPath, fillStyle, strokeStyle, stroke, fill, fillRect, moveTo, lineTo, arc, font, fillText, strokeText, createLinearGradient, createRadialGradient, addColorStop, drawImage, bezierCurveTo, quadraticCurveTo.
To sum up, the above 2D drawing API can do following jobs.

  1. moving a point;
  2. drawing a line;
  3. drawing an arc or circle;
  4. drawing a bezier curve;
  5. make a path;
  6. stroke along the path;
  7. fill the closed path with image, colors, gradient effect or patterns;

Then, we count the transformation APIs: save, restore, translate, rotate, scale, transform. No need any more illustrations, is it? The transform one maybe the hardest one, but still can be acceptable, which equals to the transformation matrix in another platform, it can do all the jobs of transformation, translate, rotate and scale.
There is so much similarity between different platforms when came to the basic 2D drawing API and transformation functions. Then after I learned the sample code of this, I porting them to Android OS. Here are the screen shots, the above one is the native Android implementation, the bottom one is the Cordova implementation.




And this is not the ending, I also wrote an electron desktop app, which just wrap the source code to electron’s container.





Here are the source codes, for the Android native and Cordova container implementations check my AndroidClock project. For the electron clock implementation check my electron-clock project. I like the electron, who not, huh.
At the last, thanks liuyubobobo and his excellent courses again.

Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp