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

bug using attr function in a SVG.Text element #13

Closed
jfgodoy opened this issue Mar 18, 2013 · 4 comments
Closed

bug using attr function in a SVG.Text element #13

jfgodoy opened this issue Mar 18, 2013 · 4 comments

Comments

@jfgodoy
Copy link

jfgodoy commented Mar 18, 2013

In SVG.Element.attr function this.lines can be undefined, because in SVG.Text constructor you dont set this.lines = []

    /* treat x differently on text elements */
    if (a == 'x' && this._isText()) {
      for (var i = this.lines.length - 1; i >= 0; i--)
        this.lines[i].attr(a, v)

(excuse my bad english)

@wout
Copy link
Member

wout commented Mar 18, 2013

In which scenario can this.lines be empty? I suppose only if no text is supplied when creating a text element:

draw.text()

But that is bad practice because the first argument is required. It would be like ordering a glass of water but asking the waiter to leave the glass empty. If one would like to create an empty text element, this is the proper way to do it:

draw.text('')

@jfgodoy
Copy link
Author

jfgodoy commented Mar 18, 2013

That's true, I wasn't using the api provided in the documentation, but my scenario is this:

I fetch objects from the server in json format, like

var obj = {
  type: "Text",
  attrs: {...},
  transforms: {...},
  styles: {...}
}

so in order to create a new object, I do the next

var foo = draw.put(new SVG[obj.type]())
foo.attr(obj.attrs)
foo.transform(obj.transforms)
foo.style(obj.styles)

@wout
Copy link
Member

wout commented Mar 18, 2013

Ok, I see where you're getting at. In this case it might be better do a little bit more coding:

var element = obj.type == 'rect' || obj.type == 'ellipse' ?
  draw[obj.type](0,0) :
obj.type == 'image' || obj.type == 'text' ?
  draw[obj.type]('') :
obj.type == 'line' ?
  draw.line(0,0,0,0) :
obj.type == 'g' ?
  draw.group() :
  draw[obj.type]()

element.attr(obj.attrs)

@wout
Copy link
Member

wout commented Mar 18, 2013

An additional thought, it is always safer to work with the official api because it is much less likely to change.

@wout wout closed this as completed Mar 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants