I'm trying to fill SVG image with a pattern in HTML, but I'm not successfull. If I fill with the pattern path, it works. But I cannot apply it onto svg image.
Could you help me please?
Here is example.
Here is example code:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" width="400" height="400">
<defs>
<pattern id="image" x="0" y="0" width="400" height="400" patternUnits="userSpaceOnUse">
<image x="0" y="0" xlink:href="latka.jpg" width="100" height="100" />
</pattern>
</defs>
<image x="0" y="0" width="400" height="400" xlink:href="kosile.svg" fill="url(#image)"/>
</svg>
It makes no sense to apply a fill attribute to an embedded SVG. I assume what you were trying to do is create a tiled background on which to superimpose the linked SVG. The easiest way to do this is by adding a <rect> element filled with the background pattern, then put your embedded SVG image on top of this.
Here's an example:
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="200" height="200" viewBox="0 0 200 200">
<defs>
<!-- define a pattern using a yellow tile image -->
<pattern id="bgimg" x="0" y="0" width="60" height="60"
patternUnits="userSpaceOnUse">
<image x="0" y="0" width="60" height="60"
xlink:href="http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Shades_of_yellow.png/60px-Shades_of_yellow.png" />
</pattern>
</defs>
<!-- use this pattern to fill the SVG background -->
<rect x="0" y="0" width="200" height="200" fill="url(#bgimg)" />
<!-- Embed another SVG (purple circle) on top of this background -->
<image x="40" y="40" width="120" height="120"
xlink:href="http://upload.wikimedia.org/wikipedia/commons/5/5e/FF0084_circle.svg" />
</svg>
Related
<div>
<svg id="svg_viewport"
width="800" height="800"
style="background-color: pink"
>
<svg id="o_1"
x="10" y="10" width="200" height="200"
>
<image href="https://www.1kfa.com/table/img/image.png"
height="200" width="200"></image>
</svg>
<svg id="o_2"
x="0" y="100" width="100" height="100"
>
<rect id="r_2"
width="100" height="100"
fill="green"
></rect>
</svg>
</svg>
</div>
This works in Chromium, but in Firefox, the green rect gets cut off. It's like the browser is rendering it "inside" the image of svg o_1.
Has anyone faced this before? Workarounds?
I think I've found a workaround.
By adding a viewBox, Firefox decides to render it properly. See line 8:
<div>
<svg id="svg_viewport"
width="800" height="800"
style="background-color: pink"
>
<svg id="o_1"
x="10" y="10" width="200" height="200"
viewBox="0 0 200 200"
>
<image href="https://www.1kfa.com/table/img/image.png"
height="200" width="200"></image>
</svg>
<svg id="o_2"
x="0" y="100" width="100" height="100"
>
<rect id="r_2"
width="100" height="100"
fill="green"
></rect>
</svg>
</svg>
</div>
I have two SVG's - one being logo of rectangle shapes and the other being a single svg of a pattern image for the background.
The logo gets repeated across the document multiple times and when the <rect> is set to be filled by #rect-fill, the multiple instances of the pattern svg with id of #rect-fill elements breaks it and the background image does not show, so now I seperated them.
Now, since the pattern id is outside of the #rect-container svg, the pattern image does not show and no image can be loaded.
Is there anyway to reference a pattern for a rect outside of its containing <svg> in another <svg>?
<!-- the logo svg -->
<svg id="rect-container" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- left squares -->
<rect />
<rect />
<rect />
<!-- right squares -->
<rect />
<rect />
<rect />
</svg>
<!-- the background image pattern -->
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
<pattern id="rect-fill" patternUnits="userSpaceOnUse" x="0" y="0" width="1112" height="100%">
<image xlink:href="../assets/images/hero.png" width="100%" height="100%" preserveAspectRatio="xMinYMid slice"/>
</pattern>
</svg>
<style>
/* Reference the pattern outside of the rect's svg */
#rect-container rect {
fill(#rect-fill);
}
</style>
First, you need to create one svg as a resource available to the others using <defs>.
<!-- the background image pattern -->
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
<defs>
<pattern id="rect-fill" patternUnits="userSpaceOnUse" x="0" y="0" width="1112" height="100%">
<image xlink:href="../assets/images/hero.png" width="100%" height="100%" preserveAspectRatio="xMinYMid slice"/>
</pattern>
</defs>
</svg>
Then you will be able to reuse this element. More information here
<!-- the logo svg -->
<svg id="rect-container" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- left squares -->
<rect fill="url(#rect-fill)"/>
</svg>
I am trying to get areas of states to fill up based off certain criteria. I have a pattern and it is working, but it seems like the pattern fills up the territory based off the shape of the territory. I want to have the lines the same when they fill every single shape. You can see in the map below that the lines are not at the same degree nor are they the same size in each state. Is this possible? I am new to SVG.
Patterns:
<defs>
<pattern id="pattern-stripe" width="5" height="4" patternUnits="userSpaceOnUse" patternTransform="rotate(75)">
<rect width="2" height="4" transform="translate(0,0)" fill="white"></rect>
</pattern>
<mask id="mask-stripe">
<rect x="0" y="0" width="100%" height="100%" fill="url(#pattern-stripe)" />
</mask>
<pattern id="other" height="100%" width="100%" patternContentUnits="objectBoundingBox">
<image height="10" width="10" preserveAspectRatio="none" xlink:href="images/other-stripe.png" />
</pattern>
<pattern id="blue" height="100%" width="100%" patternContentUnits="objectBoundingBox">
<image height="10" width="10" preserveAspectRatio="none" xlink:href="images/blue-stripe.png" />
</pattern>
<pattern id="orange" height="100%" width="100%" patternContentUnits="objectBoundingBox">
<image height="10" width="10" preserveAspectRatio="none" xlink:href="images/orange-stripe.png" />
</pattern>
</defs>
I tried to replicate the error in CodePen, but without the complete SVG code, it didn't work. However, I think if you replace preserveAspectRatio="none" with preserveAspectRatio="xMidYMid meet" it will fix your problem. If it doesn't, but it makes progress, try the other solutions here.
You want preserveAspectRatio="xMinYMin slice". Meet will change your aspect ratio until the minimum of height or width fits the bounding box. Slice will crop the fill to the available space.
Your problem is that your patterns are using patternContentUnits="objectBoundingBox".
This means that the stripey image in your patterns is being scaled to match the size of the shape the pattern is applied to.
Demo:
<svg>
<defs>
<pattern id="blue" height="100%" width="100%" patternContentUnits="objectBoundingBox">
<image height="1" width="1" preserveAspectRatio="none" xlink:href="http://placekitten.com/100/100" />
</pattern>
</defs>
<rect x="0" y="0" width="150" height="150" fill="url(#blue)"/>
<rect x="175" y="25" width="100" height="100" fill="url(#blue)"/>
</svg>
To fix this, you need to use patternContentUnits that are independent of the size of the shape the pattern is applied to. You do this by specifying patternContentUnits="userSpaceOnUse".
<svg>
<defs>
<pattern id="blue" height="100%" width="100%" patternContentUnits="userSpaceOnUse">
<image height="150" width="150" preserveAspectRatio="none" xlink:href="http://placekitten.com/100/100" />
</pattern>
</defs>
<rect x="0" y="0" width="150" height="150" fill="url(#blue)"/>
<rect x="175" y="25" width="100" height="100" fill="url(#blue)"/>
</svg>
I have the following svg in a webpage. Besides the title and desc tags I have added, is there anything else I can do to make this svg more accessible? For instance, are there attributes, roles, etc. I can add to the image tags for visually impaired users?
<svg id="SvgjsSvg1001" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.dev/svgjs">
<rect id="SvgjsRect1008" width="206" height="420" x="0" y="0" fill="#80ff72"></rect>
<rect id="SvgjsRect1010" width="206" height="420" x="246.5" y="0" fill="#80ff72"></rect>
<rect id="SvgjsRect1011" width="40.5" height="420" x="206" y="0" fill="#7ee8fa"></rect>
<image id="SvgjsImage1012" xlink:href="./assets/river-crossing/common/raft.svg" width="206" height="206" x="206" y="107"></image>
<image id="SvgjsImage1013" xlink:href="./assets/river-crossing/goat-apple-wolf/goat.svg" width="98" height="98" x="0" y="0"></image>
<image id="SvgjsImage1014" xlink:href="./assets/river-crossing/goat-apple-wolf/apple.svg" width="98" height="98" x="0" y="108"></image>
<image id="SvgjsImage1015" xlink:href="./assets/river-crossing/goat-apple-wolf/wolf.svg" width="98" height="98" x="0" y="216"></image>
<image id="SvgjsImage1016" xlink:href="./assets/river-crossing/goat-apple-wolf/farmer.svg" width="98" height="98" x="0" y="324"></image>
<title>Animation</title>
<desc>Displays the animation</desc>
</svg>
A clear 'title' and a descriptive 'desc' are essential for Screen reader users to understand what the image conveys. Genereic information like 'Animation', 'Displays Animation' etc. doesn't help visually impaired users. Make it clear and descriptive if possible.
SVG title and desc are not uniformly supported by screen readers. role="img" and aria-labelledby should be used in the SVG tag to include title and desc id to arrive at a more consistent accessible name for the image.
<svg id="SvgjsSvg1001" width="100%" height="100%" role="img" aria-labelledby="titleid descid" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.dev/svgjs">
<rect id="SvgjsRect1008" width="206" height="420" x="0" y="0" fill="#80ff72"></rect>
<rect id="SvgjsRect1010" width="206" height="420" x="246.5" y="0" fill="#80ff72"></rect>
<rect id="SvgjsRect1011" width="40.5" height="420" x="206" y="0" fill="#7ee8fa"></rect>
<image id="SvgjsImage1012" xlink:href="./assets/river-crossing/common/raft.svg" width="206" height="206" x="206" y="107"></image>
<image id="SvgjsImage1013" xlink:href="./assets/river-crossing/goat-apple-wolf/goat.svg" width="98" height="98" x="0" y="0"></image>
<image id="SvgjsImage1014" xlink:href="./assets/river-crossing/goat-apple-wolf/apple.svg" width="98" height="98" x="0" y="108"></image>
<image id="SvgjsImage1015" xlink:href="./assets/river-crossing/goat-apple-wolf/wolf.svg" width="98" height="98" x="0" y="216"></image>
<image id="SvgjsImage1016" xlink:href="./assets/river-crossing/goat-apple-wolf/farmer.svg" width="98" height="98" x="0" y="324"></image>
<title id="titleid">Clear title</title>
<desc id="descid">Description of the image</desc>
</svg>
The goal is to show image but with another color inside svg.
I tried -webkit-mask-image with background-color, but this combination does not work in svg. Here is the example:
<div class="dashboard-buttons">
<svg width="367" height="243.5" viewBox="252.5 60.5 367 243.5">
<g>
<rect width="46" height="46" rx="4" ry="4" transform="translate(-23, -23)" style="fill:white" x="560" y="224"></rect>
<a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#element3" href="#element3" onclick="window.location.href = '#element3';">
<image height="32" width="32" transform="translate(-16, -16)" x="560" y="224" style="background-color: Red; -webkit-mask-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuM4zml1AAAACqSURBVFhH7ZYhDsJAFESHBMkBOCYGyQV6BQ6BQ0EwWByiuwj0Fk5AgO00+XJpCd20gpnkqfZnXrJmoCiVx+rmcWyD/2zCGXM7yZvK4c2S2InDiRIzO8uXZNkHgsM2HjC10zxJFXWwjsDEzvsnUfANVz7JpS93j+JXgSyEEs9RBRokIAEJSEACEpDA+AIcpa/UhyHg2n6A02jJabSjyH5Qms4SC1uGyt8GqAEZtGBDEZcerAAAAABJRU5ErkJggg==');width: 32px; height: 32px;"></image>
</a>
<a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#element3"><text filter="url(#fillbackground)" x="560" y="257">out</text></a>
</g>
</svg>
</div>
If you remove svg tags from it then this sample works and the color changed.
Do you know how to change image color inside svg correctly?
EDIT:
The following code works in Chrome, but not in Firefox:
<div class="wrapper">
<svg width="200" height="300" class="svg">
<defs>
<mask id="mask">
<image xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuM4zml1AAAACqSURBVFhH7ZYhDsJAFESHBMkBOCYGyQV6BQ6BQ0EwWByiuwj0Fk5AgO00+XJpCd20gpnkqfZnXrJmoCiVx+rmcWyD/2zCGXM7yZvK4c2S2InDiRIzO8uXZNkHgsM2HjC10zxJFXWwjsDEzvsnUfANVz7JpS93j+JXgSyEEs9RBRokIAEJSEACEpDA+AIcpa/UhyHg2n6A02jJabSjyH5Qms4SC1uGyt8GqAEZtGBDEZcerAAAAABJRU5ErkJggg==">
</image>
</mask>
</defs>
<rect width="50" height="50" style="fill:blue;" x="0" y="0"
mask="url(#mask)"></rect>
</svg>
</div>
Can't see your example, but:
<rect x="10" y="10" width="100" height="100" stroke="blue" fill="purple"
fill-opacity="0.5" stroke-opacity="0.8"/>
Use the fill option!
EDIT:
Now I can see your code.
If you insert an image as PNG:
data:image/png
Then I doubt you can change the color with HTML directive. PNG are bitmaps and not SVG, you would have to edit the PNG itself to achieve that.