2017-04-02 13 views
1

html要素に適用すると、要素全体にグラデーションを適用するフィルタを作成したいと思います。 CSSのグラデーションは背景にのみ適用されるので、私が望むものではありません。 私が欲しいのは、要素に三角形と円が含まれているとします。要素にフィルタを適用すると、三角形と円に勾配が適用されます。svgフィルタを使用してグラデーションを適用する方法

.my-element{ 
 
    filter: url(svg.svg#filter); 
 
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 
 
     <defs> 
 
     <linearGradient id="lightGradient" gradientTransform="rotate(-5)"> 
 
      <stop stop-color="white" stop-opacity="0.4" offset="0%"/> 
 
      <stop stop-color="white" stop-opacity="0.5" offset="5%"/> 
 
      <stop stop-color="black" stop-opacity="0.2" offset="6%"/> 
 
      <stop stop-color="black" stop-opacity="0.3" offset="19%"/> 
 
      <stop stop-color="black" stop-opacity="0.2" offset="12%"/> 
 
      <stop stop-color="white" stop-opacity="0.8" offset="13%"/> 
 
      <stop stop-color="white" stop-opacity="0.9" offset="15%"/> 
 
      <stop stop-color="white" stop-opacity="0" offset="15%"/> 
 
      <stop stop-color="white" stop-opacity="0" offset="16%"/> 
 
      <stop stop-color="white" stop-opacity="0.8" offset="16%"/> 
 
      <stop stop-color="white" stop-opacity="0.9" offset="18%"/> 
 
      <stop stop-color="white" stop-opacity="0" offset="18%"/> 
 
      <stop stop-color="white" stop-opacity="0" offset="20%"/> 
 
      <stop stop-color="white" stop-opacity="1" offset="20%"/> 
 
      <stop stop-color="white" stop-opacity="1" offset="25%"/> 
 
     </linearGradient> 
 

 
     <rect id="recGradient" x="0" y="0" width="100%" height="100%" fill="url(#lightGradient)"/> 
 

 
     <filter id="filter" primitiveUnits="objectBoundingBox"> 
 
      <feImage x="0" y="0" width="100%" height="100%" preserveAspectRatio="none" xlink:href="#recGradient"/> 
 

 
      <feComposite operator="in" in="SourceGraphic"/> 
 
     </filter> 
 
     </defs> 
 
    </svg>

編集:上記のコードで要素がちょうどFirefoxで見えなくなり、クロームで動作しているように

これは私がこれまで持っているものです。

だから、私は間違っています。私はどのようにsvgフィルタを作成するか分からないので、任意の助けに感謝します。

+0

で矩形を作成してみてください勾配をつけて、直角にfeImageフィルターを向ける。 –

+0

@robertこんにちは、サンプルコードを表示できますか?私は ''を追加しようとしましたが、解析エラーが発生します:プレフィックスは名前空間にバインドされていません – BigName

+0

@RobertLongson ok xmlns:xlink = "http://www.w3.org/1999/xlink "、それは今解析するが、私は望む結果を得ていない。私は何をしたのかを示すために投稿を編集します。 – BigName

答えて

1

これは、ここで必要な機能を備えたクロスブラウザフィルタを構築する方法です。あなたがDataURI内に適用されたグラデーションを持つ矩形を定義してからfeImageを使ってそれをフィルタに引っ張り、それをCSSフィルタのSVGフィルタトラップドアを使ってHTMLコンテンツに適用するので、サイジングは厄介です。しかし、それは動作します。

(元のテキストの色を保持し、不透明度グラデーションを適用するか、白黒グラデーションを元の色に重ねるかどうかはわかりません。 feCompositeで「中」。これは、あなたがグラデーションまたはその逆にSourceGraphicとの不透明度を適用しているかどうかを管理するための「IN2」に変更する。)

div { 
 
    filter: url(#myGradient); 
 
}
<svg> 
 
    <defs> 
 
<filter id="myGradient" primitiveUnits="objectBoundingBox"> 
 
     <feImage x="0%" y="0%" width="100%" height="100%" preserveAspectRatio="none" xlink:href="data:image/svg+xml;charset=utf8,%3Csvg%20width%3D%22200px%22%20height%3D%22200px%22%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20xmlns%3Axlink%3D'http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink'%3E%3Cdefs%3E%3ClinearGradient%20id%3D'lightGradient'%20gradientTransform%3D'rotate(-5)'%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0.4'%20offset%3D'0%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0.5'%20offset%3D'5%25'%2F%3E%3Cstop%20stop-color%3D'black'%20stop-opacity%3D'0.2'%20offset%3D'6%25'%2F%3E%3Cstop%20stop-color%3D'black'%20stop-opacity%3D'0.3'%20offset%3D'19%25'%2F%3E%3Cstop%20stop-color%3D'black'%20stop-opacity%3D'0.2'%20offset%3D'12%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0.8'%20offset%3D'13%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0.9'%20offset%3D'15%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0'%20offset%3D'15%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0'%20offset%3D'16%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0.8'%20offset%3D'16%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0.9'%20offset%3D'18%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0'%20offset%3D'18%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'0'%20offset%3D'20%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'1'%20offset%3D'20%25'%2F%3E%3Cstop%20stop-color%3D'white'%20stop-opacity%3D'1'%20offset%3D'25%25'%2F%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%0A%20%20%3Crect%20id%3D'recGradient'%20x%3D'0%25'%20y%3D'0%25'%20width%3D'100%25'%20height%3D'100%25'%20fill%3D'url(%23lightGradient)'%2F%3E%3C%2Fsvg%3E"/> 
 
    <feComposite operator="in" in2="SourceGraphic"/> 
 
</filter> 
 
    </defs> 
 
</svg> 
 

 

 

 

 
<div> 
 
    Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text Imagine a very large piece of text 
 
</div>

+0

こんにちは、Firefoxを使用しています。コードスニペットを実行するときに何も表示されません。 – BigName

+0

uuuh。何かが私のデータURIにうんざりしています。私は –

+0

をデバッグしました。 - Firefoxは、UTF-8でエンコードされたURLを望んでいます。更新された例 - 今すぐ動作します。 –

関連する問題