Is Starburst effect doable in CSS3? - html

does anybody know if the following image could be made in CSS? Light and dark lines could and should be equal width and edges fade in to darker color so that overall background would be dark color (dark blue in this case).
Any help is well appreciated. My google skills didn't provide any help on this kind of effect, only 'starburst stickers / badges kind of things' was found.

No. Sadly, the css3 generated image specs do not include conical/angular gradients (though they might come out in the next revision!) which would be the most likely way to do this using only css. However, you can do this using css+svg. I actually had this svg document sitting around from an experiment I did once:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="512px" height="512px" viewBox="-256 -256 512 512" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Burst</title>
<defs>
<g id="burst">
<g id="quad">
<path id="ray" d="M0,0 -69,-500 69,-500 z" />
<use xlink:href="#ray" transform="rotate(30)"/>
<use xlink:href="#ray" transform="rotate(60)"/>
<use xlink:href="#ray" transform="rotate(90)"/>
</g>
<use xlink:href="#quad" transform="rotate(120)"/>
<use xlink:href="#quad" transform="rotate(240)"/>
</g>
<radialGradient id="grad" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" stop-color="white" stop-opacity="0.65"/>
<stop offset="100%" stop-color="black" stop-opacity="0.65"/>
</radialGradient>
<!-- a circle mask -->
<mask id="m"><circle r="256" fill="white"/></mask>
</defs>
<!-- added a mask and scaled it to a different aspect ratio below. scale(x,y) -->
<g mask="url(#m)" transform="scale(1, 0.75)">
<use xlink:href="#burst" fill="lightslateblue"/>
<use xlink:href="#burst" fill="darkslateblue" transform="rotate(15)"/>
<circle r="360px" fill="url(#grad)" />
</g>
</svg>
Set that as your background-image, and set the css background-size: cover. That's it. Here's a fiddle using this image in a data url.

