【发布时间】:2021-04-02 22:05:53
【问题描述】:
有一个类,其方法可以绘制随机长度的矩形。
但是,不能只对形状进行旋转()而不平移( translate() ),平移会使形状从画布上绘制出来。
那么有没有办法让它在旋转时不发生平移?
代码:
class rect {
constructor(range) {
this.boundary = 100;
this.x = random(this.boundary, width - this.boundary);
this.y = random(this.boundary, height - this.boundary);
this.xu = this.x + random(50, 200);
this.yu = this.y + random(50, 200);
this.range = range;
this.limit = random(-range, range);
this.rand_color1 = random(255);
this.rand_color2 = random(255);
this.rand_color3 = random(255);
}
custom_shapes() {
// how to make no translations occur while only perform rotation on shapes?
translate(this.x-this.margin,this.y-this.margin);
rotate(30);
fill(this.rand_color1, this.rand_color2, this.rand_color3)
quad(this.x, this.y, this.xu + this.limit, this.y, this.xu, this.yu, this.x, this.yu + this.limit);
}
}
【问题讨论】:
-
我不太明白你在问什么,但是对于旋转来说,将图形转换到中心,旋转然后转换回它需要显示的位置以获得图形通常是有意义的围绕其中心旋转。如果这是您的情况,请查看以下答案:stackoverflow.com/a/60406447/1978785
标签: javascript p5.js