我有an interactive SVG diagram几乎完全不同于这个简单的演示...
......除非:
<text>
个元素,viewBox
和理想情况下,我喜欢等同于vector-effect:non-scaling-stroke
(至少在Webkit上),但对于font-size
。这样的事情存在吗?如果没有,我最好的办法是计算viewBox大小,将它与aspectRatio
结合起来确定更大的轴,然后用JS来操作font-size
属性的CSS规则?
答案 0 :(得分:1)
不,如果更改viewBox,则没有任何自动方法可以保持文本大小不变。您必须按照自己的想法使用Javascript调整文本大小。
答案 1 :(得分:0)
这是一个基于JavaScript的解决方案。你在开始时调用recordSVGFontSizes()
一次,传递一个SVG元素和一个StyleSheet。它calculates the viewport for the SVG,并遍历样式表并查找提及font-size
的所有规则并记录原始字体大小。
然后,当您的viewBox更改时,将记录的值传递给normalizeSVGFontSizes()
。它将重新计算视口并适当更新所有字体大小。
请注意,由于视口计算目前不适用于Firefox,因此此脚本无法在此处运行。
// Do this once at startup
var recording = recordSVGFontSizes( mySVG, document.styleSheets[0] );
// Do this each time the viewport changes
normalizeSVGFontSizes(recording);
function recordSVGFontSizes(svgElement,styleSheet){
var rules = [];
var viewport = calculateViewport(svgElement);
for (var i=styleSheet.cssRules.length;i--;){
var rule = styleSheet.cssRules[i];
if (rule.style.fontSize){
var parts = rule.style.fontSize.split(/(\D+)/);
rules.push({ rule:rule, units:parts[1], perpx:parts[0] / viewport.width });
}
}
return {rules:rules, svg:svgElement};
}
function normalizeSVGFontSizes(fontSizeRecording){
var viewport = calculateViewport(fontSizeRecording.svg);
for (var i=fontSizeRecording.rules.length;i--;){
var record = fontSizeRecording.rules[i];
record.rule.style.fontSize = record.perpx*viewport.width + record.units;
}
}
// Given an <svg> element, returns an object with the visible bounds
// expressed in local viewBox units, e.g.
// { x:-50, y:-50, width:100, height:100 }
function calculateViewport(svg){ // http://phrogz.net/JS/_ReuseLicense.txt
var outer = svg.getBoundingClientRect();
var aspect = svg.preserveAspectRatio.baseVal,
viewBox = svg.viewBox.baseVal,
width = viewBox && viewBox.width || outer.width,
height = viewBox && viewBox.height || outer.height,
x = viewBox ? viewBox.x : 0,
y = viewBox ? viewBox.y : 0;
if (!width || !height || !outer.width) return;
if (aspect.align==aspect.SVG_PRESERVEASPECTRATIO_NONE || !viewBox || !viewBox.height){
return {x:x,y:y,width:width,height:height};
}else{
var inRatio = viewBox.width / viewBox.height,
outRatio = outer.width / outer.height;
var meetFlag = aspect.meetOrSlice != aspect.SVG_MEETORSLICE_SLICE;
var fillAxis = outRatio>inRatio ? (meetFlag?'y':'x') : (meetFlag?'x':'y');
if (fillAxis=='x'){
height = width/outRatio;
var diff = viewBox.height - height;
switch (aspect.align){
case aspect.SVG_PRESERVEASPECTRATIO_UNKNOWN:
case aspect.SVG_PRESERVEASPECTRATIO_XMINYMID:
case aspect.SVG_PRESERVEASPECTRATIO_XMIDYMID:
case aspect.SVG_PRESERVEASPECTRATIO_XMAXYMID:
y += diff/2;
break;
case aspect.SVG_PRESERVEASPECTRATIO_XMINYMAX:
case aspect.SVG_PRESERVEASPECTRATIO_XMIDYMAX:
case aspect.SVG_PRESERVEASPECTRATIO_XMAXYMAX:
y += diff;
break;
}
}
else{
width = height*outRatio;
var diff = viewBox.width - width;
switch (aspect.align){
case aspect.SVG_PRESERVEASPECTRATIO_UNKNOWN:
case aspect.SVG_PRESERVEASPECTRATIO_XMIDYMIN:
case aspect.SVG_PRESERVEASPECTRATIO_XMIDYMID:
case aspect.SVG_PRESERVEASPECTRATIO_XMIDYMAX:
x += diff/2;
break;
case aspect.SVG_PRESERVEASPECTRATIO_XMAXYMID:
case aspect.SVG_PRESERVEASPECTRATIO_XMAXYMIN:
case aspect.SVG_PRESERVEASPECTRATIO_XMAXYMAX:
x += diff;
break;
}
}
return {x:x,y:y,width:width,height:height};
}
}