在P5.js中创建元素对象

时间:2019-11-25 15:27:55

标签: javascript processing p5.js

所以我试图复制此代码的输出:

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>

因为我想在每个元素上添加鼠标悬停的功能,所以我正在考虑创建相同元素(正方形)的对象。现在的问题是,我想创建特定数量的对象,并且在用户输入并在start(once)中运行安装程序之前无法计算该数量。一旦完成,我不确定我可以算出

for (var x = x0; x < x1; x += 5) {
    for (var y = y0; y < y1; y +=5) {
        square(x, y, 4);

在创建对象并在构造函数中传递值的同时。

我打算在鼠标悬停在每个正方形上时更改它们的颜色(永久性地与未将鼠标悬停在它们上面的正方形进行比较)。

到目前为止,我已经到了这里。

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>

1 个答案:

答案 0 :(得分:1)

一旦设置了2个点,然后创建一个包含所有小矩形位置的网格。添加第三个值,该值指示鼠标是否将鼠标悬停在矩形上:

var pts = [];
var grid = []
function mousePressed()
{
    if (pts.length == 2) {
        pts = [];
    }
    pts.push([mouseX, mouseY])

    if (pts.length==2) { 
        grid = []
        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 (let y = y0; y < y1; y +=5) {
            let row = []
            for (let x = x0; x < x1; x += 5) {    
                row.push( [x, y, false] );
            }
            grid.push(row);
        }
    }
}

如果鼠标悬停在矩形上,请更改状态:

function mouseMoved()
{
    if (pts.length==2) {
        let x0 = grid[0][0][0];
        let y0 = grid[0][0][1];
        let row = Math.trunc((mouseY-y0) / 5);
        let col = Math.trunc((mouseX-x0) / 5);
        if (row < grid.length && col < grid[row].length) {
            grid[row][col][2] = true;
        }
    } 
}

在网格中存储的位置绘制矩形,并根据“悬停”状态设置颜色:

let c1 = color(255, 204, 0);
let c2 = color(0, 0, 255);
if (pts.length==2) { 
    for (var row = 0; row < grid.length; ++row ) {
        for (var col = 0; col < grid[row].length; ++col ) {
            fill(grid[row][col][2] ? c2 : c1);
            square(grid[row][col][0], grid[row][col][1], 4);
        }
    }
}

请参见示例:

function setup() {
    createCanvas(400, 400);
}

var pts = [];
var grid = []
function mousePressed()
{
    if (pts.length == 2) {
        pts = [];
    }
    pts.push([mouseX, mouseY])

    if (pts.length==2) { 
        grid = []
        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 (let y = y0; y < y1; y +=5) {
            let row = []
            for (let x = x0; x < x1; x += 5) {    
                row.push( [x, y, false] );
            }
            grid.push(row);
        }
    }
}

function mouseMoved()
{
    if (pts.length==2) {
        let x0 = grid[0][0][0];
        let y0 = grid[0][0][1];
        let row = Math.trunc((mouseY-y0) / 5);
        let col = Math.trunc((mouseX-x0) / 5);
        if (row < grid.length && col < grid[row].length) {
            grid[row][col][2] = true;
        }
    } 
}

function draw() {  
    background(220);

    // setup r ectangle points
    let rpts;
    if (pts.length == 2) {
        rpts = [pts[0], [pts[1][0], pts[0][1]], pts[1], [pts[0][0], pts[1][1]]];
    }
    else if (pts.length > 0) {
        rpts = [pts[0], [mouseX, pts[0][1]], [mouseX, mouseY], [pts[0][0], mouseY]];
    }
    // draw rectangles
    if (rpts) {
        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]);
        }
    }
      
    let c1 = color(255, 204, 0);
    let c2 = color(0, 0, 255);
    if (pts.length==2) { 
        for (var row = 0; row < grid.length; ++row ) {
            for (var col = 0; col < grid[row].length; ++col ) {
                fill(grid[row][col][2] ? c2 : c1);
                square(grid[row][col][0], grid[row][col][1], 4);
            }
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>