How do i fill SVG with color - javascript

Can somebody help me with css and javascript?
I need to create a page with scroll animation.
https://meetlima.com/how-it-works.php?lang=en
The 1,2,3 must be filled with color.
<div class="container">
<div class="een">
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
<g>
<title>Layer 1</title>
<text
stroke="#000000" transform="matrix(9.676384925842285,0,0,9.676384925842285,-1584.9315026253462,-1064.0772700458765) "
xml:space="preserve"
text-anchor="middle"
font-family="Fantasy"
font-size="24"
id="svg_1"
y="129.86676"
x="174.14887"
stroke-width="0"
fill="#bfbfbf">1</text>
</g>
</svg>
</div>
</div>

In fact on page you've shared as exapmle the svg numbers have transparent color.
Initially you see grey background of div.how-it-works__step__bg__inner.
On backface there is a div.how-it-works__faker which has fixed position and lowest z-index. Also has inner element div.how-it-works__faker__inner with blue background
And when you scrolling down you may see the part of div.how-it-works__faker__inner under svg image.
to make it more clear check this screen just to understand what would happens in case you set higher z-index for div.how-it-works__faker
screenshot

Related

Keeping parts of an SVG at fixed size

Essentially I needed to make the center "cut-out" keep a fixed shape and size regardless of vector scale. Is there a way to achieve this?
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" preserveAspectRatio="none" style="fill-rule:evenodd;" viewBox="0 0 2802 2657">
<path d="M290 4c-95,733 -191,1466 -286,2200 760,150 1520,300 2279,450 172,-223 343,-446 515,-669 -114,-572 -229,-1144 -343,-1716 -722,-88 -1444,-176 -2165,-264zm696 1027c-103,111 -205,222 -308,333 94,111 188,222 282,333 342,-205 684,-410 1026,-616 -333,-17 -667,-34 -1000,-51z"/>
</svg>
So I managed to do something after some editing to your SVG.
To achieve what you're asking you'll need to use / have :
- the SVG mask attribute
- A very large shape for the mask ( as much large as the max scale you want to use on the visible shape )
- The shape that you want to resize
- Resize the shape with transforms
Your SVG should looks like the following
<svg>
<defs>
<mask id="theMask">
<path fill="#ffffff" d=""/>
</mask>
</defs>
<g mask="url(#theMask)">
<path fill="#ffffff" id="shapetoresize" d=""/>
</g>
</svg>
I posted a pen as a "Proof of concept"
Feel free to fork it and use it to achieve what you're trying to do.
Codepen
note: as pointed out by #thioutp , The JS is only for demo purposes, you don't need GSAP to achieve this.

How to keep a constant font-size in svg

