Table of Contents
The knowledge mainly used in this article
Code analysis
DrawleftToRight(): The effect is to display from left to right, mainly combining the fourth parameter and the fourth parameter. The eight parameters gradually increase from 0 to the width of the picture
Home Web Front-end H5 Tutorial HTML5 Programming Practice Part 2 - Using animation to switch picture code cases

HTML5 Programming Practice Part 2 - Using animation to switch picture code cases

Mar 30, 2017 pm 01:30 PM

The knowledge mainly used in this article

This article mainly uses the knowledgeCanvasThe drawImage method in API is briefly introduced below.

Use the drawImage method to draw images in the Canvas API. This is an overloaded method. , defined as follows:

1

2

3

context.drawImage(image,x,y);

context.drawImage(image,x,y,w,h);

context.drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh);

Copy after login

The first method has three parameters. The first parameter is the image to be drawn (it can also be a video element), and x and y are the starting coordinates of the image in the canvas.

The second method has five parameters. The first three have the same meaning as the first method. w and h refer to the width and height of the image when drawing, which can be used for image scaling.

The third method has nine parameters. The first parameter has the same meaning as the first two methods. The first four of the last eight parameters take a rectangle on the source image, and the last four parameters are used to define a rectangle on the canvas. , the function of the entire method is to copy part of the source image (the part defined by the second to fifth parameters) to the canvas (the part defined by the last four parameters). This article mainly uses the third one. The method completes drawing.


Source code

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

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

