JS implements circular progress bar drag and slide

JS implements circular progress bar drag and slide

This article example shares the specific code of JS to realize the drag and slide of circular progress bar for your reference. The specific content is as follows

Effect display

Semicircular progress bar effect

Circular progress bar

Code Implementation

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <title> Drag and slide the circular progress bar</title>
</head>
<body>
<canvas id="canvasId" width="400" height="400"></canvas>
<script type="text/javascript">
    var canvas = document.getElementById("canvasId");
    var ctx = canvas.getContext("2d");
    var ox = 200;
    var oy = 200;
    var or = 180;
    var br = 15;
    var moveFlag = false;

    function offset(r,d) {//Calculate the offset coordinates based on radians and distance return {x: -Math.sin(r)*d, y: Math.cos(r)*d};
    };

    function draw(n) {
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.strokeStyle = "#99a";
        ctx.lineWidth = 5;
        ctx.beginPath();
        ctx.arc(ox,oy,or,0,Math.PI,true);//semicircle// ctx.arc(ox,oy,or,0,2*Math.PI,true);//full circlectx.stroke();
        ctx.strokeStyle = "#69f";
        ctx.lineWidth = 5;
        ctx.beginPath();
        ctx.arc(ox,oy,or,Math.PI,(n*2+0.5)*Math.PI,false);
        // ctx.arc(ox,oy,or,0.5*Math.PI,(n*2+0.5)*Math.PI,false);
        ctx.stroke();
        ctx.fillStyle = "#69f";
        ctx.font = "80px Arial";
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        ctx.fillText(Math.round(n*100-25)+"%",ox,oy);
        ctx.fillStyle = "#00f";
        ctx.beginPath();
        var d = offset(n*2*Math.PI,or);
        ctx.arc(ox+dx,oy+dy,br,0,2*Math.PI,true);
        ctx.fill();
    }

    var on = ("ontouchstart" in document)? {
        start: "touchstart", move: "touchmove", end: "touchend"
    } : {
        start: "mousedown", move: "mousemove", end: "mouseup"
    };

    function getXY(e,obj) {
        var et = e.touches? e.touches[0] : e;
        var x = et.clientX;
        var y = et.clientY;
        return {
            x : x - obj.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),
            y : y - obj.offsetTop + (document.body.scrollTop || document.documentElement.scrollTop)
        }
    }

    canvas.addEventListener(on.start, function(e) {
        moveFlag = true;
    }, false);

    canvas.addEventListener(on.move, function(e) {
        if (moveFlag) {
            var k = getXY(e,canvas);
            var r = Math.atan2(kx-ox, oy-ky);
            var hd = (Math.PI+r)/(2*Math.PI);

            // Determine the sliding range of the semicircle if (hd <= 0.75 && hd >= 0.25) {
                draw(hd);
            }
        }
    }, false);

    canvas.addEventListener(on.end, function(e) {
        moveFlag = false;
    }, false);

    draw(0.25);
</script>

</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS implements dragging the progress bar to change the transparency of elements
  • JavaScript implements draggable progress bar
  • JavaScript implements horizontal progress bar drag effect
  • js to achieve sliding progress bar effect

<<:  How to use VLAN tagged Ethernet card in CentOS/RHEL system

>>:  HTML table tag tutorial (45): table body tag

Recommend

HTML table tag tutorial (32): cell horizontal alignment attribute ALIGN

In the horizontal direction, you can set the cell...

How does Vue3's dynamic components work?

Table of contents 1. Component Registration 1.1 G...

Detailed examples of float usage in HTML/CSS

1. Basic usage examples of float 1. Let's fir...

A brief discussion on using virtual lists to optimize tables in el-table

Table of contents Preface Solution Specific imple...

How to ensure that every page of WeChat Mini Program is logged in

Table of contents status quo Solution Further sol...

Use nginx to dynamically convert image sizes to generate thumbnails

The Nginx ngx_http_image_filter_module module (ng...

A brief discussion of 3 new features worth noting in TypeScript 3.7

Table of contents Preface Optional Chaining Nulli...

Linux file management command example analysis [display, view, statistics, etc.]

This article describes the Linux file management ...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

Pure JavaScript to implement the number guessing game

Develop a number guessing game that randomly sele...

CSS specification BEM CSS and OOCSS sample code detailed explanation

Preface During project development, due to differ...

How to set Nginx to forward the domain name to the specified port

Enter /usr/local/nginx/conf sudo cd /usr/local/ng...

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...