最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用 Canvas實(shí)現(xiàn)繪畫一個(gè)未閉合的帶進(jìn)度條的圓環(huán)

  發(fā)布時(shí)間:2019-07-26 16:28:07   作者:ttentau   我要評(píng)論
這篇文章主要介紹了利用 Canvas實(shí)現(xiàn)繪畫一個(gè)未閉合的帶進(jìn)度條的圓環(huán),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
最終效果圖

一、定義變量

定義半徑,定義圓環(huán)厚度,定義圓心位置、定義默認(rèn)填充顏色

let radius = 75
let thickness= 10
let innerRadius = radius - thickness
let x = 75
let y = 75
var canvas = document.getElementById('tutorial');
var ctx = canvas.getContext('2d');
ctx.fillStyle = "#f2d7d7";

二、畫第一個(gè)圓弧

ctx.beginPath();
ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI)

注意 beginPath() 這個(gè)方法,生成路徑的第一步。本質(zhì)上,路徑是由很多子路徑構(gòu)成,這些子路徑都是在一個(gè)列表中,所有的子路徑(線、弧形、等等)構(gòu)成圖形。而每次這個(gè)方法調(diào)用之后,列表清空重置,然后我們就可以重新繪制新的圖形。
 

也就是說,這個(gè)方法可以用來給 Canvas圖像 分組,繪制新的圖形如果不調(diào)用此方法,那么新的圖形會(huì)和前面的圖形連接在一起

三、畫第一個(gè)連接處

ctx.quadraticCurveTo((x - innerRadius) - thickness / 2, y - thickness, x - innerRadius, y)

連接外是用二次貝塞爾曲線來畫的,Canvas的 quadraticCurveTo(cp1x, cp1y, x, y) 方法接受4個(gè)參數(shù),第一、二個(gè)參數(shù)為控制點(diǎn),第三、四個(gè)參數(shù)為結(jié)束點(diǎn)官方文檔
只需算出控制點(diǎn)和結(jié)束點(diǎn),就可以畫出一個(gè)圓弧

四、畫第二個(gè)圓弧

ctx.arc(x, y, innerRadius, Math.PI, Math.PI * 1.5, true)

注意方法后面最后一個(gè)參數(shù),設(shè)置為true,代表逆時(shí)針繪制(默認(rèn)是順時(shí)針)

五、畫第二個(gè)連接處

ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)

這一步其實(shí)和第三步相差不大,簡單的調(diào)換了下參數(shù)位置

六、填充

 ctx.fill();

至此,一個(gè)簡單的未閉合的圓環(huán)就完成了

畫第二個(gè)進(jìn)度條圓環(huán)

七、初始化

ctx.beginPath();
ctx.fillStyle = "#e87c7c";

beginPath 表示繪制新的圖形,如果不調(diào)用此方法,那后面畫的圖形會(huì)和前面畫的圖形連在一起

八、繪制第二個(gè)進(jìn)度條圓環(huán)

ctx.beginPath();
ctx.fillStyle = "#e87c7c";
ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI * 2)
ctx.quadraticCurveTo((x + innerRadius) + thickness / 2, y + thickness, x + innerRadius, y)
ctx.arc(x, y, innerRadius, Math.PI * 2, Math.PI * 1.5, true)
ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)

ctx.fill();

由于和第一個(gè)圓環(huán)繪制方式一模一樣,就不在重復(fù)了,區(qū)別僅僅是圓的弧度

九、旋轉(zhuǎn) Canvas

transform: rotate(-135deg);

由于css的旋轉(zhuǎn)比較方便,也省去了角度的計(jì)算,所以本人使用的是css的transform來旋轉(zhuǎn)的。當(dāng)然 Canvas 也提供了旋轉(zhuǎn)的方法

完整代碼

 

<!DOCTYPE html>
<html lang="cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>canvas</title>
    <style>
        .ring {
            width: 150px;
            height: 150px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            position: relative;
        }

        #tutorial {
            transform: rotate(-135deg);
            width: 150px; 
            height: 150px;
        }

        .fraction {
            position: absolute;
            font-size: 30px;
            font-weight: bold;
            color: red;
        }

        .small {
            font-size: 12px;
            font-weight: lighter;
        }

        .title {
            color: red;
            bottom: 0;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="ring">
        <canvas id="tutorial" width="150" height="150"></canvas>
        <span class="fraction">100 <span class="small">分</span> </span>
        <span class="title">服務(wù)分</span>
    </div>

    <script>
        let radius = 75
        let thickness = 10
        let innerRadius = radius - thickness
        let x = 75
        let y = 75
        var canvas = document.getElementById('tutorial');
        var ctx = canvas.getContext('2d');
        ctx.fillStyle = "#f2d7d7";

        ctx.beginPath();
        ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI)
        ctx.quadraticCurveTo((x - innerRadius) - thickness/2 , y - thickness, x - innerRadius, y)
        ctx.arc(x, y, innerRadius, Math.PI, Math.PI * 1.5, true)
        ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)
        ctx.fill();

        ctx.beginPath();
        ctx.fillStyle = "#e87c7c";
        ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI * 2)
        ctx.quadraticCurveTo((x + innerRadius) + thickness / 2, y + thickness, x + innerRadius, y)
        ctx.arc(x, y, innerRadius, Math.PI * 2, Math.PI * 1.5, true)
        ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)
        ctx.fill();
    </script>
</body>

</html>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

丹棱县| 辽阳市| 屯昌县| 宁津县| 定西市| 张家港市| 汽车| 五峰| 龙川县| 盱眙县| 祥云县| 洪江市| 浦东新区| 泾阳县| 雷州市| 东莞市| 铜陵市| 昭平县| 永福县| 邯郸市| 舒城县| 大港区| 商南县| 灵武市| 阳西县| 光泽县| 台南市| 三门峡市| 达尔| 深州市| 湖口县| 崇仁县| 宁陕县| 太仆寺旗| 当雄县| 宁晋县| 霍林郭勒市| 洞头县| 宝鸡市| 平顶山市| 濮阳市|