p5.js/processing求助
processing吧
全部回复
仅看楼主
level 2
mmm米🍒 楼主
[啊]1L敬度娘
2019年04月07日 06点04分 1
level 2
mmm米🍒 楼主
[泪]p5.js求转成processing
const trees = []
// Try changing these constants
Object.defineProperty(Branch, 'HEIGHT', {value: 200})
Object.defineProperty(Branch, 'B_WIDTH', {value: 4, writable: true})
Object.defineProperty(Branch, 'L_WIDTH', {value: 36, writable: true})
Object.defineProperty(Branch, 'SPEED', {value: 0.02, writable: true})
Object.defineProperty(Branch, 'VAR_ANGLE', {value: 0.2, writable: true})
Object.defineProperty(Branch, 'VAR_SCALE', {value: 0.25, writable: true})
Object.defineProperty(Branch, 'C_BRANCH', {value: [255, 255, 255], writable: true})
Object.defineProperty(Branch, 'C_LEAF', {value: [255, 255, 255], writable: true})
function setup() {
createCanvas(windowWidth, windowHeight);
background(179, 214, 110,150)
trees.push(new Branch(width/3, height - 50))
}
function draw() {
for (var i=0; i<trees.length; trees[i++].draw());
}
function mouseClicked() {
trees.push(new Branch(mouseX, mouseY, p5.Vector.fromAngle(PI/2 + random(-1,1) * 0.25), random(0.5) + 0.6))
}
function Branch(x, y, v, scale=1) {
this.x = x
this.y = y
this.v = v || p5.Vector.fromAngle(PI/2+random(-Branch.VAR_ANGLE, Branch.VAR_ANGLE))
this.v.setMag(scale * Branch.HEIGHT)
this.scale = scale
this.cLeft = null
this.cRight = null
this.size = 0
this.growing = true
}
2019年04月07日 06点04分 3
level 2
mmm米🍒 楼主
[汗]为什么老是吞我的楼!度娘!
Branch.prototype.draw = function() {
this.grow()
if (this.growing) { // adult trees don't change
if (this.scale > 0.03) {
//strokeCap(SQUARE)
stroke(Branch.C_BRANCH)
strokeWeight(Branch.B_WIDTH * this.scale)
} else {
stroke(Branch.C_LEAF)
strokeWeight(Branch.L_WIDTH * this.scale)
}
line(this.x, this.y, this.x - (this.size * this.v.x), this.y - (this.size * this.v.y))//this.x - (this.size * this.v.x), this.y - (this.size * this.v.y))
} else {
if (this.cLeft)
this.cLeft.draw()
if (this.cRight)
this.cRight.draw()
}
}
2019年04月07日 06点04分 8
level 2
mmm米🍒 楼主
[委屈]接上面的最后一段啦,好复杂啊,自己琢磨了好久也没弄明白
Branch.prototype.grow = function() {
if (this.size < this.scale)
this.size += Branch.SPEED
else {
this.growing = false
if (this.scale > 0.0125) {
const sX = this.x - (this.size * this.v.x)
const sY = this.y - (this.size * this.v.y)
if (!this.cLeft)
this.cLeft = new Branch(sX, sY, p5.Vector.fromAngle(this.v.heading() - 0.5 + random(-Branch.VAR_ANGLE, Branch.VAR_ANGLE)), this.scale * (0.60 + random(Branch.VAR_SCALE)))
if (!this.cRight)
this.cRight = new Branch(sX, sY, p5.Vector.fromAngle(this.v.heading() + 0.5 + random(-Branch.VAR_ANGLE, Branch.VAR_ANGLE)), this.scale * (0.60 + random(Branch.VAR_SCALE)))
}
}
}
2019年04月07日 06点04分 9
1