<!DOCTYPE html><html><head>

    <meta charset="UTF-8">

    <title>用动画的形式切换图片</title>

    <script type="text/javascript">

        var width, height;       

        var context, image, functionId;       

        var drawLeft, drawWidth;       

        var drawTop, drawHeight;       

        var spaceWidth, spaceHeight;       

        var speed=5000;       

        var images = ["http://i.6.cn/cvbnm/4e/7e/bb/75f251a8e2ae935d598f17b4f8275060.jpg",

        "http://i.6.cn/cvbnm/4a/6e/fb/805175016e502c483b75276f29801df3.jpg",

        "http://i.6.cn/cvbnm/6a/72/18/1787a3b2754ef48e374bbd14635f5c36.jpg",

        "http://i.6.cn/cvbnm/94/55/6c/b1ba743ba617be2891fa06b67d795511.jpg",

        "http://i.6.cn/cvbnm/02/1b/04/8018ee9e5756ac6b30f27d7ad6396b03.jpg",

        "http://i.6.cn/cvbnm/85/ea/d1/65f15857b971afb3b6e38b5fcdadc9c0.jpg"];       

        function selectFrom(iFirstValue, iLastValue) {           

        var iChoices = iLastValue - iFirstValue + 1;           

        return Math.floor(Math.random() * iChoices + iFirstValue);

        }        function showPicture(effects) {           

        var count = 0;           

        for (var o in effects) {

                count++;

            }          

             var canvas = document.getElementById(&#39;canvas&#39;);

            context = canvas.getContext(&#39;2d&#39;);

            width = canvas.width;

            height = canvas.height;           

            var currImage = 0;

            image = new Image();

            image.src = images[currImage];

            context.drawImage(image, 0, 0, width, height);

            currImage++;           

            if (count > 0) {

                setInterval(function () {                   

                var rand =  selectFrom(0, count - 1);

                    image.src = images[currImage];

                    currImage++;                   

                    if (currImage == images.length) {

                        currImage = 0;

                    }                   

                    var index = 0;                   

                    for (var effect in effects) {                       

                    if (index++ == rand) {

                            effects[effect]();                           

                            break;

                        }

                    }

                }, speed);

            }

        }

 

        window.onload=function(){

            showPicture({

                leftToRight: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    drawWidth = 0;

                    functionId = self.setInterval("drawleftToRight()", 10);

                },

                topToBottom: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    drawHeight = 0;

                    functionId = self.setInterval("drawtopToBottom()", 10);

                },

                hcenter: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    drawLeft = width / 2;

                    drawWidth = 0;

                    functionId = self.setInterval("drawhcenter()", 10);

                },

                vcenter: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    drawTop = height / 2;

                    drawHeight = 0;

                    functionId = self.setInterval("drawvcenter()", 10);

                },

                hwindow: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    spaceWidth = width / 10;

                    drawWidth = 0;

                    functionId = self.setInterval("drawhwindow()", 50);

                },

                vwindow: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    spaceHeight = height / 10;

                    drawHeight = 0;

                    functionId = self.setInterval("drawvwindow()", 50);

                },

                hvwindow: function () {

                    context.fillStyle = "#EEEEFF";

                    context.fillRect(0, 0, width, height);

                    spaceHeight = height / 10;

                    spaceWidth = width / 10;

                    drawWidth = 0;

                    drawHeight = 0;

                    functionId = self.setInterval("drawhvwindow()", 50);

                }

            });

        }        function drawleftToRight() {

            context.drawImage(image, 0, 0, drawWidth, image.height, 0, 0, drawWidth, image.height);

            drawWidth = drawWidth + 2;           

            if (drawWidth > width) {

                window.clearInterval(functionId);

            }

        }        function drawtopToBottom() {

            context.save();

            context.drawImage(image, 0, 0, image.width, drawHeight, 0, 0, image.width, drawHeight);

            drawHeight = drawHeight + 2;           

            if (drawHeight > height) {

                window.clearInterval(functionId);

            }

            context.restore();

        }       

        function drawhcenter() {

            context.save();

            context.drawImage(image, drawLeft, 0, drawWidth, image.height, drawLeft, 0, drawWidth, image.height);

            drawLeft = drawLeft - 1;

            drawWidth = drawWidth + 2;           

            if (drawLeft <= 0) {

                window.clearInterval(functionId);

            }

            context.restore();

        }       

        function drawvcenter() {

            context.save();

            context.drawImage(image, 0, drawTop, image.width, drawHeight, 0, drawTop, image.width, drawHeight);

            drawTop = drawTop - 1;

            drawHeight = drawHeight + 2;           

            if (drawTop <= 0) {

                window.clearInterval(functionId);

            }

            context.restore();

        }        function drawhwindow() {           

        for (i = 0; i < 10; i++) {

                context.drawImage(image, 0 + i * spaceWidth, 0, drawWidth,

                image.height, 0 + i * spaceWidth, 0, drawWidth, image.height);

            }

            drawWidth += 1;           

            if (drawWidth - 1 > spaceWidth) {

                window.clearInterval(functionId);

            }

        }        function drawvwindow() {

            context.save();

            context.clearRect(0, 0, width, height);           

            for (i = 0; i < 10; i++) {

                context.drawImage(image, 0, 0 + i * spaceHeight, image.width,

                drawHeight, 0, 0 + i * spaceHeight, image.width, drawHeight);

            }

            drawHeight += 1;           

            if (drawHeight - 1 > spaceHeight) {

                window.clearInterval(functionId);

            }

            context.restore();

        }        function drawhvwindow() {

            context.save();

            context.clearRect(0, 0, width, height);           

            for (i = 0; i < 10; i++) {               

            for (j = 0; j < 10; j++) {

                    context.drawImage(image, 0 + j * spaceWidth, 0 + i *

                    spaceHeight, drawWidth, drawHeight, 0 + j * spaceWidth, 0 + i *

                    spaceHeight, drawWidth, drawHeight);

                }

            }

            drawHeight += height / width;

            drawWidth += 1;           

            if (drawHeight > spaceHeight) {

                context.drawImage(image, 0, 0, width, height);

                window.clearInterval(functionId);

            }

            context.restore();

        }    </script></head><body>

    <h1>用动画的形式切换图片</h1>

    <canvas id="canvas" width="192px" height="255px"></canvas></body></html>

Copy after login

Code analysis

DrawleftToRight(): The effect is to display from left to right, mainly combining the fourth parameter and the fourth parameter. The eight parameters gradually increase from 0 to the width of the picture

drawtopToBottom(): The effect is to display from top to bottom, mainly increasing the fifth and ninth parameters from 0 to the height of the picture

Drawhcenter(): The effect is to display horizontally from the middle to both sides, mainly reducing the second and sixth parameters from half the image width to 0, and increasing the fourth and eighth parameters from 0 to the image width

Drawvcenter(): The effect is to display from the middle to both sides, similar to the previous one

drawhwindow(): The effect is horizontal blinds, similar to the principle of drawhcenter method, except that it is done from multiple places here

drawvwindow(): The effect is vertical blinds, the principle is similar to the drawvcenter method, except that it is done from multiple places here

drawhvwindow(): The effect is blinds, it is drawhwindow() Combination with drawvwindow()

Welcome everyone to add and improve

Since the picture is placed on other websites, it loads slowly and is not so smooth. You can change it when using it. This

map

film has better effect.

The above is the detailed content of HTML5 Programming Practice Part 2 - Using animation to switch picture code cases. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

See all articles