so i am trying to replicate output of this code:
function setup() {
createCanvas(400, 400);
}
var pts = [];
var bts = [];
function mousePressed()
{
if (pts.length == 2) {
pts = [];
}
pts.push([mouseX, mouseY])
loop()
}
function draw() {
background(220);
if (pts.length == 2) {
// rectangle points
let rpts = [ [pts[1][0], pts[0][1]], [pts[0][0], pts[1][1]]]
// draw rectangle
for (var i=0; i < rpts.length; ++i) {
line(rpts[i][0], rpts[i][1], rpts[(i+1) % rpts.length][0], rpts[(i+1) % rpts.length][1]);
}
text(rpts[1],255,255);
text(rpts[1][0],255,355);
}
else if (pts.length > 0) {
// draw a rubber line from last point to the mouse
line(pts[pts.length-1][0], pts[pts.length-1][1], mouseX,mouseY);
}
let c = color(255, 204, 0);
fill(c);
if (pts.length==2) {
let x0 = min(pts[0][0], pts[1][0]);
let x1 = max(pts[0][0], pts[1][0]);
let y0 = min(pts[0][1], pts[1][1]);
let y1 = max(pts[0][1], pts[1][1])
for (var x = x0; x < x1; x += 5) {
for (var y = y0; y < y1; y +=5) {
square(x, y, 4);
}
}
noLoop()
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
because i want to add a functionality of mouseover on every element, i am thinking to create objects of same element(square). Now the problem is that, i want to create a specific number of Objects and i cannot compute that number until i have user input and setup runs in the start(once). and once that is done, i am not sure i can do the math of
for (var x = x0; x < x1; x += 5) {
for (var y = y0; y < y1; y +=5) {
square(x, y, 4);
while creating objects and passing values in constructor.
I intent to change color of each square when mouse is over them (permanently for comparison with the squares whom have not had the mouse over them).
So far i have gotten here.
var pts = [];
var bts = [];
let squarex = [];
function setup() {
createCanvas(400, 400);
for (let s = 0;s<=5 ;s++) // i want, s<=x1*y1 (they are defined below).
{
squarex[s] = new square1();
}
}
function mousePressed()
{
if (pts.length == 2) {
pts = [];
}
pts.push([mouseX, mouseY])
}
function draw() {
background(220);
if (pts.length == 2) {
// rectangle points
let rpts = [ [pts[1][0], pts[0][1]], [pts[0][0], pts[1][1]]]
// draw rectangle
for (var i=0; i < rpts.length; ++i) {
line(rpts[i][0], rpts[i][1], rpts[(i+1) % rpts.length][0], rpts[(i+1) % rpts.length][1]);
}
}
else if (pts.length > 0) {
// draw a rubber line from last point to the mouse
line(pts[pts.length-1][0], pts[pts.length-1][1], mouseX,mouseY);
}
if(pts.length==2)
{
for (let s=0;s<=squarex.length;s++)
{
squarex[s].create();
}
noLoop();
}
}
class square1 {
create()
{
let y = random(height);
let x = random(width);
square(x, y, 10);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>