29

我已经看到了一些围绕这个问题的问题,所以我希望这不是多余的。理想情况下,我想要一个image/svg+xml100% 的容器。 Colorzilla让我有了一个很好的开始data:image/svg+xml

<?xml version="1.0" ?>
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 1 1" preserveAspectRatio="none">
  <linearGradient id="grad-ucgg-generated" gradientUnits="userSpaceOnUse" x1="0%" y1="0%" x2="100%" y2="0%">
    <stop offset="0%" stop-color="#ffffff" stop-opacity="0"/>
    <stop offset="100%" stop-color="#ff0000" stop-opacity="1"/>
  </linearGradient>
  <rect x="0" y="0" width="1" height="1" fill="url(#grad-ucgg-generated)" />
</svg>

注意:width="100%" height="100%"我想采用这个渐变并旋转它,比如说65deg HTML5 canvas API 为我提供了一种很好的方式来构建这个图像,然后使用.toDataURL()PNG 来填充 IE8 和 IE7,但我想要一些可扩展的 IE9 .

所以目标是复制这个:

background: linear-gradient(bottom, rgba(239, 239, 214,0) 0%, rgba(239, 239, 214,.8) 100%),
linear-gradient(left,  rgba(239, 239, 214,0) 60%,rgba(207, 223, 144,1) 100%),
linear-gradient(right, rgba(239, 239, 214,0) 0%,rgba(239, 239, 214,1) 60%),
linear-gradient(top, rgba(239, 239, 214,0) 60%,#cfdf90 100%);
}

具有image/svg+xml100% 的宽度和高度。

我确实尝试了http://svg-edit.googlecode.com,但对于我想要做的编辑类型,界面不太直观。谢谢!

4

4 回答 4

50

要旋转渐变,您可以使用“gradientTransform”属性,如下所示:

<?xml version="1.0" ?>
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" 
 viewBox="0 0 1 1" preserveAspectRatio="none">
  <linearGradient id="grad-ucgg-generated" gradientUnits="userSpaceOnUse" 
   x1="0%" y1="0%" x2="100%" y2="0%" gradientTransform="rotate(65)">
    <stop offset="0%" stop-color="#ffffff" stop-opacity="0"/>
    <stop offset="100%" stop-color="#ff0000" stop-opacity="1"/>
  </linearGradient>
  <rect x="0" y="0" width="1" height="1" fill="url(#grad-ucgg-generated)" />
</svg>

于 2012-01-27T09:44:33.863 回答
13

请注意,该gradientTransform属性会根据其在 0,0 处的锚点旋转渐变。要从“中心”旋转它,您需要计算 x1、y1、x2 和 y2 的正确百分比。一个简单的 PHP 示例:

// Rotation can be 0 to 360
$pi = $rotation * (pi() / 180);
$coords = array(
    'x1' => round(50 + sin($pi) * 50) . '%',
    'y1' => round(50 + cos($pi) * 50) . '%',
    'x2' => round(50 + sin($pi + pi()) * 50) . '%',
    'y2' => round(50 + cos($pi + pi()) * 50) . '%',
)
于 2015-09-17T18:21:08.423 回答
11

Giel Berkers 的 Javascript 解决方案是:

// angle can be 0 to 360
var anglePI = (angle) * (Math.PI / 180);
var angleCoords = {
    'x1': Math.round(50 + Math.sin(anglePI) * 50) + '%',
    'y1': Math.round(50 + Math.cos(anglePI) * 50) + '%',
    'x2': Math.round(50 + Math.sin(anglePI + Math.PI) * 50) + '%',
    'y2': Math.round(50 + Math.cos(anglePI + Math.PI) * 50) + '%',
}
于 2016-10-19T14:53:34.090 回答
7
<linearGradient gradientTransform="rotate(65)">
于 2017-07-05T12:40:46.733 回答