HTML5 Canvas implements interactive subway map
This article introduces you to the interactive subway line map based on HTML5 Canvas. It has a good reference value and I hope it can help friends in need.
Preface
I was in echarts two days ago When I was looking for inspiration on the Internet, I saw many similar examples of maps, map positioning, etc., but it seemed that there was no subway line map, so I spent some time tinkering with this interactive subway line map. Demo, the points on the subway line were randomly downloaded from the Internet. This article records some of my gains (after all, I am still a rookie) and the implementation of the code. I hope it can help some friends. Of course, if you have any opinions, you can tell me directly. Only by communicating together can we make progress.
Rendering
http://www.hightopo.com/demo/subway/index.html
The map has a little content If you want to display them all, the characters will appear a bit small, but it doesn’t matter. You can zoom in or out according to your needs. The fonts and drawn content will not be distorted. After all, they are all drawn with vectors~
Interface generation
The underlying div is generated through the ht.graph.GraphView component. Then you can use the good methods provided by HT for Web and just call the canvas brush to draw casually. Let's first see how to generate the underlying div:
1 2 3 |
|
The addToDOM function is declared as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
Now I can scribble on this div~ First I get the points on the downloaded subway map, and I put them in subway.js , this js file is all downloaded content. I have not made any other changes. The main thing is to add these points to the array according to the line, for example:
1 2 3 4 5 6 7 |
|
Next, let’s draw the subway line, I declared an array lineNum to hold the numbers of all subway lines in js, and a color array to hold the colors of all subway lines. The indexes of these colors correspond to the index of the subway line numbers in lineNum. ’s:
1 2 |
|
Then traverse lineNum, pass the elements and colors in lineNum to the createLine function, and draw the subway line and color matching based on these two parameters. After all The naming method in js files is also regular. Which line is named will be followed by a corresponding number, so we only need to combine the string with this number to obtain js The corresponding array is:
1 2 |
|
The definition of createLine is also very simple. My code has a lot of styles, so it looks a bit much. Create an ht.Polyline pipeline, we can pass The polyline.addPoint() function adds specific points to this variable, and the connection method of the points can be set through setSegments.
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 |
|
There are several situations in adding points on the subway line in the above code. This is because Line68 has a "jumping" point when setting the line in js, so we must "jump" over it. Please refer to subway.js for the specific declaration of limited Line68 array.
One thing to note here is that if you use the addPoint function and do not set segments, the added points will be connected with straight lines by default. The definition of segments is as follows:
1: moveTo, occupies 1 Point information, represents the starting point of a new path
2: lineTo, occupies 1 point information, represents the connection from the last point to this point
3: quadraticCurveTo, occupies 2 point information , the first point is used as the curve control point, the second point is used as the curve end point
4: bezierCurveTo, occupies 3 point information, the first and second points are used as curve control points, the third point As the end point of the curve
5: closePath, which does not occupy point information, represents the end of this path drawing and is closed to the starting point of the path
So we need to set segments for the "jump" behavior Just 1.
Finally draw these points on the subway line. This part is also separated in subway.js. The names start with "mark_Point", "t_Point" and "n_Point". I have the right ones in the display part of the previous js. These arrays are explained, and everyone moves their middle fingers to scroll up and take a look.
We add ht.Node nodes at these points. When the nodes are added to the dm data container, they will be displayed on the topology map. Of course, the premise is that the data container set by the topology map component gv It’s this DM. Due to limited space, I will only show the code for adding points on the subway line:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
All subway lines and stations have been added. but! You may not be able to see the graphs you drew because they are too small. At this time, you can set the fitContent function on the graphView topology component. By the way, we also set everything on the topology graph to be immovable:
1 2 3 4 |
|
Now your subway route map can be displayed~ Let’s take a look at the interaction.
Interaction
首先是鼠标移动事件,鼠标滑过具体线路时,线路会变粗,悬停一会儿还能看到这条线路的编号;当鼠标移动到“换乘站点”或“小站点”,站点对应的图标都会变大并且变色,字体也会变大,鼠标移开图标变回原来的颜色并且字体变小。不同点在于鼠标移动到“换乘站点”时,“换乘站点”会旋转。
鼠标滑动事件,我直接基于 gv 的底层 div 进行的 mousemove 事件,通过 ht 封装的 getDataAt 函数传入事件 event 参数,获取事件下对应的节点,然后就可以随意操作节点了:
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 |
|
鼠标悬停在地铁线路上时显示“具体线路信息”,我是通过设置 tooltip 来完成的(注意:要打开 gv 的 tooltip 开关):
1 2 3 4 |
|
然后我利用右下角的 form 表单,单击表单上的具体线路,或者双击拓扑图上任意一个“站点”或者线路,则拓扑图会自适应到对应的部分,将被双击的部分展现到拓扑图的中央。
form 表单的声明部分我好像还没有解释。。。就是通过 new 一个 ht.widget.FomePane 类创建一个 form 表单组件,通过 form.getView() 获取表单组件的底层 div,将这个 div 摆放在 body 右下角,然后通过 addRow 函数向 form 表单中添加一行的表单项,可以在这行中添加任意多个项,通过 addRow 函数的第二个参数(一个数组),对添加进的表单项进行宽度的设置,通过第三个参数设置这行的高度:
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 |
|
单击“站点”显示红色标注,双击节点自适应放置到拓扑图中央以及双击空白处将红色标注隐藏的内容都是通过对拓扑组件 gv 的事件监听来控制的,非常清晰易懂,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
注意 s(style) 和 a(attr) 定义是这样的,s 是 ht 预定义的一些样式属性,而 a 是我们用户来自定义的属性,一般是通过调用字符串来调用结果的,这个字符串对应的可以是常量也可以是函数,还是很灵活的。
最后还做了一个小小的部分,选中“站点”,则该“站点”的上方会显示一个红色的会“呼吸”的用来注明当前选中的“站点”。
“呼吸”的部分是利用 ht 的 setAnimation 函数来完成的,在用这个函数之前要先打开数据容器的动画开关,然后设置动画:
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 |
|
全部代码结束!
总结
这个 Demo 花了我两天时间完成,总觉得有点不甘心啊,但是有时候思维又转不过弯来,花费了不少的时间,但是总的来说收获还是很多的,我以前一直以为只要通过 getPoints().push 来向多边形中添加点就可以了,求助了大神之后,发现原来这个方法不仅绕弯路而且还会出现各种各样的问题,比如 getPoints 之前,一定要在多边形中已经有 points 才可以,但是在很多情况下,初始化的 points 并不好设置,而且会造成代码很繁琐,直接通过 addPoint 方法,直接将点添加进多边形变量中,并且还会默认将点通过直线的方式连接,也不用设置 segments,多可爱的一个函数。
还有就是因为 ht 默认缩放大小是 20,而我这个 Demo 的间距又很小,导致缩放到最大地铁线路图显示也很小,所以我在 htconfig 中更改了 ht 的默认 zoomMax 属性,记住,更改这个值一定要在所有的 ht 调用之前,因为在 htconfig 中设置的值在后面定义都是不可更改的。
相关推荐:
The above is the detailed content of HTML5 Canvas implements interactive subway map. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.
