I have downscaled images that onclick scale to the images original size. The image quality is very poor when the images are in a scaled down state. Any way to improve this?
(Here is a jsfiddle: /)
$(document).ready(function() {
var zoomed = false;
var card = $("#card0");
card.click(function() {
zoomFunction();
});
function zoomFunction() {
if (zoomed) { //card flipped so front is invisible and back is visible.
zoomed = false;
card.removeClass('zoom');
} else { //card not flipped so front is visible and back is invisible
zoomed = true;
card.addClass('zoom');
}
};
});
html {height: 100%;}
.zoom {transform: scale(1.0);}
img {
transform: scale(0.5);
transform-style: preserve-3d;
transition: 1s;
}
<img id="card0" src=".png">
I have downscaled images that onclick scale to the images original size. The image quality is very poor when the images are in a scaled down state. Any way to improve this?
(Here is a jsfiddle: https://jsfiddle/w9o2chmn/7/)
$(document).ready(function() {
var zoomed = false;
var card = $("#card0");
card.click(function() {
zoomFunction();
});
function zoomFunction() {
if (zoomed) { //card flipped so front is invisible and back is visible.
zoomed = false;
card.removeClass('zoom');
} else { //card not flipped so front is visible and back is invisible
zoomed = true;
card.addClass('zoom');
}
};
});
html {height: 100%;}
.zoom {transform: scale(1.0);}
img {
transform: scale(0.5);
transform-style: preserve-3d;
transition: 1s;
}
<img id="card0" src="http://valtterilaine.bitbucket/png/vihainen.png">
Share
Improve this question
edited May 11, 2016 at 12:29
Raidri
17.6k11 gold badges66 silver badges68 bronze badges
asked May 11, 2016 at 10:21
WaltariWaltari
1,2593 gold badges36 silver badges68 bronze badges
2
- 5 Not really...no, other than using SVG images, – Paulie_D Commented May 11, 2016 at 10:39
- Do you need to do that with css? Solution with jquery would give you better image quality. For example jsfiddle/w9o2chmn/15 – Jakob Commented May 11, 2016 at 20:08
4 Answers
Reset to default 1add
backface-visibility: hidden
https://jsfiddle/w9o2chmn/12/use
zoom:50%
instead oftransform: scale(0.5)
: https://jsfiddle/w9o2chmn/11/
both fixes are only works in chrome!
Try adding the following properties for the img tag. They may help, depending on the image.
img{
image-rendering: -moz-crisp-edges; /* Firefox */
image-rendering: -o-crisp-edges; /* Opera */
image-rendering: -webkit-optimize-contrast; /* Webkit */
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor; /* IE */
will-change: transform; // will consume additional memory
}
The will-change:transform
property will make scaled down images look better on it's own on Chrome due to the work being done on the GPU and, typically, improve speed (smoothness) of the transform animation. It will, however, take up device memory which might be a concern if there are a lot of cards, especially on mobile.
Another, more "guaranteed" solution would be to use the performant scale
transition only while animating, and when the animation pletes, remove the scale property and set the width
to half of the original width.
The basic algorithm for scaling down would be, for example:
- Set a transition that only affects transform (
transition:transform 1s;
) - Scale the image down with
transform:scale(0.5)
. The image now looks bad. - Remove the
transition:transform 1s;
property so that transforms are instant, then remove thetransform:scale(0.5)
property. The image now looks good but is of original size. - Set the width of the image to half at the same time the scale property is removed, so that the width change is invisible. The image is now half of the size and looks good.
This is better illustrated by the following modified jsfiddle
I would just use a thumbnail and set an onclick to load your larger image. You can format both the thumbnail and regular image separately this way using CSS if you don't wanna do it in jquery. But even more simple would be to save two versions previously edited.
It might be better to take the image and resize it in any photo editing application. I have found that if i need to resize the image and dont want a grainy and pixelated photo to do it in a third party app. If you want a transition when the images are clicked or hovered you could you the original image and make a second copy of the image in the desired size. Then, you can use another transition so when the photo is clicked it transforms the size of the image while looking normal even though it is two different images.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744677255a4587404.html
评论列表(0条)