Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在js中设置正弦波动画_Javascript_Animation_Canvas_Requestanimationframe_Trigonometry - Fatal编程技术网

Javascript 在js中设置正弦波动画

Javascript 在js中设置正弦波动画,javascript,animation,canvas,requestanimationframe,trigonometry,Javascript,Animation,Canvas,Requestanimationframe,Trigonometry,我试图在JS中设置正弦波的动画,但它并没有按预期的那样工作。我正在使用一个元素和window.requestAnimationFrame()方法,但它占用了CPU资源,当我用滑块改变频率时,它只会中断并显示随机波形。我也不知道画相邻的线是否是表示正弦波的最佳方式。请注意,我将使用vanilla JS,正弦的频率和振幅是由滑块设置的变量。提前谢谢 到目前为止,我得到的是: 更新:我已经完成了,这是新版本: var canvas=document.querySelector(“canvas”),

我试图在JS中设置正弦波的动画,但它并没有按预期的那样工作。我正在使用一个
元素和
window.requestAnimationFrame()
方法,但它占用了CPU资源,当我用滑块改变频率时,它只会中断并显示随机波形。我也不知道画相邻的线是否是表示正弦波的最佳方式。请注意,我将使用vanilla JS,正弦的频率和振幅是由滑块设置的变量。提前谢谢

到目前为止,我得到的是:

更新:我已经完成了,这是新版本:

