Create intersection of lines and circle using SVG and CSS - css

I am using google maps for interactive map. There is a limiatation the markers will always be a circle. I need circle along with intersection of 2 lines as shown below.
I dont have access to anything except CSS. Would it be possible to add CSS so I can get intersection of lines along SVG circle.
Here is my JSfiddle code where I tried to add lines to SVG circle.
http://jsfiddle.net/sreeram62/8QRAJ/
<svg height="210" width="500">
<circle cx="50" cy="50" r="5" stroke="black" stroke-width="0" fill="#00FFFF" />
Sorry, your browser does not support inline SVG.
</svg>
circle:after {
content: '';
display: block;
height: 1px;
width: 300px;
position: absolute;
top: 50%;
left: -125px;
background-color: #f00;
}
circle:before {
content: '';
display: block;
height: 300px;
width: 1px;
position: absolute;
left: 50%;
top: -125px;
background-color: #f00;
}

I think it's impossible or too hard, but I made sample code for this.
I use inline svg at filter property of circle element.
When I tested, this code worked fine at firefox only, not at chrome.
(I don't know this sample is useful for using google maps.)
see http://jsfiddle.net/defghi1977/39gjd/
svg:
<svg width="200px" height="200px" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<circle cx="100" cy="100" r="10" fill="red"/>
</svg>
css:
circle{
filter: url("data:image/svg+xml;utf-8,<svg xmlns='http://www.w3.org/2000/svg'><defs><filter id='f' x='-3' y='-3' width='7' height='7' primitiveUnits='objectBoundingBox'><feFlood flood-color='black' x='-3' y='0.45' width='7' height='0.1' result='hb'/><feFlood flood-color='black' x='0.45' y='-3' width='0.1' height='7' result='vb'/><feMerge><feMergeNode in='SourceGraphic'/><feMergeNode in='hb'/><feMergeNode in='vb'/></feMerge></filter></defs></svg>#f");
}

Related

Make svg element adapt to container dimensions

I've the following SVG path that is intended to be used as a border of any possible width and height. But currently, I am struggling with the following 2 points and appreciate a lot if some can share some ideas :-)
1. Is it possible to detach SVG from the viewBox and adapt to parent dimension?
2. Is it possible to detach SVG stroke width and always be as defined? (4px)
.c500x200 {
width: 500px;
height: 200px;
}
.c200x600 {
width: 200px;
height: 600px;
}
svg {
width: 100%;
height: 100%;
}
svg path {
stroke-width: 4px;
stroke: #d22f2c;
fill: none;
}
<div class='c500x200'>
<svg data-name="Layer 1" viewBox="0 0 357.5 248" xmlns="http://www.w3.org/2000/svg">
<defs>
</defs>
<path transform="translate(-289 -320)" d="M623,565.5H291.5v-243h341v231s-1,12,14,12"/>
</svg>
</div>
<div class='c200x600'>
<svg data-name="Layer 1" viewBox="0 0 357.5 248" xmlns="http://www.w3.org/2000/svg">
<defs>
</defs>
<path transform="translate(-289 -320)" d="M623,565.5H291.5v-243h341v231s-1,12,14,12"/>
</svg>
</div>
Is it possible to detach SVG from the viewBox and adapt to parent dimension?
The viewBox is the way you get an SVG to adapt to a parent's dimensions. What I think you are asking is whether it is possible to get a viewBox to stretch to fill a parent container.
The answer is yes. You can do that using preserveAspectRatio="none". But note that this stretches the scale of the shape. So curves in the shape will be distorted. See below.
Is it possible to detach SVG stroke width and always be as defined? (4px)
Yes. You can set vector-effect: non-scaling-stroke on the path elements. See below.
.c500x200 {
width: 500px;
height: 200px;
background-color: linen;
}
.c200x600 {
width: 200px;
height: 600px;
background-color: linen;
}
svg {
width: 100%;
height: 100%;
}
svg path {
stroke-width: 4px;
stroke: #d22f2c;
fill: none;
vector-effect: non-scaling-stroke;
}
<div class='c500x200'>
<svg data-name="Layer 1" viewBox="0 0 357.5 248" preserveAspectRatio="none">
<defs>
</defs>
<path transform="translate(-289 -320)" d="M623,565.5H291.5v-243h341v231s-1,12,14,12"/>
</svg>
</div>
<div class='c200x600'>
<svg data-name="Layer 1" viewBox="0 0 357.5 248" preserveAspectRatio="none">
<defs>
</defs>
<path transform="translate(-289 -320)" d="M623,565.5H291.5v-243h341v231s-1,12,14,12"/>
</svg>
</div>

CSS mask not working properly in Angular 5

I'm trying to create an Angular 5 app that consists of a masked/clipped image.
In plain ol' HTML + CSS, I can achieve what I want with the following code pen: https://codepen.io/earthican/pen/BJjgRv
body,
html,
.img {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
background-color: white;
overflow: hidden;
}
svg {
pointer-events: none;
width: 100%;
height: 100%;
position: absolute;
background: transparent;
fill: transparent;
mask: url(#polygon-mask);
}
<svg id="mask">
<defs>
<mask id="polygon-mask" x="0" y="0" width="960" height="588" >
<rect id="reverse-mask" fill="white" x="0" y="0" width="960" height="588" ></rect>
<polygon fill="red" points="112,62 162,112 162,162 62,162 62,112"></polygon>
</mask>
</defs>
<rect width="960" height="588" fill="teal"></rect>
</svg>
<div class="img">
<img src="https://i.pinimg.com/originals/53/5e/5b/535e5b3744dbb8264a7ebba5f29f44ca.jpg" style="margin-left: 0px; margin-top: 0px;">
</div>
However, I'm having trouble trying to convert the above to Angular. Here's what I have so far: https://plnkr.co/edit/w2gVe91NEIdUlCWs3qkN?p=preview
I think I'm starting to realize that Angular doesn't play very well with SVG. I should also point out that I'm fairly new to Angular 2+ and SVG, so I can't really be sure. If anyone can help or point out some useful resources, that will be greatly appreciated!
I have solved this by moving the mask attribute from CSS to the SVG tree, i.e.:
<svg mask="url(#polygon-mask)">
...
</svg>

How to make an svg masked image compatible with Internet Explorer

Lately i have created an svg masked image that works perfectly in Chrome but done not work in My version of Internet explorer. Here is the End result expected from my svg
This is my svg code
<svg width="0" height="0" viewBox="0 0 160 160">
<defs>
<clipPath id="shape">
<path d="M10,70 Q0,80 10,90 L70,150 Q80,160 90,150 L150,90 Q160,80 150,70 L90,10 Q80,0 70,10z" />
</clipPath>
</defs>
</svg>
<img src='http://i.imgur.com/NR6kefg.jpg' class='photo_rectangle_inverse' />
<img src='http://i.imgur.com/DXaH323.jpg' class='photo_rectangle_inverse' />
And this is my css
* {
padding: 0;
margin: 0;
}
.photo_rectangle_inverse {
height: 160px;
width: 170px;
-webkit-clip-path: url(#shape);
clip-path: url(#shape);
position: relative;
-webkit-transform: translateZ(1px)
}
Since the svg was not working in Internet Explorer (IE 11), after reading this article that talks about compatibility issue with Browsers, I added
<meta http-equiv="X-UA-Compatible" content="IE=edge">
To the top of my page because IE Edge based on the article seems to be the most compatible with Svg.
But still the svg shape is not displaying.
Here is a Jsfiddle . Note Jsfiddle does not allow meta tag
How to make an svg masked image compatible with Internet Explorer ?
Tks
IE won't apply an SVG clip to a html element, so you need an SVG <image> element rather than an HTML <img> element e.g.
* {
padding: 0;
margin: 0;
}
html, body {
height: 100%;
width: 100%;
}
.photo_rectangle_inverse {
-webkit-clip-path: url(#shape);
clip-path: url(#shape);
position: relative;
-webkit-transform: translateZ(1px)
}
<svg height="100%" width="100%" >
<defs>
<clipPath id="shape">
<path d="M10,70 Q0,80 10,90 L70,150 Q80,160 90,150 L150,90 Q160,80 150,70 L90,10 Q80,0 70,10z" />
</clipPath>
</defs>
<image height="160px" width="170px" xlink:href='http://i.imgur.com/NR6kefg.jpg' class='photo_rectangle_inverse'/>
<image transform="translate(170,0)" height="160px" width="170px" xlink:href='http://i.imgur.com/DXaH323.jpg' class='photo_rectangle_inverse' />
</svg>'

Colour overlay for SVG using CSS?

Is there a way to apply a colour overlay to an SVG using CSS?
I have some SVGs (icons, shapes etc) that I need to be able to "tint" - adding a solid colour overlay but keep the transparency.
I read about the CSS filters, but none of them cater for adding a colour over the top, only stuff like blur or desaturate.
Please check this code snippet. I hope it will be help you.
<svg width="0" height="0" class="svg-visiblity">
<defs>
<path id="hex" d="M11.5,20.9L44.3,2c3.7-2.2,8.3-2.2,12.1,0l32.8,18.9c3.7,2.2,6,6.1,6,10.4v37.8c0,4.3-2.3,8.3-6,10.4 L56.3,98.4c-3.7,2.2-8.3,2.2-12.1,0L11.5,79.5c-3.7-2.2-6-6.1-6-10.4V31.3C5.4,27,7.7,23,11.5,20.9z"/>
<clipPath id="hex-clip-200">
<use xlink:href="#hex" transform="scale(2 2)" />
</clipPath>
</defs>
</svg>
<svg class="image-200-2">
<rect class="border" width="100%" height="100%" transform="scale(1.02)" style="clip-path: url(#hex-clip-200);" /></rect>
<image xlink:href="http://placehold.it/200x200" width="200" height="200" transform="translate(2 2)" style="clip-path: url(#hex-clip-200);"></image>
</svg>
CSS
.image-200-2 {
display: inline-block;
height: 205px;
width: 205px;
}
.image-200-2 .border {
fill: rgba(0, 0, 0, 0.5);
}
.svg-visiblity {
height: 0;
opacity: 0;
position: absolute;
width: 0;
}
See here: https://jsfiddle.net/zyr8wovg/4/

image cropped as a triangle on webpage

This is a real css-challenge, I don't think this is possible:
I've made some white css triangles. and when you hover on a triangle, the white triangle should change in a photo also cropped like a triangle. I've made a jsfiddle for it:
fiddleLink
Any help appreciated
You can use svg to achieve this effect: http://jsfiddle.net/xTd6Y/4/
<div id="image-wrapper">
<svg id="svg-1" class="clip-svg">
<rect class='svg-background' width="300" height="300" fill="#ffffff" />
<image id="img-1" class='svg-image' width="300" height="300" xlink:href="http://lorempixel.com/300/300" />
</svg>
<svg id="svg-2" class="clip-svg">
<rect class='svg-background' width="300" height="300" fill="#ffffff" />
<image id="img-2" class='svg-image' width="300" height="300" xlink:href="http://lorempixel.com/300/301" />
</svg>
<svg id="svg-3" class="clip-svg">
<rect class='svg-background' width="300" height="300" fill="#ffffff" />
<image id="img-3" class='svg-image' width="300" height="300" xlink:href="http://lorempixel.com/300/302" />
</svg>
</div>
<svg id="svg-defs">
<defs>
<clipPath id="clip-triangle">
<polygon points="0, 200 100, 0 200, 200"/>
</clipPath>
</defs>
</svg>
css
body {
background-color: #e0e0e0;
}
#image-wrapper {
position: relative;
}
.svg-background, .svg-image {
clip-path: url(#clip-triangle);
}
.svg-image {
visibility: hidden;
}
.clip-svg .svg-image:hover {
visibility: inherit;
}
/* size and positioning */
svg.clip-svg {
position: absolute;
height: 250px;
width: 250px;
}
#svg-1 {
top: 110px;
left: 50px;
}
#svg-2 {
top: 40px;
left: 140px;
}
#svg-3 {
top: 160px;
left: 250px;
}
The clipping path is defined in svg#svg-defs, and can be set to whatever you like.
Image attributes are visible to / accessible by js and css.
You can apply the clipping path to any html element with css of
myElement {
clip-path: url(#clip-triangle);
}
but this is only reliable on firefox so far as I can tell.
Note: solution only tested on FF and chrome
note: small edit to move :hover from the svg to the embedded image, to correct problem with hover triggered outside clip area

Resources