みかづきブログ その3

本ブログは更新を終了しました。通算140万ユーザーの方に観覧頂くことができました。長い間、ありがとうございました。

👆

引越し先はこちらです!

夏なのでCSSで蚊取り線香をつくって蚊が寄ってこないページをつくりました。

See the Pen mosquito coil with mosquito sound by kimmy (@kimmy) on CodePen.

まず、CSSで蚊取り線香をつくりました。

半円を互い違いに組み合わせて実装しております。

また、クリックすると火が着くのですが、

f:id:kimizuka:20170811004025p:plain

その際、蚊の嫌がる周波数の音を出しています。

f:id:kimizuka:20170811004036p:plain

なので、原理的にはサイト上の蚊取り線香に火をつければ蚊が寄ってこないわけです。
バナーや夏フェスのサイトとかに組み込むと、とても良いと思ってます。

Pug

#mosquito-coil
  - var length = 6;
  - for (var i = 0; i < length; ++i) {
    .circle
  - }

SCSS

$length: 6;
$width: 10px;
$color: #004D40;

body {
  background: #212121;
}

#mosquito-coil {
  position: absolute;
  top: 50%; left: 50%;
  cursor: pointer;
  
  &:before {
    display: block;
    position: absolute;
    top: $width;
    @if ($length % 2 == 0) {
      left: $width * -2 * ceil($length / 2);
    } @else {
      left: $width * (1 + $length);
    }
    border-radius: 50%;
    width: $width; height: $width;
    background: $color;
    content: "";
    transform: scale(.9);
    transition: background 12s ease-in-out;
  }

  &:after {
    display: block;
    position: absolute;
    top: $width; left: 0;
    border-radius: 50%;
    width: $width; height: $width;
    background: $color;
    content: "";
  }

  .circle {
    position: absolute;
    border: solid $width $color;

    &:nth-child(odd) {
      border-radius: 50% 50% 0 0 / 100% 100% 0 0;
      border-bottom: none;
    }

    &:nth-child(even) {
      border-radius: 0 0 50% 50% / 0 0 100% 100%;
      border-top: none;
    }

    @for $i from 0 through $length {
      &:nth-child(#{$i + 1}) {
        @if $i % 2 == 1 {
          top: $width * 1.5;
        } @else {
          top: -$width * $i;
        }
        left: $width * -2 * ceil($i / 2);
        width: $width * (1 + $i * 2);
        height: $width * (0.5 + 1 * $i); 
      }
    }
  }
  
  &.on {
    &:before {
      background: #4d0000;
      animation: mosquito-coil-blink 1.8s ease-in-out infinite alternate;
    }
  }
}

@keyframes mosquito-coil-blink {
  0% {
    box-shadow: 0 0 0 rgba(255, 255, 255, .8);
  }
  100% {
    box-shadow: 0 0 5px rgba(255, 255, 255, .8);
  }
}

JavaScript

(() => {
  
  "use strict";
  
  let AudioContext = window.AudioContext || window.webkitAudioContext,
      ctx = new AudioContext(),
      oscillator = ctx.createOscillator(),
      coil = document.getElementById("mosquito-coil"),
      isOn = false;
  
  coil.addEventListener("click", () => {
    isOn = !isOn;
    if (isOn) {
      oscillator = ctx.createOscillator();
      oscillator.frequency.value = 17000;
      oscillator.connect(ctx.destination);
      coil.classList.add("on");
      oscillator.start();
    } else {
      oscillator.stop();
      coil.classList.remove("on");
    }

  }, false);
  
})();