- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理 R gauge
小部件定制。使用 Canvas 属性,我必须将我的 Gauge
针尺寸从中心销减小到起始角。
this.drawNeedle = this.DrawNeedle = function (value, color, index) {
var type = prop['chart.needle.type'];
co.lineWidth = 0.5;
co.strokeStyle = 'gray';
co.fillStyle = color;
var angle = (this.endAngle - this.startAngle) * ((value - this.min) / (this.max - this.min));
angle += this.startAngle;
if (typeof (prop['chart.needle.size']) == 'object' && prop['chart.needle.size'] && typeof (prop['chart.needle.size'][index]) == 'number') {
var size = prop['chart.needle.size'][index];
} else if (typeof (prop['chart.needle.size']) == 'number') {
var size = prop['chart.needle.size'];
} else {
var size = this.radius - 25 - prop['chart.border.width'];
}
if (type == 'line') {
co.beginPath();
co.lineWidth = prop['chart.needle.width'];
co.strokeStyle = color;
co.arc(this.centerx, this.centery, 82, angle, angle + 0.0001, false);
// co.moveTo(this.centerx-58, this.centery-58);
// co.lineTo(this.centerx-43, this.centery-43);
co.lineTo(this.centerx, this.centery);
if (prop['chart.needle.tail']) {
co.arc(this.centerx, this.centery, this.radius * 0.2, angle + RG.PI, angle + 0.00001 + RG.PI, false);
}
co.lineTo(this.centerx, this.centery);
co.stroke();
} else {
co.beginPath();
co.arc(this.centerx, this.centery, size, angle, angle + 0.00001, false);
co.arc(this.centerx, this.centery, this.centerpinRadius * 0.5, angle + RG.HALFPI, angle + 0.00001 + RG.HALFPI, false);
if (prop['chart.needle.tail']) {
co.arc(this.centerx, this.centery, this.radius * 0.2, angle + RG.PI, angle + 0.00001 + RG.PI, false);
}
co.arc(this.centerx, this.centery, this.centerpinRadius * 0.5, angle - RG.HALFPI, angle - 0.00001 - RG.HALFPI, false);
co.stroke();
co.fill();
this.angle = angle;
}
};
而不是大针(从中心到起始角),针的尺寸应该小,类似于下图。
最佳答案
请阅读我在代码中的注释
const canvas = document.getElementById("canvas");
const co = canvas.getContext("2d");
let cw = canvas.width = 200,
cx = cw / 2;
let ch = canvas.height = 200,
cy = ch / 2;
//gauge starts at a1
let a1 = 120*Math.PI/180;
//gauge ends at a1
let a2 = 60*Math.PI/180;
// arrow angle
let a = -60*Math.PI/180;
//gauge angle
let R = 82;
// arrow starts at this distance from the center
let r = 70;
// the center
let c = {x:100,y:100}
// coords to draw the arrow
let x1 = c.x+r*Math.cos(a);
let y1 = c.x+r*Math.sin(a);
let x2 = c.x+(R+5)*Math.cos(a);
let y2 = c.x+(R+5)*Math.sin(a);
co.lineWidth = 5;
co.strokeStyle = 'white';
co.lineCap = "round";
//draw the gauge
co.beginPath();
co.arc(c.x, c.y, R, a1, a2);
co.stroke();
//draw the arrow
co.beginPath();
co.moveTo(x1, y1);
co.lineTo(x2, y2);
co.stroke();
body {
background-color: #222;
}
canvas {
border:1px solid #999;
}
<canvas id="canvas"></canvas>
OP 评论说:指针应该指向将动态更新的比例值。这就是我动态执行的方式:
我正在添加一个输入类型范围以动态更改角度值
const canvas = document.getElementById("canvas");
const co = canvas.getContext("2d");
let cw = (canvas.width = 200),
cx = cw / 2;
let ch = (canvas.height = 200),
cy = ch / 2;
// arrow angle
let a = aval.getAttribute("value") * Math.PI / 180;
//gauge starts at a1
let a1 = aval.getAttribute("min") * Math.PI / 180;
//gauge ends at a1
let a2 = aval.getAttribute("max") * Math.PI / 180;
//gauge angle
let R = 82;
// arrow starts at this distance from the center
let r = 70;
// the center
let c = { x: 100, y: 100 };
co.lineWidth = 5;
co.strokeStyle = "white";
co.lineCap = "round";
// a function to draw the gauge
function drawGauge() {
//draw the gauge
co.beginPath();
co.arc(c.x, c.y, R, a1, a2);
co.stroke();
}
// a function to draw the arrow
function drawArrow(a) {
// coords to draw the arrow
let x1 = c.x + r * Math.cos(a);
let y1 = c.x + r * Math.sin(a);
let x2 = c.x + (R + 5) * Math.cos(a);
let y2 = c.x + (R + 5) * Math.sin(a);
//draw the arrow
co.beginPath();
co.moveTo(x1, y1);
co.lineTo(x2, y2);
co.stroke();
}
drawGauge();
drawArrow(a);
aval.addEventListener("input", () => {
// on input the angle of the arrow is changing
let a = parseInt(aval.value) * Math.PI / 180;
// clear the canvas
co.clearRect(0, 0, cw, ch);
// and redraw everything
drawGauge();
drawArrow(a);
});
body {
background-color: #222;
}
canvas {
border:1px solid #999;
}
<canvas id="canvas"></canvas>
<input id="aval" type="range" min="-240" max="60" value="-60" />
OP 正在评论:
my min value is 40 and max is 700..... So I need to show the needle based on the value between these value range.
为了做到这一点,我需要将角度映射到这些值,所以我添加了一个映射函数并更新了代码:
const canvas = document.getElementById("canvas");
const co = canvas.getContext("2d");
let cw = (canvas.width = 200),
cx = cw / 2;
let ch = (canvas.height = 200),
cy = ch / 2;
let min = parseInt(aval.getAttribute("min"));// min value
let a_min = -240 * Math.PI / 180;// min angle to draw the gauge
let max = parseInt(aval.getAttribute("max"));// max value
let a_max = 60 * Math.PI / 180;// max angle to draw the gauge
let val = parseInt(aval.getAttribute("value"));
let angle = getAngle(val) * Math.PI / 180;
//gauge angle
let R = 82;
// arrow starts at this distance from the center
let r = 70;
// the center
let c = { x: 100, y: 100 };
co.lineWidth = 5;
co.strokeStyle = "white";
co.lineCap = "round";
// a function to draw the gauge
function drawGauge() {
//draw the gauge
co.beginPath();
co.arc(c.x, c.y, R, a_min, a_max);
co.stroke();
}
// a function to draw the arrow
function drawArrow(a) {
// coords to draw the arrow
let x1 = c.x + r * Math.cos(a);
let y1 = c.x + r * Math.sin(a);
let x2 = c.x + (R + 5) * Math.cos(a);
let y2 = c.x + (R + 5) * Math.sin(a);
//draw the arrow
co.beginPath();
co.moveTo(x1, y1);
co.lineTo(x2, y2);
co.stroke();
}
drawGauge();
drawArrow(angle);
aval.addEventListener("input", () => {
// on input the angle of the arrow is changing
let val = parseInt(aval.value);
let angle = getAngle(val);
// clear the canvas
co.clearRect(0, 0, cw, ch);
// and redraw everything
drawGauge();
drawArrow(angle);
});
//a p5.js function
function map5(n, a, stop1, start2, stop2, withinBounds) {
var newval = (n - a) / (stop1 - a) * (stop2 - start2) + start2;
if (!withinBounds) {
return newval;
}
if (start2 < stop2) {
return this.constrain(newval, start2, stop2);
} else {
return this.constrain(newval, stop2, start2);
}
};
function constrain(n, low, high) {
return Math.max(Math.min(n, high), low);
};
function getAngle(val){
return map5(val, min, max, a_min, a_max)
}
body {
background-color: #222;
}
canvas {
border:1px solid #999;
}
<canvas id="canvas"></canvas>
<input id="aval" type="range" min="40" max="700" value="460" />
关于html - 如何使用 Canvas 减少从中心到起始角度的线条大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870095/
我在尝试生成具有“价格”轴和“量”轴的图表时遇到问题,类似于 example given 中的图表。在 Highstock 网站上。它可以很好地显示成交量轴,但不能显示价格。 在尝试确定问题的原因时,
起始 - HTML
在我的 HTML 项目中,我试图提及 标签。但是,VS Code 将其解释为实际的 标签,它会导致奇怪的事情发生。有人有办法解决这个问题吗?预先感谢您! 最佳答案 使用<代替 . 顺便说一下,使
起始 - HTML
在我的 HTML 项目中,我试图提及 标签。但是,VS Code 将其解释为实际的 标签,它会导致奇怪的事情发生。有人有办法解决这个问题吗?预先感谢您! 最佳答案 使用<代替 . 顺便说一下,使
The allocation function attempts to allocate the requested amount of storage. If it is successful, i
这是我的Program.cs: public static void Main(string[] args) { var host = new WebHostBuilder()
我希望我的应用程序独立于操作系统。因此,我的 config.properties 和日志文件存储在资源文件夹中,我通过相对路径获取这些资源。这是我的项目结构。 这是我的 AppConfig 类: pu
(前言:这是我在 Stack Overflow 上提出的第一个与音频相关的问题,因此我会尽力用最好的措辞来表达。欢迎编辑。) 我正在创建一个允许用户循环播放音乐的应用程序。目前,我们的原型(proto
我有一个 Pandas DataFrame,我想将其用作 Scrapy Start URL,函数 get_links 打开一个到 DataFrame 的 xlsx,其中有一个我想在其上运行蜘蛛的 Co
我有几个大的 DTD 文件。我用过 trang将它们转换为 XSD 文件,这样我就可以轻松地从 JAXB 和其他实用程序中使用它。但是,生成的 XSD 文件的所有声明元素都位于顶层。这意味着任何元素都
是否有任何工具可以将文件从给定的起始偏移量复制到给定的(结束)偏移量。我还想通过运行 md5sum 确认该工具已正确复制指定的字节。像这样的东西 1) Copy source file star
所以,我有一个程序,我可以使用 Path2D 对象将形状添加到 JPanel,然后我可以单击并拖动它们。我想要做的是能够找到药物后形状的最终 X 和 Y 坐标。坐标必须是左上角坐标。有什么想法吗? /
我是一名优秀的程序员,十分优秀!