我正在为大学完成 P5 作业,并且努力理解我在网上找到的一些代码。我已经包含了代码输出的屏幕截图和下面的实际代码。我了解创建基本网格的逻辑,但我想知道是否有人可以向我解释它究竟是如何改变正方形的大小的?
这是一个代码笔链接,可以查看它的工作原理: https ://codepen.io/therealpamelahalpert/pen/rNaYrbP
'use strict';
var stepX = 60;
var stepY = 60;
var maxDistance = 600;
function setup() {
createCanvas(1240, 1748);
noStroke();
colorMode(HSB, 360, 100, 100);
}
function draw() {
clear();
for (var gridY = 0; gridY < height; gridY += stepY) {
for (var gridX = 0; gridX < width; gridX += stepX) {
//Creating the variable distanceBetweenTwoPoints which is getting the distance between 2 points on the canvas
distanceBetweenPoints = dist(mouseX, mouseY, gridX, gridY);
var diameter = distanceBetweenPoints / maxDistance * 60;
strokeWeight(4);
stroke(0,0,0);
rect(gridX, gridY, diameter, diameter);
}
}
}