3

受这个SO answer的启发,我有这段代码,它可以工作,但它会弹出并且听起来不像它可以的那样流畅。

$('.btn').each(initDraggable);
function initDraggable() {
	var param = {}
	param.drag = drag
	param.stop = stopper
	$(this).draggable(param)
}
var Variables = {}
Variables.Frequency = 0
function drag(myEvent,myUI) {
	var Frequency = myUI.position.left + 100
	var Duration = 5000
	if (Math.abs(Variables.Frequency-Frequency) > 25) {
		if (typeof(Variables.osc) === 'undefined') {
		} else {
			Variables.osc.stop(0)
		}
		Variables.Frequency = Frequency
		Oscillator(Frequency,Duration)
	}
}
function stopper() {
	Variables.osc.stop(0)
}


Variables.ctx = new(window.audioContext || window.webkitAudioContext)
function Oscillator(argFrequency,argDuration) {
	Variables.osc = Variables.ctx.createOscillator()
	Variables.osc.type = 0
	Variables.osc.connect(Variables.ctx.destination)
	Variables.osc.frequency.value = argFrequency
	Variables.osc.start(0)
	setTimeout(myTimeout,argDuration)
	function myTimeout() {
		Variables.osc.stop(0)
	}
}
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.css">
<span class="btn btn-primary">Drag me</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/bootstrap/latest/js/bootstrap.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui.touch-punch/latest/jquery.ui.touch-punch.js"></script>

问:有没有一种简单的方法可以让这听起来更流畅一些?

4

1 回答 1

1

没有必要重新创建oscillatoron drag,我认为这就是导致流行音乐的原因。但是你可以改变frequency value一个开始的oscillator,所以你可以通过在拖动时创建振荡器来实现你的结果start,停止它stop,然后简单地改变频率值drag。像这样:

$('.btn').each(initDraggable);

function initDraggable() {
  var param = {}
  param.drag = drag
  param.stop = stopper
  param.start = startOsc
  $(this).draggable(param)
}
var Variables = {}
Variables.Frequency = 0

function drag(myEvent, myUI) {
  var Frequency = myUI.position.left + 100
  var Duration = 5000
  if (Math.abs(Variables.Frequency - Frequency) > 25) {
    //You could put some validation on creation, but since you're not creating it, it's not necessary here	
    /*if (typeof(Variables.osc) === 'undefined') {
		} else {
			Variables.osc.stop(0)
		}*/

    Variables.Frequency = Frequency;
    //Oscillator(Frequency,Duration)
    Variables.osc.frequency.value = Frequency;
  }
}

function startOsc(myEvent, myUI) {
  var Frequency = myUI.position.left + 100
  Oscillator(Frequency) //Since it plays in continue you don't need duration
}

function stopper() {
  Variables.osc.stop(0)
}


Variables.ctx = new(window.AudioContext || window.webkitAudioContext)

function Oscillator(argFrequency /*,argDuration*/ ) {
  Variables.osc = Variables.ctx.createOscillator()
  Variables.osc.type = 0
  Variables.osc.connect(Variables.ctx.destination)
  Variables.osc.frequency.value = argFrequency
  Variables.osc.start(0)
    /*setTimeout(myTimeout,argDuration)
	function myTimeout() {
		Variables.osc.stop(0)
	}*/
}
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.css">
<span class="btn btn-primary">Drag me</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/bootstrap/latest/js/bootstrap.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui.touch-punch/latest/jquery.ui.touch-punch.js"></script>

于 2015-07-23T01:50:35.700 回答