This article shares with you the text animation special effects based on HTML5 Canvas. It is very useful. Friends in need can use it for reference
Preface
Text is the most basic element in a web page. Generally, we display static text on web pages, but in terms of effect, it is still relatively boring. of. The animation effect of text fading in and out is very practical in projects. If there are certain key words, this dynamic effect can be used to remind users to read.
Dynamic renderings
{ "width": 10,//设置矢量的宽 "height": 10,//设置矢量的高 "comps": [//矢量图形的组件Array数组,每个数组对象为一个独立的组件类型,数组的顺序为组件绘制先后顺序 { "type": "text",//文本类型 "text": "H",//文本内容 "color": "rgb(69,69,69)",//文本颜色 "align": "center",//文本在矢量中的对齐方式 "opacity": {//文本透明度 "func": "attr@text.opacity",//设置业务属性,对文本进行透明度的数据绑定 "value": 1//如果func中的值为空或者undefined,那么就直接用这个值 }, "clipDirection": "bottom",//裁切方向为“从上到下” "rect": [//指定组件绘制在矢量中的矩形边界 0,//代表左上角坐标x 0,//代表左上角坐标y 10,//代表组件的width 10//代表组件的height ] }] }
arr = [ {label: 'H', image: 'symbols/H.json'}, {label: 'T', image: 'symbols/T.json'}, {label: 'f', image: 'symbols/f.json'}, {label: 'o', image: 'symbols/o.json'}, {label: 'r', image: 'symbols/r.json'}, {label: 'W', image: 'symbols/W.json'}, {label: 'e', image: 'symbols/e.json'}, {label: 'b', image: 'symbols/b.json'}, ];
##
对象的图片已经生成,接下来就是创建对象了,这里英文字母总共 8 个,那么我们创建 8 个节点对象:
var s = 80;
arr.forEach(function(obj, index) {
var text = obj.label;
name = 't' + text;
window[name] = createNode(obj.image, 100+s*index, 100);
});
function createNode(image, x, y) {//节点对象声明 var node = new ht.Node();//这个类为 ht 中定义的节点 node.setSize(0, 0);//设置节点大小 if(image) node.setImage(image);//设置节点图片 if(x && y) node.setPosition(x, y);//设置节点摆放位置 dm.add(node);//将节点添加进数据容器 datamodel 中 return node; }
关于上面的 ht.Node 节点的生成,其实这个只是 HT 封装好的类,这个类上面有很多很方便的 API。然后将这个生成的节点添加进数据容器 dm 中,这个数据容器又是整个拓扑图 gv 的数据容器。
拓扑图生成
来看看如何生成这个拓扑图吧:
dm = new ht.DataModel();//数据容器gv = new ht.graph.GraphView(dm);//拓扑图 通过 gv.getView() 可获得这个拓扑图的底层 pgv.addToDOM();//将 gv 添加进 body 中
实际上 HT 的原理就是在一个 p 中的 canvas 上绘制图形,也就是说这个 gv 就是一个 canvas。
然后通过 getView 获取这个 canvas 的底层 p,这样我们就能将这个 p 添加到 html 页面的任何地方了,addToDOM 的定义如下:
addToDOM = function(){ var self = this, view = self.getView(), //获取底层p style = view.style; document.body.appendChild(view); //将底层p添加到body中 style.left = '0';//因为 HT 默认将组件的position设置为absolute 所以要设置位置 style.right = '0'; style.top = '0'; style.bottom = '0'; window.addEventListener('resize', function () { self.iv(); }, false);//窗口大小变化触发事件,调用最外层组件invalidate(即iv)函数进行更新。}
现在刷新页面,你会看到一片空白,为什么?因为前面设置节点的大小为 0 啊,怎么会显示,这个 Demo 的效果就是从无到有,又从有到无。那接下来看看如何“从无到有”。
文本动画
就像我刚刚说过的,要想让节点显示,肯定是需要设置节点的大小为我们肉眼可视的范围才会出现,但是我的目的不仅是从无到有,也是从小到大,这个能够一气呵成么?感觉好像代码内容简单,但是代码量却不小的一个任务,我定义了一个函数用来将节点从无到有,从小到大:
function setSize(node) { if(node) { var s = 80, size = node.getSize().width;//获取节点当前的大小中的宽度,因为我知道宽高都是一样的,所以简写了 var sw = s - size; ht.Default.startAnim({//HT 封装的动画函数,内容也是 JSON 格式的对象 duration: 1000,// 动画周期毫秒数 easing: function(t) { return t*t },//动画缓动函数 action: function(v, t) {//action函数必须提供,实现动画过程中的属性变化 第一个参数v代表通过easing(t)函数运算后的值,t代表当前动画进行的进度[0~1],一般属性变化根据v参数进行 node.setSize(//设置节点的大小 (有一个缓动的过程 通过 sw*v 实现的) size + sw*v, size + sw*v ); } }); } }
从大到小,从有到无的过程也跟上面类似,我就不赘述了。
要让这些字母按照时间的先后顺序出现和消失,肯定需要用到 setTimeout 方法,要想实现一次的显示消失是非常容易的,但是我在实现的过程掉到了 setTimeout 的一个陷阱中,只能说自己学艺不精吧。因为我们需要给不同的字母设置不同的出现和消失时间,一般比较简单的方法就是设置一个固定的值,然后乘以对应节点专属的 index:
function animateIn() { for(let i = 0; i <p><br></p><p style="margin:10px auto;color:rgb(0,0,0);font-family:'PingFang SC', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:14px;">可是如果我直接在 for 循环中设置 setTimeout 的时间为动态变化的,那么这个动态变化的值肯定是只取 for 循环的最后一个值,所以我将 setTimeout 的方法抽取出来作为一个单独的函数:</p><p class="cnblogs_code" style="background-color:rgb(245,245,245);font-family:'Courier New';font-size:12px;border:1px solid rgb(204,204,204);padding:5px;margin:5px 0px;color:rgb(0,0,0);"><br></p><pre style="font-family:'Courier New';margin:5px 8px;padding:5px;" class="brush:html;toolbar:false;">function animateLetterIn(node, i) { setTimeout(function() { setSize(node); }, i * 200);//这时候这个 i 取的就是节点对应的 i 而不是最后一个值了 if(i === arr.length - 1) {//当节点为最后一个节点时,设置节点淡出动画 setTimeout(function() { animateOut();//节点淡出动画 }, (arr.length + 3) * 200); } }
节点淡出动画也是类似的方法,只是需要循环调用这些动画函数,这样才能做到无限循环字母的大小控制。
相关推荐:
The above is the detailed content of Text animation special effects based on HTML5 Canvas. For more information, please follow other related articles on the PHP Chinese website!

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment