草庐IT

在其中心旋转帆布弧

程序员大本营 2024-04-07 原文

我有一个带有弧形和一些标签的画布。

这是小提琴链接 - 小提琴 以下是代码:

    var canvas = document.getElementById("myCanvas"),    ctx = canvas.getContext("2d"),    x = canvas.width / 2,    y = canvas.height / 2,    radius = 100;ctx.lineWidth = 2;var numberofArcs = 10,    sengmentWidth = 1.5 * Math.PI / numberofArcs,    pieAngle = 1.5 * Math.PI / numberofArcs;console.log(pieAngle);var labeltext = '',    font = 16,    hightlight = 1;drawSegments(radius, font, hightlight);ctx.translate(x, y);ctx.rotate(135 * Math.PI);ctx.translate(-x, -y);function drawSegments(radius, font, highlight) {    var offset = 0;    for (var i = 0; i < numberofArcs; i++) {        (i<=8) ? offset = 3 : offset = 8;                ctx.save();        ctx.beginPath();        ctx.moveTo(x, y);        ctx.arc(x, y, radius, i * pieAngle, (i + 1) * pieAngle);        var hueValue = i * 15;        ctx.fillStyle = 'hsl(' + hueValue + ',70%, 60%)';        ctx.fill();        ctx.lineTo(x, y);        ctx.lineWidth = 3;        ctx.strokeStyle = '#f3f5f6';        ctx.stroke();        ctx.beginPath();        ctx.moveTo(x, y);        ctx.arc(x, y, radius - 10, i * pieAngle, (i + 1) * pieAngle);        ctx.fillStyle = '#f3f5f6';        ctx.fill();        ctx.lineTo(x, y);        ctx.lineWidth = 0;        labeltext = i + 1;        ctx.font = '16px bold white';        var width = ctx.measureText(labeltext).width;        ctx.fillStyle = '#CCC';        if ((i + 1) == highlight) {            ctx.textBaseline = 'middle';            console.log(offset);            ctx.beginPath();            ctx.moveTo(x + offset +(radius + 30) * Math.cos(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2), y + (radius + 30) * Math.sin(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2));            ctx.arc(x + offset + (radius + 30) * Math.cos(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2), y + (radius + 30) * Math.sin(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2), 10, 0,  2 * Math.PI);            ctx.fillStyle = 'red';            ctx.fill();            ctx.fillStyle = "#000";            ctx.fillText(labeltext, x + (radius + 30) * Math.cos(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2), y + (radius + 30) * Math.sin(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2));        } else {            ctx.fillStyle = "black";            ctx.fillText(labeltext, x + (radius + 30) * Math.cos(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2), y + (radius + 30) * Math.sin(i * pieAngle + ((i + 1) * pieAngle - i * pieAngle) / 2));        }    }}
canvas {    border: 1px dotted black;    color: black;}
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>

我要实现的是将整个弧线与标签一起旋转135度。我尝试了此代码,但它不起作用:

ctx.translate(x, y);ctx.rotate(135 * Math.PI/180);ctx.translate(-x, -y);

其中x和y是画布中心点。任何帮助,将不胜感激。PS:我是画布的新手!

看答案

由于这是对立的,因此旋转时,很容易将我们绘制的段的角度添加到我们要旋转圆的数量的角度。

有很多无需做的代码,因此可以将代码削减以使您更易于管理。

