简体   繁体   English

图像Blob加载和CSS过滤器

[英]Image blob loading and css filters

Is it somehow possible to do something, not on load, but on image render in javascript? 是否有可能做一些事情,而不是在加载时,而是在javascript中渲染图像?

We have a following scenario: 我们有以下情形:

  1. Image data is downloaded from a remote server with xhr, and turned into a blob. 使用xhr从远程服务器下载图像数据,然后将其转换为blob。
  2. The blobs url is passed to the image element image.src = url; Blob网址会传递到图片元素image.src = url;
  3. Image has an onload listener, which sets a css filter for an image, different for each image. Image具有onload侦听器,该侦听器为图像设置css过滤器,每个图像均不同。

Sometimes, especially for bigger images, there is a blink of an unfiltered image, and than a properly filtered image is displayed. 有时,特别是对于较大的图像,会出现未过滤图像的闪烁,然后显示正确过滤的图像。 On the other hand, setting the filter during the src assign causes the previously displayed image to flicker for a moment with a wrong filter. 另一方面,在src分配期间设置滤镜会导致先前显示的图像在滤镜错误的情况下闪烁片刻。 This happens in all major browsers. 在所有主要的浏览器中都会发生这种情况。

Any ideas how to make it silky smooth? 有什么想法如何使它柔滑吗? :) :)

Try this. 尝试这个。

  1. Image data is downloaded from a remote server with xhr, and turned into a blob. 使用xhr从远程服务器下载图像数据,然后将其转换为blob。
  2. Before assigning the src , hide the img contents, by changing either display or visibility attribute. 在分配src之前,请通过更改displayvisibility属性来隐藏img内容。

     display: none; //OR visibility: hidden; 
  3. The blobs url is passed to the image element image.src = url; Blob网址会传递到图片元素image.src = url; .

    This would load the img content but it would be invisible. 这将加载img内容,但将不可见。

  4. Now apply the CSS filters as per your requirement. 现在,根据您的要求应用CSS过滤器。 The img would be invisible so there would be no flicker or anything. img将是不可见的,因此不会出现闪烁或其他任何情况。

  5. Now change the display or visibility attribute back. 现在改回displayvisibility属性。

     display: initial; //OR visibility: visible; 

    Additionally, if you are using JQuery or some other library you can animate the transition using functions such as fadeIn() , slideDown() , so that it all appears super smooth and fancy. 另外,如果您使用的是JQuery或其他库,则可以使用诸如fadeIn()slideDown()类的动画效果对过渡进行动画处理,从而使它们看起来都非常流畅和漂亮。

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

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