var canvas=document.querySelector(“canvas”),
ctx=canvas.getContext(“2d”),
cHeight=画布高度,
cWidth=canvas.width,
频率=document.querySelector(“#f”).值,
振幅=80,
x=0,
y=cHeight/2,
点y=0;
window.onload=init;
函数init(){
document.querySelector(“#f”).addEventListener(“输入”,函数(){
频率=该值;
document.querySelector(“#output_f”)。值=频率;
},假);
drawSine();
}
函数drawSine(){
ctx.clearRect(0,0,cWidth,cHeight);
ctx.beginPath();
ctx.moveTo(0,y);
ctx.strokeStyle=“红色”;
ctx.lineTo(cWidth,y);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.strokeStyle=“黑色”;
对于(x=0;x<600;x++){
点y=振幅*-数学sin((频率/95.33)*x)+y;
ctx.lineTo(x,点y);
}
ctx.stroke();
ctx.closePath();
请求动画帧(drawSine);
}
画布{
边框:1px纯红;
利润率:10px;
}

20

看看这个例子是否能对你有所帮助

函数init()
{
设定间隔(OnDraw,200);
}
var时间=0;
var color=“#ff0000”;
函数OnDraw()
{
时间=时间+0.2;
var canvas=document.getElementById(“mycanvas”);
var dataLine=canvas.getContext(“2d”);
var值=document.getElementById(“线宽”);
clearRect(0,0,canvas.width,canvas.height);
dataLine.beginPath();

对于(cnt=-1;cnt,请查看此示例是否对您有所帮助

函数init()
{
设定间隔(OnDraw,200);
}
var时间=0;
var color=“#ff0000”;
函数OnDraw()
{
时间=时间+0.2;
var canvas=document.getElementById(“mycanvas”);
var dataLine=canvas.getContext(“2d”);
var值=document.getElementById(“线宽”);
clearRect(0,0,canvas.width,canvas.height);
dataLine.beginPath();

对于(cnt=-1;cnt,我已经把正弦波弄得乱七八糟了,因为我正在做一个涉及动画正弦波的小项目。我有一些代码你可能会感兴趣看一看。就像前面提到的,你需要确保你在循环中使用了正确的增量,这样线就不会看起来参差不齐

window.requestAnimationFrame=window.requestAnimationFrame||
window.mozRequestAnimationFrame||
window.webkitRequestAnimationFrame||
window.msRequestAnimationFrame||
功能(f){
返回设置超时(f,1000/60)
};
var canvas=document.getElementById(“canvas”);
var ctx=canvas.getContext(“2d”);
var startTime=new Date().getTime();
函数getPath(高度){
var width=canvas.width;
var间距=0.08;
var-loopNum=0;
var pointList=[];
var i=0;
对于(i=0;i0;i--){
点列表[loopNum]=[loopNum,Math.sin(loopNum*间距)*(i*高度)+100];
loopNum++;
}
返回点列表;
}
函数绘图(){
var currentTime=new Date().getTime();
var runTime=currentTime-startTime;
clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.lineWidth=2;
ctx.strokeStyle=“rgb(80100230)”;
变量高度=Math.sin(运行时*0.008)*0.2;
var pointList=getPath(高度);
对于(变量i=0;i<500;i++){
如果(i==0){
ctx.moveTo(pointList[0][0],pointList[0][1]);
}否则{
ctx.lineTo(pointList[i][0],pointList[i][1]);
}
}
ctx.stroke();
window.requestAnimationFrame(绘制);
}
window.requestAnimationFrame(绘制);

很抱歉,我没有真正编辑代码,这只是我正在编写的代码的直接副本。希望它能有所帮助。

我已经把正弦波弄得一团糟,因为我正在做一个涉及动画正弦波的小项目。我有一些代码你可能会感兴趣看一看。就像前面提到的,你需要确保在循环中使用了正确的增量,以使线条看起来不参差不齐

window.requestAnimationFrame=window.requestAnimationFrame||
window.mozRequestAnimationFrame||
window.webkitRequestAnimationFrame||
window.msRequestAnimationFrame||
功能(f){
返回设置超时(f,1000/60)
};
var canvas=document.getElementById(“canvas”);
var ctx=canvas.getContext(“2d”);
var startTime=new Date().getTime();
函数getPath(高度){
var width=canvas.width;
var间距=0.08;
var-loopNum=0;
var pointList=[];
var i=0;
对于(i=0;i0;i--){
点列表[loopNum]=[loopNum,Math.sin(loopNum*间距)*(i*高度)+100];
loopNum++;
}
返回点列表;
}
函数绘图(){
var currentTime=new Date().getTime();
var runTime=currentTime-startTime;
clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.lineWidth=2;
ctx.strokeStyle=“rgb(80100230)”;
变量高度=Math.sin(运行时*0.008)*0.2;
var pointList=getPath(高度);
对于(变量i=0;i<500;i++){
如果(i==0){
ctx
    function init()
    {
        setInterval(OnDraw, 200);
    }

    var time = 0;
    var color = "#ff0000";

    function OnDraw()
    {
        time = time + 0.2;
        var canvas = document.getElementById("mycanvas");
        var dataLine = canvas.getContext("2d");
        var value = document.getElementById("lineWidth");

        dataLine.clearRect(0, 0, canvas.width, canvas.height);

        dataLine.beginPath();

        for(cnt = -1; cnt <= canvas.width; cnt++)
        {
            dataLine.lineTo(cnt, canvas.height * 0.5 - (Math.random() * 2 + Math.cos(time + cnt * 0.05) * 20 ));
        }

        dataLine.lineWidth = value.value * 0.1;
        dataLine.strokeStyle = color;
        dataLine.stroke();
    }
window.requestAnimationFrame = window.requestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.msRequestAnimationFrame ||
function(f) {
  return setTimeout(f, 1000 / 60)
};

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var startTime = new Date().getTime();

function getPath(height) {
  var width = canvas.width;
  var spacing = 0.08;
  var loopNum = 0;
  var pointList = [];
  var i = 0;

  for (i = 0; i < width / 2; i++) {
    pointList[loopNum] = [loopNum, Math.sin(loopNum * spacing) * (i * height) + 100];
    loopNum++;
  }
  for (i = width / 2; i > 0; i--) {
     pointList[loopNum] = [loopNum, Math.sin(loopNum * spacing) * (i * height) + 100];
     loopNum++;
  }
  return pointList;
}

function draw() {
  var currentTime = new Date().getTime();
  var runTime = currentTime - startTime;

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  ctx.lineWidth = 2;
  ctx.strokeStyle = "rgb(80, 100, 230)";

  var height = Math.sin(runTime * 0.008) * 0.2;
  var pointList = getPath(height);

  for (var i = 0; i < 500; i++) {
    if (i === 0) {
      ctx.moveTo(pointList[0][0], pointList[0][1]);
    } else {
      ctx.lineTo(pointList[i][0], pointList[i][1]);
    }
  }
  ctx.stroke();

  window.requestAnimationFrame(draw);
}

window.requestAnimationFrame(draw);