javascript - 在 P5.js 中改变对象高度的正弦函数
问题描述
我在 P5.js 中有一个接近按预期工作的草图,只是我不知道如何控制周期或波长。如果您运行代码,您将看到,动画会创建长扫波,但我希望能够根据参数设置波长。这只是一个编码练习 - 任何帮助将不胜感激!
请注意,代码不是直接在 SO 上运行,但可以在此处运行,https://editor.p5js.org/knectar/sketches/ONxJGvmJH
var arr = []; //array that holds rectangles
var rectNum; //number of rectangles drawn
var xPos; // horizontal position of rectangle
var yPos; // vertical position of rectangle
var w = 5; // rectangle width
var h; // rectangle height
var rPadding = 4; //space between bars
var amplitude = 40;
var frequency = 2;
function setup() {
createCanvas(1000, 400);
angleMode(DEGREES); //slows the animation down (maybe an issue?)
rectNum = floor(width/(w+rPadding)); //limit array size to width of canvas
yPos = height*.6; // positions objects in vertical center
for (var i = 0; i < rectNum+1; i++){
var bar = new Bar(i*(w+rPadding), yPos, w, -h); //builds out the objects
arr.push(bar); //loads objects into array
}
}
function draw() {
background(1);
for (var i = 0; i < arr.length; i++){
arr[i].make();
arr[i].move(i);
}
}
function Bar(xPos, yPos, w, h){
this.xPos = xPos;
this.yPos = yPos;
this.width = w;
this.height = h;
this.move = function(i){
this.height = sin(frameCount*frequency+i)*amplitude+(amplitude*2); // sine function to control rectange height
}
this.make = function(){
strokeWeight(0);
stroke(51);
fill(160, 0, 124);
rect(this.xPos, this.yPos, this.width, -this.height); // negative height sets wave to point up
}
}
解决方案
在处理sin
函数的参数时,必须以度为单位进行设置。
如果您在所有Bar
对象上分布 360 度(分布在 上arr.length
),那么您将生成 1 条完整的正弦曲线:
this.move = function(i){
var alpha = 360.0*i/arr.length;
this.height = sin(alpha) * amplitude + (amplitude*2);
}
波频率为1.0 / wavelength
。这意味着角度必须除以波长。
例如 4 个完整的正弦曲线可以由 生成wavelength = 0.25
。
var wavelength = 0.25;
this.move = function(i){
var alpha = 360.0*i/arr.length;
this.height = sin(frameCount*frequency + alpha/wavelength) * amplitude + (amplitude*2);
}
var arr = []; //array that holds rectangles
var rectNum; //number of rectangles drawn
var xPos; // horizontal position of rectangle
var yPos; // vertical position of rectangle
var w = 5; // rectangle width
var h; // rectangle height
var rPadding = 4; //space between bars
var amplitude = 40;
var frequency = 2;
function setup() {
createCanvas(600, 300);
angleMode(DEGREES); //slows the animation down (maybe an issue?)
rectNum = floor(width/(w+rPadding)); //limit array size to width of canvas
yPos = height*.6; // positions objects in vertical center
for (var i = 0; i < rectNum+1; i++){
var bar = new Bar(i*(w+rPadding), yPos, w, -h); //builds out the objects
arr.push(bar); //loads objects into array
}
}
function draw() {
background(1);
for (var i = 0; i < arr.length; i++){
arr[i].make();
arr[i].move(i);
}
}
function Bar(xPos, yPos, w, h){
this.xPos = xPos;
this.yPos = yPos;
this.width = w;
this.height = h;
frequency = arr.length / (2.0 * Math.PI);
var wavelength = 0.25;
this.move = function(i){
var alpha = 360.0*i/arr.length;
this.height = sin(frameCount*frequency + alpha/wavelength) * amplitude + (amplitude*2);
}
this.make = function(){
strokeWeight(0);
stroke(51);
fill(160, 0, 124);
rect(this.xPos, this.yPos, this.width, -this.height); // negative height sets wave to point up
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
推荐阅读
- bash - 无法直接从 bashoperator 运行 .sh 或 .py 文件
- css - 如何自动增加文本区域的宽度?
- mongodb - org.apache.kafka.connect.runtime.rest.errors.BadRequestException
- protractor - 如果片段中没有“禁用”,如何检查量角器是否禁用垫选择?
- ruby-on-rails - ActiveStorage 如何在上传到 S3 之前转换图像?
- java - JSON.stringify() 不适用于大 html 数据
- lotus-notes - rtlib-lotus notes富文本库是否支持IBM Domino Server 64bit
- r - map_dfr 函数将向量绑定到矩阵并并行运行的类比是什么?
- python - 如果将带有 None 值的 *args 传递给 namedtuple 对象,则会丢失所需的位置参数
- javascript - 标签按下时的焦点问题