Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods overwrite current transformation #38

Closed
sergi opened this issue May 17, 2013 · 5 comments
Closed

Methods overwrite current transformation #38

sergi opened this issue May 17, 2013 · 5 comments
Labels

Comments

@sergi
Copy link

sergi commented May 17, 2013

Let's say I have the following code:

var el = draw.group();
el.attr('transform', 'matrix(1,0,0,-1,0,0)');

The node of this object has now the attribute transform="matrix(1,0,0,-1,0,0)".
If I now try move the element:

el.move(100, 0);

It resets all previous transformations, and its node transform attribute looks like transform="translate(0,100)".

Is that expected? Right now the only way to wor around this behavior is doing something like this:

  el.scale(1, -1);
  var t = el.transform();
  t.x = t.x + 100;
  el.transform(t);
@wout
Copy link
Member

wout commented May 18, 2013

Yes this is expected. You should use the transfrom() method instead:

el.transform('matrix', '1,0,0,-1,0,0')

Transform values are numerically stored in order to be able to animate them. Or rather, not having to parse them over and over again. That is why all transformation should be applied through the transform() method.

The example above can also be achieved like this:

el.transform({ d: -1 })

As in matrix(a,b,c,d,e,f).

@sergi
Copy link
Author

sergi commented May 18, 2013

Right. So if I want to apply a scaleY of -1 and then later move the element 100px to the right I should to something like:

el.transform({ d: -1 });
//Other stuff happens, time passes
el.transform({ e: el.transform().x + 100 });

(e meaning the x component of the matrix, I assume that it could also be x instead of e). Please correct me if I'm wrong.

@wout
Copy link
Member

wout commented May 18, 2013

Well, not exactly. Translations and matrices are applied separately. In other words, transformations are not combined into a matrix. If you move an element with:

rect.transform({ x: 100, y: 50 })

The value of the transform attribute will be translate(100,50).

If you add a matrix transformation afterwards, let's say:

rect.transform({ d: -1 })

The value of the transform attribute will be translate(100,50) matrix(1,0,0,-1,0,0).

Transformations are combined in svg. Recalculating transformations into a singular matrix would remove that.

To be honest, I have been thinking about going that way (one matrix to rule them all). But I decided to stay as close as possible to SVG's native behaviour.

@wout
Copy link
Member

wout commented May 24, 2013

Are you with me on this?

@sergi
Copy link
Author

sergi commented May 26, 2013

Yeah. Perhaps adding extra utility methods that translate, scale, etc.
using matrix transformations is not a bad idea though (I am doing all with
matrix operations now). You could prefix them and keep the svg-standard
methods for users that prefer 1:1 mapping with the generated svg .

On the other hand, I would always expect that svg.js takes the most
performing path, regardless of the methods I use. For example, if I do
obj.x = 10 I would expect that a matrix transformation is applied behind
the scenes, if that's more performing.

On Friday, May 24, 2013, Wout Fierens wrote:

Are you with me on this?


Reply to this email directly or view it on GitHubhttps://github.com//issues/38#issuecomment-18420057
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants