


HTML5 Programming Practice Part 2 - Using animation to switch picture code cases
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:
context.drawImage(image,x,y); context.drawImage(image,x,y,w,h); context.drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh);
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
<!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('canvas'); context = canvas.getContext('2d'); 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 id="用动画的形式切换图片">用动画的形式切换图片</h1> <canvas id="canvas" width="192px" height="255px"></canvas></body></html>
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
mapThe 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!

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
