
在本教程中,您将学习如何使用 p5.js 创建动态且丰富多彩的几何动画。形状将在画布上随机移动,改变颜色并创建视觉上迷人的效果。
下载p5.js:
创建一个新项目:
让我们首先设置 p5.js 草图的基本结构。这包括定义 setup() 和 draw() 函数。
function setup() {
createcanvas(windowwidth, windowheight);
nostroke(); // disable stroke for the shapes
}
function draw() {
background(30, 30, 60, 25); // dark background with transparency
}
说明:
现在,让我们添加代码来创建具有不同颜色、位置和大小的随机形状。
function draw() {
background(30, 30, 60, 25); // dark background with transparency
for (let i = 0; i < 5; i++) {
let x = random(width);
let y = random(height);
let size = random(20, 80);
let colorr = random(255);
let colorg = random(255);
let colorb = random(255);
let shapetype = random(['ellipse', 'rect', 'triangle']);
fill(colorr, colorg, colorb, 150); // set fill color with some transparency
if (shapetype === 'ellipse') {
ellipse(x, y, size, size);
} else if (shapetype === 'rect') {
rect(x, y, size, size);
} else if (shapetype === 'triangle') {
triangle(x, y, x + size, y, x + size / 2, y - size);
}
}
}
说明:
随机变量:
形状类型:
为了确保画布随窗口调整大小,请添加以下函数:
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
以上就是使用 ps 创建动态几何动画的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号