CSS blur only in one direction (motion blur) - css

I need to dynamically blur an image on my page, but only along one axis (Y specifically). So here are my requirements:
Has to be done "live" (I can't pre-render a blurred version of the image)
Like I said, only on the Y axis (like a motion blur, but vertical)
Needs to animate in
Should work in IE9+
My first thought was to use a simple CSS filter:
img {
filter: blur(20px);
}
I can animate that by adding a transition (transition: filter 0.2s linear), but it only creates Gaussian blurs, which isn't the effect I want. The syntax doesn't support something like filter: blur(0 10px); to restrict the blur only to one axis.
Then I read that the blur filter (amongst others) is really just a shorthand for an SVG filter, which you can write manually if you want. So I created an SVG called filter.svg that specifies a 20px blur only along the Y axis (0 20):
<?xml version="1.0" standalone="no"?>
<svg width="1" height="1" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter id="blur">
<feGaussianBlur in="SourceGraphic" stdDeviation="0 20" />
</filter>
</defs>
</svg>
And applied it like this:
img {
filter: url("filter.svg#blur");
}
And that works perfectly...but only in Firefox. Safari/Chrome don't support url() as a value for filter. Plus, I can't animate it because the value is a URL rather than a number, so transition doesn't work.
On top of all that, I don't think either of these approaches work in IE9.
So: is there any way to do what I'm trying to do? I've looked into using canvas as an alternative, but can't find any examples of a blur that only goes in one direction.

If I'm understanding the question right it can be donewith JQuery.
CSS3 does have it's limits and it's very limited in interactive values.
Jquery also adds cross-platform stability.
JQuery
$(document).ready(function() {
var $img = $('.image')
$img.hide();
$img.show().animate({
opacity: 100,
paddingTop: '+=80'
}, 500)
});
Here is an example of how it could work with javacript with a little
fooling around on opacity.
function myMove() {
var elem = document.getElementById("animate");
var pos = 0;
var id = setInterval(frame, 5);
function frame() {
if (pos == 150) {
clearInterval(id);
} else {
pos++;
elem.style.left = pos + 'px';
}
}
}
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
<p>
<button onclick="myMove()">Click Me</button>
</p>
<div id="container">
<div id="animate"></div>
</div>

Related

How to make an element of such a complex shape? [duplicate]

So I'm working on a site and I was wondering if it's possible to, purely using HTML5, CSS3 (and JavaScript if needed), make a div with a curved bottom, so it will look practically like this:
Or can this only be done using a background image?
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<ul class="nav">
<li>Home</li>
</ul>
</div>
</div>
</body>
There are different approaches that can be adopted to create this shape. Below is a detailed description of possibilities:
SVG Based Approaches:
SVG is the recommended way to create such shapes. It offers simplicity and scale-ability. Below are a couple of possible ways:
1- Using Path Element:
We can use SVG's path element to create this shape and fill it with some solid color, gradient or a pattern.
Only one attribute d is used to define shapes in path element. This attribute itself contains a number of short commands and few parameters that are necessary for those commands to work.
Below is the necessary code to create this shape:
<path d="M 0,0
L 0,40
Q 250,80 500,40
L 500,0
Z" />
Below is a brief description of path commands used in above code:
M command is used to define the starting point. It appears at the beginning and specify the point from where drawing should start.
L command is used to draw straight lines.
Q command is used to draw curves.
Z command is used to close the current path.
Output Image:
Working Demo:
svg {
width: 100%;
}
<svg width="500" height="80" viewBox="0 0 500 80" preserveAspectRatio="none">
<path d="M0,0 L0,40 Q250,80 500,40 L500,0 Z" fill="black" />
</svg>
2- Clipping:
Clipping means removing or hiding some parts of an element.
In this approach, we define a clipping region by using SVG's clipPath element and apply this to a rectangular element. Any area that is outside the clipping region will be hidden.
Below is the necessary code:
<defs>
<clipPath id="shape">
<path d="M0,0 L0,40 Q250,80 500,40 L500,0 Z" />
</clipPath>
</defs>
<rect x="0" y="0" width="500" height="80" fill="#000" clip-path="url(#shape)" />
Below is brief description of the elements used in above code:
defs element is used to define element / objects for later use in SVG document.
clipPath element is used to define a clipping region.
rect element is used to create rectangles.
clip-path attribute is used to link the clipping path created earlier.
Working Demo:
svg {
width: 100%;
}
<svg width="500" height="80" viewBox="0 0 500 80" preserveAspectRatio="none">
<defs>
<clipPath id="shape">
<path d="M0,0 L0,40 Q250,80 500,40 L500,0 Z" />
</clipPath>
</defs>
<rect x="0" y="0" width="500" height="80" fill="#000" clip-path="url(#shape)" />
</svg>
CSS Based Approaches:
1- Using Pseudo Element:
We can use ::before or ::after pseudo element to create this shape. Steps to create this are given below:
Create a layer with ::before OR ::after pseudo element having width and height more than its parent.
Add border-radius to create the rounded shape.
Add overflow: hidden on parent to hide the unnecessary part.
Required HTML:
All we need is a single div element possibly having some class like shape:
<div class="shape"></div>
Working Demo:
.shape {
position: relative;
overflow: hidden;
height: 80px;
}
.shape::before {
border-radius: 100%;
position: absolute;
background: black;
right: -200px;
left: -200px;
top: -200px;
content: '';
bottom: 0;
}
<div class="shape"></div>
2- Radial Gradient:
In this approach we will use CSS3's radial-gradient() function to draw this shape on the element as a background. However, this approach doesn't produce very sharp image and it might have some jagged corners.
Required HTML:
Only single div element with some class will be required i.e.
<div class="shape"></div>
Necessary CSS:
.shape {
background-image: radial-gradient(120% 120px at 50% -30px, #000 75%, transparent 75%);
}
Working Demo:
.shape {
background: radial-gradient(120% 120px at 50% -30px, #000 75%, transparent 75%) no-repeat;
height: 80px;
}
<div class="shape"></div>
JavaScript Based Approaches:
Although not required in this case but for the sake of completeness, I'm adding this approach as well. This can be useful in some cases as well:
HTML5 Canvas:
We can draw this shape on HTML5 Canvas element using path functions:
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, 40);
ctx.quadraticCurveTo(311, 80, 622, 40);
ctx.lineTo(622, 0);
ctx.fill();
<canvas id="canvas" width="622" height="80"></canvas>
Below is a brief description of the methods used in above code:
beginPath() is used to create a new path. Once a new path is created, future drawing commands are directed into the path.
moveTo(x, y) moves the pen to the coordinates specified by x and y.
lineTo(x, y) draws a straight line from the current pen position to the point specified by x and y.
quadraticCurveTo(cp1x, cp1y, x, y) draws a curve from current pen position to the point specified by x and y using control point specified by cp1x and cp1y.
fill() fills the current path using non-zero or even-odd winding rule.
Useful Resources:
Radial Gradient: Specs, MDN
SVG: Specs, MDN
HTML5 Canvas: Specs, MDN
CSS:
div{
background-color:black;
width:500px;
height:50px;
border-bottom-left-radius:50%;
border-bottom-right-radius:50%;
}
see is this ok for you
div {
background-color: black;
width: 500px;
height: 50px;
border-bottom-left-radius: 50%;
border-bottom-right-radius: 50%;
}
<div>
</div>
This is what you want:
div{
background-color: black;
width: 500px;
height: 300px;
border-radius: 0 0 50% 50% / 50px;
}
Unlike the accepted answer, this works even when the height of the div is increased.
Demo: jsFiddle
Yes, you can do this in CSS - basically make your div wider than the page to fix the too-rounded edges, then left-positioned to compensate, with bottom border radius using both x & y values, and negative bottom margin to compensate for the gap:
.round-bottom {
border-bottom-left-radius: 50% 200px; // across half & up 200px at left edge
border-bottom-right-radius: 50% 200px; // across half & up 200px at right edge
width: 160%; overflow: hidden; // make larger, hide side bits
margin-bottom: -50px; // apply negative margin to compensate for bottom gap
position: relative; left:-30%; // re-position whole element so extra is on each side (you may need to add display:block;)
}
.round-bottom {
border-bottom-left-radius: 50% 150px !important;
border-bottom-right-radius: 50% 150px !important;
position: relative;
overflow: hidden;
width: 160%;
margin-bottom:-50px;
left:-30%;
background-color:#444;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/a/a2/Tropical_Forest_with_Monkeys_A10893.jpg'); background-position: center center;
background-size: 42% auto;
height:150px;
}
.container { width: 100%; height: height:100px; padding-bottom:50px; overflow:hidden;}
<div class="container"><div class="round-bottom"></div></div>
Try this
.navbar{
border-radius:50% 50% 0 0;
-webkit-border-radius:50% 50% 0 0;
background:#000;
min-height:100px;
}
jsFiddle File

How to control SVG filter attribute values using CSS?

I want to create animation in CSS, part of which I need to control the values of the SVG filter attributes.
Below, I try to call a CSS variable within the specularConstant attribute, and the browser returns an error.
Alternatively, is it possible to set the attribute using a selector in the CSS code, or is there any way to control an SVG attribute like this, via CSS?
Below a Reproducible Example:
#property --illumination-power {
syntax: '<number>';
initial-value: 1;
inherits: true
}
:root {
animation: my-animation 5s;
}
#keyframes my-animation {
0% {
--illumination-power: 0;
}
100% {
--illumination-power: 1.2;
}
}
body {
margin: 0;
background-color: black;
}
<svg width="100vw" height="100vh">
<defs>
<filter id="spotlight">
<feSpecularLighting specularConstant="var(--illumination-power)"
specularExponent="10" lighting-color="white">
<fePointLight x="200" y="100" z="70"/>
</feSpecularLighting>
</filter>
</defs>
<rect id="light-background" x="-500%" y="-500%" width="1000%" height="1000%" fill-opacity="0" filter="url(#spotlight)"/>
</svg>
You need to use the SVG <animate> element or JavaScript to do it.

