Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <svg id="svg"></svg>

<header>
  <h1>Random Flower Generator</h1>
  <a href="https://codepen.io/collection/DPjZxo/" target="_blank">SVG Collection</a>
</header>
<footer>
  Click to generate random flowers
</footer>

              
            
!

CSS

              
                body {
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  font-size: 0.9rem;
  color: #000;
  text-shadow: 1px 1px 1px #fff;
}

#svg {
  position: fixed;
  width: 100%;
  height: 100%;
  cursor: pointer;
  filter: drop-shadow(0 0 4px #000);
}

.flower path {
  stroke-linecap: round;
  stroke-linejoin: round;
}

.layer {
  animation-name: layerAnim1;
  animation-duration: 4s;
  animation-fill-mode: both; 
  transform-box: fill-box;
  transform-origin: center center;
}

.layer path {
  stroke: rgba(0, 0, 0, 0.7);
  stroke-width: 0.75px;
}

a {
  color: #555;
  text-decoration: none;
  border-bottom: 0.25rem solid transparent;
  transition: all 0.4s;
}
a:hover {
  color: #000;
  border-bottom-color: rgba(0, 0, 0, 0.7);
}

header, footer {
  position: absolute;
  width: 100%;
  text-align: center;
}
header h1 {
  font-size: 2em;
  margin: 0.5em 0 0;
}
footer {
  bottom: 0.5em;
}

@-moz-document url-prefix() {
  #svg {
    filter: none;
  }
  .layer {
    filter: drop-shadow(0 0 2px #000);
  }
}

@keyframes layerAnim1 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes layerAnim2 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: scale3d(0.2, 0.2, 0.2); }
  to { opacity: 1; transform: scale3d(1, 1, 1); }
}

@keyframes layerAnim3 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: rotateZ(-270deg); }
  to { opacity: 1; opacity: 1; transform: rotateZ(0deg); }
}

@keyframes layerAnim4 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: rotateZ(360deg) scale3d(0.2, 0.2, 0.2); }
  to { opacity: 1; opacity: 1; transform: rotateZ(0deg) scale3d(1, 1, 1); }
}

@keyframes layerAnim5 {
  from, 20%, 40%, 60%, 80%, to {
    animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  }

  0% {
    opacity: 0;
    transform: scale3d(0.3, 0.3, 0.3);
  }

  20% {
    transform: scale3d(1.1, 1.1, 1.1);
  }

  40% {
    transform: scale3d(0.9, 0.9, 0.9);
  }

  60% {
    transform: scale3d(1.03, 1.03, 1.03);
  }

  80% {
    transform: scale3d(0.97, 0.97, 0.97);
  }

  to {
    opacity: 1;
    transform: scale3d(1, 1, 1);
  }
}

@keyframes layerAnim6 {
  from, 20%, 40%, 60%, 80%, to {
    animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  }

  0% {
    opacity: 0;
    transform: rotateZ(180deg) scale3d(0.3, 0.3, 0.3);
  }

  20% {
    transform: rotateZ(-25.71deg) scale3d(1.1, 1.1, 1.1);
  }

  40% {
    transform: rotateZ(25.71deg) scale3d(0.9, 0.9, 0.9);
  }

  60% {
    transform: rotateZ(-7.71deg) scale3d(1.03, 1.03, 1.03);
  }

  80% {
    transform: rotateZ(7.71deg) scale3d(0.97, 0.97, 0.97);
  }

  to {
    opacity: 1;
    transform: rotateZ(0deg) scale3d(1, 1, 1);
  }
}

@keyframes layerAnim7 {
  from, 20%, 40%, 60%, 80%, to {
    animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  }

  0% {
    opacity: 0;
    transform: rotateZ(-180deg) scale3d(0.3, 0.3, 0.3);
  }

  20% {
    transform: rotateZ(25.71deg) scale3d(1.1, 1.1, 1.1);
  }

  40% {
    transform: rotateZ(-25.71deg) scale3d(0.9, 0.9, 0.9);
  }

  60% {
    transform: rotateZ(7.71deg) scale3d(1.03, 1.03, 1.03);
  }

  80% {
    transform: rotateZ(-7.71deg) scale3d(0.97, 0.97, 0.97);
  }

  to {
    opacity: 1;
    transform: rotateZ(0deg) scale3d(1, 1, 1);
  }
}

@keyframes layerAnim8 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: rotate3d(1, 1, 1, 360deg); }
  to { opacity: 1; transform: rotate3d(1, 1, 1, 0deg); }
}

@keyframes layerAnim9 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: rotate3d(1, 1, 1, 360deg) scale3d(0.1, 0.1, 0.1); }
  to { opacity: 1; transform: rotate3d(1, 1, 1, 0deg) scale3d(1, 1, 1); }
}

@keyframes layerAnim10 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: scale3d(2, 2, 2); }
  to { opacity: 1; transform: scale3d(1, 1, 1); }
}

@keyframes layerAnim11 {
  from, to { animation-timing-function: ease-out; }
  from { opacity: 0; transform: rotateZ(-360deg) scale3d(2, 2, 2); }
  to { opacity: 1; transform: rotateZ(0deg) scale3d(1, 1, 1); }
}

              
            
!

JS

              
                const { random, PI, sin, cos, round, floor, ceil, min, max } = Math;

