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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <header>
          <svg width="30px" height="30px" viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg">
          <title>Composi Logo</title>
          <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
            <g id="Composi-Logo-Solid" fill="#DF0000">
              <path d="M1.77635684e-15,0 L95,0 L95,38 L209,38 L209,0 L300,0 L300,94 L265,93.8571663 L265,209 L300,209 L300,300 L209,300 L209,265 L95,265 L95,300 L1.77635684e-15,300 L1.77635684e-15,209 L40,209 L40,94 L1.77635684e-15,93.8571663 L1.77635684e-15,0 Z M107,107 L107,192 L192,192 L192,107 L107,107 Z" id="Combined-Shape"></path>
            </g>
          </g>
        </svg>
  <h1>Dancing Pythagoras Tree</h1>
</header>
<section>
  <img id='fractal-tree' src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/77047/fractal-tree.png' height='295' width='419' >
</section>
              
            
!

CSS

              
                /* Bootstrap 4 Reset */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-family: sans-serif;
  line-height: 1.15;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  -ms-overflow-style: scrollbar;
  -webkit-tap-highlight-color: transparent;
  font-size: 62.5%;
}

@-ms-viewport {
  width: device-width;
}

article, aside, dialog, figcaption, figure, footer, header, hgroup, main, nav, section {
  display: block;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  font-size: 1.4rem;
  font-weight: normal;
  line-height: 1.5;
  color: #212529;
  text-align: left;
  background-color: #fff;
}

[tabindex="-1"]:focus {
  outline: none !important;
}

hr {
  box-sizing: content-box;
  height: 0;
  overflow: visible;
}

h1, h2, h3, h4, h5, h6 {
  margin-top: 0;
  margin-bottom: .5rem;
}

p {
  margin-top: 0;
  margin-bottom: 1rem;
}

abbr[title],
abbr[data-original-title] {
  text-decoration: underline;
  -webkit-text-decoration: underline dotted;
          text-decoration: underline dotted;
  cursor: help;
  border-bottom: 0;
}

address {
  margin-bottom: 1rem;
  font-style: normal;
  line-height: inherit;
}

ol,
ul,
dl {
  margin-top: 0;
  margin-bottom: 1rem;
}

ol ol,
ul ul,
ol ul,
ul ol {
  margin-bottom: 0;
}

dt {
  font-weight: bold;
}

dd {
  margin-bottom: .5rem;
  margin-left: 0;
}

blockquote {
  margin: 0 0 1rem;
}

dfn {
  font-style: italic;
}

b,
strong {
  font-weight: bolder;
}

small {
  font-size: 80%;
}

sub,
sup {
  position: relative;
  font-size: 75%;
  line-height: 0;
  vertical-align: baseline;
}

sub {
  bottom: -.25em;
}

sup {
  top: -.5em;
}

a {
  color: #007bff;
  text-decoration: none;
  background-color: transparent;
  -webkit-text-decoration-skip: objects;
}

a:hover {
  color: #0056b3;
  text-decoration: underline;
}

a:not([href]):not([tabindex]) {
  color: inherit;
  text-decoration: none;
}

a:not([href]):not([tabindex]):focus, a:not([href]):not([tabindex]):hover {
  color: inherit;
  text-decoration: none;
}

a:not([href]):not([tabindex]):focus {
  outline: 0;
}

pre,
code,
kbd,
samp {
  font-family: monospace, monospace;
  font-size: 1em;
}

pre {
  margin-top: 0;
  margin-bottom: 1rem;
  overflow: auto;
  -ms-overflow-style: scrollbar;
}

figure {
  margin: 0 0 1rem;
}

img {
  vertical-align: middle;
  border-style: none;
}

svg:not(:root) {
  overflow: hidden;
}

a,
area,
button,
[role="button"],
input:not([type=range]),
label,
select,
summary,
textarea {
  -ms-touch-action: manipulation;
  touch-action: manipulation;
}

table {
  border-collapse: collapse;
}

caption {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
  color: #868e96;
  text-align: left;
  caption-side: bottom;
}

th {
  text-align: inherit;
}

label {
  display: inline-block;
  margin-bottom: .5rem;
}

button {
  border-radius: 0;
  border: solid 1px #ccc;
  background-color: #ccc;
  overflow: hidden;
}

button:focus {
  outline: 1px dotted;
  outline: 5px auto -webkit-focus-ring-color;
}

input,
button,
select,
optgroup,
textarea {
  margin: 0;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
}

button,
input {
  overflow: visible;
}

button,
select {
  text-transform: none;
}

button,
html [type="button"],
[type="reset"],
[type="submit"] {
  -webkit-appearance: button;
}

button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
  padding: 0;
  border-style: none;
}

input[type="radio"],
input[type="checkbox"] {
  box-sizing: border-box;
  padding: 0;
}

input[type="date"],
input[type="time"],
input[type="datetime-local"],
input[type="month"] {
  -webkit-appearance: listbox;
}

textarea {
  overflow: auto;
  resize: vertical;
}

fieldset {
  min-width: 0;
  padding: 0;
  margin: 0;
  border: 0;
}

legend {
  display: block;
  width: 100%;
  max-width: 100%;
  padding: 0;
  margin-bottom: .5rem;
  font-size: 1.5rem;
  line-height: inherit;
  color: inherit;
  white-space: normal;
}

progress {
  vertical-align: baseline;
}

[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
  height: auto;
}

[type="search"] {
  outline-offset: -2px;
  -webkit-appearance: none;
}

[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}

::-webkit-file-upload-button {
  font: inherit;
  -webkit-appearance: button;
}

output {
  display: inline-block;
}

summary {
  display: list-item;
}

