Skip to content

Commit

Permalink
feat: add sound effect
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Sep 28, 2020
1 parent 01d0955 commit 420bf92
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
Binary file added src/assets/sounds/switch.mp3
Binary file not shown.
4 changes: 4 additions & 0 deletions src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from "react";
import Sidebar from "../components/sidebar/Sidebar";
import threeApp from "../three/index";
import Permalink from "../components/sidebar/Permalink";
import SwitchSound from "../assets/sounds/switch.mp3";

export default function Home() {
const rootEl = useRef(null);
Expand All @@ -15,6 +16,9 @@ export default function Home() {
<>
{showSidebar && <Sidebar />}

<audio id="switch-sound">
<source src={SwitchSound} type="audio/mpeg" />
</audio>
<div
id="canvas-wrapper"
ref={rootEl}
Expand Down
2 changes: 2 additions & 0 deletions src/three/key/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
updateMaterials,
} from "./materials";
import { keyGeometry, keyGeometryISOEnter } from "./geometry";
import SoundUtil from "../../util/sound";

export const KEYSTATES = {
INITIAL: 0, // full height
Expand Down Expand Up @@ -178,6 +179,7 @@ export class Key {
this.setState(KEYSTATES.MOVING_UP);
this.queueRelease = false;
}
SoundUtil.playClic();
}
updateColors(textureOnly) {
this.colorway = ColorUtil.colorway;
Expand Down
11 changes: 11 additions & 0 deletions src/util/sound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class SoundUtil {
static switchSound = undefined;
static playClic() {
if (!this.switchSound) {
this.switchSound = document.getElementById("switch-sound");
}
if (this.switchSound) {
this.switchSound.cloneNode(true).play();
}
}
}

0 comments on commit 420bf92

Please sign in to comment.