首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >P5js中的多重插值

P5js中的多重插值
EN

Stack Overflow用户
提问于 2017-01-22 18:56:41
回答 1查看 151关注 0票数 0

我试图在p5js中创建多个内插,为此,我尝试在javaScript中重新创建Ben的Integrator类。我意识到从处理到p5js的转换和转换是相当困难的,所以如果这不可能的话,如果您给我一个提示以另一种方式这样做,我将非常感激。谢谢。

以下是我迄今所做的工作。。。

代码语言:javascript
复制
   function Integrator(value, damping, attraction)
{
  this.DAMPING=0.5;
  this.ATTRACTION=0.2;

  this.value;
  this.vel;
  this.accel;
  this.force;
  this.mass=1;

  this.damping=this.DAMPING;
  this.attraction=this.ATTRACTION;

  this.targeting; //boolean
  this.target;

  this.value=value;
  this.damping=damping;
  this.attraction=attraction;

  this.set =function(v)
  {
    this.value=v;
  }

  this.update = function()
  {
    if(this.targeting)
    {
      this.force +=this.attraction *(this.target-this.value);
    }

    this.accel = this.force/this.mass;
    this.vel = (this.vel+this.accel)*this.damping;
    this.value +=this.vel;

    this.force=0;
  }

  this.target = function(t)
  {
    this.targeting=true;
    this.target=t;
  }

  this.noTarget = function()
  {
    this.targeting = false;
  }

}

以下也是Ben Fry http://benfry.com/writing/map/Integrator.pde原始处理代码的链接

EN

回答 1

Stack Overflow用户

发布于 2022-03-19 11:31:37

将类转换为JavaScript (见下文)相对简单。尽管需要注意的一件事是变量和同名函数,因为在JS中不允许这样做。你可以看到这里的演示

代码语言:javascript
复制
class Integrator {
  
  constructor(value = 100, damping = 0.5, attraction = 0.2) {
    this.force = 0;
    this.vel = 0;
    this.accel = 0;
    this.mass = 1;
    
    this.damping = damping;
    this.attraction = attraction;
    
    this._value = value;
    this._target = value;
  }

  value(v) {
    if (typeof v !== 'undefined') {
      this._value = v;
      return this;
    }
    return this._value;
  }

  update() {
    if (this.targeting) {
      this.force += this.attraction * (this._target - this._value);
    }

    this.accel = this.force / this.mass;
    this.vel = (this.vel + this.accel) * this.damping;
    this._value += this.vel;

    this.force = 0;
  }

  target(t) {
    if (typeof t !== 'undefined') {
      this.targeting = true;
      this._target = t;
      return this;
    }
    return this._target;
  }

  noTarget() {
    this.targeting = false;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41794922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档