function App() {
  const svg = document.getElementById('svg');
  let width, height, cx, cy;

  let zoom = 100;
  let vbOX = 0;
  let vbOY = 0;

  init();

  function init() {
    onResize();
    initEventHandlers();

    new Flower({
      parent: svg,
      x: 0,
      y: 0,
      petals: 8,
      rings: 12,
      step: 4,
      colors: ['#21B91B', '#FFFF80', '#D1104D', '#7F266A'],
      alpha: 0.8,
      angle: rnd(PI),
      layerAnim: 'layerAnim5',
      reverseDelay: false,
    });
  }

  function initEventHandlers() {
    window.addEventListener('resize', onResize);
    document.body.addEventListener('click', onClick);
  }

  function onClick(e) {
    createFlower({
      x: e.clientX - cx,
      y: e.clientY - cy,
    });
  }
  
  function createFlower(params) {
    const iRadiusCoef = rnd(1, 7);
    const oRadiusCoef = rnd(iRadiusCoef, 7);
    new Flower({
      parent: svg,
      petals: rndInt(4, 12),
      rings: rndInt(3, 8),
      step: rndInt(3, 8),
      alpha: rnd(0.7, 1),
      angle: rnd(PI),
      iRadius: rnd(2, 5),
      iRadiusCoef,
      oRadius: rnd(5, 10),
      oRadiusCoef,
      ...params
    });
  }

  function onResize() {
    const r = svg.getBoundingClientRect();
    width = r.width;
    height = r.height;
    cx = width / 2;
    cy = height / 2;
    updateViewBox();
  }

  function updateViewBox() {
    const vbW = (width * 100) / zoom;
    const vbH = (height * 100) / zoom;
    const vbX = vbOX - vbW / 2;
    const vbY = vbOY - vbH / 2;
    svg.setAttribute('viewBox', `${vbX} ${vbY} ${vbW} ${vbH}`);
  }
}

class Flower {
  constructor(conf) {
    this.conf = {
      alpha: 1,
      angle: 0,
      iRadius: 2, iRadiusCoef: 5,
      oRadius: 5, oRadiusCoef: 10,
      layerAnim: 'layerAnim' + (rndInt(1, 12)),
      reverseDelay: rnd(1) > 0.5,
      ...conf
    };
    this.create();
  }
  create() {
    let { parent, x, y, petals, rings, step, colors, alpha, angle, iRadius, iRadiusCoef, oRadius, oRadiusCoef, layerAnim, reverseDelay } = this.conf;

    this.group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
    this.group.classList.add('flower');
    parent.appendChild(this.group);

    if (!colors) {
      colors = [chroma.random(), chroma.random()];
      if (rings > 6) colors.push(chroma.random());
    }

    const cscale = chroma.scale(colors);
    let layer, np, color, di;
    for (let i = rings; i > 0; i--) {
      layer = document.createElementNS('http://www.w3.org/2000/svg', 'g');
      layer.classList.add('layer');

      np = floor((i + step - 1) / step) * petals;
      color = cscale(i * 1 / rings).alpha(alpha);
      this.createPetalsRing(layer, x, y, np, iRadius + i * iRadiusCoef, oRadius + i * oRadiusCoef, angle + i % 2 * PI / np, color);

      di = reverseDelay ? (rings - i + 1) : i;
      layer.style.animationName = layerAnim;
      layer.style.animationDuration = (rings * 0.3) + 's';
      layer.style.animationDelay = (di * 0.15) + 's';
      this.group.appendChild(layer);
    }
  }
  createPetalsRing(parent, x, y, n, iRadius, oRadius, angle, fill) {
    const da = 2 * PI / n;
    const dr = oRadius - iRadius;
    const cpda = rnd(0.5 * da / 5, 1.5 * da / 5);
    const cpdr = rnd(dr * 0.25, dr * 1.1);
    let a;
    for (let i = 0; i < n; i++) {
      a = angle + i * da;
      new Petal({
        parent,
        x, y,
        startA: a,
        endA: a + da,
        iRadius,
        oRadius,
        cpda,
        cpdr,
        fill,
      });
    }
  }
}

class Petal {
  constructor({ parent, x, y, startA, endA, iRadius, oRadius, cpda, cpdr, fill }) {
    this.parent = parent;
    this.fill = fill;

    const da = endA - startA;
    const iRadius0 = iRadius / 5;
    this.sp = [x + cos(startA + da / 2) * iRadius0, y + sin(startA + da / 2) * iRadius0];
    this.cp1 = [x + cos(startA - cpda) * (iRadius + cpdr), y + sin(startA - cpda) * (iRadius + cpdr)];
    this.cp2 = [x + cos(endA + cpda) * (iRadius + cpdr), y + sin(endA + cpda) * (iRadius + cpdr)];
    this.ep = [x + cos(startA + da / 2) * oRadius, y + sin(startA + da / 2) * oRadius];

    this.curve1 = new QCurve({ sp: this.sp, cp: this.cp1, ep: this.ep });
    this.curve2 = new QCurve({ sp: this.sp, cp: this.cp2, ep: this.ep });
    this.create();
  }
  create() {
    this.path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
    this.path.setAttribute('d', this.pathD());
    this.path.style.fill = this.fill;
    this.parent.appendChild(this.path);
  }
  pathD() {
    return [
      this.curve1.pathD(),
      this.curve2.pathD(),
    ].join(' ');
  }
}

class QCurve {
  constructor({ sp, cp, ep }) {
    this.sp = sp;
    this.cp = cp;
    this.ep = ep;
  }
  pathD() {
    return [
      `M${this.sp[0]} ${this.sp[1]}`,
      `Q${this.cp[0]} ${this.cp[1]}`,
      `${this.ep[0]} ${this.ep[1]}`,
    ].join(' ');
  }
}

function clamp(v, min, max) {
  return min(max(v, min), max);
}

function rnd(min, max) {
  if (max === undefined) { max = min; min = 0; }
  return random() * (max - min) + min;
}

function rndInt(min, max) {
  if (max === undefined) { max = min; min = 0; }
  min = ceil(min);
  max = floor(max);
  return floor(random() * (max - min)) + min;
}

App();

              
            
!
999px

Console