【发布时间】:2014-01-09 21:08:17
【问题描述】:
这是我遇到的一个问题的 jsfiddle:
鉴于此 html:
<div id="box"></div>
还有这个js(都在jsfiddle中演示过):
var paper = Raphael('box', 300, 300);
var center = { x:150, y:150 }, size = { x:10, y:10 };
var look = { stroke:'#000000', 'stroke-width':1,
fill:'#888888', opacity:1, 'stroke-opacity':0.9,
'fill-opacity': 0.7, 'stroke-dasharray': '- '
};
var path = paper.path('').attr(look);
function reset() {
// reset to small square
path.attr({ path:[
[ 'M', center.x - size.x, center.y - size.y ],
[ 'L', center.x + size.x, center.y - size.y ],
[ 'L', center.x + size.x, center.y + size.y ],
[ 'L', center.x - size.x, center.y + size.y ],
[ 'z' ]
] }).transform([]);
console.log(path._.transform);
}
function add_state_one() {
// move down and left slightly and scale up
path.transform(path._.transform.concat([
[ 'T', -50, 10 ],
[ 'S', 2.5, 7.5 ]
]));
console.log(path._.transform);
}
function add_state_two() {
// use bounding box center
var bb = path.getBBox(),
center = { x:(bb.x + bb.x2)/2, y:(bb.y + bb.y2)/2 };
path.transform(path._.transform.concat([
[ 'R', 30, center.x, center.y ],
[ 'S', 0.8, 0.7, center.x, center.y ]
]));
console.log(path._.transform);
}
function add_state_three() {
// use original center
path.transform(path._.transform.concat([
[ 'R', 45, center.x, center.y ],
[ 'S', 0.7, 0.5, center.x, center.y ]
]));
console.log(path._.transform);
}
// buttons
var blook = { 'stroke-dasharray':'', stroke:'transparent' };
var btn = {
rst:paper.path([ [ 'M', 0, 0 ], [ 'L', 10, 0 ],
[ 'L', 10, 10 ], [ 'L', 0, 10 ], [ 'z' ] ])
.attr(look).attr(blook).click(reset),
st1:paper.path([ [ 'M', 20, 0 ], [ 'L', 30, 0 ],
[ 'L', 30, 10 ], [ 'L', 20, 10 ], [ 'z' ] ])
.attr(look).attr(blook).attr({ fill:'#0000ff' })
.click(add_state_one),
st2:paper.path([ [ 'M', 40, 0 ], [ 'L', 50, 0 ],
[ 'L', 50, 10 ], [ 'L', 40, 10 ], [ 'z' ] ])
.attr(look).attr(blook).attr({ fill:'#ff0000' })
.click(add_state_two),
st3:paper.path([ [ 'M', 60, 0 ], [ 'L', 70, 0 ],
[ 'L', 70, 10 ], [ 'L', 60, 10 ], [ 'z' ] ])
.attr(look).attr(blook).attr({ fill:'#00ff00' })
.click(add_state_three)
};
// click grey
reset();
基本上我需要应用比例,然后应用旋转,旋转不受比例影响。
想象你有一个正方形。现在您对其应用比例,使其成为一个高矩形。现在你想旋转这个生成的矩形,使它保持一个矩形,有直角,但呈 45 度角。我们正在尝试实现一个矩形,它在一个角上保持平衡,倾斜 45 度角。相反,如 jsfiddle 中所示,当我应用缩放(生成矩形),然后旋转(将其倾斜到角落)时,我会以倾斜的矩形(或看起来怪异的菱形)结束。
我相信有些机构知道如何做到这一点。最终,我计划从多个原点添加多个不同的变换(平移、旋转和缩放),但我不希望这种倾斜效应发生。只是想弄清楚这一点。任何帮助表示赞赏。
谢谢
【问题讨论】:
-
不确定我是否错过了您的观点,只需单独应用 add_state_three,因为这样可以在不倾斜的情况下进行缩放和旋转?
-
我的意思是,如果你想组合多个变换,你最好计算正确的总变换并只应用一次。
标签: javascript html svg raphael