I'm new with SVG and I try to add some text in a shape that I created. I've done it like this : https://fiddle.jshell.net/30kL3mzt/
But the text in the SVG change with the polygone. Is it possible to keep a constant size of text? Or at least to control the size of letters?
<div id="tab">
<div class='chevron'>
<svg width="25%" height='100%' viewBox="0 0 20 20" preserveAspectRatio="none">
<polygon fill=steelblue stroke-width=0
points="0,0 2,10 0,20 18,20 20,10 18,0" />
<text x="0" y="10" font-family="Verdana" font-size="2" >I love SVG!</text>
</svg>
</div>
</div>
<div id="middle">
<svg width="25%" height='100%' viewBox="0 0 20 20" preserveAspectRatio="none">
<polygon fill=steelblue stroke-width=0
points="0,0 2,10 0,20 18,20 20,10 18,0" />
<text x="0" y="10" font-family="Verdana" font-size="2" >I love SVG!</text>
</svg>
</div>
Why do you need the textual content to be in the SVG if you don't want it to scale? Probably it's better to have the text outside and style it with CSS to be where you want it.
Instead of adding svg directly inside your HTML, why not use a font library like fontello/icomoon?
I know for a fact, when you choose the same grid size for your icons you can scale up your icons separately from your text.
This is because an icon from a font file uses CSS on the pseudo-elements :before and/or :after. Combined with line-height you have complete control on the size of an icon. => Line-height is best calculated by the height of the layer divided by the font-size.
For example: 24px div / 20px text = 1.2
In my opinion you're too far off from an x-browser solution to your problem.
convert your shapes into font library (import svg files)
add the proper CSS from the download (or read the readme.txt)
make sure to support .woff on the server
use font icon classes where needed
override the proper font-sizes to your need in CSS (there's your solution)

How can I cut text from a shape in SVG?

I have an SVG file that looks like below:
Is there a way to make the text transparent? That is, instead of a fill color I want to cut out the layers and show what's in the background (of the SVG i.e. whatever lies underneath the SVG). In other words, make the intersection of the path & text to be transparent?
Contents of the SVG file:
<svg width="36.087" height="34.314" viewBox="0 0 36.087 34.313999" x="1190.56" y="753.5780000000001">
<path fill="#63a95c" d="M36.087 13.107l-13.305-.66L18.047 0l-4.742 12.446L0 13.106l10.377 8.352L6.89 34.314l11.157-7.285 11.14 7.284-3.475-12.856" fill-rule="evenodd"/>
<text font-size="10px" x="10.498" y="23.484" fill="#ffffff" fill-opacity="1" font-family="OpenSans-Bold">8.5</text>
</svg>
I tried changing the transparency of the text element, but that only affects the text. The text inside the SVG is variable is populated dynamically so I can't "pre-process" the SVG file. Is there a way perhaps using evenodd fill or something similar to create an "exclusion" for intersection? Is it possible using one of the SVG JS libraries such as snap.svg or svg.js?
Edit:
The final SVG should look like this:
The SVG code posted above is for the star and the text. The final SVG should have the background color showing through the text while retaining the outer shape of the star.
Create a mask, put the text in it via a text element and then use the mask on the shape you want to clip a hole in. Something like this...
head, body {
width:100%;
height:100%;
}
<svg width="100%" height="100%" viewBox="0 0 200 200">
<defs>
<mask id="sample" maskUnits="userSpaceOnUse">
<rect width="100%" height="100%" fill="white"/>
<text x="12" y="23" font-size="10" font-family="Impact">9.0</text>
</mask>
</defs>
<path fill="#63a95c" d="M36.087 13.107l-13.305-.66L18.047 0l-4.742 12.446L0 13.106l10.377 8.352L6.89 34.314l11.157-7.285 11.14 7.284-3.475-12.856" fill-rule="evenodd" mask="url(#sample)"/>

SVG Hover State with Multiple Elements

Good afternoon everyone,
I'm defining an SVG on my page with the following defs.
<svg width="0" height="0">
<defs>
<g id="stroke-hexagon">
<polygon fill="#002663" stroke="#FFFFFF" stroke-width="6" stroke-miterlimit="12" points="57.8,185 5.8,95 57.8,5 161.8,5 213.8,95 161.8,185 "/>
</g>
<g id="hexagon">
<polygon fill="#006890" points="52,180 0,90 52,0 156,0 208,90 156,180 "/>
</g>
</defs>
</svg>
...and implementing it later in the HTML using this:
<svg width="208px" height="180px" viewBox="0 0 208 180" >
<use xlink:href="#hexagon"></use>
<text class="faicon" x="50%" y="70px" fill="white" font-size="80px" text-anchor="middle">&#xf040</text>
<text text-anchor="middle" x="50%" y="70%" fill="white">Logo Here</text>
</svg>
Works totally fine. I am also able to style the polygon's fill with simple CSS. Looks like this:
#hexagon:hover polygon {
fill:#990000;
}
The hover effect fails, however, whenever the mouse leaves the polygon and instead hovers over either of the 'text' elements within the svg. Is there a way to define a CSS rule that prevents this behavior. Or, would it be better (easier) to change the attribute using JS / jQuery?
Thanks!
Your texts are rendered on top of your polygon and are therefore intercepting mouse events. You should set up a css rule like
text {
pointer-events: none;
}
This will prevent the text from becoming a target of mouse events which should give you the desired hover effect for the polygon.

Color SVG image with CSS

Is there a way I can apply colors to SVG image as a whole? not going down to each path and circle and line I have and doing it one by one?
I tried to group my svg elements with
<g class="myImage">
and in the myImage class i put fill:red to make it apply to all the elements in that group, but that doesnt work!!
How can I make it so I can only apply a color once and it goes to the whole image or elements in a group?
--added code
This is my SVG file (this is just a sample, i know the circle repeats 3 times)
<?xml version="1.0" standalone="no"?>
<?xml-stylesheet href="../css/logo.css" type="text/css"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<g class="logo">
<circle cx="50%" cy="50%" r="35%"/>
<circle cx="50%" cy="50%" r="35%"/>
<circle cx="50%" cy="50%" r="35%"/>
</g>
</svg>
and in my logo.css file i have a class
.logo {
fill:red;
}
Hope this helps
You're doing it right. The fill should inherit from the <g> element and all the circles will be red, one on top of another. Firefox displays a big red circle.

Categories

Resources