マウスを左右に動かして、動く図形の速度と方向を制御します。
distance abs
Math Interaction
View Source Code
/*
* @name Distance 1D
* @arialabel One thin grey bar and wider grey bar travel on the top half of the screen, and another set of these two bars travel on the bottom half. The bars change speed and direction as the user’s mouse moves across the screen
* @description Move the mouse left and right to control
* the speed and direction of the moving shapes.
*/
let xpos1;
let xpos2;
let xpos3;
let xpos4;
let thin = 8;
let thick = 36;
function setup() {
// createCanvas(710, 400);
createCanvas(windowWidth, windowHeight);
noStroke();
xpos1 = width / 2;
xpos2 = width / 2;
xpos3 = width / 2;
xpos4 = width / 2;
}
function draw() {
background(0);
let mx = mouseX * 0.4 - width / 5.0;
fill(102);
rect(xpos2, 0, thick, height / 2);
fill(204);
rect(xpos1, 0, thin, height / 2);
fill(102);
rect(xpos4, height / 2, thick, height / 2);
fill(204);
rect(xpos3, height / 2, thin, height / 2);
xpos1 += mx / 16;
xpos2 += mx / 64;
xpos3 -= mx / 16;
xpos4 -= mx / 64;
if (xpos1 < -thin) {
xpos1 = width;
}
if (xpos1 > width) {
xpos1 = -thin;
}
if (xpos2 < -thick) {
xpos2 = width;
}
if (xpos2 > width) {
xpos2 = -thick;
}
if (xpos3 < -thin) {
xpos3 = width;
}
if (xpos3 > width) {
xpos3 = -thin;
}
if (xpos4 < -thick) {
xpos4 = width;
}
if (xpos4 > width) {
xpos4 = -thick;
}
}
License
Source code is available on GitHub p5.js website legacy.
All examples are licensed under CC BY-NC-SA 4.0.