简体   繁体   English

背景进度栏画布

[英]Background Progress bar Canvas

My question is how to create background of progress bar with canvas like this image: 我的问题是如何使用如下图所示的画布创建进度条的背景:

在此处输入图片说明

I've already written code for it, but I think there's a better way, for example I'd like to know if I can do this code with one canvas or not: 我已经为它编写了代码,但是我认为有一种更好的方法,例如,我想知道是否可以在一个画布上执行此代码:

 $(document).ready(function () { var canvasAnimation = document.getElementById("animation"); var ctxAnimation = canvasAnimation.getContext("2d"); ctxAnimation.beginPath(); ctxAnimation.arc(75, 75, 65, 0, 2 * Math.PI); ctxAnimation.lineWidth = 10; ctxAnimation.strokeStyle = "#F3F3F3"; ctxAnimation.stroke(); var canvasBackground = document.getElementById("background"); var ctxBackground = canvasAnimation.getContext("2d"); ctxBackground.beginPath(); ctxBackground.arc(75, 75, 65, 1.2, 2 * Math.PI); ctxBackground.lineWidth = 10; ctxBackground.strokeStyle = "#1ABC9C"; ctxBackground.stroke(); }) 
 .my-container{ position:relative; width:150px; height:150px; margin:50px auto; } canvas{ position:absolute; top:0; left:0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="my-container"> <canvas id="background" width="150" height="150"></canvas> <canvas id="animation" width="150" height="150"></canvas> </div> 

Second problem: 第二个问题:

The second problem is I'd like to make my stroke have border-radius like image above 第二个问题是我想使笔触具有上图所示的border-radius

This is definitely possible using a single canvas. 使用单个画布绝对可以做到这一点。 You just need to remove the second canvas and its context, and replace any reference to ctxBackground with ctxAnimation. 您只需要删除第二个画布及其上下文,并用ctxAnimation替换对ctxBackground的任何引用。 This works because the Canvas API, like SVGs, use a painter's rendering model : 之所以可行,是因为Canvas API与SVG一样,使用了画家的渲染模型

Paint is applied in successive operations to the output device such that each operation paints over some area of the output device. 在连续的操作中将油漆施加到输出设备,以使每个操作都在输出设备的某些区域上进行油漆。 When the area overlaps a previously painted area the new paint partially or completely obscures the old. 当该区域与先前绘制的区域重叠时,新的油漆会部分或完全遮盖旧的油漆。

In your example if we draw the light grey circle first, and then the teal line, the teal line will be painted over the circle. 在您的示例中,如果我们先绘制浅灰色的圆圈,然后再绘制青绿色线,则将青绿色线绘制在该圆上。 By applying this technique, we get the following code: 通过应用此技术,我们得到以下代码:

const canvas = document.getElementById("animation");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(75, 75, 65, 0, 2 * Math.PI);
ctx.lineWidth = 10;
ctx.strokeStyle = "#F3F3F3";
ctx.stroke();

ctx.beginPath();
ctx.arc(75, 75, 65, 1.2, 2 * Math.PI);
ctx.lineWidth = 10;
ctx.strokeStyle = "#1ABC9C";
ctx.stroke();

As for enabling a "border-radius" effect, you can set the lineCap property to round: ctx.lineCap = "round"; 至于启用“边界半径”效果,可以将lineCap属性设置为round: ctx.lineCap = "round";

Here's a fiddle of the final code. 这是最终代码的小提琴。

You might want to check out some of the canvas documentation here. 您可能想在这里查看一些画布文档

Monica's answer is great, you could also achieve this using SVG's stroke-dashoffset and stroke-dasharray properties Monica的答案很好,您也可以使用SVG的stroke-dashoffsetstroke-dasharray属性来实现

Check out this pen for an example 查看此笔示例

This is built with two SVG circles, neither have fill, they only have stroke so their centre is blank, the bottom circle has it's stroke colored. 这是用两个SVG圆构建的,它们都没有填充,它们只有笔触,所以它们的中心是空白,底部的圆具有描边颜色。

When the fill button is clicked it adds a class of .filled to the top circle, it animates in the stroke using a dash that covers the entire circle, for a better explanation of how this works, read this article 单击填充按钮时, .filled在顶部圆上添加.filled类,它使用覆盖整个圆的破折号在笔画中进行动画处理,以更好地说明其工作原理, 请阅读本文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM