|
1 | | -/* svg.js 0.1a - svg container element group arrange clip doc defs shape rect circle ellipse path image sugar - svgjs.com/license */ |
| 1 | +/* svg.js 0.1a - svg container element group arrange clip doc defs shape rect circle ellipse path image text sugar - svgjs.com/license */ |
2 | 2 | (function() { |
3 | 3 |
|
4 | 4 | this.SVG = { |
|
121 | 121 | return this.place(new SVG.Image(), v); |
122 | 122 | }, |
123 | 123 |
|
| 124 | + text: function(v) { |
| 125 | + return this.place(new SVG.Text(), v); |
| 126 | + }, |
| 127 | + |
124 | 128 | place: function(e, v) { |
125 | 129 | if (v != null) { |
126 | 130 | if (v.x != null && v.y != null) |
|
129 | 133 | if (v.width != null && v.height != null) |
130 | 134 | e.size(v.width, v.height); |
131 | 135 |
|
132 | | - if (v.data != null) |
133 | | - e.data(v.data); |
134 | | - |
135 | | - if (v.src != null) |
136 | | - e.load(v.src); |
| 136 | + v.data != null ? |
| 137 | + e.data(v.data) : |
| 138 | + v.src != null ? |
| 139 | + e.load(v.src) : |
| 140 | + v.text != null ? |
| 141 | + e.text(v.text) : |
| 142 | + void 0; |
137 | 143 | } |
138 | 144 |
|
139 | 145 | this.add(e); |
|
408 | 414 | move: function(x, y) { |
409 | 415 | this.attrs.x = x; |
410 | 416 | this.attrs.y = y; |
411 | | - this.center(); |
412 | | - |
413 | | - return this; |
| 417 | + |
| 418 | + return this.center(); |
414 | 419 | }, |
415 | 420 |
|
416 | 421 | // custom size function (no height value here!) |
417 | 422 | size: function(w) { |
418 | | - this.attr('r', w / 2); |
419 | | - this.center(); |
420 | | - |
421 | | - return this; |
| 423 | + return this.attr('r', w / 2).center(); |
422 | 424 | }, |
423 | 425 |
|
424 | 426 | // position element by its center |
425 | 427 | center: function(x, y) { |
426 | 428 | var r = this.attrs.r || 0; |
427 | 429 |
|
428 | | - this.attr('cx', x || ((this.attrs.x || 0) + r)); |
429 | | - this.attr('cy', y || ((this.attrs.y || 0) + r)); |
| 430 | + return this.attr({ |
| 431 | + cx: (x || ((this.attrs.x || 0) + r)), |
| 432 | + cy: (y || ((this.attrs.y || 0) + r)) |
| 433 | + }); |
430 | 434 | } |
431 | 435 |
|
432 | 436 | }); |
|
445 | 449 | move: function(x, y) { |
446 | 450 | this.attrs.x = x; |
447 | 451 | this.attrs.y = y; |
448 | | - this.center(); |
449 | | - |
450 | | - return this; |
| 452 | + |
| 453 | + return this.center(); |
451 | 454 | }, |
452 | 455 |
|
453 | 456 | // custom size function |
454 | 457 | size: function(w, h) { |
455 | | - this.attr('rx', w / 2); |
456 | | - this.attr('ry', h / 2); |
457 | | - this.center(); |
458 | | - |
459 | | - return this; |
| 458 | + return this. |
| 459 | + attr({ rx: w / 2, ry: h / 2 }). |
| 460 | + center(); |
460 | 461 | }, |
461 | 462 |
|
462 | 463 | // position element by its center |
463 | 464 | center: function(x, y) { |
464 | | - this.attr('cx', x || ((this.attrs.x || 0) + (this.attrs.rx || 0))); |
465 | | - this.attr('cy', y || ((this.attrs.y || 0) + (this.attrs.ry || 0))); |
| 465 | + return this.attr({ |
| 466 | + cx: (x || ((this.attrs.x || 0) + (this.attrs.rx || 0))), |
| 467 | + cy: (y || ((this.attrs.y || 0) + (this.attrs.ry || 0))) |
| 468 | + }); |
466 | 469 | } |
467 | 470 |
|
468 | 471 | }); |
|
480 | 483 |
|
481 | 484 | // set path data |
482 | 485 | data: function(d) { |
483 | | - this.attr('d', d); |
484 | | - return this; |
| 486 | + return this.attr('d', d); |
485 | 487 | } |
486 | 488 |
|
487 | 489 | }); |
|
491 | 493 | }; |
492 | 494 |
|
493 | 495 | // inherit from SVG.Element |
494 | | - SVG.Image.prototype = new SVG.Element(); |
495 | | - |
496 | | - // include the container object |
497 | | - SVG.extend(SVG.Image, SVG.Container); |
| 496 | + SVG.Image.prototype = new SVG.Shape(); |
498 | 497 |
|
499 | 498 | // Add image-specific functions |
500 | 499 | SVG.extend(SVG.Image, { |
501 | 500 |
|
502 | 501 | // (re)load image |
503 | 502 | load: function(u) { |
504 | | - this.attr('href', u, SVG.xlink); |
| 503 | + return this.attr('href', u, SVG.xlink); |
| 504 | + } |
| 505 | + |
| 506 | + }); |
| 507 | + |
| 508 | + SVG.Text = function Text() { |
| 509 | + this.constructor.call(this, SVG.create('text')); |
| 510 | + |
| 511 | + this.style = { 'font-size': 16 }; |
| 512 | + this.leading = 1.2; |
| 513 | + }; |
| 514 | + |
| 515 | + // inherit from SVG.Element |
| 516 | + SVG.Text.prototype = new SVG.Shape(); |
| 517 | + |
| 518 | + // Add image-specific functions |
| 519 | + SVG.extend(SVG.Text, { |
| 520 | + |
| 521 | + text: function(t) { |
| 522 | + this.content = t = t || 'text'; |
| 523 | + |
| 524 | + var i, |
| 525 | + p = this.parentDoc(), |
| 526 | + a = t.split("\n"); |
| 527 | + |
| 528 | + while (this.node.hasChildNodes()) |
| 529 | + this.node.removeChild(this.node.lastChild); |
| 530 | + |
| 531 | + for (i = 0, l = a.length; i < l; i++) |
| 532 | + this.node.appendChild(new TSpan(). |
| 533 | + text(a[i]). |
| 534 | + attr('style', this._style()). |
| 535 | + attr({ dy: this.style['font-size'] * this.leading, x: (this.attr('x') || 0) }).node ); |
| 536 | + |
| 537 | + return this; |
| 538 | + }, |
| 539 | + |
| 540 | + font: function(o) { |
| 541 | + var i, a = ('size family weight stretch variant style').split(' '); |
| 542 | + |
| 543 | + for (i = a.length - 1; i >= 0; i--) |
| 544 | + if (o[a[i]] != null) |
| 545 | + this.style['font-' + a[i]] = o[a[i]]; |
| 546 | + |
| 547 | + a = ('leading kerning anchor').split(' '); |
| 548 | + |
| 549 | + for (i = a.length - 1; i >= 0; i--) |
| 550 | + if (o[a[i]] != null) |
| 551 | + this[a[i]] = o[a[i]]; |
| 552 | + |
| 553 | + return this.text(this.content); |
| 554 | + }, |
| 555 | + |
| 556 | + _style: function() { |
| 557 | + var i, s = '', a = ('size family weight stretch variant style').split(' '); |
| 558 | + |
| 559 | + for (i = a.length - 1; i >= 0; i--) |
| 560 | + if (this.style['font-' + a[i]] != null) |
| 561 | + s += 'font-' + a[i] + ':' + this.style['font-' + a[i]] + ';'; |
| 562 | + |
| 563 | + a = ('leading kerning anchor').split(' '); |
| 564 | + |
| 565 | + for (i = a.length - 1; i >= 0; i--) |
| 566 | + if (this[a[i]] != null) |
| 567 | + s += a[i] + ':' + this[a[i]] + ';'; |
| 568 | + |
| 569 | + return s; |
| 570 | + } |
| 571 | + |
| 572 | + }); |
| 573 | + |
| 574 | + |
| 575 | + function TSpan() { |
| 576 | + this.constructor.call(this, SVG.create('tspan')); |
| 577 | + }; |
| 578 | + |
| 579 | + // inherit from SVG.Element |
| 580 | + TSpan.prototype = new SVG.Shape(); |
| 581 | + |
| 582 | + // include the container object |
| 583 | + SVG.extend(TSpan, { |
| 584 | + |
| 585 | + text: function(t) { |
| 586 | + this.node.appendChild(document.createTextNode(t)); |
| 587 | + |
505 | 588 | return this; |
506 | 589 | } |
507 | 590 |
|
|
0 commit comments