【问题标题】:How to scale inline SVG如何缩放内联 SVG
【发布时间】:2013-06-14 09:06:26
【问题描述】:

我想在 html 中缩放内联 SVG 文档。我有这样的代码:

var svg_width = svg.attr('width');
var svg_height = svg.attr('height');
function resize(width, height) {
    svg.attr('width', width).attr('height', height);
    var group = svg.find('#resize_group');
    if (!group.length) {
        group = svg.children().not('metadata, defs').wrapAll('<g/>').
            parent().attr('id', 'resize_group');
    }
    var matrix = svg[0].createSVGMatrix();
    //var matrix = group[0].getCTM();
    matrix.a = width/svg_width;
    matrix.d = width/svg_height;
    group.attr('transform', matrix.asString());
}

SVGMatrix.prototype.asString = function() {
    return 'matrix(' + this.a + ' ' + this.b + ' ' + this.c + ' ' + this.d +
           ' ' + this.e + ' ' + this.f + ')';
};

注意:我需要转换 svg 中的元素,否则图像会被修剪。

但是当我调用 resize 函数时,整个 svg 消失了。我已将 svg 保存到文件并在 Inkscape 中打开它,它看起来很正常(它已缩放)。为什么svg消失了? (我在 Firefox、Chrome 和 Opera 上测试过)

【问题讨论】:

  • 它接缝使用 wrapAll('') 使 svg 消失。
  • 我想它会在 html 命名空间而不是 svg 命名空间中创建一个 元素。
  • @RobertLongson 我检查了 google chrome 检查器和$('#resize_group')[0].__proto__ == SVGGElement,所以我认为这很好。

标签: html svg scale


【解决方案1】:

我发现了快速破解,用 svg 的文本替换 svg 以强制重绘。

$.fn.xml = function() {
    return (new XMLSerializer()).serializeToString(this[0]);
};

$.fn.SVGRedraw = function() {
    return $(this.xml()).replaceAll(this);
};

【讨论】:

    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2015-03-15
    相关资源
    最近更新 更多