Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <body>
  <!-- remove the class d-none to see the markers -->
  <div class="marker red d-none"></div>
  <div class="marker blue d-none"></div>

  <div class="wrapper">
    <div class="platypus-wrapper absolute">
      <div class="tail-wrapper flex-center-start">
        <div class="tail"></div>
      </div>

      <div class="platypus-body-marker">
        <div class="head-marker">
          <div class="beak-marker"></div>
        </div>
      </div>

      <div class="platypus-body">
        <div class="head">
          <div class="eyes-wrapper">
            <div class="eyes"></div>
          </div>
          <div class="beak"></div>
          <div class="legs">
            <div class="leg"></div>
            <div class="leg"></div>
          </div>
        </div>

        <div class="legs">
          <div class="leg"></div>
          <div class="leg"></div>
        </div>
      </div>
    </div>

  </div>

  <div class="sign">
    by masahito / <a href="http://www.ma5a.com/" >ma5a.com</a>
  </div>
</body>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

p, h1, h2, h3, h4 {
  display: inline-block;
  margin-block-start: 0em;
  margin-block-end: 0em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
  padding-inline-start: 0px;
}

body {
  background-color: rgb(49, 203, 206);
  font-family: Arial, Helvetica, sans-serif;
}

.wrapper {
  width: 100%;
  height: 100dvh;
  overflow: hidden;
}

.absolute,
.platypus-wrapper div:not(.leg) {
  position: absolute;
}

.platypus-wrapper,
.platypus-wrapper div {
  width: calc(var(--w) * 2px);
  height: calc(var(--h) * 2px);
  background-size: calc(var(--w) * 2px) calc(var(--h) * 2px) !important; 
  background-repeat: no-repeat !important;
  image-rendering: pixelated;
}

.flex-center-start {
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}


.platypus-wrapper {
  transition: 3s ease;
  --oval: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABsAAAAUCAYAAAB8gkaAAAAAAXNSR0IArs4c6QAAAHFJREFUSEtjZCAAHFTF/xNSA5M/cPslIz61WCVJsQCX4dgsRrGMGpagW45sKdwyWliEHrxgy2hpEbKF9LWMHr6C+Y5x1DJiMzbeTD0ajEMvGIdvCQKLC1qmSljJPzBVDHJqo4YvCVae2JI3KRYTahYAAEKjO+0fJvd3AAAAAElFTkSuQmCC);
}

.head {
  background-image: var(--oval);
  transition: 1s;
}

.eyes-wrapper,
.eyes {
  --w: 27;
}

.eyes-wrapper {
  --h: 6;
  top: 8px;
}

.eyes {
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABsAAAADCAYAAABiUXSjAAAAAXNSR0IArs4c6QAAACBJREFUKFNjZMAC/v///5+RkZERmxwxYrj0YzVwWFgGAFhWGARPxBgqAAAAAElFTkSuQmCC);
  --h: 3;
  transition: 0.4s;
}

.beak,
.beak-marker {
  --w: 15;
  --h: 9;
}

.beak {
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAJCAYAAADtj3ZXAAAAAXNSR0IArs4c6QAAAEBJREFUKFNjZEADjqVT/6OLwfj7u7MZkeXgHHya0A2DGQLWTIpGZFcwkqMRZsAAaqbIzzD3k+J3lNBGjgpS4hkA/Eke6GdrhXYAAAAASUVORK5CYII=);
  transition: 0.7s;
  z-index: 1;
}

.platypus-wrapper,
.platypus-body,
.platypus-body-marker,
.head,
.head-marker,
.tail-wrapper {
  --w: 27;
  --h: 20;
}

.platypus-body {
  background-image: var(--oval);
  transition: 0.5s;
}

.legs {
  display: flex;
  justify-content: space-between;
  --w: 27;
  --h: 8;
  z-index: -2;
  bottom: -10px;
  padding: 0 3px;
}

