viewbox.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. define(function(require, exports, module) {
  2. return require('../core/class').createClass('ViewBox', {
  3. getViewBox: function() {
  4. var attr = this.node.getAttribute('viewBox');
  5. if (attr === null) {
  6. // firefox:
  7. // 1. viewBox 没有设置过的时候获得的是 null
  8. // 2. svg 标签没有指定绝对大小的时候 clientWidth 和 clientHeigt 为 0,需要在父容器上查找
  9. // TODO: 第 2 条取得的不准确(假如有 padding 之类的)
  10. return {
  11. x: 0,
  12. y: 0,
  13. width: this.node.clientWidth || this.node.parentNode.clientWidth,
  14. height: this.node.clientHeight || this.node.parentNode.clientHeight
  15. };
  16. } else {
  17. attr = attr.split(' ');
  18. return {
  19. x: +attr[0],
  20. y: +attr[1],
  21. width: +attr[2],
  22. height: +attr[3]
  23. };
  24. }
  25. },
  26. setViewBox: function(x, y, width, height) {
  27. this.node.setAttribute('viewBox', [x, y, width, height].join(' '));
  28. return this;
  29. }
  30. });
  31. });