I have been trying to make a Guitar String animation with sound and so far I have made the line flick and go up and down to simulate a guitar string pluck, but for some reason I can't make it do that twice. What I mean is first time I hover over the string it acts as it should, but second time I hover over it just the sound plays, but the string doesn't repeat the upwards/downwards motion. This is the code for the string:
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.moveTo(0, 5);
context.bezierCurveTo(0, 5, 250, 5, 500, 5);
context.strokeStyle = '#00DFFF';
context.stroke();
var a=5; var interv = null;
$(document).ready(function(){
var beepOne = $("#beep-one")[0];
$("#myCanvas")
.mouseenter(function() {
beepOne.play();
interv = setInterval(pluck, 50);
});
});
function pluck() {
var x=5;
if(a>0) a-=0.1; else if(a==0){ unpluck; a=5;} else a+=0.1;
a=a*-1;
x=x+a;
canvas.width = canvas.width;
context.moveTo(0, 5);
context.bezierCurveTo(0, 5, 250, x, 500, 5);
context.strokeStyle = '#00DFFF';
context.stroke();
}
function unpluck() {
clearInterval(interv);
}
Can someone help with this issue?