HTML, CSS, JS

just a lil synthy thing made with tone.js

fork
loading
Files
  • index.html
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>synth</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>

  <style>
    #pad {
      position: absolute;

      top: 0;
      left: 0;
      right: 0;
      bottom: 0;

      background: #222;

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

    #label {
      font: 40px Arial;
      color: white;

      /* This makes the label text non-selectable */
      user-select: none;
    }

  </style>

  <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.3.11/Tone.min.js"></script>

    <div id="pad">
      <div id="label">CLICK / DRAG</div>
    </div>
  </body>

  <script>
    // I copied this synth setup from https://github.com/Tonejs/Tone.js/blob/master/examples/jump.html
    var synth = new Tone.Synth({
			"oscillator" : {
				"type" : "fatsawtooth",
				"count" : 3,
				"spread" : 30
			},
			"envelope": {
				"attack": 0.01,
				"decay": 0.1,
				"sustain": 0.5,
				"release": 0.4,
				"attackCurve" : "exponential"
			},
		}).toMaster();

    var pad = document.getElementById("pad");
    var label = document.getElementById("label");

    var dragging = false;

    function getFrequency(x){
      // Far left of the screen means fraction=0, far right means fraction=1
      let fraction = x/window.innerWidth;
      // Our maximum frequency is 1000Hz
      return fraction*1000;
    }

    function getBackgroundColor(x){
      if (dragging) {
        let fraction = x/window.innerWidth;
        // Create a "Hue/Saturation/Lightness" (hsl) color for the background
        // On left side of screen, hue=0 (red)
        // On right side of screen, hue=360 (also red)
        return "hsl("+(fraction*360)+", 100%, 50%)";
      }
      else return "#222";
    }

    function getLabelColor(x){
      if (dragging) {
        let fraction = x/window.innerWidth;
        // Create a "Hue/Saturation/Lightness" (hsl) color for the label
        // This one is the opposite of the background; the hue is offset by +180
        return "hsl("+(fraction*360+180)+", 100%, 50%)";
      }
      else return "white";
    }

    function getLabel(x){
      if (dragging) {
        var frequency = getFrequency(x);
        return Math.round(frequency)+"Hz";
      }
      else return "CLICK / DRAG";
    }

    function down(event) {
      // This is a little workaround for a problem with audio in Chrome;
      // Chrome disables any noisy sound things by default until you have clicked/touched something on the page, at which point we are allowed to turn sound back on.
      // That's what this little snippet does: It turns sound back on if it's disabled.
      if (Tone.context.state !== 'running') {
        Tone.context.resume();
      }

      dragging = true;
      var x = event.pageX;
      synth.triggerAttack(getFrequency(x));
      label.innerHTML = getLabel(x);
      label.style.color = getLabelColor(x);
      pad.style.background = getBackgroundColor(x);
    }

    function up(event) {
      dragging = false;
      synth.triggerRelease();
      label.innerHTML = getLabel();
      label.style.color = getLabelColor();
      pad.style.background = getBackgroundColor();
    }

    function move(event) {
      if (dragging) {
        var x = event.pageX;
        synth.setNote(getFrequency(x));
        label.innerHTML = getLabel(x);
        label.style.color = getLabelColor(x);
        pad.style.background = getBackgroundColor(x);
      }
    }

    pad.addEventListener("mousedown", down);
    pad.addEventListener("mouseup", up);
    pad.addEventListener("mousemove", move);

    pad.addEventListener("touchstart", event => {
      event.preventDefault();
      down(event.changedTouches[0]);
    });
    pad.addEventListener("touchend", event => {
      event.preventDefault();
      up(event.changedTouches[0]);
      });
    pad.addEventListener("touchmove", event => {
      event.preventDefault();
      move(event.changedTouches[0]);
    });
  </script>
</html>
result
console