2011-04-17 53 views
0

我想了解HTML5画布一些和JavaScript的y位置,我想创建这些典型插画的太阳射线:计算X,帆布点

enter image description here

但我的问题是,我想将其自动化并使其全屏。

要计算中间点的坐标不难,这是我无法控制的外部点。

K,所以这就是我得到的。 问题在于为外部坐标创建数组的for循环。

因此它从屏幕中心开始计算。 如果它是第一个点(我们现在忽略内点),它将使用x_coordinate变量(它是屏幕的水平中心)并将width_between_rays除以2(因为我想模仿上面的图片,两条较高的光线)。

其余的点被检查,如果他们除以二,看看我是否应该添加width_between_rays(应该可能偏移或某事)或width_of_rays到最后点坐标。

好吧,这看起来很直接,但由于窗口大小不是一个固定的大小,我需要某种方式来计算点应该在哪里,例如;点的位置超出屏幕的宽度/高度。 所以我的计算方式不起作用(我认为)。

无论如何,有人(谁显然比我聪明)指向我在正确的方向吗?

function sun_rays(z_index, element, color, number_of_rays, width_of_rays, width_between_rays) { 
     // Start the canvas stuff 
     var canvas = document.getElementById(element); 
     var ctx = canvas.getContext("2d"); 
     console.log(); 
     ctx.canvas.width = $(window).width(); 
     ctx.canvas.height = $(window).width(); 
     ctx.fillStyle = color; 

     // calculate the window size and center position 
     var window_width = $(window).width(); 
     var window_hight = $(window).height(); 
     var x_coordinate = window_width/2; 
     var y_coordinate = window_hight/2; 

     // create an array for the center coordinates 
     var center_coordinate_array = new Array(); 
     for(i=0; i < number_of_rays; i++){ 
      center_coordinate_array[i] = new Array(x_coordinate, y_coordinate); 
     } 

     // create an array for the outer coordinates 
     var outer_coordinate_array = new Array(); 
     for(i=1; i == number_of_rays*2; i++){ 

      if(i == 1) { 
       // X 
       var last_outer_x_coordinate = x_coordinate + (width_between_rays/2); 
       // Y 
       if(last_outer_x_coordinate < window_width) { 
        last_outer_y_coordinate = last_outer_y_coordinate; 
       } else { 
        $x_coordinate_difference = last_outer_x_coordinate - window_width; 
        last_outer_y_coordinate = x_coordinate_difference; 
       } 

       center_coordinate_array[i] = new Array(last_outer_x_coordinate, last_outer_y_coordinate); 
      } else { 
       if(i % 2 == 0) { 
        // X 
        last_outer_x_coordinate = last_outer_x_coordinate + width_of_rays; 
        // Y 
        //calculate the y position 

        center_coordinate_array[i] = new Array(last_outer_x_coordinate); 
       } else { 
        // X 
        last_outer_x_coordinate = last_outer_x_coordinate + width_between_rays; 
        // Y 
        //calculate the y position 

        center_coordinate_array[i] = new Array(last_outer_x_coordinate); 
       } 
      } 

     } 
    } 

回答

1

好像你应该使用三角函数做这样的事情。

var coordinate_array = []; 
var xCoord = 0; 
var yCoord = 0; 
var angleIncrement = 15; 
var i = 0; 

//iterate over angles (in degrees) from 0 to 360 
for (var theta = 0; theta < 360; theta += angleIncrement) { 
    //angle is in sector from bottom right to top right corner 
    if (theta >= 315 || theta <= 45) 
    { 
     xCoord = $(window).width();//point on right side of canvas 
     yCoord = abs($(window).width()/2 * tan(theta)); 
     yCoord = tranformY(theta,yCoord); 
    } 
    //angle is in sector from top right to top left corner 
    else if (theta > 45 && theta <= 135) 
    { 
     yCoord = 0; //top is zero 
     xCoord = abs($(window).height()/2 * tan(theta)); 
     xCoord = transformX(theta, xCoord); 
    } 
    //angle is in sector from top left to bottom left corner 
    else if (theta > 135 && theta <= 225) 
    { 
     xCoord = 0; //left edge on a canvas is zero 
     yCoord = abs($(window).width()/2 * tan(theta); 
     yCoord = transformY(theta, yCoord); 
    } 
    //angle is in sector from bottom left to bottom right corner 
    else // theta > 225 && theta < 315 
    { 
     yCoord = $(window).height(); 
     xCoord = abs($(window).height()/2 * tan(theta)); 
     xCoord = transformX(theta, xCoord); 
    } 
    coordinate_array[i++] = new Array(xCoord, yCoord);   
} 

//Transform from cartesian coordinates to top left is 0,0 
function tranformY(theta, y) 
{ 
    var centerYCoord = $(window).height()/2; 
    //if angle falls in top half (Quadrant 1 or 2) 
    if(theta > 0 && theta < 180) 
    { 
    return centerYCoord - y; 
    } 
    elseif(theta > 180 && theta < 360) 
    { 
    return centerYCoord + y; 
    } 
    //coord falls on 0/360 or 180 (vert. median) 
    return centerYCoord; 
} 

//Transform from cartesian coordinates to top left is 0,0 
function transformX(theta, x) 
{ 
    var centerXCoord = $(window).width()/2; 
    //if angle falls in right half (Quadrant 1 or 4) 
    if(theta > 270 || theta < 90) 
    { 
    return centerXCoord + x; 
    } 
    elseif(theta > 90 && theta < 270)  
    { 
    return centerXCoord - x; 
    } 
    //coordinate falls on 270 or 90 (center) 
    return centerXCoord; 
} 

//now draw your rays from the center coordinates to the points in coordinate_array 
//NOTE: This code will need to be cleaned up - I just wrote it in the textbox. 
+0

我似乎无法找出代码,或者它实际上做了什么。 :/所以这里是一个小提琴,所以你可以看到我做了什么。也许你可以帮我:) http://jsfiddle.net/Fnf3u/ – Patrik 2011-04-18 09:18:27

+0

我刚更新了一些东西:http://jsfiddle.net/Fnf3u/3/ – Patrik 2011-04-18 09:31:23

+0

我不相信小提琴。这是我的页面上的工作画布http://canvas.patrikelfstrom.se它使用相同的HTML,CSS和一些js – Patrik 2011-04-18 09:37:58

0

Coordintate Points

前面的代码把坐标为红色点到一个数组。

这个问题的本质是与角度的增量变化有关。您的解决方案需要使用trig函数处理角度。

+0

我想通了,所以我试图将该数组与中心点的坐标数组合并打印出来,但结果是黑色的。 – Patrik 2011-04-18 13:27:22

+0

我看到我的代码存在问题。当乘以tan()时,我正在使用转换后的坐标,并且它正在搞砸。计算后应该插入转换后的值。 – 2011-04-18 17:06:41

+0

谢谢。现在有一些积极的结果。 http://jsfiddle.net/qARRb/(似乎只适用于Chrome浏览器和Safari浏览器)http://canvas.patrikelfstrom.se/(在ff,chrome和safari中工作)。虽然,不完全是我所追求的。下班回家时我必须尝试。顺便说一句,从屏幕上的哪个位置开始计算度数?中心顶部? – Patrik 2011-04-19 07:55:05