p5js/16.平移.html
2023-01-06 11:00:34 +08:00

83 lines
2.8 KiB
HTML

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="/css/style.css" />
<title>平移</title>
<script src="/lib/p5/p5.min.js"></script>
<script src="/lib/p5/addons/p5.sound.min.js"></script>
<script src="/lib/utils.js"></script>
<script>
let x,
y,
z = 50;
function setup() {
createCanvas(800, 800);
colorMode(HSB);
x = createSlider(-width / z, width / z, 0, 1);
x.position(10, 10);
y = createSlider(-height / z, height / z, 0, 1);
y.position(180, 10);
}
function draw() {
background(50);
drawGrid(255, 32, 50);
noStroke();
fill(0, 0, 0, 1);
text(
`${x.value() < 0 ? "左" : "右"}平移 ${abs(x.value())}`,
10,
10
);
text(
`${y.value() < 0 ? "上" : "下"}平移 ${abs(y.value())}`,
180,
10
);
push();
translate(width / 2, height / 2);
stroke(128, 50, 50, 1);
noFill();
beginShape();
vertex(0 * z, 0 * z);
vertex(0 * z, -3 * z);
vertex(1 * z, -2 * z);
vertex(1 * z, 0 * z);
endShape(CLOSE);
beginShape();
vertex(-2 * z, 0 * z);
vertex(3 * z, 0 * z);
vertex(2 * z, 1 * z);
vertex(-1 * z, 1 * z);
endShape(CLOSE);
stroke(128, 100, 100, 1);
fill(128, 100, 100, 0.5);
beginShape();
vertex((0 + x.value()) * z, (0 + y.value()) * z);
vertex((0 + x.value()) * z, (-3 + y.value()) * z);
vertex((1 + x.value()) * z, (-2 + y.value()) * z);
vertex((1 + x.value()) * z, (0 + y.value()) * z);
endShape(CLOSE);
beginShape();
vertex((-2 + x.value()) * z, (0 + y.value()) * z);
vertex((3 + x.value()) * z, (0 + y.value()) * z);
vertex((2 + x.value()) * z, (1 + y.value()) * z);
vertex((-1 + x.value()) * z, (1 + y.value()) * z);
endShape(CLOSE);
pop();
textInfo("平移", "2023/01/06");
}
</script>
</head>
<body>
<main></main>
</body>
</html>