Skip to content

Commit d360ff9

Browse files
Added Wavepaint html file
1 parent bca3977 commit d360ff9

1 file changed

Lines changed: 154 additions & 6 deletions

File tree

examples/wavepaint/wavepaint.html

Lines changed: 154 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,163 @@
1616
<div class="badge click"></div>
1717
<div class="badge click white"></div>
1818
</div>
19-
19+
<script src="../../node_modules/@createjs/easeljs/dist/easeljs.min.js"></script>
20+
<script src="../../dist/soundjs-NEXT.js"></script>
2021
<script>
2122
var canvas = document.getElementById("canvas"),
2223
stage = new createjs.Stage(canvas);
2324
createjs.Ticker.on("tick", tick);
2425
createjs.Ticker.timingMode = createjs.Ticker.RAF;
2526

27+
28+
let waveGraphArea = new createjs.Shape();
29+
stage.addChild(waveGraphArea);
30+
let paintedWave = new createjs.Shape();
31+
stage.addChild(paintedWave);
32+
33+
let waveSamples = [];
34+
let graphWidth = 720, graphHeight = 300;
35+
let bufferLength = 720; // This length causes a frequency for middle C
36+
//let sample = new createjs.Sample();
37+
38+
// Default wave:
39+
for(let i = 0; i < bufferLength; i++){
40+
waveSamples.push(Math.sin(i * Math.PI / 360));
41+
}
42+
43+
let buffer = createjs.Sound.context.createBuffer(1,bufferLength*20,createjs.Sound.context.sampleRate);
44+
let bufferData = buffer.getChannelData(0);
45+
46+
for(let i = 0; i < bufferData.length; i++){
47+
bufferData[i] = waveSamples[i%waveSamples.length];
48+
}
49+
50+
let effectsGroup = new createjs.Group();
51+
let reverb = new createjs.FX.Reverb(1);
52+
reverb.dryGain = 1;
53+
effectsGroup.addEffect(reverb);
54+
55+
let wavePlayer = new createjs.Sample(buffer);
56+
let wavePlayerNode = createjs.Sound.context.createBufferSource();
57+
wavePlayerNode.buffer = buffer;
58+
wavePlayerNode.connect(effectsGroup.inputNode);
59+
wavePlayerNode.loop = true;
60+
createjs.Sound.rootGroup.volume = 0.3;
61+
wavePlayer.volume = 0.3;
62+
2663
// Init Code
2764
handleResize();
65+
updateWave();
66+
2867

2968
// Tick Code
30-
function tick(event) {
69+
function tick(event){
70+
stage.update();
3171
}
3272

73+
let lastX = -1;
74+
let lastY = -1;
75+
window.addEventListener("mouseup", handleMouseUp);
76+
77+
function handleMouseUp(e){
78+
lastX = lastY = -1;
79+
}
80+
81+
82+
window.addEventListener("mousedown", playWave);
83+
window.addEventListener("mouseup", stopWave);
84+
85+
waveGraphArea.on("mousedown", handleMouseDown);
86+
87+
waveGraphArea.on("pressmove", handlePressMove);
88+
waveGraphArea.x = 200;
89+
waveGraphArea.y = 50 + graphHeight/2;
90+
91+
function handleMouseDown(e){
92+
let local = waveGraphArea.globalToLocal(e.stageX, e.stageY);
93+
lastX = clamp(Math.round(local.x * waveSamples.length / graphWidth), 0, waveSamples.length-1);
94+
lastY = clamp(local.y / (graphHeight/2), -1, 1);
95+
}
96+
97+
function handlePressMove(e){
98+
let local = waveGraphArea.globalToLocal(e.stageX, e.stageY);
99+
100+
let newX = clamp(Math.round(local.x * waveSamples.length / graphWidth), 0, waveSamples.length-1);
101+
let newY = clamp(local.y / (graphHeight/2), -1, 1);
102+
let xDist = newX - lastX;
103+
let yDist = newY - lastY;
104+
105+
let leftBound, rightBound, leftY, rightY;
106+
107+
if(newX > lastX){
108+
leftBound = lastX;
109+
leftY = lastY;
110+
rightBound = newX;
111+
rightY = newY;
112+
}else if(newX < lastX){
113+
leftBound = newX;
114+
leftY = newY;
115+
rightBound = lastX;
116+
rightY = lastY;
117+
}
118+
119+
for(let i = leftBound; i <= rightBound; i++){
120+
waveSamples[i] = leftY + yDist * (i-rightBound)/xDist;
121+
}
122+
123+
lastX = newX;
124+
lastY = newY;
125+
126+
updateWave();
127+
128+
}
129+
130+
function updateWave(){
131+
drawWaveform();
132+
updateBuffer();
133+
}
134+
135+
function drawWaveform(){
136+
// Redraw wave from samples:
137+
paintedWave.graphics.clear();
138+
paintedWave.graphics.setStrokeStyle(2);
139+
paintedWave.graphics.beginStroke("black");
140+
paintedWave.graphics.moveTo(0, waveSamples[0] * graphHeight/2);
141+
for(let i = 1; i < waveSamples.length; i++){
142+
paintedWave.graphics.lineTo(i/waveSamples.length * graphWidth, waveSamples[i] * graphHeight/2);
143+
}
144+
paintedWave.graphics.endStroke();
145+
}
146+
147+
function updateBuffer(){
148+
let buffer = wavePlayerNode.buffer;
149+
let bufferData = buffer.getChannelData(0);
150+
151+
for(let i = 0; i < bufferData.length; i++){
152+
bufferData[i] = waveSamples[i%waveSamples.length];
153+
}
154+
155+
//wavePlayerNode.buffer = buffer;
156+
}
157+
158+
function playWave(){
159+
console.log("running play wave");
160+
wavePlayerNode = createjs.Sound.context.createBufferSource();
161+
wavePlayerNode.buffer = createjs.Sound.context.createBuffer(1,bufferLength,createjs.Sound.context.sampleRate);
162+
updateBuffer();
163+
wavePlayerNode.loop = true;
164+
wavePlayerNode.connect(effectsGroup.inputNode);
165+
wavePlayerNode.start();
166+
}
167+
168+
function stopWave(){
169+
170+
wavePlayerNode.stop();
171+
}
172+
173+
paintedWave.x = 200;
174+
paintedWave.y = 50 + graphHeight/2;
175+
33176
// Resize Code
34177
window.addEventListener("resize", handleResize, false);
35178
function handleResize(event) {
@@ -38,19 +181,24 @@
38181
canvas.width = w; canvas.height = h;
39182
// Layout other assets
40183

41-
let waveGraphArea = new createjs.Shape();
42-
let paintedWave = new createjs.Shape();
43184
let g = null;
44185

45186
g = waveGraphArea.graphics;
46187
g.beginFill("lightgrey");
47-
g.drawRect(100,100,100,100);
188+
// Drawing the square like this lines up the 0 point in the vertical with the middle of the graph, which lines up
189+
// with our wave painting's -1 to 1 values
190+
g.drawRect(0, -graphHeight/2,graphWidth,graphHeight);
48191
g.endFill();
49-
stage.addChild(waveGraphArea);
192+
193+
50194

51195
stage.update();
52196
}
53197

198+
function clamp(input, min, max){
199+
return Math.max(Math.min(input, max), min);
200+
}
201+
54202

55203
</script>
56204
</body>

0 commit comments

Comments
 (0)