简体   繁体   English

使用javascript在1个点上绘制自定义数量的直线

[英]Drawing a custom number of straight lines across 1 point with javascript

I'm trying to develop a small application using html5 and canvas/KineticJS.我正在尝试使用 html5 和 canvas/KineticJS 开发一个小型应用程序。 I'd like to trace a number of rays that start from a 2d point to infinite, just setting a custom angle degree.我想跟踪从 2d 点开始到无限的许多光线,只需设置自定义角度。 For example, if I set 90° the app should render four rays (two straight lines, one vertical and one horizontal that meet in my 2d point).例如,如果我设置 90°,应用程序应该渲染四条光线(两条直线,一条垂直线和一条水平线在我的 2d 点上相交)。 If I set 60° I should see 3 straight lines, like an asterisk *如果我设置 60°,我应该看到 3 条直线,就像一个星号 *

The longest line you'll ever have to draw is the size of the canvas's diagonal:您必须绘制的最长线是画布对角线的大小:

var r = Math.sqrt(Math.pow(canvas.width, 2) + Math.pow(canvas.height, 2));

Use sin and cos to calculate each of your end points at that radius:使用sincos计算该半径处的每个端点:

var theta = delta * Math.PI / 180.0;
var dx = r * Math.cos(n * theta);
var dy = r * Math.sin(n * theta);

Then, just draw lines from (x, y) to (x + dx, y + dy).然后,只需绘制从 (x, y) 到 (x + dx, y + dy) 的线。 Simples.简单。

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

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