javascript - HTML5 透视网格

标签 javascript html math canvas

我试图在我的 Canvas 上做一个透视网格,我已经改变了另一个网站的功能,结果是这样的:

    function keystoneAndDisplayImage(ctx, img, x, y, pixelHeight, scalingFactor) {
    var h = img.height,
            w = img.width,

            numSlices = Math.abs(pixelHeight),

            sliceHeight = h / numSlices,

            polarity = (pixelHeight > 0) ? 1 : -1,
            heightScale = Math.abs(pixelHeight) / h,

            widthScale = (1 - scalingFactor) / numSlices;

    for(var n = 0; n < numSlices; n++) {

        var sy = sliceHeight * n,
                sx = 0,
                sHeight = sliceHeight,
                sWidth = w;

        var dy = y + (sliceHeight * n * heightScale * polarity),
                dx = x + ((w * widthScale * n) / 2),
                dHeight = sliceHeight * heightScale,
                dWidth = w * (1 - (widthScale * n));

        ctx.drawImage(img, sx, sy, sWidth, sHeight,
                dx, dy, dWidth, dHeight);
    }
}

它创建了几乎不错的透视网格,但它没有缩放高度,所以每个正方形都有相同的高度。 Here's a working jsFiddle以及它的外观,就在 Canvas 下方。我想不出任何数学公式来扭曲与“透视距离”(顶部)成比例的高度。 我希望你明白。抱歉语言错误。

任何帮助将不胜感激

问候

最佳答案

遗憾的是,除了使用 3D 方法之外没有合适的方法。但幸运的是,它并没有那么复杂。

以下将生成一个可按 X 轴旋转的网格(如您的图片所示),因此我们只需要关注该轴。

要了解发生了什么:我们在笛卡尔坐标空间中定义网格。花哨的词是说我们将点定义为向量而不是绝对坐标。也就是说,一个网格单元可以从 0,0 到 1,1,而不是例如 10,20 到 45,45 只是为了取一些数字。

投影阶段,我们将这些笛卡尔坐标投影到我们的屏幕坐标中。

结果是这样的:

snapshot 3d grid

ONLINE DEMO

好的,让我们深入研究 - 首先我们设置一些投影等所需的变量:

fov = 512,         /// Field of view kind of the lense, smaller values = spheric
viewDist = 22,     /// view distance, higher values = further away
w = ez.width / 2,  /// center of screen
h = ez.height / 2,
angle = -27,       /// grid angle
i, p1, p2,         /// counter and two points (corners)
grid = 10;         /// grid size in Cartesian

要调整网格,我们不调整循环(见下文),而是更改 fovviewDist 以及修改 grid 增加或减少细胞数量。

假设您想要更极端的 View - 通过将 fov 设置为 128 并将 viewDist 设置为 5,您将使用相同的 grid Angular :

enter image description here

执行所有数学运算的“神奇”函数如下:

function rotateX(x, y) {

    var rd, ca, sa, ry, rz, f;

    rd = angle * Math.PI / 180; /// convert angle into radians
    ca = Math.cos(rd);
    sa = Math.sin(rd);

    ry = y * ca;   /// convert y value as we are rotating
    rz = y * sa;   /// only around x. Z will also change

    /// Project the new coords into screen coords
    f = fov / (viewDist + rz);
    x = x * f + w;
    y = ry * f + h;

    return [x, y];
}

就是这样。值得一提的是,正是新的 Y 和 Z 的组合使得线条在顶部(在这个 Angular )变小了。

现在我们可以像这样在笛卡尔空间中创建一个网格,并将这些点直接旋转到屏幕坐标空间中:

/// create vertical lines
for(i = -grid; i <= grid; i++) {
    p1 = rotateX(i, -grid);
    p2 = rotateX(i, grid);
    ez.strokeLine(p1[0], p1[1], p2[0], p2[1]); //from easyCanvasJS, see demo
}

/// create horizontal lines
for(i = -grid; i <= grid; i++) {
    p1 = rotateX(-grid, i);
    p2 = rotateX(grid, i);
    ez.strokeLine(p1[0], p1[1], p2[0], p2[1]);
}

还要注意位置 0,0 是屏幕的中心。这就是为什么我们使用负值在左侧或上方离开。可以看到两条中心线是直线。

这就是它的全部。要为单元格着色,您只需选择笛卡尔坐标,然后通过调用 rotateX() 对其进行转换,您将获得 Angular 落所需的坐标。

例如 - 选择一个随机细胞数(在 X 轴和 Y 轴上都在 -10 和 10 之间):

c1 = rotateX(cx, cy);         /// upper left corner
c2 = rotateX(cx + 1, cy);     /// upper right corner
c3 = rotateX(cx + 1, cy + 1); /// bottom right corner
c4 = rotateX(cx, cy + 1);     /// bottom left corner

/// draw a polygon between the points
ctx.beginPath();
ctx.moveTo(c1[0], c1[1]);
ctx.lineTo(c2[0], c2[1]);
ctx.lineTo(c3[0], c3[1]);
ctx.lineTo(c4[0], c4[1]);
ctx.closePath();

/// fill the polygon
ctx.fillStyle = 'rgb(200,0,0)';
ctx.fill();

An animated version 这有助于了解发生了什么。

关于javascript - HTML5 透视网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17799165/

相关文章:

javascript - php html javascript 错误结果

javascript - 读取单选按钮的值时,值未定义

java - 如何将小数点后 10 到 5 的幂四舍五入

javascript - Javascript 中的太阳弧路径

c++ - 将 LookAt 相机旋转一定角度

javascript - 从 Javascript 函数中导出变量

javascript - 为什么 JSON.parse 删除双引号?

javascript - 我的天啊...我仍然无法获取要显示的单选按钮的值 - Javascript dom

javascript - 视频标签后,触摸事件在 iPad 上停止工作

javascript - 如何在 jquery 中替换 Anchor 标签的 href 中的一些文本