CSS SVG Wave shape

that's the result I'm trying to achieve
and here's what I've done: https://codepen.io/demedos/pen/gjQNOM
HTML structure:
.container
.header
.page-1
#wave
#dot
#text
There is some problem though:
Items are positioned using absolute positioning, while I want them anchored to the main wavy line
Containers are smaller than their content
I want the line to be at 50% of the screen, filling the above space with its background color
Here's a solution using a little bit of Javascript. I've simplified your example to keep things clear.
I want the line to be at 50% of the screen, filling the above space with its background color
We use a vertical flex box arrangement to fill the height of the screen.
We set the viewBox to fit the wave curve and let the browser do normal SVG centering.
We use a very tall wave path and rely on SVG overflowing to make the wave fill to the top of the cell.
We use SVGPathElement.getPointAtLength() to calculate the correct position on the path for each dot.
function positionDot(dotId, fractionAlongWave) {
// Get a reference to the "wave-curve" path.
var waveCurve = document.getElementById("wave-curve");
// Get the length of that path
var curveLength = waveCurve.getTotalLength();
// Get the position of a point that is "fractionAlongWave" along that path
var pointOnCurve = waveCurve.getPointAtLength(curveLength * fractionAlongWave);
// Update the dot position
var dot = document.getElementById(dotId);
dot.setAttribute("cx", pointOnCurve.x);
dot.setAttribute("cy", pointOnCurve.y);
}
// Position the first dot 25% the way along the curve
positionDot("dot1", 0.25);
// Position the second dot 90% of the way along the curve
positionDot("dot2", 0.90);
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
}
.container {
display: flex;
flex-direction: column;
width: 640px;
height: 100vh;
margin: 0 auto;
border: 1px solid blue;
}
.header {
background-color: #333835;
}
.page-1 {
flex: 1;
border: 1px solid red;
position: relative;
}
.page-1 svg {
position: absolute;
width: 100%;
height: 100%;
border: 1px solid red;
}
#wave {
fill:#333835;
}
#dot1,
#dot2 {
fill:#e05a5a;
}
<div class='container'>
<div class='header'>
header
</div>
<div class='page-1'>
<!-- Set the viewBox to match the curve part of the wave.
Then we can rely on the browser to centre the SVG (and thus the curve) in the parent container. -->
<svg viewBox="0 342 1366 283">
<defs>
<!-- A copy of the curve part of the wave, which we will use to calculate
the correct position of the dot using getPointAtLength(). -->
<path id="wave-curve" d="M0,342c595,0,813,283,1366,283"/>
</defs>
<!-- SVGs are "overflow: visible" by default.
If we make this path extend vertically a long way, it will fill to the
top of the SVG no matter how high the page is. -->
<path id="wave" d="M0,342c595,0,813,283,1366,283 V -10000 H 0 Z"/>
<circle id="dot1" cx="0" cy="0" r="12.5"/>
<circle id="dot2" cx="0" cy="0" r="12.5"/>
</svg>
</div>
</div>

