Home Web Front-end H5 Tutorial 三分钟HTML5画布(Canvas)动画教程

三分钟HTML5画布(Canvas)动画教程

May 28, 2018 pm 04:32 PM

推荐手册HTML5最新版参考手册

此文下面的教程中将使用的是另外一个叫做kinetic的Web动画工具包。

它们都是开源的。

把鼠标放到上面的小丑脸上,然后移开,就会有如下效果。
这里写图片描述

这里写图片描述

第一步,画五官

这个小丑没有耳朵和眉毛,所以只剩下三官,但它的两个眼睛我们要分别绘制,所以一共是四个部分。下面先看看代码。

绘制左眼的代码

1

2

3

4

5

6

7

8

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    

      });

Copy after login

绘制右眼的代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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  

      });

绘制鼻子的代码var nose = new Kinetic.Line({

        points: [240, 280, sw/2, 300, sw-240,280],

        tension: 0.5,

        closed: true,

        stroke: 'white',

        strokeWidth: 10

      });

Copy after login

绘制嘴巴的代码

1

2

3

4

5

6

7

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

      });

Copy after login

简单讲解一下上面的代码。Kinetic就是我们使用的js工具包。在页面的头部,我们需要这样引用它:

1

<script type="text/javascript" src="/js/kineticv5.0.1.min.js"></script>

Copy after login

其它几个分别是几个关键点,线条弹性,颜色,宽度等。这些都很容易理解。

第二步,让图动起来

这个动画之所以能吸引人,是因为它能响应你的鼠标动作,和用户有互动,这是一个成功的动画最关键的地方。如果你仔细观察,这个小丑五官的变化只是形状的变化,眼睛变大,嘴巴变大,鼻子变大,但特别的是这个变化不是瞬间变化,而是有过渡性的,这里面有一些算法,这就是最让人发愁的地方。幸运的是,这算法技术都非常的成熟,不需要我们来思考,在这些动画引擎库里都把这些技术封装成了非常简单方便的接口。下面我们来看看如何让动起来。

左眼的动画

1

2

3

4

5

6

var leftEyeTween = new Kinetic.Tween({

        node: leftEye,

        duration: 1,

        easing: Kinetic.Easings.ElasticEaseOut,        y: -100,

        points: [0, 200, 50, 150, 100, 200, 50, 200]

      });

Copy after login

右眼的动画

1

2

3

4

5

6

var rightEyeTween = new Kinetic.Tween({

        node: rightEye,

        duration: 1,

        easing: Kinetic.Easings.ElasticEaseOut,        y: -100,

        points: [0, 200, 50, 150, 100, 200, 50, 200]

      });

Copy after login

鼻子的动画

1

2

3

4

5

6

var noseTween = new Kinetic.Tween({

        node: nose,

        duration: 1,

        easing: Kinetic.Easings.ElasticEaseOut,        y: -100,

        points: [220, 280, sw/2, 200, sw-220,280]

      });

Copy after login

嘴巴的动画

1

2

3

4

5

6

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]

      });

Copy after login

这些代码非常的简单,而且变量名能自释其意。稍微有点经验的程序员想看懂这些代码应该不难。基本每段代码都是让你提供一些点,指定动画动作的衰退模式和持续时间。

完整的动画代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

<style>

  body {        margin: 0px;        padding: 0px;      }

  #container {        background-color: black;      }

</style>

 

 

<p id="container"></p>

<script src="/js/lib/kinetic-v5.0.1.min.js"></script>

