p5js/lib/utils.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-12-13 13:26:05 +00:00
function loadJavaScript(path) {
const script = document.createElement("script");
script.src = path;
document.head.appendChild(script);
}
2022-12-13 17:09:46 +00:00
function playdemos(list, delay = 10) {
2022-12-17 15:59:32 +00:00
let fps = frameRate();
fps = 60;
let n = floor((frameCount / (delay * fps)) % list.length);
let func = list[n];
func();
2022-12-13 17:09:46 +00:00
}
2022-12-17 15:59:32 +00:00
function drawGrid(color = 255, fade = 32) {
2022-12-13 13:26:05 +00:00
push();
colorMode(RGB);
2022-12-17 11:31:24 +00:00
stroke(color, fade);
2022-12-13 13:26:05 +00:00
strokeWeight(1);
for (let x = 0; x < width; x += 10) line(x, 0, x, height);
for (let y = 0; y < height; y += 10) line(0, y, width, y);
pop();
}
2022-12-17 11:31:24 +00:00
function textInfo(title, date, name = "赵海洋爸爸", color = 255) {
2022-12-13 13:26:05 +00:00
push();
colorMode(RGB);
2022-12-17 11:31:24 +00:00
fill(color);
2022-12-13 13:26:05 +00:00
noStroke();
text(`作品:${title}`, 30, height - 70);
text(`作者:${name}`, 30, height - 50);
text(`日期:${date}`, 30, height - 30);
pop();
}
function drawVector(vector, color = "#FFFFFF") {
push();
fill(color);
stroke(color);
line(0, 0, vector.x, vector.y);
circle(0, 0, 4);
translate(vector.x, vector.y);
rotate(vector.heading());
triangle(0, 0, -4, -2, -4, +2);
pop();
}
function vectorProjection(v1, v2) {
const v2n = v2.copy().normalize();
const sp = v1.dot(v2n);
return v2n.setMag(sp);
}
function star(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}