.leg {
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAICAYAAAArzdW1AAAAAXNSR0IArs4c6QAAADNJREFUKFNjZIACx9Kp/2FsGL2/O5sRxAYTIEBQETYFyKYx4lMAU0hvRTCfwbwLciM6GwCYkCExoGXR0AAAAABJRU5ErkJggg==);
  --w: 9;
  --h: 8;
  transform-origin: top center;
}

.head .leg:nth-child(1),
.leg:nth-child(2) {
  animation: swim-1 infinite ease-in-out 3s;
}

.head .leg:nth-child(2),
.leg:nth-child(1) {
  animation: swim-2 infinite ease-in-out 3s;
}

.head .leg.swim,
.leg.swim {
  animation-duration: 0.5s;
}

@keyframes swim-1 {
  0%, 100% {
    transform: rotate(-25deg);
    margin-top: 5px;
  }
  50% {
    transform: rotate(25deg);
    margin-top: 0px;
  }
} 

@keyframes swim-2 {
  0%, 100% {
    transform: rotate(25deg);
    margin-top: 5px;
  }
  50% {
    transform: rotate(-25deg);
    margin-top: 0px;
  }
} 

.tail-wrapper {
  transition: 1.5s ease;
  z-index: -1;
}

.tail {
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAVCAYAAACg/AXsAAAAAXNSR0IArs4c6QAAAGtJREFUOE9jZMACHFTF/2MTB4kduP2SEV0ORQCfZnSNyIbBDSHFAJiBMIPAhpBjALJB1DGEElfAXMM4aghGOhxuYTJ4UiwsqMlJdCgZkFwvYRhCqkFYyxNSvIVeumEUdcSEDUFDCHkLWxkLAKLwP+4zWBf2AAAAAElFTkSuQmCC);
  --w: 17;
  --h: 21;
  margin-top: -15px;
}

.marker {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-top: -5px;
  margin-left: -5px;
  z-index: 100;
}

.red { 
  background-color: rgb(223, 74, 41); 
}
.blue { 
  background-color: rgb(140, 238, 250); 
}

.d-none {
  display: none;
}

.sign {
  position: fixed;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
  bottom: 10px;
  right: 10px;
  font-size: 10px;
  text-transform: none;
}

a {
  color: white;
  text-decoration: none;
  text-transform: none;
}

a:hover { text-decoration: underline; }

.indicator {
  width: 200px;
  color: white;
  bottom: 20px;
  left: 20px;
}
              
            
!

