我想了解HTML5画布一些和JavaScript的y位置,我想创建这些典型插画的太阳射线:计算X,帆布点
但我的问题是,我想将其自动化并使其全屏。
要计算中间点的坐标不难,这是我无法控制的外部点。
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);
}
}
}
}
我似乎无法找出代码,或者它实际上做了什么。 :/所以这里是一个小提琴,所以你可以看到我做了什么。也许你可以帮我:) http://jsfiddle.net/Fnf3u/ – Patrik 2011-04-18 09:18:27
我刚更新了一些东西:http://jsfiddle.net/Fnf3u/3/ – Patrik 2011-04-18 09:31:23
我不相信小提琴。这是我的页面上的工作画布http://canvas.patrikelfstrom.se它使用相同的HTML,CSS和一些js – Patrik 2011-04-18 09:37:58