简体   繁体   English

在字体大小上执行jQuery动画时避免文本换行

[英]Avoid text wrapping while doing a jQuery animate on font-size

I have some content which is displayed at at a preview scale, by using em for everything and then scaling the root font size when I want to reduce/enlarge, so can you see the full content by clicking on the preview, at which point I use jQuery to animate the font-size up to 100%: 我有一些内容以预览比例显示,方法是将em用作所有内容,然后在我要缩小/放大时缩放根字体大小,因此您可以通过单击预览查看全部内容,这时我使用jQuery动画化字体大小,最高可达100%:

http://jsfiddle.net/5fCd5/3/ http://jsfiddle.net/5fCd5/3/

So basically: 所以基本上:

CSS: CSS:

.example section {
  font-size: 30%;    
}

JS: JS:

zoomed.animate({
  fontSize: '100%'
});

But see the fiddle to get a better idea. 但是,请摆弄小提琴以获得更好的主意。

The problem is, that although the container is scaling with the font size, the browser will wrap the text at slightly different points throughout the animation - I understand the logistics of why this happens (different proportions of characters when rendered at different sizes/hinting etc.), but it looks awful. 问题是,尽管容器根据字体大小缩放,但浏览器将在整个动画中将文本包装在稍有不同的位置上-我理解为什么会发生这种情况(以不同大小/提示呈现时,不同比例的字符等) 。),但看起来很糟糕。 It's much more noticeable in Chrome than it is in Firefox. 在Chrome中,它比在Firefox中更引人注目。 Interestingly IE10 doesn't change where the lines wrap at all. 有趣的是,IE10根本不会改变行的换行位置。 This is on Windows - possibly the font rendering on OSX doesn't have this issue. 这是在Windows上-可能OSX上的字体呈现没有此问题。

Can anyone think of some kind of fix or workaround? 谁能想到某种修复或解决方法?

One workaround that I considered initially was: 我最初考虑的一种解决方法是:

Create the preview at 100% font size, split the text on whitespace, add it to the container one word at a time, when the container increases in height store the position, then wrap each line in an element with whitespace no-wrap and give the container overflow: hidden. 以100%字体大小创建预览,在空白处分割文本,一次将其添加到容器中一个单词,当容器高度增加时,存储位置,然后将每行换成空白且不换行并添加到元素中容器溢出:隐藏。

However as I want to use arbitrary HTML (including images sized in ems) for the previews, this isn't really feasible. 但是,由于我想使用任意HTML(包括ems大小的图像)进行预览,所以这实际上是不可行的。

the idea is that by enlarging regular text, you are giving away the decision of how to enlarge it to css/html. 这个想法是通过扩大常规文本,您放弃了如何将其放大为css / html的决定。 You want to devise a method where you have more control on the animating and zooming of your text. 您想设计一种方法,使您可以更好地控制文本的动画和缩放。

An example of that would be converting your text into a bitmap then scaling it. 例如,将文本转换为位图,然后对其进行缩放。 That would be a poor choice for obvious reasons, such as the high cost of converting a font into a bitmap, as well as that a bitmap itself will look pixelated once it's scaled (ie there is quality loss). 出于显而易见的原因,这将是一个糟糕的选择,例如将字体转换为位图的高昂成本,以及位图本身在缩放后会显得像素化(即质量下降)。 Additionally, even using css scale() causes some blurring when you zoom in and out (see comments under original question). 此外,即使使用css scale()也会在放大和缩小时造成一些模糊 (请参阅原始问题下的注释)。

my suggestion: 我的建议:

  1. First of all convert your font into a canvas equivalent using cufon . 首先,使用cufon 字体转换为等效的画布。 Cufon uses a mixture of Canvas and VML to render the fonts. Cufon使用CanvasVML的混合物来渲染字体。
  2. Using cufon/canvas is a good first step. 使用cufon / canvas是一个不错的第一步。 However, if you look at an example of a cufonned text here you will notice that if you zoom in and out, you will see the same pixelation/quality loss symptom that's normally associated with bitmaps. 但是,如果您在此处查看经过修改的文本的示例,则会注意到,如果放大和缩小,您将看到通常与位图相关的相同像素化/质量损失症状。 Further research proves the point that canvas zooming will show pixelation (see this example http://jsfiddle.net/mBzVR/4/ ). 进一步的研究证明了画布缩放将显示像素化这一点(请参见此示例http://jsfiddle.net/mBzVR/4/ )。
  3. To go around that, instead of simply scaling your canvas by multiplying with and height by a number for example.. you can render your canvas at a 25 Frames Per Second (FPS), slightly increasing/decreasing its size at every frame rendered at 1/25 second. 要解决此问题,而不是简单地例如通过用倍数和高度乘以数字来缩放画布。您可以以每秒25帧(FPS)的速度渲染画布,在以1渲染的每一帧处略微增加/减小其大小/ 25秒。 I got this idea from Flot which is a jQuery library for rending canvas charts. 我从Flot那里得到了这个想法, Flot是一个用于发布画布图表的jQuery库。 If you look at their home page example.. you will see a chart being animated by panning left/right. 如果您查看他们的主页示例,则会看到通过左右左右移动动画的图表。 If you save that page.. you will see something like this in flot.demo.js: 如果保存该页面..您将在flot.demo.js中看到以下内容:

from flotcharts.org : 来自flotcharts.org

// Update the random dataset at 25FPS for a smoothly-animating chart
setInterval(function updateRandom() {
    series[0].data = getRandomData();
    plot.setData(series);
    plot.draw();
}, 40);

This way you can totally control your animation wrapping and ensure that no pixelation/zooming is happening. 这样,您可以完全控制动画的环绕,并确保没有像素化/缩放发生。

I suggest using a monospaced font - that would eliminate the small spacing changes that you're seeing when the font size is changing on animation. 我建议使用等宽字体-可以消除动画中字体大小变化时看到的小间距变化。 Here is one that you can try on Google Web Fonts to see if, at least, you don't get the same issues. 您可以尝试以下一种使用Google网络字体的方法,看看是否至少不会遇到同样的问题。

http://www.fonts.com/search/web-fonts?searchtext=Helvetica+Monospaced&SearchType=WebFonts&src=GoogleWebFonts http://www.fonts.com/search/web-fonts?searchtext=Helvetica+Monospaced&SearchType=WebFonts&src=GoogleWebFonts

Using css letter-spacing ing the div.padder like: 使用CSS字母间距将div.padder为:

letter-spacing: 1px;

makes a bit of difference, thought I don't know if it's exactly the change you're hoping for. 有所作为,以为我不知道这是否正是您所希望的更改。

Hm. 嗯。 On my Mac, Firefox has no issues whatsoever, but Chrome is worse than I think you've noticed–the text exceeds the bounds of the container after the resize. 在我的Mac上,Firefox完全没有问题,但是Chrome的性能比我想像的还要差-调整大小后,文本超出了容器的范围。

I think we might need to back up one step and think the problem through one more time. 我认为我们可能需要备份一个步骤,再思考一次问题。 So: 所以:

  • there are arbitrarily-sized (though identical) containers of text ("text containers") 有任意大小(尽管相同)的文本容器(“文本容器”)
  • these containers are contained in another arbitrarily-sized container ("outer container") 这些容器包含在另一个任意大小的容器(“外部容器”)中
  • browsers wrap text according to how many words can fit into the width of the container (assuming overflow: auto; ) 浏览器根据可以容纳多少个单词的宽度来包装文本(假设overflow: auto;
  • so, assuming that all of these things maintain their relative sizes during the resize , the symptom you describe should be impossible (absent browser bugs and whatnot...) 因此, 假设所有这些事情在调整大小过程中都保持其相对大小 ,那么您描述的症状应该是不可能的(缺少浏览器错误和诸如此类的东西...)

That process of reasoning led me to check your jsfiddle, and sure enough, it is not the case that the various parts in your example retain their relative sizes during the resize . 这种推理过程使我检查了jsfiddle,并且可以肯定的是, 在调整大小期间,示例中的各个部分并非都保持其相对大小

On my machine (a laptop, so the pixel sizes are small, but that doesn't affect the logic here), the individual text containers (the innermost .padder ) have an aspect ratio of 143px / 53px = 2.70 , while the outer container ( .examples ) has an aspect ratio of 526px / 253px = 2.08 . 在我的机器上(一台笔记本电脑,因此像素很小,但这并不影响此处的逻辑),单个文本容器(最里面的.padder )的长宽比为143px / 53px = 2.70 ,而外部容器( .examples )的宽高比为526px / 253px = 2.08

This has the following consequences: 这具有以下后果:

  • the proportion of line-length to container-width changes during resize 调整大小时线长与容器宽度的比例 变化
  • so, at least for some lines in some cases, the locations of the linebreaks must also change during resize 因此,至少在某些情况下,对于某些行,换行的位置也必须在调整大小时更改

There is only one solution I can see to prevent the problem you're experiencing: force the outer container and text containers to have the same aspect ratio (ie since any solution that involves using actual HTML text and changes the aspect ratio during transformation will have the same issue). 我只能看到一种解决方案来防止遇到问题: 强制外部容器和文本容器具有相同的长宽比 (即,因为任何涉及使用实际HTML文本在转换过程中更改长宽比的解决方案都将具有相同的问题)。

This should also correct the problem that I'm seeing in Chrome where the text height exceeds the height of the container. 这也应该纠正我在Chrome中看到的文本高度超过容器高度的问题。 This is happening because the number of lines changes, but the height of the scaled-up text container is predetermined by the outer container (which is completely unconnected to the height of the text in the text container). 发生这种情况是因为行数发生了变化,但是按比例缩放的文本容器的高度由外部容器预先确定(外部容器完全与文本容器中的文本高度无关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM