We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello. I've got two objects here : Red prototype "square" and its child green "zz". What I want is to click on the object and focus the screen on it. Make it stuck to it even when it moves. I can manage the clicking function but need a hint for the centering concept here.
var angle;
var Polygon= function(x, y, hi, wei) {
this.x = x;
this.y = y;
this.height = hi;
this.width = wei;
}
Polygon.prototype.display = function() {
push();
translate(this.x, this.y);
rect(0, 0,this.height, this.width);
pop();
}
Polygon.prototype.area = function () {
return this.calcArea();
}
Polygon.prototype.calcArea = function () {
return this.height * this.width;
}
function Protogon(x, y, hi, wei, word) {
Polygon.call(this, x, y, hi, wei);
this.word = word;
};
Protogon.prototype= Object.create(Polygon.prototype);
Protogon.prototype.constructor = Protogon;
Protogon.prototype.display = function() {
push();
translate(this.x, this.y);
rotate(angle);
rect(50, 0,this.height, this.width);
text(this.word, 50, -10);
pop();
}
function setup() {
createCanvas(720, 400);
angle = 1.0;
square = new Polygon(width/2, height/2, 20, 40);
zz = new Protogon(square.x+20, square.y+20, 60, 40, "Hello");
}
function draw() {
background(50, 89, 100);
stroke(1);
fill('red');
square.display();
fill('green');
zz.display();
angle+=0.01 ;
}
Answers
Have you looked up rectMode() & textAlign() already: :-/
Oh, you've swapped the width & height parameters for rect() everywhere: L-)
rect(0, 0, this.height, this.width);
http://p5js.org/reference/#/p5/rect
Another tip: If you wanna do OOP in JS, go w/ classes instead: *-:)
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class
Especially for inheritance/subclassing, keyword
extends
gets rid of the painful boilerplating: :-bdhttps://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends
It's a great feature, thanx for telling about it. But I still confused how to make the screen follow the rotating object (its center). These two objects must keep the same distance between them. Like keeping object1.Xpos = width/2; object1.Ypos = height/2 rectMode(CENTER) just makes the shape center be at its x and y coordinates. could u pls give an example of how it will work with it? And thank you for for the other tips ! All of them are really helpful
I'm not the right person for translate() or rotate() things, sorry.
Maybe some1 else more knowledgeable shows up. :-\"