JS

              
                const setStyles = ({ target, h, w, x, y }) =>{
    if (h) target.style.height = h
    if (w) target.style.width = w
    target.style.transform = `translate(${x || 0}, ${y || 0})`
  }

  const elements = {
    body: document.querySelector('.wrapper'),
    platypus: {
      wrapper: document.querySelector('.platypus-wrapper'),
      body: document.querySelector('.platypus-body'),
      head: document.querySelector('.head'),
      beak: document.querySelector('.beak'),
      bodyMarker: document.querySelector('.platypus-body-marker'),
      headMarker: document.querySelector('.head-marker'),
      beakMarker: document.querySelector('.beak-marker'),
      tail: document.querySelector('.tail-wrapper'),
      eyes: document.querySelector('.eyes-wrapper'),
      legs: document.querySelectorAll('.leg'),
    },
    marker: document.querySelectorAll('.marker'),
  }

  const positionMarker = (i, pos) => {
    elements.marker[i].style.left = px(pos.x)
    elements.marker[i].style.top = px(pos.y)
  }

  const px = num => `${num}px`
  const radToDeg = rad => Math.round(rad * (180 / Math.PI))

  const setAngle = ({ target, angle }) => {
    target.style.transform = `rotate(${angle}deg)`
  }

  const getTargetAngle = () =>{
    const { x, y } = getPlatypusBeakPos()
    return angle = radToDeg(Math.atan2(y - control.y, x - control.x)) - 90
  }
  
  const control = {
    x: null, 
    y: null,
    platypusTimer: null,
  }

  const getValueWithinBound = ({ value, min, max, buffer }) => {
    return value = value < (min - buffer)
    ? min - buffer
    : value > (max + buffer)
    ? max + buffer
    : value
  }

  const moveWithinBound = ({ target, boundary, pos, buffer }) => {
    const { left: hX, top: hY, width, height } = boundary.getBoundingClientRect()

    setStyles({ 
      target, 
      x: px(getValueWithinBound({
          value: pos.x - (target.clientWidth / 2),
          min: hX,
          max: hX + width - target.clientWidth,
          buffer: buffer.x
        }) - hX), 
      y: px(getValueWithinBound({
          value: pos.y - (target.clientHeight / 2),
          min: hY,
          max: hY + height - target.clientHeight,
          buffer: buffer.y
        }) - hY), 
    })
  }

  const movePlatypus = pos => {
    control.x = pos.x
    control.y = pos.y
    positionMarker(0, control)

    const { body, bodyMarker, wrapper, head, headMarker, beak, beakMarker, eyes, tail } = elements.platypus

    ;[
      { 
        target: body, 
        boundary: wrapper,
      },
      { 
        target: bodyMarker, 
        boundary: wrapper,
      },
      { 
        target: head, 
        boundary: body,
      },
      { 
        target: headMarker, 
        boundary: bodyMarker,
      },
      { 
        target: beak, 
        boundary: head,
        buffer: { x: 5, y: 3 }
      },
      { 
        target: beakMarker, 
        boundary: headMarker,
        buffer: { x: 5, y: 3 }
      },
      { 
        target: eyes.childNodes[1],
        boundary: eyes,
        buffer: { x: 5, y: 3 }
      }
    ].forEach(item => {
        moveWithinBound({
          target: item.target,
          boundary: item.boundary,
          pos: control,
          buffer: item.buffer || { x: 15, y: 15 } 
        })
      })

    setAngle({
      target: tail,
      angle: getTargetAngle() - 180
    })
  }

  const getPlatypusBeakPos = () => {
    const { x, y, width, height } = elements.platypus.beakMarker.getBoundingClientRect()
    return {
      x: x + (width / 2),
      y: y + (height / 2)
    }
  }

  const callPlatypusTowardsClick = () => {
    const { x, y } = elements.platypus.wrapper.getBoundingClientRect()
    const newX = control.x + (x - getPlatypusBeakPos().x)
    const newY = control.y + (y - getPlatypusBeakPos().y)

    positionMarker(1, {
      x: newX,
      y: newY
    })
    setStyles({
      target: elements.platypus.wrapper,
      x: px(newX),
      y: px(newY)
    })

    elements.platypus.legs.forEach(leg => {
      leg.classList.add('swim')
    })
    setTimeout(()=>{
      elements.platypus.legs.forEach(leg => {
        leg.classList.remove('swim')
      })
    }, 3000)
  }

  const positionPlatypus = () => {
    clearTimeout(control.platypusTimer)
    const { offsetWidth, offsetHeight } = elements.body
    control.x = offsetWidth / 2
    control.y = (offsetHeight / 2) - elements.platypus.wrapper.clientHeight

    callPlatypusTowardsClick()
    movePlatypus({
      x: control.x + 10,
      y: control.y + 100
    })
    control.platypusTimer = setTimeout(()=>{
      movePlatypus({
        x: control.x,
        y: control.y + 80
      })
    }, 2000)
  }


  elements.body.addEventListener('mousemove', e => movePlatypus({
    x: e.pageX,
    y: e.pageY
  }))
  window.addEventListener('click', callPlatypusTowardsClick)
  window.addEventListener('resize', positionPlatypus) 
  
  positionPlatypus()

              
            
!
999px

Console