Home Web Front-end JS Tutorial Javascript production svg progress ball example sharing

Javascript production svg progress ball example sharing

Mar 15, 2018 am 09:27 AM
javascript js share

With the development of SVG to this day, it has been used in various applications on the Internet, including the production of progress bars and progress balls. How to make this type of animated interaction? Let’s reveal the secret to you next!

1. Interest Guidance > Final effect - SVG progress ball:

Javascript production svg progress ball example sharing

2. HTML (including SVG) Structure

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style type="text/css">
		.perText{font-size:58pt;font-family:Arial Rounded MT Bold;fill:#AD054A;text-anchor:middle;dominant-baseline: middle;text-shadow: 3px 0 6px #fff;transform:translate3d(6px,0,0);}
	</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="384.415px" height="383.232px" viewBox="0 0 384.415 383.232" >
	<circle style="fill:#E8427D;" cx="192.668" cy="195.399" r="180"/>
	<circle style="fill:#FFFFFF;" cx="192.668" cy="195.399" r="150"/>
	<circle style="display:none;fill:none;stroke:#000000;stroke-miterlimit:10;" cx="796.667" cy="-58.434" r="140.123"/>
	<path style="fill:none;" d="M656.667,8386.899"/>
	<path style="fill:none;" d="M656.667-7996.101"/>
	<g>

		<!-- 定义变量 -->
		<defs>
			<circle id="SVGID_1_" cx="191.668" cy="195.069" r="135" fill="red"/>
		</defs>
		<clipPath id="SVGID_2_">
			<use xlink:href="#SVGID_1_"  style="overflow:visible;" cx="191.668" cy="295.069" />
		</clipPath>
		<linearGradient id="SVGID_3_" gradientUnits="userSpaceOnUse" x1="431.4199" y1="221.6279" x2="-31.4133" y2="488.8449">
			<stop  offset="0" style="stop-color:#DA1654"/>
			<stop  offset="0.6452" style="stop-color:#E1457C;stop-opacity:0.4731"/>
			<stop  offset="1" style="stop-color:#F7F8F8;stop-opacity:0.3"/>
		</linearGradient>
		<linearGradient id="SVGID_4_" gradientUnits="userSpaceOnUse" x1="462.0762" y1="286.5215" x2="-63.9186" y2="590.2048">
			<stop  offset="0" style="stop-color:#F7F8F8;stop-opacity:0.3"/>
			<stop  offset="0.5" style="stop-color:#E1457C;stop-opacity:0.4731"/>
			<stop  offset="0.871" style="stop-color:#DA1654"/>
		</linearGradient>
		
		<!-- clip-path:url(#SVGID_2_) -->
		<g class="group" id=&#39;pathGroup&#39; style="clip-path:url(#SVGID_2_)">
			<!--<path class="bgPath bgPath_01" style="fill:url(#SVGID_3_);" id="bgPathOne">
					<animate 
						dur="5s" 
						attributeName="d" 
						attributeType="XML" 
						repeatCount="indefinite" 
						values="" 
						calcMode="linear"
						keyTimes="0;.6;1"></animate>			
			</path> -->
			<path class="bgPath bgPath_02" style="fill:url(#SVGID_3_);" d="">
				<animate 
						dur="5s" 
						attributeName="d" 
						attributeType="XML" 
						repeatCount="indefinite" 
						values="" 
						calcMode="linear"
						keyTimes="0;.3;1"></animate>	
			</path>-->
		</g>
		
		<text x=&#39;192.668&#39; y=&#39;195.399&#39; class="perText">50%</text>
	</g>
</svg>
</body>
</html>
Copy after login

3. JavaScript calculates the path and controls the svg animation

class svgPercent
{

	constructor({y1,y2,group,text})
	{
		this.y1 = y1 ? y1 : 0;
		this.y2 = y2 ? y2 : 0;
		this.group = group;
		this.xmlns = &#39;http://www.w3.org/2000/svg&#39;;
		this.textBox = text;
		this.currentPercentText = &#39;0%&#39;;

		//初始 进度球
		this.init();
	}

	init()
	{	
		//1.获取路径数据
		this.getSvgPathData(this.y1,this.y2);
		
		//2.根据数据绘制路径
		this.createPath();

		//3.设置百分比
		this.setPercentText();

		//4.模拟进度增长的情况
		this.changePathShape();

	}

	initChangeShape()
	{
		//1.获取路径数据
		this.getSvgPathData(this.y1,this.y2);

		//2. 设置路径形状改变
		this.setPaths();
	}

	//获取路径数据
	getSvgPathData(y,y2)
	{
		this.d1=`M327.898,${225.235+y}c3.086,${-11.496+y},4.74,${-11.496+y},4.74,${-36.167+y}c0,${0+y},-31.712,${-28.628+y},-140.67,${-2+y}c-120.71,${29.5+y},-125.21,${11+y}-140.67,${0.35+y}c0.032,${13.105+y},1.862,${25.787+y},5.247,${37.817+y}h-90.043 v390 h467 v-390 H327.898 z`;

		this.a1=`M 327.898,${225.235+y}c 3.086,${-11.496+y},4.74,${-23.611+y},4.74,${-36.167+y}c 0,${0+y},-23.971,${54.165+y},-140.67,${-2+y}c-111.97,${-53.888+y}-135.301,${-9.835+y}-140.67,${0.35+y}c 0.032,${13.105+y},1.862,${25.787+y},5.247,${37.817+y}h-90.043 v390 h 467 v-390 H 327.898 z`;	

		this.d2 = `M 327.898,${237.031+y2}c 3.086,${-14.234+y2},4.74,${-29.236+y2},4.74,${-44.785+y2}c 0,${0+y2}-30.305,${36.653+y2}-140.67,${-2.477+y2}c-118.866,${-42.144+y2}-134.529,${-9.191+y2}-140.67,${0.434+y2}c 0.032,${16.229+y2},1.862,${31.933+y2},5.247,${46.828+y2}h-90.043 v 405.865 h 467 V ${237.031+y2} H 327.898 z`;

		this.a2 = `M 327.898,${237.031+y2}c 3.086,${-14.234+y2},4.74,${-29.236+y2},4.74,${-44.785+y2}c 0,${0+y2}-56.638,${-36.347+y2}-140.67,${-2.477+y2}C 74.997,${236.916+y2},63,${199.232+y2},51.299,${190.203+y2}c 0.032,${16.229+y2},1.862,${31.933+y2},5.247,${46.828+y2}h-90.043 v 405.865 h 467 V ${237.031+y2} H 327.898 z`;
	}

	//创建path路径
	createPath(group)
	{
		this.pathOne = document.createElementNS(this.xmlns,&#39;path&#39;);
		this.animate = document.createElementNS(this.xmlns,&#39;animate&#39;);
		this.pathOne.setAttribute(&#39;style&#39;,&#39;fill:url(#SVGID_3_)&#39;);
		this.pathOne.setAttribute(&#39;d&#39;,this.d1);
		this.animate.setAttribute(&#39;dur&#39;,&#39;5s&#39;);
		this.animate.setAttribute(&#39;attributeName&#39;,&#39;d&#39;);
		this.animate.setAttribute(&#39;attributeType&#39;,&#39;XML&#39;);
		this.animate.setAttribute(&#39;repeatCount&#39;,&#39;indefinite&#39;);
		this.animate.setAttribute(&#39;keyTimes&#39;,&#39;0;0.55;1&#39;);
		this.animate.setAttribute(&#39;values&#39;,this.d1+&#39;;&#39;+this.a1+&#39;;&#39;+this.d1);
		this.pathOne.appendChild(this.animate);
		this.group.appendChild(this.pathOne);	 
		
		this.pathTwo = document.createElementNS(this.xmlns,&#39;path&#39;);
		this.animate2 = document.createElementNS(this.xmlns,&#39;animate&#39;);
		this.pathTwo.setAttribute(&#39;style&#39;,&#39;fill:url(#SVGID_3_)&#39;);
		this.pathTwo.setAttribute(&#39;d&#39;,this.d2);
		this.animate2.setAttribute(&#39;dur&#39;,&#39;5s&#39;);
		this.animate2.setAttribute(&#39;attributeName&#39;,&#39;d&#39;);
		this.animate2.setAttribute(&#39;attributeType&#39;,&#39;XML&#39;);
		this.animate2.setAttribute(&#39;repeatCount&#39;,&#39;indefinite&#39;);
		this.animate2.setAttribute(&#39;keyTimes&#39;,&#39;0;0.55;1&#39;);
		this.animate2.setAttribute(&#39;values&#39;,this.d2+&#39;;&#39;+this.a2+&#39;;&#39;+this.d2);
		this.pathTwo.appendChild(this.animate2);
		this.group.appendChild(this.pathTwo);
	}

	//设置path路径
	setPaths()
	{
		this.pathOne.setAttribute(&#39;d&#39;,this.d1);
		this.pathTwo.setAttribute(&#39;d&#39;,this.d2);
		this.animate.setAttribute(&#39;values&#39;,this.d1+&#39;;&#39;+this.a1+&#39;;&#39;+this.d1);
		this.animate2.setAttribute(&#39;values&#39;,this.d2+&#39;;&#39;+this.a2+&#39;;&#39;+this.d2);
	}

	//设置百分比文字
	setPercentText(val)
	{	
		let vals = val ? val : this.currentPercentText;
		this.textBox.textContent = vals;
	}

	//改变路径形状
	changePathShape()
	{	
		let dis = 0.3;
		let percent = &#39;&#39;;
		let p = &#39;&#39;;
		let start = this.y1;
		let end = -50;

		let This = this;
		function step()
		{
			This.y1 -= dis;
			This.y2 -= dis;
			
			This.initChangeShape();
			percent = parseInt((Math.abs(This.y1 - start) / Math.abs(end-start))*100);
			p = percent + &#39;%&#39;;
			This.setPercentText(p);
			
			if(percent < 50){
				requestAnimationFrame(step);
			}
		}

		requestAnimationFrame(step);	
	}
}

// 初始化配置参数调用
let obj = {
	y1: 50,
	y2: 50,
	group: document.querySelector(&#39;#pathGroup&#39;),
	text: document.querySelector(&#39;.perText&#39;)
}

new svgPercent(obj);
Copy after login

4. Summary:

(1) Control the d attribute of the path through animate (note , there are pitfalls, values ​​​​have at least three sets of values ​​values="original value; value to be changed; original value")

(2) The d attribute of path is used through js control (note that there are pitfalls , when splicing strings, there cannot be a semicolon at the end, and an error will be reported; when splicing values, you need to add a semicolon separately)

(3) The use of clipPath tags is how this case implements rolling ripples from A core from bottom to top.

The above is the detailed content of Javascript production svg progress ball example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the &quot;Share to&quot; option at the bottom, and then select the first &quot;WeChat Moments&quot; allows you to share content to WeChat Moments.

How to share files with friends on Baidu Netdisk How to share files with friends on Baidu Netdisk Mar 25, 2024 pm 06:52 PM

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the &quot;Shared Members&quot; column 】, and finally check all

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Mango tv member account sharing 2023 Mango tv member account sharing 2023 Feb 07, 2024 pm 02:27 PM

Mango TV has various types of movies, TV series, variety shows and other resources, and users can freely choose to watch them. Mango TV members can not only watch all VIP dramas, but also set the highest definition picture quality to help users watch dramas happily. Below, the editor will bring you some free Mango TV membership accounts for users to use, hurry up and take a look Take a look. Mango TV latest member account free sharing 2023: Note: These are the latest member accounts collected, you can log in directly and use them, do not change the password at will. Account number: 13842025699 Password: qds373 Account number: 15804882888 Password: evr6982 Account number: 13330925667 Password: jgqae Account number: 1703

Share two installation methods for HP printer drivers Share two installation methods for HP printer drivers Mar 13, 2024 pm 05:16 PM

HP printers are essential printing equipment in many offices. Installing the printer driver on the computer can perfectly solve problems such as the printer being unable to connect. So how to install HP printer driver? The editor below will introduce you to two HP printer driver installation methods. The first method: download the driver from the official website 1. Search the HP China official website in the search engine, and in the support column, select [Software and Drivers]. 2. Select the [Printer] category, enter your printer model in the search box, and click [Submit] to find your printer driver. 3. Select the corresponding printer according to your computer system. For win10, select the driver for win10 system. 4. After downloading successfully, find it in the folder

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

See all articles