Customize visible area of HTML element clipped by SVG <clipPath> - html

I have an HTML element that I clip using a path in an external SVG, which makes for a setup like this:
CSS:
video {
clip-path: url(path.svg#clip)
}
HTML:
<html>
<body>
<video src="foo.png" width="480" height="150" />
</body>
</html>
SVG:
<svg width="480px" height="92px" viewBox="0 0 480 92">
<defs>
<clipPath id="clip">
<path d="…"></path>
<path d="…"></path>
<path d="…"></path>
</clipPath>
</svg>
Note that the image I want to clip is slightly higher than the path that clips it. How can I position the image's position to change which region is visible?

I have solved the problem by wrapping the <video> inside another container and applying the mask to it. The container is positioned relatively and using absolute positioning i can then move the video around inside it.
CSS:
.container {
position: relative;
width: 480px;
height: 150px;
overflow: hidden;
clip-path: url(path.svg#clip)
}
.container video {
position: absolute;
/* … */
}
HTML:
<html>
<body>
<div class="container">
<video src="foo.png" width="480" height="150" />
</div>
</body>
</html>

Related

HTML/CSS svg auto height & width

Is there a way to make the svg automatically scale to the size of the circle inside it? Something like or something. At the moment I give the svg a size, and the circle a size. Which causes them to not match (and I don't want to manually try and match both every time).
See how the blue background is bigger than the circle. I know I can just change the height and width, but it would be nice to have this change according to the circle size. I only want to change one element, not match both.
.divOrangeCircles {
display: inline-block;
background-color: aqua;
opacity: 0.5;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="divOrangeCircles"><svg height="200" width="200"><circle cx="90" cy="90" r="90" fill="#F48043"/></svg></div>
</body>
</html>
What you want is the viewBox='x y width height' attribute, which will define the view-box of your SVG drawings.
SVG units are (generally) relative to their parent's box, by setting the viewBox attribute, you define how much of this units your SVG should display.
.divOrangeCircles {
display: inline-block;
background-color: aqua;
opacity: 0.5;
}
svg {
display: block;
}
<div class="divOrangeCircles">
<svg viewBox="0 0 180 180" height="200" width="200">
<circle cx="90" cy="90" r="90" fill="#F48043"/>
</svg>
</div>
If you want to svg scale according to parent div then use this code. Don't be add manually dimensions in percentage.
.divOrangeCircles svg{
width: 100%;
height: 100%;
}
Give your cx, cy and r should be in percentages in order to modify according to the Parent svg size.
Sample Fiddle
For controlling sizes from css u can make svg height and width as 100% and give size from css.
<div class="divOrangeCircles"><svg height="100%" width="100%"><circle cx="50%" cy="50%" r="50%" fill="#F48043"/></svg></div>
Updated Fiddle
Help :)

Position SVG to the left when embedded via img

I'm embedding a SVG with a width of 100% and automatic height. The maximum height is 80% of the viewport.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<style>
html, body{
width: 100%;
height: 100%;
}
img{
width: 100%;
height: auto;
max-height: 80vh;
}
</style>
</head>
<body>
<img src="example.svg">
</body>
</html>
If the SVG doesn't fit into the 80% of the viewport it'll be cropped by the specified maximum height. The problem is that this also centeres the SVG:
What I need is to keep the embedding via <img> as this is what I receive from a CMS and position the SVG to the left like a background-position: left top; would do. How to position the <img> contents to the left, even if the image is cropped?
Try to add preserveAspectRatio="xMinYMin meet" to root svg element of your svg file, like this.
<svg width="200px" height="200px"
viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="xMinYMin meet">
<rect width="100%" height="100%" fill="red"/>
</svg>
Have you tried doing:
float:left;

SVG clipPath issue

I have a svg tag with two circle elements inside that which are the same as each other. with same X and Y and R. but when I put one of them inside a clipPath and link it to an image or div, the position of the clipped circle will change. what is the problem about it?
It would be appreciated if anyone can help.
Here is the html code:
<div class="clip-background"></div>
<svg width="500" height="500">
<clipPath id="clipping-area">
<circle cx="200" cy="200" r="100">
</clipPath>
<circle class="circle-border" cx="200" cy="200" r="100">
</svg>
And here is the css code:
.clip-background{
position:absolute;
width:500px;
height:500px;
background-color:pink;
clip-path: url(#clipping-area);
-webkit-clip-path: url(#clipping-area);
}
.circle-border{
fill:none;
stroke:#666;
stroke-width:2;
}
codepen here.
This is happening because in your clipPath, the coordinates of the circle are being treated as being relative to the top left of the page. Whereas the circle coordinates in the SVG are relative to the top left of the <svg>. But the SVG is affected by the default margins/paddings on the HTML <body>. And so is not positioned in the same place.
If you get rid of the body margins in the standard way:
BODY {
padding: 0;
margin: 0;
}
you will see they both line up.
http://codepen.io/anon/pen/VjjOzm

Svg and div issue

I have a svg image that doesn't fit into my div. When I re-size my svg the div doesn't re-size to fit the svg. The svg should be at max size 700px and then re-size down as browser window re-sizes. I had it working with another element in Div1 that stretched the div1 to max-width:700px, but as I deleted that element it stopped working.
I removed the fiddle, since the issue is resolved.
If you have an svg image it will grow as large as its container allows.
This is both in width and height.
So if the svg element looks like this: <svg height="100%" width="100%">
It will scale up until to its container size.
I use css to modify how to control its size:
.svg-class { //put this class on your svg element
width: 100%;
}
So as long as there is room in the parent containers it will grow. Now we want to limit it to 700px: Just add max-width: 700px to the size control:
.svg-class {
width: 100%;
max-width: 700px;
}
Now it will only grow until 700px width. Even if the container is bigger.
body {
margin: 0;
}
.top {
background-color: black;
}
.sprite {
max-width: 700px;
width: 100%;
}
<div class="top">
<svg class="sprite" viewBox="0 0 100 100">
<rect x="0" y="0" width="50" height="50" fill="red" />
<rect x="50" y="0" width="50" height="50" fill="yellow" />
<rect x="0" y="50" width="50" height="50" fill="green" />
<rect x="50" y="50" width="50" height="50" fill="blue" />
</svg>
</div>
I found the answer. I added image tags around the svg and set the width. Works now.

How to get ScrollBars in SVG?

I have a SVG-element with a lot of elements inside. The SVG-element has a viewbox, so you can press a zoom-button and the elements appear bigger or smaller. Works well. Now the problem is, when the elements overflow the parent SVG-element no ScrollBars appear.
Example:
<div width="100%" height="100%">
<svg height="100%" width="100%" style="overflow-x: auto; overflow-y: auto; "viewBox="0 0 793 1122">
<g>
...
<line y2="44.9792mm" y1="44.9792mm" x1="197.203mm" x2="12.7028mm"></line>
<line y2="44.9792mm" y1="44.9792mm" x1="197.203mm" x2="12.7028mm"></line>
<text x="43.4516mm" y="52.9167mm" style="font-size: 11pt;">S</text>
<rect x="0" width="210mm" y="0" height="297mm"></rect>
...
</g>
</svg>
</div>
//here I set the viewbox after clicking the zoomOut-Button
float width = svg.getViewBox().getBaseVal().getWidth();
float height = svg.getViewBox().getBaseVal().getHeight();
svg.getViewBox().getBaseVal().setHeight((float) (height / 0.9));
svg.getViewBox().getBaseVal().setWidth((float) (width / 0.9));
Can someone help me?
I put the overflow attribut in the svg and also in the div tag. doesn't work.
Try making the SVG element bigger than the div, and let the div handle the overflow using scroll.
For example, see this jsfiddle, which utilizes the following css:
div#container {
height: 400px;
width: 400px;
border:2px solid #000;
overflow: scroll;
}
svg#sky {
height: 100px;
width: 1100px;
border:1px dotted #ccc;
background-color: #ccc;
}
Part of the point of SVG is so that it can scale to fit the screen. However, I think if you want to get something like what you are describing, then you need to set explicit width and height to the svg element. Something like http://jsfiddle.net/qTFxJ/13/ where I set the width and height in pixels to match your viewBox size.