How to apply clipPath to a div with the clipPath position being relative to the div position

Not sure if I've formulated the title properly, but here goes the question.
I have an SVG path of a cloud-like shape which I would like to use in CSS with the clip-path property.
<path d="M46.9819755,61.8637972 C42.0075109,66.8848566 35.0759468,70 27.4091794,70 C12.2715076,70 0,57.8557238 0,42.875 C0,32.9452436 5.3914988,24.2616832 13.4354963,19.534921 C14.8172134,8.52285244 24.3072531,0 35.8087666,0 C43.9305035,0 51.0492374,4.2498423 55.01819,10.6250065 C58.2376107,8.87215568 61.9363599,7.875 65.8704472,7.875 C78.322403,7.875 88.4167076,17.8646465 88.4167076,30.1875 C88.4167076,32.1602271 88.1580127,34.0731592 87.6723639,35.8948845 L87.6723639,35.8948845 C93.3534903,38.685457 97.2583784,44.4851888 97.2583784,51.1875 C97.2583784,60.6108585 89.5392042,68.25 80.0171204,68.25 C75.4841931,68.25 71.3598367,66.5188366 68.2822969,63.6881381 C65.5613034,65.4654463 62.3012892,66.5 58.7971106,66.5 C54.2246352,66.5 50.0678912,64.7384974 46.9819755,61.8637972 Z" fill="lightblue" />
When I add an SVG element in HTML and define <clipPath> with that path, the browser positions the clipping path in the top-left corner. If I apply a margin to the clipped element, the mask is not linked and stays in its initial position.
Other similar threads state that the clipPathUnits="objectBoundingBox" attribute should be added to the <clipPath> object, but that doesn't seem to solve my problem. I even transformed the path from absolute to relative and tried it like that, but got the same result.
Is it possible to somehow link the clipping path with the clipped element so that when positioning is applied to the clipped element, the clipping path moves as well?
Here is the relative path, if it helps:
<path d="M46.9819755,61.8637972c-4.974,5.021,-11.906,8.136,-19.573,8.136c-15.137,0,-27.409,-12.144,-27.409,-27.125c0,-9.93,5.392,-18.613,13.436,-23.34c1.381,-11.012,10.871,-19.535,22.373,-19.535c8.122,0,15.24,4.25,19.209,10.625c3.22,-1.753,6.918,-2.75,10.852,-2.75c12.452,0,22.547,9.99,22.547,22.313c0,1.972,-0.259,3.885,-0.745,5.707l0,0c5.682,2.791,9.586,8.59,9.586,15.293c0,9.423,-7.719,17.062,-17.241,17.062c-4.533,0,-8.657,-1.731,-11.735,-4.562c-2.721,1.778,-5.981,2.812,-9.485,2.812c-4.572,0,-8.729,-1.761,-11.815,-4.636z fill="lightblue" />
As well as some test HTML/CSS. (I've set the left property to 10px so that you see the clipping issue occur)
.clippedElement {
width: 200px;
height: 200px;
position: absolute;
left: 10px;
top: 0;
background-color: lightblue;
-webkit-clip-path: url(#cloudClip);
-moz-clip-path: url(#cloudClip);
clip-path: url(#cloudClip);
}
<svg>
<defs>
<clipPath id="cloudClip">
<path d="M46.9819755,61.8637972 C42.0075109,66.8848566 35.0759468,70 27.4091794,70 C12.2715076,70 0,57.8557238 0,42.875 C0,32.9452436 5.3914988,24.2616832 13.4354963,19.534921 C14.8172134,8.52285244 24.3072531,0 35.8087666,0 C43.9305035,0 51.0492374,4.2498423 55.01819,10.6250065 C58.2376107,8.87215568 61.9363599,7.875 65.8704472,7.875 C78.322403,7.875 88.4167076,17.8646465 88.4167076,30.1875 C88.4167076,32.1602271 88.1580127,34.0731592 87.6723639,35.8948845 L87.6723639,35.8948845 C93.3534903,38.685457 97.2583784,44.4851888 97.2583784,51.1875 C97.2583784,60.6108585 89.5392042,68.25 80.0171204,68.25 C75.4841931,68.25 71.3598367,66.5188366 68.2822969,63.6881381 C65.5613034,65.4654463 62.3012892,66.5 58.7971106,66.5 C54.2246352,66.5 50.0678912,64.7384974 46.9819755,61.8637972 Z"
/>
</clipPath>
</defs>
</svg>
<div class="clippedElement"></div>
Thanks to Robert's comment, I was able to solve the issue I had.
Here is a PHP snippet I used to convert the absolute path to relative, so that the values are between 0 and 1.
$absolute_path = "M46.9819755,61.8637972 C42.0075109,66.8848566 35.0759468,70 27.4091794,70 C12.2715076,70 0,57.8557238 0,42.875 C0,32.9452436 5.3914988,24.2616832 13.4354963,19.534921 C14.8172134,8.52285244 24.3072531,0 35.8087666,0 C43.9305035,0 51.0492374,4.2498423 55.01819,10.6250065 C58.2376107,8.87215568 61.9363599,7.875 65.8704472,7.875 C78.322403,7.875 88.4167076,17.8646465 88.4167076,30.1875 C88.4167076,32.1602271 88.1580127,34.0731592 87.6723639,35.8948845 L87.6723639,35.8948845 C93.3534903,38.685457 97.2583784,44.4851888 97.2583784,51.1875 C97.2583784,60.6108585 89.5392042,68.25 80.0171204,68.25 C75.4841931,68.25 71.3598367,66.5188366 68.2822969,63.6881381 C65.5613034,65.4654463 62.3012892,66.5 58.7971106,66.5 C54.2246352,66.5 50.0678912,64.7384974 46.9819755,61.8637972 Z";
function regex_callback($matches) {
static $count = -1;
$count++;
$width = 98;
$height = 70;
if($count % 2) {
return $matches[0] / $height;
} else {
return $matches[0] / $width;
}
}
$relative_path = preg_replace_callback('(\d+(\.\d+)?)', 'regex_callback', $absolute_path);
Since the clipping path is not rectangular, I couldn't divide the values with one number, but had to use the width and the height of the clipping path itself.
.clippedElement {
width: 98px;
height: 70px;
position: absolute;
left: 10px;
top: 0;
background-color: lightblue;
-webkit-clip-path: url(#cloudClip);
-moz-clip-path: url(#cloudClip);
clip-path: url(#cloudClip);
}
<svg width="98" height="70">
<defs>
<clipPath id="cloudClip" clipPathUnits="objectBoundingBox">
<path d="M0.47940791326531,0.88376853142857 C0.42864807040816,0.95549795142857 0.3579178244898,1 0.27968550408163,1 C0.12521946530612,1 0,0.82651034 0,0.6125 C0,0.47064633714286 0.055015293877551,0.34659547428571 0.13709690102041,0.2790703 C0.15119605510204,0.12175503485714 0.24803319489796,0 0.36539557755102,0 C0.44827044387755,0 0.52091058571429,0.060712032857143 0.56141010204082,0.15178580714286 C0.59426133367347,0.12674508114286 0.63200367244898,0.1125 0.67214742040816,0.1125 C0.79920819387755,0.1125 0.90221130204082,0.25520923571429 0.90221130204082,0.43125 C0.90221130204082,0.45943181571429 0.89957155816327,0.48675941714286 0.89461595816327,0.51278406428571 L0.89461595816327,0.51278406428571 C0.95258663571429,0.55264938571429 0.99243243265306,0.63550269714286 0.99243243265306,0.73125 C0.99243243265306,0.86586940714286 0.91366534897959,0.975 0.81650122857143,0.975 C0.77024686836735,0.975 0.72816159897959,0.95026909428571 0.69675813163265,0.90983054428571 C0.66899289183673,0.93522066142857 0.63572744081633,0.95 0.59997051632653,0.95 C0.55331260408163,0.95 0.51089684897959,0.92483567714286 0.47940791326531,0.88376853142857 Z"></path>
</clipPath>
</defs>
</svg>
<div class="clippedElement"></div>

CSS not overriding presentational SVG attributes

I have an SVG shape as part of my SVG sprite:
<symbol id="flickr-logo" viewBox="-41.5 532.5 200 91.626">
<circle fill="#0063DB" cx="4.313" cy="578.312" r="45.813" class="left" ></circle>
<circle fill="#FF0084" cx="112.687" cy="578.312" r="45.813" class="right"></circle>
</symbol>
Now when I use <svg ..><use ...></use></svg> etc to include the actual shape on my page it works nicely and the fills inside the SVG circles show up fine.
Now, when I add my css from below:
.left {
fill: #ffffff;
}
.right {
fill: #ffffff;
}
Nothing happens. I see the style is applying but the fill="???" presentational attribute on on the circle elements seem to be overriding. Is there a way to get the CSS to win out?
If I remove the fill="????" attribute then the css styles work perfectly but I need to keep them in.
I thought about editing the colours in the SVG but I can't as I need to display this shape in two locations. One in it's default colours and once in another place where I need to change the fills to white.
Any thoughts about how to do this?
Thanks,
Neil
I would suggest that you remove the fill then CSS can be applied and work as expected.
You say you can't do this as you use the SVG in two places.
However your mark-up shows
<symbol id="flickr-logo" viewBox="-41.5 532.5 200 91.626">
So I guess that, you must be using another ID somewhere else (or you could use another ID somewhere else) such as id="flickr-logo-alt" as all the ID's on the page actually really need to unique.
You can then apply two little bits CSS to style both versions.
#flickr-logo .left {
fill: #0063DB;
}
#flickr-logo .right {
fill: #FF0084;
}
#flickr-logo-alt .left {
fill: #ffffff;
}
#flickr-logo-alt .right {
fill: #ffffff;
}
If you have other styling that references the ID and you don't have two on the page at the same time, then you could add a class to the symbol for the alternate case "alt" use different classes like this. The class can be applied directly or through scripting.
<symbol class="alt" id="flickr-logo" viewBox="-41.5 532.5 200 91.626">
#flickr-logo .left {
fill: #0063DB;
}
#flickr-logo .right {
fill: #FF0084;
}
#flickr-logo.alt .left {
fill: #ffffff;
}
#flickr-logo.alt .right {
fill: #ffffff;
}
or just apply an additional class to the left and right that already exist in the alt case. Which can be applied directly or though scripting.
<circle cx="4.313" cy="578.312" r="45.813" class="left alt" ></circle>
<circle cx="112.687" cy="578.312" r="45.813" class="right alt"></circle>
.left {
fill: #0063DB;
}
.right {
fill: #FF0084;
}
.left.alt {
fill: #ffffff;
}
.right.alt {
fill: #ffffff;
}

Resources