<script defer="defer">

  var sw = 578;      var sh = 400;      var stage = new Kinetic.Stage({

    container: 'container',

    width: 578,

    height: 400

  });      var layer = new Kinetic.Layer({

    y: -30

  });      var leftEye = new Kinetic.Line({

    x: 150,

    points: [0, 200, 50, 190, 100, 200, 50, 210],

    tension: 0.5,

    closed: true,

    stroke: &#39;white&#39;,

    strokeWidth: 10    

  });      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  

  });      var nose = new Kinetic.Line({

    points: [240, 280, sw/2, 300, sw-240,280],

    tension: 0.5,

    closed: true,

    stroke: 'white',

    strokeWidth: 10

  });      var mouth = new Kinetic.Line({

    points: [150, 340, sw/2, 380, sw - 150, 340, sw/2, sh],

    tension: 0.5,

    closed: true,

    stroke: &#39;red&#39;,

    strokeWidth: 10

  });

 

  layer.add(leftEye)

       .add(rightEye)

       .add(nose)

       .add(mouth);

 

  stage.add(layer);      // tweens

 

  var leftEyeTween = new Kinetic.Tween({

    node: leftEye,

    duration: 1,

    easing: Kinetic.Easings.ElasticEaseOut,

    y: -100,

    points: [0, 200, 50, 150, 100, 200, 50, 200]

  });

 

  var rightEyeTween = new Kinetic.Tween({

    node: rightEye,

    duration: 1,

    easing: Kinetic.Easings.ElasticEaseOut,

    y: -100,

    points: [0, 200, 50, 150, 100, 200, 50, 200]

  });      var noseTween = new Kinetic.Tween({

    node: nose,

    duration: 1,

    easing: Kinetic.Easings.ElasticEaseOut,

    y: -100,

    points: [220, 280, sw/2, 200, sw-220,280]

  });

 

  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]

  });

 

  stage.getContainer().addEventListener('mouseover', function() {

    leftEyeTween.play();

    rightEyeTween.play();

    noseTween.play();

    mouthTween.play();

  });

 

  stage.getContainer().addEventListener('mouseout', function() {

    leftEyeTween.reverse();

    rightEyeTween.reverse();

    noseTween.reverse();

    mouthTween.reverse();

  });    </script>

Copy after login

我相信你已经在3三分钟内看完并看懂了这个动画的制作过程和原理。当然,这个动画很简单,我们这里只是粗浅的讲解一些HTML5画布(Canvas)动画技术的皮毛。如果想真正的成为Web动画告诉,那你还需要做很多的努力。但如果你只是业余爱好,我相信这已经足够让你骄傲了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I use viewport meta tags to control page scaling on mobile devices? How do I use viewport meta tags to control page scaling on mobile devices? Mar 13, 2025 pm 08:00 PM

The article discusses using viewport meta tags to control page scaling on mobile devices, focusing on settings like width and initial-scale for optimal responsiveness and performance.Character count: 159

How do I handle user location privacy and permissions with the Geolocation API? How do I handle user location privacy and permissions with the Geolocation API? Mar 18, 2025 pm 02:16 PM

The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

How do I use the HTML5 Drag and Drop API for interactive user interfaces? How do I use the HTML5 Drag and Drop API for interactive user interfaces? Mar 18, 2025 pm 02:17 PM

The article explains how to use the HTML5 Drag and Drop API to create interactive user interfaces, detailing steps to make elements draggable, handle key events, and enhance user experience with custom feedback. It also discusses common pitfalls to a

How do I use the HTML5 Page Visibility API to detect when a page is visible? How do I use the HTML5 Page Visibility API to detect when a page is visible? Mar 13, 2025 pm 07:51 PM

The article discusses using the HTML5 Page Visibility API to detect page visibility, improve user experience, and optimize resource usage. Key aspects include pausing media, reducing CPU load, and managing analytics based on visibility changes.

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

How do I use the HTML5 WebSockets API for bidirectional communication between client and server? How do I use the HTML5 WebSockets API for bidirectional communication between client and server? Mar 12, 2025 pm 03:20 PM

This article explains the HTML5 WebSockets API for real-time, bidirectional client-server communication. It details client-side (JavaScript) and server-side (Python/Flask) implementations, addressing challenges like scalability, state management, an

How do I use the HTML5 Notifications API to display desktop notifications? How do I use the HTML5 Notifications API to display desktop notifications? Mar 13, 2025 pm 07:57 PM

The article explains how to use the HTML5 Notifications API to display desktop notifications, focusing on permission requirements, customization, and browser support.

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

See all articles