Yes, it's possible.
But it takes some work.
I had this exact same question, and could not find any examples of this effect being done with pure CSS. And so, I decided to tinker around with it.
After a lot of experimentation, I finally came up with a solution that not only were in CSS, but also one that is rather portable (relatively speaking).
I started off with creating just the top half and the bottom half separately, using linear-gradient trickery on ::before and ::after pseudo elements, and the second step (which by far was the most time consuming step) was merging the two halves into a single element.
Working demo: http://codepen.io/pestbarn/pen/aBybeK
(the original poster wanted a vignette effect, which I've created separately in the above demo, using a div overlay with a radial gradient)
I've provided both the vanilla CSS and a Sass mixin at github.com/pestbarn/starburst.css, and you'll find some examples at the official demo page.
Is this cross-browser?
As far as I can tell, yes. You can see the current browser support in the repository.
Can I animate it?
Yes, like you would animate any other elements. Try it out for yourself!
Are there any caveats?
Alas, there is. Using two colors that are substantially different from another (e.g. in brightness) will create jagged edges. I'd therefore recommend using colors that are quite similar to one another.
Also, since the effect is created using pseudo elements, you will in some cases need to explicitly set the element's height and width.
Feel free to experiment!

There is an experimental property in a draft for CSS4 by Lea Verou:
div
{
repeating-conical-gradient(black, black 5%, #f06 5%, #f06 10%)
}
But as far as I understand, this is just a proposal and is not possible in CSS3 alone. Just stick with a background image, or you could try using triangle images in rotated elements.

With the addition of conic-gradient() to the CSS spec, it is now possible to create the effect using CSS that is intended for that type of effect.
Essentially, you'll set gradient stops like this:
background-image: conic-gradient( circle,
black 0%, black 5%,
white 5%, white 10%,
black 10%, black 15%,
...
);
Here's a working fiddle
And here's an animated version :-) The animation is a bit choppy on my browser, though. It could maybe be optimized a bit.
Note that this only works in WebKit browsers; that is, Chrome and Safari. But honestly, that covers the lion's share of users, and I'm sure the rest will add support for it soon enough.

There is currently a better solution than the "conic-gradient ()" method to extend the "marcus erronius" answer. There is a repeating-conic-gradient, which creates an image consisting of a repeating gradient.
div {
height: 500px;
background: repeating-conic-gradient(
hsl(0deg 0% 80% / 31%) 0deg 15deg,
hsla(0,0%,100%,0) 0deg 30deg
) #3c1c32
}
<div></div>
You read more about it at W3 CSS Image Values.
This property is not compatible with all browsers. Check caniuse for more information.

Related

Is it possible to clip a HTML element using an SVG image file as the source?

I'm trying to achieve the following effect, this is just done in photoshop with the pattern cut out of the background.
The patterns you see in the left side are cut out of the dark background to display the image that is sitting behind them.
I have the SVG file of that image and was wondering if it is possible, if so how, to clip a solid background to those shapes using the SVG file as the source?
Yes, you can do this by giving the HTML element either a mask property that references a <mask> element in your SVG file or a clip-path property that references a <clipPath> element in your SVG file.
.element {
/* just because of your example: */
background-image: url(forest-road.jpg);
/* use one of these two: */
clip-path: url(spirograph.svg#myClipPath);
mask: url(spirograph.svg#myMask);
}
<svg ...>
<!-- use one of these two: -->
<clipPath id="myClipPath">
<!-- shapes, etc., go here -->
</clipPath>
<mask id="myMask">
<!-- shapes, etc., go here -->
</mask>
</svg>
The two CSS features work slightly differently, with masking being newer and more flexible but having some residual gaps in browser support (including needing prefixes). Either way, you'll need to adjust your SVG to be suitable. A <mask> should make a pixel white for opaque and black for transparent, with greys being intermediate opacities. On the other hand, a <clipPath> defines the region that is visible as the union of a series of shapes.

why is this SVG getting cropped instead of scaled down (only in React)

When I change the width or height of the SVG it ends up looking like this instead of scaling properly. (getting cropped)
the problem only happens when the inline SVG is in React. I have tried to fix this for like 7 hours but I've got nothing to work.
The SVG tag is this:
<svg baseprofile="tiny"
fill="#ececec"
stroke="black"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width=".2"
version="1.2"
viewbox="0 0 2000 857"
xmlns="http://www.w3.org/2000/svg">
However, this is probably not relevant since I tested it without any properties and it still won't scall properly. (In pure HTML and CSS I can get this to work easily)
Adding Overflow: visible; does make it show the cropped area but it's still not scaled down.
The problem was that vanilla HTML is okay with it being "viewbox" but React requires it to be in camelCase as "viewBox"

Does SVG text have a minimal size?

It seems I'm hitting a lower limit on font-size or text in SVG. I can't seem to find anything about it in the docs, though.
I have a huge div, that contains a huge SVG part of which is filling the viewport completely. As a result my font-size can be less than 0.0105pt. At which point the text just disappears!
See this jsfiddle: https://jsfiddle.net/wc95t4dk/30/
<div style="position:absolute;top:-100000px; left: -100000px; width: 200000px; height:200000px">
<svg width="100%" height="100%" viewbox="0 0 100 100">
<text x="50.1" y="50.1" font-size="0.1pt" stroke="red" stroke-width="0.01" fill="red">My text</text>
<text x="50.15" y="50.15" font-size="0.011pt" stroke="green" stroke-width="0.001" fill="green">My text2</text>
<text x="50.25" y="50.15" font-size="0.01pt" stroke="blue" stroke-width="0.001" fill="blue">My text2</text>
</svg>
</div>
The red and green text displays fine but the blue one doesn't!
I am not sure about all browsers, but neither Chrome nor Firefox will render text whose font size is less than 0.5 screen pixels.
See: http://jsfiddle.net/vsxgy3fq/5/
However things get more complicated when there are SVG viewBox transforms (and other transforms) involved. I know that browsers have (or had) bugs related to this size check.
According to this answer https://stackoverflow.com/a/27165622/227507
it is caused by implementation detail where only 8 bits of precision (1/256) are reserved for decimal digits

SVG with transparency

I'm aiming to create an effect just like the one found on this codepen. There's an SVG with transparency over a background video. So far I have an AI document with a path shape selected that I exported from photoshop. Ideally I want the inside of the path to be transparent, but the rest to be white just like in the example. How can I achieve an effect like this?
it looks like it has something to do with this line of code, but I'm definitely misunderstanding something.
svg > rect {
fill: white;
-webkit-mask: url(#mask);
mask: url(#mask);
}
I don't think my SVG is ready with just being a path, as if I export it it shows up as blank and if I put a stroke on the path either everything but the stroke is transparent or it doesn't appear with any transparency at all.
normally my workflow as a web developer never has me working this in depth with SVG's so I'm a little stuck on this one.
The #mask in that CSS rule, refers to this element in the SVG:
<mask id="mask" x="0" y="0" width="1920" height="1080" >
...
</mask>
You can learn about how masks work by reading the SVG specification or one of the many tutorials available on the web.

Path with relative points in SVG

I have got svg with flexible height and width.
I'm trying to produce code equivalent to this:
<div id="svg-container">
<svg width='100%' height='100%' viewBox="0 0 100 100" preserveAspectRatio="none" style='background-color: yellow'>
<path
style="fill:none;stroke:blue;stroke-width:5;"
d="M0% 10% L50% 10% L50% 90% L100% 90%" <-here is the problem
/>
</svg>
</div>
So when I scale #svg-container i will get:
But since I can't create path with percent coordinates the best I can do is: (JSFiddle)
<div id="svg-container">
<svg width='100%' height='100%' viewBox="0 0 100 100" preserveAspectRatio="none" style='background-color: yellow'>
<path
style="fill:none;stroke:blue;stroke-width:5;"
d="M0 10 L50 10 L50 90 L100 90"
/>
</svg>
</div>
Which gives me this results:
On the second picture you can see problem with stretched stroke-width.
My question is: how to achieve behavior like on the first picture?
I'm not looking for JavaScript answers and breaking path.
Seems like you want a style of vector-effect: non-scaling-stroke
Note that not all UAs implement this but Chrome and Firefox certainly do.
I am sorry to say I think you will only be able to do this with JavaScript, even if this is not the answer that you're looking for. What is happening is that you are first generating the SVG path, then resizing it afterward so the image becomes stretched.
To use JavaScript you can use (browser.width/100)*10 to get 10%, for example, and this should work for all sizes of browser screen.
How are you re-scaling the image (i.e. is it a CSS #media query)? It might be possible to draw the path after the re-scale, but again I feel this will need JS as you will need to load the content after the browser load.
Sorry to answer in opposition to what you have asked, but unless there is an alternative I think this is the only way you can do this.