首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在p5.js框架中的8x8网格单元中居中对象

如何在p5.js框架中的8x8网格单元中居中对象
EN

Stack Overflow用户
提问于 2019-10-12 10:54:40
回答 1查看 435关注 0票数 1

这是我下象棋的密码。

代码语言:javascript
复制
let pawn1; //variable for object
var offset = 10;
function setup(){
	createCanvas(600,600); // canvas
	pawn1 = new Pawn(); // pawn1 is a new Pawn object
}

function draw(){
	background(0); //black background
	checkboard();  //to make an 8x8 grid	
	pawn1.show();  //shows pawn1 object on canvas
	drag();        //allows the pawn object to be dragged
}

function Pawn(){
	this.x = 25; // x position of object
	this.y = 25; // y position of object
	this.w = 20; // width of object
	this.h = 20; // height of object

	this.show = function(){
		fill(255); // object is white
		rect(this.x, this.y, this.w, this.h); //object is a rectangle with x,y,w,h variables
	}
}

// grid maker function
function checkboard(){
	for (var x = 0; x < width; x += width / 8) {
		for (var y = 0; y < height; y += height / 8) {
			stroke(255); //grid lines is white
			strokeWeight(1); // lines drawn are 1 units thick
			line(x, 0, x, height); // vertical lines
			line(0, y, width, y); // horizontal lines
		}
	}
}

function drag(){
    // if mouse is clicked down and inside of the dimensions of the object then:
	if(mouseIsPressed){
		if(mouseX > pawn1.x && mouseY > pawn1.y){
			if(mouseX<(pawn1.x+pawn1.w) && mouseY<(pawn1.y+pawn1.h)){
                //square will move with the mouse pointer 
				pawn1.x = mouseX - offset;
				pawn1.y = mouseY - offset;
			}
		}
	}
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>

我不知道如何确保拖放对象何时被删除,它将在其已被删除的单元格中居中。--我想代码将作为code语句放在“拖动函数”中,但除此之外,我被卡住了。有人能帮忙吗?

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-12 11:39:33

计算单元格的宽度和高度。例如:

代码语言:javascript
复制
var cell_w = width / 8;
var cell_h = height / 8;  

单元格的宽度和高度是固定的,那么您也可以使用常量值。例如:

代码语言:javascript
复制
var cell_w = 75;
var cell_h = 75;  

计算当前单元格的索引,将当前鼠标位置除以单元格大小,并通过int()截断结果。例如:

代码语言:javascript
复制
var cell_ix = int(mouseX / cell_w);
var cell_iy = int(mouseY / cell_h);

计算鼠标所在的单元格的中心点:

代码语言:javascript
复制
var cell_cx = (cell_ix+0.5) * cell_w;
var cell_cy = (cell_iy+0.5) * cell_h;

根据单元格的中心和对象的大小计算对象的新位置:

代码语言:javascript
复制
pawn1.x = cell_cx - pawn1.w/2;
pawn1.y = cell_cy - pawn1.h/2;

您可以在mouseReleased()回拨中这样做。这会导致对象被平滑地拖动,但当鼠标释放时立即“跳转”到单元格的中心:

代码语言:javascript
复制
function mouseReleased() {
    if (mouseX > pawn1.x && mouseY > pawn1.y &&
        mouseX<(pawn1.x+pawn1.w) && mouseY<(pawn1.y+pawn1.h)) {

        var cell_w = width / 8;
        var cell_h = height / 8;

        var cell_ix = int(mouseX / cell_w);
        var cell_iy = int(mouseY / cell_h);

        var cell_cx = (cell_ix+0.5) * cell_w;
        var cell_cy = (cell_iy+0.5) * cell_h;

        pawn1.x = cell_cx - pawn1.w/2;
        pawn1.y = cell_cy - pawn1.h/2;
    }
}

参见示例,其中将函数添加到原始代码中:

代码语言:javascript
复制
let pawn1; //variable for object
var offset = 10;
function setup(){
    createCanvas(600,600); // canvas
    pawn1 = new Pawn(); // pawn1 is a new Pawn object
}

function draw(){
    background(0); //black background
    checkboard();  //to make an 8x8 grid    
    pawn1.show();  //shows pawn1 object on canvas
    drag();        //allows the pawn object to be dragged
}

function Pawn(){
    this.x = 25; // x position of object
    this.y = 25; // y position of object
    this.w = 20; // width of object
    this.h = 20; // height of object

    this.show = function(){
        fill(255); // object is white
        rect(this.x, this.y, this.w, this.h); //object is a rectangle with x,y,w,h variables
    }
}

// grid maker function
function checkboard(){
    for (var x = 0; x < width; x += width / 8) {
        for (var y = 0; y < height; y += height / 8) {
            stroke(255); //grid lines is white
            strokeWeight(1); // lines drawn are 1 units thick
            line(x, 0, x, height); // vertical lines
            line(0, y, width, y); // horizontal lines
        }
    }
}

function drag(){
    // if mouse is clicked down and inside of the dimensions of the object then:
    if(mouseIsPressed){
        if(mouseX > pawn1.x && mouseY > pawn1.y){
            if(mouseX<(pawn1.x+pawn1.w) && mouseY<(pawn1.y+pawn1.h)){
                //square will move with the mouse pointer 
                pawn1.x = mouseX - offset;
                pawn1.y = mouseY - offset;
            }
        }
    }
}

function mouseReleased() {
    if (mouseX > pawn1.x && mouseY > pawn1.y &&
        mouseX<(pawn1.x+pawn1.w) && mouseY<(pawn1.y+pawn1.h)) {

        var cell_w = width / 8;
        var cell_h = height / 8;
        
        var cell_ix = int(mouseX / cell_w);
        var cell_iy = int(mouseY / cell_h);

        var cell_cx = (cell_ix+0.5) * cell_w;
        var cell_cy = (cell_iy+0.5) * cell_h;

        pawn1.x = cell_cx - pawn1.w/2;
        pawn1.y = cell_cy - pawn1.h/2;
    }
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58353391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档