template {
  display: none;
}

[hidden] {
  display: none !important;
}

html, body, h1, h2, h3, h4, h5, h6, p, ol, ul, li, dl,
dt, dd, blockquote, address, table {
  margin: 0;
  padding: 0;
}

* {
  font-weight: 100;
}


/****************/
/* Local Styles */
/****************/

html, body {
  height: 100%;
  -webkit-font-smoothing: antialiased;
}
header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: #333;
  box-shadow: 0 1px 12px 3px rgba(233, 0, 0, 1);
  padding: 10px 20px 20px;
  text-align: center;
}
header h1 {
  color: #fff;
  margin: 10px 0 0;
  font-size: 2.5rem;
  font-weight: 300;
}
body {
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
body * {
  -webkit-user-select: none;
}

section {
  margin-bottom: 20px;
  border-bottom: solid 1px #ccc;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  position: relative;
  touch-action: none;
}
section #fractal-tree {
  position: relative;
  z-index: 10000;
}
section > img#fractal-tree {
  position: absolute;
  bottom: 0;
  left: 35%
}

              
            
!

JS

              
                /** @jsx h */
const { h, render, Component } = composi

const {interpolateWarm, select, scaleLinear} = d3

Math.deg = radians => radians * (180 / Math.PI)

const memoizedCalc = (function() {
  const memo = {}
  const key = ({ w, heightFactor, lean }) => [w, heightFactor, lean].join("-")
  return args => {
    const memoKey = key(args)
    if (memo[memoKey]) {
      return memo[memoKey]
    } else {
      const { w, heightFactor, lean } = args
      const trigH = heightFactor * w
      const result = {
        nextRight: Math.sqrt(trigH ** 2 + (w * (0.5 + lean)) ** 2),
        nextLeft: Math.sqrt(trigH ** 2 + (w * (0.5 - lean)) ** 2),
        A: Math.deg(Math.atan(trigH / ((0.5 - lean) * w))),
        B: Math.deg(Math.atan(trigH / ((0.5 + lean) * w)))
      }
      memo[memoKey] = result
      return result
    }
  }
})()

const Pythagoras = ({
  w,
  x,
  y,
  heightFactor,
  lean,
  left,
  right,
  lvl,
  maxlvl
}) => {
  if (lvl >= maxlvl || w < 1) {
    return null
  }
  const { nextRight, nextLeft, A, B } = memoizedCalc({
    w: w,
    heightFactor: heightFactor,
    lean: lean
  })
  let rotate = ""
  if (left) {
    rotate = `rotate(${-A} 0 ${w})`
  } else if (right) {
    rotate = `rotate(${B} ${w} ${w})`
  }
  return (
    <g id='svg-root' ns="svg" transform={`translate(${x} ${y}) ${rotate}`}>
      <rect
        width={w}
        height={w}
        x={0}
        y={0}
        style={{
          fill: interpolateWarm(lvl / maxlvl)
        }}
      />

      <Pythagoras
        w={nextLeft}
        x={0}
        y={-nextLeft}
        lvl={lvl + 1}
        maxlvl={maxlvl}
        heightFactor={heightFactor}
        lean={lean}
        left
      />

      <Pythagoras
        w={nextRight}
        x={w - nextRight}
        y={-nextRight}
        lvl={lvl + 1}
        maxlvl={maxlvl}
        heightFactor={heightFactor}
        lean={lean}
        right
      />
    </g>
  )
}


const SVG_WIDTH = () => window.innerWidth
const SVG_HEIGHT = () => document.querySelector('section').clientHeight
const SVG = null

class Tree extends Component {
  constructor(props) {
    super(props)
    this.realMax = 11
    this.state = {
      currentMax: 10,
      baseW: 80,
      heightFactor: 0,
      lean: 0
    }
  }
  
  render(data) {
    this.SVG_HEIGHT = SVG_HEIGHT()
    let {currentMax} = this.state
    if (currentMax < this.realMax)
    this.setState({ currentMax: currentMax + 1 })
    return (    
      <svg
        id='fractal-tree'
        width={SVG_WIDTH()}
        height={this.SVG_HEIGHT}
      >
        <Pythagoras
          w={this.state.baseW}
          h={this.state.baseW}
          heightFactor={this.state.heightFactor}
          lean={this.state.lean}
          x={SVG_WIDTH() / 2 - 40}
          y={this.SVG_HEIGHT - this.state.baseW}
          lvl={0}
          maxlvl={this.state.currentMax}
        />
      </svg>
    )
  }
  componentDidMount() {
    this.element.addEventListener('mousemove', this)
    this.element.addEventListener('touchmove', this)
    document.body.addEventListener('touchcancel', this)
    const section = this.element.querySelector('section')
    this.SVG_HEIGHT = section && section.clientHeight || 300
  }
  handleEvent(e) {
    e.preventDefault()
    this.onMouseMove(e)
  }
  onMouseMove(e) {
    e.preventDefault()
    var x = e.clientX
    var y = e.clientY
    if (e.touches) {
      x = e.pageX
      y = e.pageY
    }
    var scaleFactor = scaleLinear().domain([this.SVG_HEIGHT, 0]).range([0, 0.8]);
    var scaleLean = scaleLinear().domain([0, SVG_WIDTH() / 2, SVG_WIDTH()]).range([0.5, 0, -0.5]);
    this.move({
      heightFactor: scaleFactor(y),
      lean: scaleLean(x)
    })
  }
  move({ heightFactor, lean }) {
    this.setState({ heightFactor, lean })
  }
}


const tree = new Tree({
  container: 'section',
  hydrate: 'img'
})
              
            
!
999px

Console