简体   繁体   English

用画布翻译元素

[英]Translating an element with canvas

I'm trying to learn canvas by implementing a pie chart. 我正在尝试通过实现饼图来学习画布。 I've managed to parse my data, draw the slices, and calculate the center of each arc, as noted by the black circles. 我已设法解析我的数据,绘制切片,并计算每个弧的中心,如黑色圆圈所示。 But now I'm trying to draw one of the slices as though it had been "slid out". 但是现在我试图绘制其中一个切片,就好像它已经“滑出”一样。 Not animate it (yet), just simply draw the slice as though it had been slid out. 没有动画它(还),只是简单地绘制切片,好像它已经滑出一样。

I thought the easiest way would be to first calculate the point at which the new corner of the slice should be (free-hand drawn with the red X), translate there, draw my slice, then translate the origin back. 我认为最简单的方法是首先计算切片的新角应该是的点(用红色X自由绘制),在那里平移,绘制我的切片,然后将原点转换回来。 I thought I could calculate this easily, since I know the center of the pie chart, and the point of the center of the arc (connected with a free-hand black line on the beige slice). 我想我可以很容易地计算出来,因为我知道饼图的中心和圆弧中心的点(用米色切片上的自由黑线连接)。 But after asking this question , it seems this will involve solving a system of equations, one of which is second order. 但在提出这个问题后 ,似乎这将涉及求解一个方程组,其中一个是二阶。 That's easy with a pen and paper, dauntingly hard in JavaScript. 使用笔和纸很容易,JavaScript很难实现。

Is there a simpler approach? 有更简单的方法吗? Should I take a step back and realize that doing this is really the same as doing XYZ? 我应该退后一步,意识到这样做与XYZ一样吗?

I know I haven't provided any code, but I'm just looking for ideas / pseudocode. 我知道我没有提供任何代码,但我只是在寻找创意/伪代码。 (jQuery is tagged in the off chance there's a plugin will somehow help in this endeavor) (jQuery被标记为有机会有一个插件将以某种方式帮助这项努力)

在此输入图像描述

Merely translating an element on a canvas is very easy and there shouldn't be any tricky equations here. 仅仅在画布上翻译元素非常容易,这里不应该有任何棘手的方程式。 In the most basic sense it is: 从最基本的意义上讲,它是:

ctx.save();
ctx.translate(x, y);
// Draw the things you want offset by x, y
ctx.restore();

Here's a rudimentary example of a square pie and the same pie with one of the four "slices" translated: 这是一个方形饼的基本例子,以及翻译过的四个“切片”之一的相同馅饼:

http://jsfiddle.net/XqwY2/ http://jsfiddle.net/XqwY2/

To make the pie piece "slide out" the only thing you need to calculate is how far you want it to be. 为了使饼图“滑出”,你需要计算的唯一事情就是你想要的程度。 In my simple example the blue block is slid out 10, -10 . 在我的简单示例中,蓝色块滑出10, -10

If you are wondering merely how to get the X and Y you want in the first place, well, that's not quite a javascript/canvas question. 如果你想知道如何获得你想要的X和Y,那么,这不是一个javascript / canvas问题。 For points on a line given a distance this question: Finding points on a line with a given distance seems the most clear 对于给定距离的线上的点,这个问题: 在具有给定距离的线上找到点似乎是最清楚的

Edit , here you are (from comments): 编辑 ,在这里(来自评论):

  // Center point of pie
  var x1 = 100;
  var y1 = 100;

  // End of pie slice (your black dot)
  var x2 = 200;
  var y2 = 0;

  // The distance you want
  var distance = 3;

  var vx = x2 - x1; // x vector
  var vy = y2 - y1; // y vector
  var mag = Math.sqrt(vx*vx + vy*vy); // length
  vx = mag/vx;
  vy = mag/vy;

  // The red X location that you want:
  var px = x1 + vx * ( distance);
  var py = y1 + vy * ( distance);

This would give you a px,py of (104.24, 95.76) for my made-up inputs. 这将给你一个px,py为(104.24, 95.76)我的(104.24, 95.76)输入。

Getting the x and y of the translation is easy enough. 获得翻译的xy很容易。

// cx and cy are the coordinates of the centre of your pie
// px and py are the coordinates of the black circle on your diagram
// off is the amount (range 0-1) by which to offset the arc
//      adjust off as needed.
// rx and ry will be the amount to translate by

var dx = px-cx, dy = py-cy,
    angle = Math.atan2(dy,dx),
    dist = Math.sqrt(dx*dx+dy*dy);
rx = Math.cos(angle)*off*dist;
ry = Math.sin(angle)*off*dist;

Plug that into the code Simon Sarris gave you and you're done. 将其插入Simon Sarris给你的代码中,你就完成了。 I'd suggest an off value of 0.25. 我建议off值为0.25。

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

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