var canvas = document.getElementById("myCanvas");var ctx = canvas.getContext("2d");var x = canvas.width / 2;var y = canvas.height / 2;const radius = 100;const numberofArcs = 10;const sengmentWidth = 1.5 * Math.PI / numberofArcs;const pieAngle = 1.5 * Math.PI / numberofArcs;const spacePixels = 4;const spaceRadians = spacePixels / radius;const fontHeight = 16;const textOffset = 30;var hightlight = 1;var angle = 135 * (Math.PI / 180);drawSegments(x, y, radius, hightlight, angle);function drawSegments(x, y, radius, highlight, angle) {    ctx.textBaseline = 'middle';    ctx.textAlign = "center";    ctx.font = fontHeight + 'px bold white';    // move center to x,y pos    ctx.setTransform(1,0,0,1,x,y);    x = y = 0;    // draw the light gray pie and border    var offset = 0;    ctx.beginPath();    ctx.moveTo(0,0);    ctx.arc(0,0,radius + spacePixels, angle, angle + numberofArcs * pieAngle);    ctx.lineTo(0,0); // or could use closePath    ctx.fillStyle = '#f3f5f6';    ctx.strokeStyle = '#e3e5e6';    ctx.lineWidth = 4;    ctx.stroke();    ctx.fill();        // for each segment draw coloured arc and text    for (var i = 0; i < numberofArcs; i++) {        var pA = pieAngle * i; // angle of start of pie pA;        var tDir  = (i + 0.5) * pieAngle;  // angle of text        pA += angle;  // add the rotated angle        tDir += angle;        var dist = radius + textOffset; // text distance from center        var sR = spaceRadians; // the spacnig between segments                ctx.strokeStyle = '#d3d5d6';  // for adding outline to coloured arcs        ctx.lineWidth = 4;  // outline width is half this amount        ctx.fillStyle = 'hsl(' + (i*15) + ',70%, 60%)';        ctx.beginPath();        ctx.arc(x, y, radius, pA + sR, pA + pieAngle -sR);  // outside CW        ctx.arc(x, y, radius - 10,pA + pieAngle -sR, pA + sR,true);  // then inside CCW        ctx.stroke();        ctx.fill();        if ((i + 1) === highlight) {                        ctx.fillStyle = 'red';            ctx.beginPath();            ctx.arc(dist * Math.cos(tDir), dist  * Math.sin(tDir), 10, 0,  2 * Math.PI);            ctx.fill();        }        ctx.fillStyle = "black";        ctx.fillText(""+(i + 1), Math.cos(tDir) * dist, Math.sin(tDir) * dist);     }}
canvas {    border: 1px dotted black;    color: black;}
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>

有关在其中心旋转帆布弧的更多相关文章

  1. 旋转矩阵的几何意义 - 2

    点向量坐标矩阵的几何意义介绍旋转矩阵的几何含义之前,先介绍一下点向量坐标矩阵的几何含义点:在一维空间下就是一个标量,如同一条直线上,以任意某一个位置为0点,以一定的尺度间隔为1,2,3...,相反方向为-1,-2,-3...;如此就形成了一维坐标系,这时候任何一个点都可以用一个数值表示,如点p1=5,即即从原点出发沿着x轴正方向移动5个尺度;点p2=-3,负方向移动3个尺度;     在一维坐标系上过原点做垂直于一维坐标系的直线,则形成了二维坐标系,此时描述一个点需要两个数值来表示点p3=(3,2),即从原点出发沿着x轴正方向移动3个尺度,在此基础上沿着y轴正方向移动两个尺度的位置就是点p3。

  2. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  3. ruby-on-rails - 如何以递归方式将 YAML 文件扁平化为 JSON 对象,其中键是点分隔的字符串? - 2

    例如,如果我有YAML文件en:questions:new:'NewQuestion'other:recent:'Recent'old:'Old'这最终会变成一个json对象,例如{'questions.new':'NewQuestion','questions.other.recent':'Recent','questions.other.old':'Old'} 最佳答案 由于问题是关于在Rails应用程序上使用YAML文件进行i18n,因此值得注意i18ngem提供了一个辅助模块I18n::Backend::Flatten完全像

  4. ruby - 使用 Ruby CSV 创建 Rails 记录,其中字符串字段不可查询 - 2

    我正在尝试将种子数据从CSV文件加载到我的Rails应用程序中。我最初安装了fastercsvgem,却发现从ruby​​1.9开始,fastercsv已被弃用,取而代之的是CSV库。所以在收到一个非常有用的错误告诉我切换后,我切换到CSV。然而,现在我遇到了最奇怪的现象,当我加载数据时一切看起来都很正常,但我似乎无法查询字符串字段。字符串字段由看似正确的字符串填充,但我无法访问它们。我可以查询任何数字字段,结果将返回,但不会返回字符串字段。我尝试使用引号的定界符,但无济于事。我什至从我的csv文件中删除了所有引号,但我仍然无法查询字符串字段。下面是我的代码,以及一些来自Rails控制

  5. ruby - Rails 3.2 CRUD : . 其中 'or' 有条件 - 2

    使用ruby​​onrails,我想做类似的事情:@tasks=Task.where(:def=>true||:house_id=>current_user.house_id)执行此操作的最有效/最干净的方法是什么? 最佳答案 你可以这样做:Task.where("def=?orhouse_id=?",true,current_user.house_id)一般情况是:Model.where("column=?orother_column=?",value,other_value)您还可以利用Arel:t=Task.arel_tabl

  6. 欧拉角、旋转矩阵及四元数 - 2

    欧拉角、旋转矩阵及四元数1.简介2.欧拉角2.1欧拉角定义2.2右手系和左手系2.3转换流程3.旋转矩阵4.四元数4.1四元数与欧拉角和旋转矩阵之间等效变换4.2测试Matlab代码5.总结1.简介常用姿态参数表达方式包括方向余弦矩阵、欧拉轴/角参数、欧拉角、四元数以及罗德里格参数等。高分辨率光学遥感卫星主要采用欧拉角与四元数对姿态参数进行描述。这里着重讲解欧拉角、旋转矩阵和四元数。2.欧拉角2.1欧拉角定义欧拉角是表征刚体旋转的一种方法之一,由莱昂哈德·欧拉引入的三个角度,用于描述刚体相对于固定坐标系的方向。在摄影测量、空间科学或其它技术领域,一般用一组(三个)欧拉角描述两个空间坐标之间的旋

  7. Ruby:是否可以在不对类名进行硬编码的情况下确定我的 Ruby 方法在其中执行的类? - 2

    我是Ruby的Nuby。我正在寻找一种方法来获取当前执行行的方法的包含类对象。如果不对类名进行硬编码,这可能吗?#hardcodedexampleclassAdefto_s"Iama"+A.to_s#Class"A"ishardcodedhere.IsthereanotherwaytoreferencetheclassA?endend我想也许self.class会起作用,但是当类被子类化时,它并没有给我我想要的东西。#FollowingOutputs=>IamaCamelIamaCamelIamaCamel#butIwant=>IamaCamelIamaMammalIamaAnimal

  8. arrays - 给定一个大小为 y 的数组,其中包含大小为 n 的数组,我如何使用 Ruby 返回所有逻辑组合? - 2

    我想做的是处理n个集合,而我在下面提供的代码正好处理4个集合。defshow_combinations@combos=[]['A','noA'].eachdo|a|['B','noB'].eachdo|b|['C','noC'].eachdo|c|['D','noD'].eachdo|d|@combos我如何重构以下代码来处理以下场景:鉴于我有一个大小为y的数组,其中包含大小为n的数组,我想返回所有组合。请务必注意,每个子数组中只能有一个项目出现在结果中。(如“已完成资料”不能同时出现在“未完成资料”的结果中)背景:用户可能有一些任务:例如,“完成配置文件”或“设置电子邮件”或其他任何

  9. Ruby:是否可以设置实例变量的值,其中实例变量通过字符串命名? - 2

    不确定这个模式叫什么,但场景是这样的:classSome#thisclasshasinstancevariablescalled@thing_1,@thing_2etc.end有没有办法设置实例变量的值,其中实例变量名是由字符串创建的?类似于:i=2some.('thing_'+i)=55#setsthevalueofsome.thing_2to55 最佳答案 在Object上搜索“instance_variable”:some.instance_variable_get(("@thing_%d"%2).to_sym)some.in

  10. ruby-on-rails - HTTP POST 中的多个键/值对,其中键是相同的名称 - 2

    我正在开发一个接受来自远程客户端的数据的API,其中一些HTTPPOST中的键几乎用作数组。在英语中,这意味着我的服务器上有一个名为“类”的资源。从这个意义上讲,类(class)是学生上课和老师类的类型。当用户提交HTTPPOST为他们的应用程序创建新类(class)时,许多键值对如下所示:学生姓名:鲍勃·史密斯学生姓名:简·史密斯学生姓名:克里斯·史密斯如果我的服务器是RubyonRails,那么在客户端(假设客户端是cURL或ActiveResource,等等)处理这个问题的最佳方法是什么?在服务器端处理这个问题的合适方法是什么应用程序?需要一种方法来允许多个键具有相同的名称并且没

随机推荐