Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

html - Combine two SVG LinearGradient

How can I combine two gradients with SVG?

One from left to right,

one from top to bottom

I want something like this:

Example:

color range 1 : (100,100,0) to (150,150,0)

color range 2 : (0, 0, 50) to (0,0,130)

I want to display every possible combination ( color range 1 + color range 2) Is that possible?

question from:https://stackoverflow.com/questions/65934383/combine-two-svg-lineargradient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I've commented you can try to use 2 overlapped rectangles each with one gradient.

Observation: in css the first image stays on top of the second. In SVG the second rect stays on top of the first. So in SVG you need to reverse the order.

#div,svg{
  height:40vh;
  width:90vw;
  border:solid;
  margin:.5em;
}

#div{
  background:linear-gradient(0deg, rgba(255,0, 0 ,.8), rgba(255,0, 0 ,0)),
             linear-gradient(90deg, rgba(0,255,0,.8), rgba(0,255, 0 ,0))
}
<div id="div"></div>

<svg viewBox="0 0 100 100" preserveAspectRatio="none">
  
<linearGradient id="lg1"  y1="100%" y2 = "0%" x2="0%">
     <stop offset = "0%" stop-color = "rgba(255,0, 0 ,.8)" />
     <stop offset = "100%" stop-color = "rgba(255,0, 0 ,0)" />
</linearGradient>
<linearGradient id="lg2"  x1="0%" x2 = "100%" >
     <stop offset = "0%" stop-color = "rgba(0,255, 0 ,.8)" />
     <stop offset = "100%" stop-color = "rgba(0,255, 0 ,0)" />
</linearGradient>
  <rect width="100" height="100" fill="url(#lg2)"/>
  <rect width="100" height="100" fill="url(#lg1)"/>  
</svg>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...