This article mainly introduces the pop-up box effect based on HTML5 Canvas. Friends who need it can refer to it
When the user moves the mouse, a pop-up box appears. Such a requirement is very common. This is simple when processing HTML elements, but if you are processing graphics composed of HTML5 Canvas, this method is no longer applicable, because Canvas uses another set of mechanisms. No matter how many graphics are drawn on Canvas, Canvas is the same overall. The graphics themselves are actually part of the Canvas and cannot be obtained separately, so it is impossible to directly add JavaScript events to a graphics. However, in HT for Web, this requirement is easily realized. The scenario is as follows:
This scene graph is based on the JSON file of HT for Web. You may have some questions about how to generate it. There is confusion about such a JSON file. In fact, this is based on the "HTML5 topology editor" (www.hightopo.com/demo/2deditor_20151010/HT-2D-Editor.html), which is small and complete. It is very easy to expand. Just customize the topology editor to meet my needs. Not only that, in this Demo, the vector images 'tips1.json', 'tips2.json', and 'tips3.json' of the three types of bullet boxes defined are through this vector editor (http://www.hightopo. com/demo/vector-editor/index.html), I drew it briefly and it is quite easy to use. In the above scenario, when the user moves the mouse into an object such as grass, a pop-up box will display its detailed information. Demo address: http://www.hightopo.com/demo/blog_meadow_20170605/index.html
The specific implementation is as follows:
Preparation work
Introduction to our HT (www.hightopo.com/):
<script src='ht.js'></script> dataModel = new ht.DataModel(); graphView = new ht.graph.GraphView(dataModel); graphView.addToDOM();
HT provides a custom JSON format vector description form. The JSON vector format defined in the HT standard can also be registered and used as an image. HT's vector method saves space more than traditional formats and does not cause distortion when scaling. Click HT for Web for details. Here, three shapes of JSON pop-up boxes are registered as images for subsequent calls:
ht.Default.setImage('tips1', 'symbols/tips1.json'); ht.Default.setImage('tips2', 'symbols/tips2.json'); ht.Default.setImage('tips3', 'symbols/tips3.json');
Then obtain objects with interactive effects, where the attribute names in each object are the label names set for each primitive:
//树 var tree = { 'tree1' : true, 'tree2' : true, 'tree3' : true }; //草地 var grass = { 'grass1' : true, 'grass2' : true, 'grass3' : true }; //山 var mountain = { 'mountain': true };
Pop-up box
In fact, the essence of the pop-up box is a Node. When the user moves the mouse in and out,
1. Control the Node The hiding and displaying can achieve the effect of a pop-up box;
2. The change of the mouse position is accompanied by the change of the Node position;
3. When the mouse moves to different objects, the Node The texture also changes accordingly;
4. The attribute values in Node also change with the mouse position.
Therefore, to implement the pop-up box, you should first create a new Node and set its level to 'higher'. Before that, you need to deserialize the JSON file of the scene graph and deserialize it. The graphics elements are all set to the level 'lower' to prevent them from being blocked by existing graphics elements:
ht.Default.xhrLoad('meadow.json', function(text) { const json = ht.Default.parse(text); if(json.title) document.title = json.title; dataModel.deserialize(json); //设置层级 dataModel.each(function(data){ data.setLayer('lower'); }); //新建node var node = new ht.Node(); node.s('2d.visible',false); node.setLayer('higher'); dataModel.add(node); })
Then, monitor the mousemove event on the underlying p to determine whether the mouse position is above the above three objects. According to the object type, call the layout() function to re-layout the Node:
graphView.getView().addEventListener('mousemove', function(e) { node.s('2d.visible',false); var hoverData = graphView.getDataAt(e); pos = graphView.getLogicalPoint(e); if(!hoverData) return; if(tree[hoverData.getTag()]){ layout(node, pos, 'tips1'); } else if (grass[hoverData.getTag()]) { layout(node, pos, 'tips2'); } else if (mountain[hoverData.getTag()]) { layout(node, pos, 'tips3'); } });
What the layout() function does has been explained in detail before. Among them, the update of the attribute value in the pop-up box is to update the JSON file The text attribute is used for data binding. The binding format is very simple. Just replace the previous parameter value with an object with the func attribute. The content of func has the following types:
1 , function type, call the function directly and pass in the relevant Data and view objects. The parameter value is determined by the function return value, that is, func(data, view); call.
2. String type:
style@*** starts with, the data.getStyle(***) value is returned, where *** represents the attribute name of style.
ATTR@*** Starting at the beginning, the data.getattr (***) value is returned, where *** represents the attribute name of ATTR.
Field@*** Starting at the beginning, the data. *** value is returned, where *** represents the attribute name of ATTR.
If the above conditions are not matched, directly use the string type as the function name of the data object to call data***(view), and the return value will be used as the parameter value.
In addition to the func attribute, you can also set the value attribute as the default value. If the value obtained by the corresponding func is undefined or null, the default value defined by the value attribute will be used. For details, see HT for Web Data Binding Specification manual (http://www.hightopo.com/guide/guide/core/datamodel/ht-datamodel-guide.html). For example, here, the result of data binding the sunshine value in the 'tips1.json' file is as follows:
"text": { "func": "attr@sunshine", "value": "阳光值" },
The source code of the layout() function is pasted below:
function layout(node, pos, type){ node.s('2d.visible',true); node.setImage(type); if(type == 'tips1'){ node.setPosition(pos.x + node.getWidth()/2, pos.y - node.getHeight()/2); node.a({ 'sunshine' : '阳光值 : '+ (pos.x/1000).toFixed(2), 'rain' : '雨露值 : '+ (pos.y/1000).toFixed(2), 'love' : '爱心值 : ***' }); } else if(type == 'tips2'){ node.setPosition(pos.x , pos.y - node.getHeight()/2); node.a({ 'temp' : '温度 : 30', 'humidity' : '湿度 : '+Math.round(pos.x/100)+'%' }); } else if(type == 'tips3'){ node.setPosition(pos.x - node.getWidth()/2, pos.y - node.getHeight()/2); node.a({ 'hight' : '海拔 : ' + Math.round(pos.y)+'米', 'landscapes' : '地貌 : 喀斯特' }); } }
CloudMove
Finally, our Demo also has a cloud mobile animation effect. Under the design architecture of HT's data model-driven graphic components, animation can be understood as changing certain attributes from start to finish. The process of the initial value gradually changing to the target value. HT provides the animation function of ht.Default.startAnim. ht.Default.startAnim supports two methods of animation: Frame-Based and Time-Based:
Frame-Based方式用户通过指定frames动画帧数,以及interval动画帧间隔参数控制动画效果;
Time-Based方式用户只需要指定duration的动画周期的毫秒数即可,HT将在指定的时间周期内完成动画。
详情见HT for Web。
在这里我们用的是Time-Based方式,源码如下:
var cloud = dataModel.getDataByTag('cloud'); parent = dataModel.getDataByTag('mountain'); round1 = parent.getPosition().x - parent.getWidth()/2 + cloud.getWidth()/2; round2 = parent.getPosition().x + parent.getWidth()/2 - cloud.getWidth()/2; end = round1; //云运动动画 var animParam = { duration: 10000, finishFunc: function() { end = (end == round1) ? round2 : round1; ht.Default.startAnim(animParam); }, action: function(v, t) { var p = cloud.getPosition(); cloud.setPosition(p.x + (end - p.x) * v , p.y); } }; ht.Default.startAnim(animParam);
The above is the detailed content of Share an example of the effect of pop-up box in HTML5. For more information, please follow other related articles on the PHP Chinese website!

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.

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.


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

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

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
