Webkit-based blurry/distorted text post-animation via translate3d
This issue appears to affect all WebKit-based brow开发者_StackOverflow中文版sers, including the iPhone.
First some background. The site I'm working on uses a JavaScript-based 'slider' animation.
I'm using -webkit-transform: translate3d
to 'power' the actual animation. When using this method, as opposed to a JavaScript-based method, the text becomes all blurry once the content has been animated. This is especially noticeable on the iPhone.
A few workarounds I saw were to remove an relative positioning, which I did, and to add a rule for -webkit-font-smoothing: antialiased
, which I also did. Neither change made the slightest difference.
The only way I could make this work properly without blurry text was to use regular JavaScript for the animation and bypass the translate3d
altogether. I'd prefer to use translate3d
because it performs much faster on WebKit-enabled devices, but for the life of me I cannot figure out why it is affecting the text in such a poor way.
Any suggestions or solutions would be greatly appreciated.
None of these seem to have worked for me but I've found a slightly dirty solution which seemed to do the trick:
top: 49.9%;
left: 49.9%;
-webkit-transform: translate(-50.1%, -50.1%);
transform: translate(-50.1%, -50.1%);
As @Robert mentioned above, sometimes adding background helps, but not always.
So, for the example Dmitry added that's not the only thing you must do: except from the background, you must tell browser to explicitly use the proper anti-aliasing, so, there is a fixed Dmitry's example: http://jsfiddle.net/PtDVF/1/
You need to add these styles around (or for the) blocks where you need to fix the anti-aliasing:
background: #FFF; /* Or the actual color of your background/applied image */
-webkit-font-smoothing: subpixel-antialiased;
I had the exact same problem described in Ken Avila's post: CSS: transform: translate(-50%, -50%) makes texts blurry
The problem was of course that I used transform: translate(-50%, -50%) which made my centered content become blurry, but only in safari on osx.
It is not only the text that becomes blurry, but the entire content, including images. I read on: http://keithclark.co.uk/articles/gpu-text-rendering-in-webkit/ that the "blurryness" is due to that the element is rendered on a non-integer boundary.
I also discovered that I could avoid using transform translate on the horizontal part of my centering from this post: https://coderwall.com/p/quutdq/how-to-really-center-an-html-element-via-css-position-absolute-fixed -The only minus was that I had to introduce a wrapper.
I discovered that using transform: translateY(-50%) didn't create any "bluryness", maybe because my element has a set height and thus does not end up rendering on a non-integer boundary.
My solution therefore ended up like this:
.wrapper {
position: fixed;
left: 50%;
top: 50%;
}
.centered {
position: relative;
left: -50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
<div class="wrapper">
<div class="centered">
Content
</div>
</div>
Elements you are translating must be divisible by two with even numbers.
It's important that whatever element you're trying to shift over by half is divisible by two for both it's width and height. Very similar to the responsive images, when things can be moved by 50% without splitting pixels.
A div with a width of: 503px and a height of 500px will cause blurring, no matter which way you move it or by how much when using translateX or Y. Using transform, it utilizes GPU graphics accelerator which should result is very crisp, smooth edges. It might also be a good idea to set box-sizing: border-box; to ensure calculated widths include padding and borders.
Be careful when using percentage widths. If it's relative to screen size, ever other screen pixel width will cause this blur.
I fixed this problem by adding a translate3d
style to the element before any animation occurs.
-webkit-transform: translate3d(0,0,0);
This is the best solution
translateX(calc(-50% + 0.5px))
Probably the easiest solution:
transform: translate(-50%, -50%) scale(2);
zoom:.5;
Scale-and-zoom takes care of rounding the pixel values to full numbers
None of these answers worked for me but using
display: inline-table;
did the trick
Neil Taylor's solution was the best, but it can still be improved:
transform: translateX(-50%) translateX(.5px)
This way it has 2 advantages:
- It works in crappy browsers like IE11 (which doesn't support calc inside translate)
- It only calculates at parse time, not every time the browser evaluates.
I hope this is about centering the object into exact center of the screen. Blurring happens with the transform: translate(-50%,-50%);
so instead doing
position: absolute;
margin:0;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
I tried injecting style in to element using javascript. (React.js)
const node = ReactDOM.findDOMNode(this);
var width = node.offsetWidth;
var height = node.offsetHeight;
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
style={
left : (windowWidth/2) - (this.state.width/2) + 'px',
right: (windowWidth/2) - (this.state.width/2) + 'px',
top: (windowHeight/2) - (this.state.height/2) + 'px',
bottom: (windowHeight/2) - (this.state.height/2) + 'px'
}
inject style into element by javascript style
Eg.
export default class Dialog extends React.Component {
constructor(props) {
super(props);
this.state = {
width: '0px',
height: '0px'
};
}
setWidthhandHeight(node){
if (this.state.width !== node.offsetWidth) {
this.setState({
width: node.offsetWidth,
height: node.offsetHeight
});
}
}
componentDidMount() {
this.setWidthhandHeight(node);
}
render() {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
return (
<dialog className={this.props.className + " dialog"}
style={{
left : (windowWidth/2) - (this.state.width/2) + 'px',
right: (windowWidth/2) - (this.state.width/2) + 'px',
top: (windowHeight/2) - (this.state.height/2) + 'px',
bottom: (windowHeight/2) - (this.state.height/2) + 'px'
}}>
{this.props.title ? (
<header><span>{this.props.title}</span></header>
)
: null
}
{this.props.children}
</dialog>
);
}
}
Reduce the vertical margin(margin-top or margin-bottom) or height of any element in the container on which transform translate property is being applied by just 1 pixel.
Resetting Chrome's zoom to 100% worked for me.
If the percentage is used for animation it still acceptable if we also consider it's performance, but when you're not using translate
for animation it would different because the text will always blurry.
When using percentage it's possible of having fractional number like 0.5px
and that the reason why the text was blurry. On my experiment using translate(0.5px, 0.5px)
would make the text very blurry, so you should avoid fractional number between 0.3px <=> 0.7px
while 0.2px and 0.8px
still acceptable.
My suggestion is using JavaScript and turn the percentage into rounded pixel for animating the element, and using pixel instead of percentage if translate
is being used for positioning the element.
The other alternative is using left
and top
to set the initial position with percentage then use transform: translate()
with pixel for the target position and animate the transform
only with transition-property: transform;
For an alternative solution try:
-webkit-text-stroke: 0.35px
Using zoom
solved it for me.
I just added zoom: 1.04;
Solution found in my case was setting a height
value of the first child element
@kizu These answers and suggestions do not help. For your jsfiddle you need to add
-webkit-perspective-origin: 50% 50%; -webkit-perspective: 1400px;
to the parent element of the element you want to use translate3d on, otherwise translating the z-axis doesn't actually do anything. In this case you are applying it to the button you click, I think you meant to apply it to the content.
But anyway, adding those to activate z-axis change causes the blur to the button in your example.
I would love to find a solution to this as well, I fear there isn't one.
I use will-change
on these types of issues, it usually solves scaling issues or rounding errors, such as seeing a 1px white line separating elements for example.
will-change: transform;
Read more here: https://developer.mozilla.org/en-US/docs/Web/CSS/will-change
Using the HTML5 doctype solved the issue for me.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
精彩评论