


Sharing examples of using HTML5 Canvas to create keyboard and mouse animations_html5 tutorial skills
Keyboard controls the movement of the ball
As we all know, the animation we see is actually a series of rapid switching of static images, which gives the naked eye the visual effect of "moving images" due to visual afterimages. After understanding this, drawing animation effects on canvas becomes relatively simple. We only need to clear a certain static graphic first, and then redraw it at another location. Repeat this to make the static graphic move according to a certain trajectory to produce an animation effect.
Next, we draw a solid ball on the canvas, and then use the arrow keys on the keyboard to control the movement of the ball to produce dynamic effects. The sample code is as follows:
- "UTF-8">
-
html5 canvas drawing movable ball entry example - "moveBall(event)">
- Your browser does not support the canvas tag.
Please use a browser that supports HTML5 to open the following web page to see the actual effect (use the arrow keys to move):
Use canvas to draw a movable ball.
Clown Smiley Face
You only need to understand a few APIs, and then use the required animation parameters to create this interesting web animation that can respond to your movements.
The first step is to draw the facial features
This clown has no ears and eyebrows, so only three organs are left, but we have to draw its two eyes separately, so there are four parts in total. Let’s take a look at the code first.
Code for drawing left eye
- var leftEye = new Kinetic.Line({
- x: 150,
- points: [0, 200, 50, 190, 100, 200, 50, 210],
- tension: 0.5,
- closed: true,
- stroke: 'white',
- strokeWidth: 10
- });
Code for drawing the right eye
- var rightEye = new Kinetic.Line({
- x: sw - 250,
- points: [0, 200, 50, 190, 100, 200, 50, 210],
- tension: 0.5,
- closed: true,
- stroke: 'white',
- strokeWidth: 10
- });
Code for drawing nose
- var nose = new Kinetic.Line({
- points: [240, 280, sw/2, 300, sw-240,280],
- tension: 0.5,
- closed: true,
- stroke: 'white',
- strokeWidth: 10
- });
Code for drawing mouth
- var mouth = new Kinetic.Line({
- points: [150, 340, sw/2, 380, sw - 150, 340, sw/2, sh],
- tension: 0.5,
- closed: true,
- stroke: 'red',
- strokeWidth: 10
- });
Would you feel disappointed? It turns out that it’s just a few lines of code. Yes, it’s that simple. I believe you are starting to feel confident that you can become a web game animation programmer!
Briefly explain the above code. Kinetic is the js toolkit we use. At the head of the page, we need to reference it like this:
- var mouth = new Kinetic.Line({
- points: [150, 340, sw/2, 380, sw - 150, 340, sw/2, sh],
- tension: 0.5,
- closed: true,
- stroke: 'red',
- strokeWidth: 10
- });
The others are several key points, line elasticity, color, width, etc. These are easy to understand.
The second step is to make the picture move
The reason why this animation is attractive is that it can respond to your mouse movements and interact with the user. This is the most critical aspect of a successful animation. If you observe carefully, the changes in the clown's facial features are just changes in shape. The eyes become larger, the mouth becomes larger, and the nose becomes larger. But the special thing is that this change is not an instant change, but a transitional one. There are some algorithms in it. This is the most worrying part. Fortunately, these algorithm technologies are very mature and do not require us to think about them. These animation engine libraries have encapsulated these technologies into very simple and convenient interfaces. Let’s take a look at how to get things moving.
Left eye animation
- var leftEyeTween = new Kinetic.Tween({
- node: leftEye,
- duration: 1,
- easing: Kinetic.Easings.ElasticEaseOut,
- y: -100,
- points: [0, 200, 50, 150, 100, 200, 50, 200]
- });
Right eye animation
- var rightEyeTween = new Kinetic.Tween({
- node: rightEye,
- duration: 1,
- easing: Kinetic.Easings.ElasticEaseOut,
- y: -100,
- points: [0, 200, 50, 150, 100, 200, 50, 200]
- });
Animation of nose
- var noseTween = new Kinetic.Tween({
- node: nose,
- duration: 1,
- easing: Kinetic.Easings.ElasticEaseOut,
- y: -100,
- points: [220, 280, sw/2, 200, sw-220,280]
- });
Mouth animation
- var mouthTween = new Kinetic.Tween({
- node: mouth,
- duration: 1,
- easing: Kinetic.Easings.ElasticEaseOut,
- points: [100, 250, sw/2, 250, sw - 100, 250, sw/2, sh-20]
- });
The code is very simple and the variable names are self-explanatory. It should not be difficult for programmers with a little experience to understand these codes. Basically every piece of code lets you provide some points to specify the decay mode and duration of the animation action.
Complete animation code
-
"container">

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
