【问题标题】:Raphael.js perspective transformationRaphael.js 透视变换
【发布时间】:2014-09-07 23:41:09
【问题描述】:

我正在使用 Raphael.js,但在创建透视变换时遇到了问题。它应该是一个 svg / raphael 解决方案,因为它是创建图像的一个步骤。这是现在的样子:

目标是使用变换使其在 z 轴上倾斜。它应该如下图所示:


代码:

paper.setStart();
Obj=paper.path('M 2500 3750 L 2500 3481.9 L 0 3481.9 L 0 3750 L 2500 3750Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 3482.2 L 2500 3214.1 L 0 3214.1 L 0 3482.2 L 2500 3482.2Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 3214.3 L 2500 2946.2 L 0 2946.2 L 0 3214.3 L 2500 3214.3Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 2946.5 L 2500 2678.4 L 0 2678.4 L 0 2946.5 L 2500 2946.5Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 2678.6 L 2500 2410.6 L 0 2410.6 L 0 2678.6 L 2500 2678.6Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 2410.8 L 2500 2142.7 L 0 2142.7 L 0 2410.8 L 2500 2410.8Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 2142.9 L 2500 1874.9 L 0 1874.9 L 0 2142.9 L 2500 2142.9Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 1875.1 L 2500 1607 L 0 1607 L 0 1875.1 L 2500 1875.1Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 1607.3 L 2500 1339.2 L 0 1339.2 L 0 1607.3 L 2500 1607.3Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 1339.4 L 2500 1071.3 L 0 1071.3 L 0 1339.4 L 2500 1339.4Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 1071.6 L 2500 803.5 L 0 803.5 L 0 1071.6 L 2500 1071.6Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 803.8 L 2500 535.7 L 0 535.7 L 0 803.8 L 2500 803.8Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 535.9 L 2500 267.8 L 0 267.8 L 0 535.9 L 2500 535.9Z');
Obj.attr({'fill':field_color1,'stroke':'none', 'fill-opacity': opacity});
Obj=paper.path('M 2500 268 L 2500 0 L 0 0 L 0 268 L 2500 268Z');
Obj.attr({'fill':field_color2,'stroke':'none', 'fill-opacity': opacity});

Obj=paper.path('M 1237.1 597.3 C 1237.1 604.4 1242.9 610.2 1250 610.2 C 1257.1 610.2 1262.9 604.4 1262.9 597.3 C 1262.9 590.1 1257.1 584.4 1250 584.4 C 1242.9 584.4 1237.1 590.1 1237.1 597.3');
Obj.attr({'fill': line_color,'stroke':'none'});
Obj=paper.path('M 1262.9 3151.7 C 1262.9 3144.6 1257.1 3138.8 1250 3138.8 C 1242.9 3138.8 1237.1 3144.6 1237.1 3151.7 C 1237.1 3158.9 1242.9 3164.7 1250 3164.7 C 1257.1 3164.7 1262.9 3158.9 1262.9 3151.7');
Obj.attr({'fill': line_color,'stroke':'none'});
Obj=paper.path('...');
Obj.attr({'fill': line_color,'stroke':'none'});
var st = paper.setFinish();

编辑:Fiddle

有什么解决办法吗?

【问题讨论】:

  • 你能在 jsfiddle 上找到一个测试示例吗?
  • 我已经更新了问题:)

标签: javascript svg raphael


【解决方案1】:

我不知道倾斜是否是这里的答案。问题是您正在尝试在纸上应用 3d 效果,而不是 2d。
不确定是否也可以在纯 svg 和 raphael 中完成,但这是使用 css3 3d 变换 (rotateX) 和透视的纯 css 解决方案:

body {
    /* the lower the value, the higher the distortion */
    perspective: 600px;
}

#raph-rotate {
    width: 500px;
    height: 750px;
    margin: 0 auto;
    transform: rotateX(30deg);
    transform-origin: 50% 100%;
}

这是更新后的小提琴:http://jsfiddle.net/Tj9bJ/2/

【讨论】:

  • 是的,你是对的。这是一个透视变换而不是倾斜。我会更新我的帖子。您的解决方案工作正常,但我需要一个 raphael / svg 解决方案,因为转换是创建图像的中间步骤 :)
  • Raphael 和 SVG 是 2D 技术,它们都不会直接做你想要的那种变换。
【解决方案2】:

我认为这在拉斐尔是不可能的。一个简单的问题:你为什么不直接把它画成你想要的形状呢?我的意思是,这不是一个很难画的形状。

话虽如此,我认为你应该画不同的东西。我会这样做(我重新使用了您在“代码”部分中给我们的路径):

var path;
var set = paper.set();
function obj(chosenPath,color){
                switch(color){

                    case 1:
                    path = paper.path(chosenPath).attr({
                        fill:field_color1,
                        stroke : "none",
                        "fill-opacity":opacity
                    });
                    break;

                    case 2:
                    path = paper.path(chosenPath).attr({
                        fill:field_color2,
                        stroke : "none",
                        "fill-opacity":opacity
                    });
                    break;

                    case 3:
                    path = paper.path(chosenPath).attr({
                        fill:line_color,
                        stroke : "none",
                    });
                    break;

                    default:
                    return hue;
                }

                set.push(path);

            }

obj('M 2500 3750 L 2500 3481.9 L 0 3481.9 L 0 3750 L 2500 3750Z',1);
obj('M 2500 3482.2 L 2500 3214.1 L 0 3214.1 L 0 3482.2 L 2500 3482.2Z',2);
obj('M 2500 3214.3 L 2500 2946.2 L 0 2946.2 L 0 3214.3 L 2500 3214.3Z',1);
obj('M 2500 2946.5 L 2500 2678.4 L 0 2678.4 L 0 2946.5 L 2500 2946.5Z',2);
obj('M 2500 2678.6 L 2500 2410.6 L 0 2410.6 L 0 2678.6 L 2500 2678.6Z',1)
obj('M 2500 2410.8 L 2500 2142.7 L 0 2142.7 L 0 2410.8 L 2500 2410.8Z',2)
obj('M 2500 2142.9 L 2500 1874.9 L 0 1874.9 L 0 2142.9 L 2500 2142.9Z',1);
obj('M 2500 1875.1 L 2500 1607 L 0 1607 L 0 1875.1 L 2500 1875.1Z',2);
obj('M 2500 1607.3 L 2500 1339.2 L 0 1339.2 L 0 1607.3 L 2500 1607.3Z',1);
obj('M 2500 1339.4 L 2500 1071.3 L 0 1071.3 L 0 1339.4 L 2500 1339.4Z',2);
obj('M 2500 1071.6 L 2500 803.5 L 0 803.5 L 0 1071.6 L 2500 1071.6Z',1);
obj('M 2500 803.8 L 2500 535.7 L 0 535.7 L 0 803.8 L 2500 803.8Z',2);
obj('M 2500 535.9 L 2500 267.8 L 0 267.8 L 0 535.9 L 2500 535.9Z',1);
obj('M 2500 268 L 2500 0 L 0 0 L 0 268 L 2500 268Z',2);
obj('M 1237.1 597.3 C 1237.1 604.4 1242.9 610.2 1250 610.2 C 1257.1 610.2 1262.9 604.4 1262.9 597.3 C 1262.9 590.1 1257.1 584.4 1250 584.4 C 1242.9 584.4 1237.1 590.1 1237.1 597.3',3);
obj('M 1262.9 3151.7 C 1262.9 3144.6 1257.1 3138.8 1250 3138.8 C 1242.9 3138.8 1237.1 3144.6 1237.1 3151.7 C 1237.1 3158.9 1242.9 3164.7 1250 3164.7 C 1257.1 3164.7 1262.9 3158.9 1262.9 3151.7',3)

通过这种方式,您可以修改集合中游戏场的每一个元素,而无需多次重写“.attr({stuff...})”。

【讨论】:

  • 上面的“代码”是由将svg文件转换为raphael代码的工具生成的:)目前我只想尝试一些效果。稍后我会把它变成漂亮的 OOP 风格:)
  • 我明白了 :) 不过,我可能遗漏了一些东西,但是您为什么不尝试直接将它绘制成您想要的形状呢?在我看来这很简单。
  • 我想创建一个用户可以动态更改的图像。颜色、不透明度和透视应该是选项。
  • 如果在 Raphael 中无法实现您想要的效果,您可以绘制多个版本的运动场。这样,您可以限制用户对透视值的选择。你知道,在你想要的轴上每 10° 10°,类似的东西。不过,这是一项漫长的工作。
猜你喜欢
  • 2012-07-06
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2011-08-26
  • 1970-01-01
  • 2011-03-09
相关资源
最近更新 更多