在圆柱体内部绘制带有摄像机视图的html5帆布圆柱体

时间:2013-03-28 12:46:26

标签: javascript jquery html5 canvas

我认为标题是不言自明的。 我需要在圆柱体的中心构建带有摄像机视图的html5 canvas cylindre。 在圆柱体的墙壁上,我需要构建像图片,圆柱体旋转和缩放。

如果有人想知道从哪里开始使用相机和气缸,请帮忙。

为了帮助您理解我的描述,我发布了一张图片 http://s21.postimg.org/fpnmukbef/inside_cilindre.jpg

1 个答案:

答案 0 :(得分:1)

我认为您必须处理2d图片并在最后一步转换所需的部分

将好的部分转换为3d,我认为你必须根据你选择的抛物线方程拉伸每个冒号

function getStretch(x,maxx)
{ /* here i choice :
     f(x) = (x*x)+3 for top
     f(x) = -(x*x)+1 for bottom
     when y >= 0 and y <= 4
     x >= -1 and x <= +1
  */

   // convert x ~ maxx to -1 ~ 1
   var ratio = 2/maxx; // -1 to 1 === 2
   x = x * ratio;
   x -= 1; // switch from 0 ~ 2 to -1 ~ 1

   var sizeY = 4;
   var y = -(x*x)+1; // bottom get y >= 0 and y <= 1 result, easier to compute
   y *= 2; // double to add up and bottom
   var colonHeight = sizeY - y;
   var colonRatio = colonHeight / sizeY;
   return colonRatio; // return the height ratio for this colon
}

101冒号视图的值:http://jsfiddle.net/L9YgL/1/

var view_start_x = 200; // current view start at 200px from left source image
var view_size_x  = 399; // current view work on 400 px width

// get our stretch ratio for each colon
var stretch_values = [];
for(var x=0;x<=view_size_x;x++) stretch_values[x] = getStretch(x,view_size_x);

// now we need a function to get the colon height and position

var colonHeight = 400; // height of source image, and max height of out

function getColonInfo(x)
{ if(x < 0 || x > view_size_x) return {h:42,y:0};
  // compute current colon height
  var ratio  = stretch_values[x];
  var height = parseInt( colonHeight * ratio ,10);
  var posY = parseInt( (colonHeight - height)/2 ,10);
  return {h:height,y:posY};
}

// save this info for each out colon
var colon_info = [];
for(var x=0;x<=view_size_x;x++) colon_info[x] = getColonInfo(x);

// now a function to render a colon with a zoom

function renderColon(x,sourceX)
{    var info = colon_info[x];
     var originalHeight = colonHeight;
     var height = info.h;
     var y = info.y;
     // now do your reduce/blit magic here
     // render at "x","y" a colon with height of "height"
     // from source colon "sourceX" who get height of "originalHeight"

}

// and a function to iterate each colon and do the magic  

function my2dTo3d()
{
    for(x=0; x<=view_size_x; x++)
    {    var offset = x + view_start_x;
         renderColon(x,offset);
    }
}
好吧,确实有一个更好的方法来做到这一点,我在手边做,也许是css3?