html - 如何将颜色设置为圆形画布的边框
问题描述
我使用画布 2D 在 QML 中创建了圆圈。半圈有浅绿色和另一半天蓝色,但现在我不知道如何为这个圈设置边框颜色。有人知道如何为这个圆圈的边框设置颜色吗?
Canvas {
anchors.fill: parent
onPaint: {
var ctx = getContext("2d");
ctx.reset();
var centreX =900;
var centreY = 150;
ctx.beginPath();
ctx.fillStyle = "#16AA55"; //green
ctx.moveTo(centreX, centreY);
ctx.arc(centreX, centreY,100, Math.PI * 0.01, Math.PI * 1, false);
ctx.lineTo(centreX, centreY);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "#26A7CA";
ctx.moveTo(centreX, centreY);
ctx.arc(centreX, centreY, 100, Math.PI * 1, Math.PI * 2.01, false);
ctx.lineTo(centreX, centreY);
ctx.fill();
}
解决方案
您需要该strokeStyle
属性来执行此操作。在将形状绘制到画布上之前,将其设置为您想要的任何颜色。更多关于这个
下面是一个片段,它绘制了两个具有不同边框颜色的圆圈。
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.strokeStyle = "#FF0000";
ctx.stroke();
ctx.beginPath();
ctx.arc(210, 75, 50, 0, 2 * Math.PI);
ctx.strokeStyle = "blue";
ctx.stroke();
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
希望这可以帮助 :)
推荐阅读
- php - Laravel - 登录后永久使用语言
- python - 在日期时间中格式化月份名称缩写(Jan/Feb/...)
- git - 即使更改了 github 存储库,网站内容也没有更新
- php - 使用带有纯 php 代码的 php (PHPOffice/PHPPresentation) 的 Ppt 到图像
- oop - 面向数据处理管道的 OOP 设计
- javascript - 从js中获取值并作为参数传递
- git - 未跟踪文件的 git diff 与具有相同文件的分支的 git diff 显示差异
- javascript - 将日期字符串从 json 转换为 UI 的最佳实践
- c++ - 修改后的 LinkedList 不在文本文件本身中更新
- c# - 用 Observable.Create 包裹 Observable.FromEventPattern