


HTML5 Application-Happy Birthday Animation - Sample Code Sharing to Realize Stars
Before talking about drawing star animation, let me introduce some javascript knowledge.
Object-oriented: JavaScript is not an object-oriented language in nature, but a scripting language. It is generally only suitable for simple programs with a small amount of code, because scripts that are too complex will directly cause browser exceptions. . But JavaScript still has object-oriented characteristics. For multi-process and multi-object script programs, it is recommended to build objects, so that it is very convenient to maintain, modify and call the script. It is very simple to construct objects in javascript, much simpler than java and c++. For example, constructing a car object:
<pre name="code" class="javascript"><span style="font-size:18px;">var Car=function()//构建车的对象 { this.color="red"; this.price=100000; this.length=2.5; this.speed=80; //车的一些属性 this.x=0; this.updatePos=function() //更新车位置的方法 { this.x+=this.speed; } } </span>
构建好对象之后就可以构建对象了, var mycar=new Car();
setInterval():这是javascript中的定时器函数,有两个参数,前面是待执行的代码, 后面一个是间隔时间。但是有几点需要注意的问题。
1:假设有一个函数GetPos()需要用定时器执行, 可以setInterval(GetPos,1000)或者setInterval("GetPos()",1000) 如果写成setInterval(GetPos,1000)一般只会执行一次这个函数,我曾经犯过这个错误。
2:在setInterval()中尽量使用全局变量,因为它重复调用,局部变量容易出错。
好了,罗嗦了一会儿,我想大家应该都知道这些的。
这个gif图片中包括了星星对象和文字(后面讲述)对象。 对于绘制星星,应该充分利用五角星的对称性。 在HTML5 canvas对象中,大家最好要灵活使用坐标变换,我把主要的坐标变换函数说一下:
<span> <span>save() //保存当前画布状态 restore()回复画布状态 translate(x,y) //将画布中心坐标平移至x,y rotate(angle) //将画布旋转angle角度 transform 和setTransform是对画布矩形进行变换的,比较难用</span></span>现在构建星星对象,星星在天空中需要哪些属性呢? 一般有坐标、亮度、大小、倾斜角度等,我定义的如下
<span> this.x = -1; this.y = -1; //表示横纵坐标 this.style = ""; this.r = -1; this.scale = 1; //表示缩放倍数 this.angle = 0; //旋转的角度 this.angle1 = 0; //辅助参数</span>
其次需要绘制星星的方法,前面说过需要利用星星的对称性,因此实际上只需要绘制五分之一, 再利用旋转就可以绘制全部的图形了。星星需要填充颜色,HTML5中可以对路径进行填充, 因此可以使用路径beginPath()和closePath(),但是最后记住使用fill()函数填充的。
例如我绘制五分之一图形函数:
<span> this.drawPartStar = function () //部分 { cxt.save(); cxt.beginPath(); cxt.lineCap = "round"; cxt.lineWidth = 5; cxt.fillStyle = this.style; cxt.translate(this.x, this.y); //位移 cxt.rotate(this.angle1); //cxt.globalAlpha = this.alpha; //设置透明度 cxt.moveTo(0, 0); var xx = 0 - this.r * Math.sin(36 / 180 * 3.14); var yy = 0 - this.r * Math.cos(36 / 180 * 3.14); cxt.lineTo(xx, yy); xx = 0; yy = 0 - this.r * Math.cos(36 / 180 * 3.14) - this.r * Math.sin(36 / 180 * 3.14) * Math.tan(72 / 180 * 3.14); cxt.lineTo(xx, yy); xx = this.r * Math.sin(36 / 180 * 3.14); yy = 0 - this.r * Math.cos(36 / 180 * 3.14); cxt.lineTo(xx, yy); cxt.lineTo(0, 0); cxt.closePath(); cxt.fill(); cxt.restore(); }</span>
然后利用画布旋转就可以得到整个的图形了
<span>this.drawStar = function () //绘制完整的花 { for (var i = 0; i </span>
关于星星的坐标,半径大小,倾斜角度都可以使用随机函数实现,利用实现m到n之间的随机数:
<span>var x=(n-m)*Math.random()+m;</span>
通过合理控制m,n就可以得到需要的值了。
好了,到这里星星对象创建过程结束,其次需要构建这些星星。 由于星星数目多,可以使用数组对象,每一个数组对象都是一个星星对象:
<span> var stars = new Array(); //创建星星对象 var starCount = 40; //星星的数目,默认是40 for (var m = 0; m </span>
最后使用定时器,使星星具有闪烁效果:
<span> /***************演示闪烁星星的函数**********************/ function playStars() //演示星星 { for (var n = 0; n </span>setInterval("playStars()",500);
这样,闪烁的星星就全部做完了。
以上就是我说的利用对象构建闪烁星星的过程。 大家可以看到利用对象的简便和独立性,希望还不熟悉的朋友可以熟悉这种方法。
最后把star.js中关于星星对象的全部代码写在这里供大家参考
<span> /******star.js******/ /****以下是星星的对象**********************************/ var star = function (cxt) //定义星的对象 { this.x = -1; this.y = -1; //表示横纵坐标 this.style = ""; this.r = -1; this.scale = 1; //表示缩放倍数 this.angle = 0; //旋转的角度 this.angle1 = 0; //辅助参数 this.getPos = function () //获取随机坐标 { var xx = 20 + 1200 * Math.random(); var yy = 20 + 250 * Math.random(); this.x = Math.ceil(xx); this.y = Math.ceil(yy); //获取了随机坐标 } this.getAngle = function () //得到随机的角度 { this.angle = Math.random() * Math.PI; } this.getR = function () //获取半径 { var i = 1 + 4 * Math.random(); this.r = Math.ceil(i); //获取随机半径 } this.getColor = function () //获取随机颜色 { var n = Math.random(); if (n </span>
The above is the detailed content of HTML5 Application-Happy Birthday Animation - Sample Code Sharing to Realize Stars. For more information, please follow other related articles on the PHP Chinese website!

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.


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

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
