首页 > 解决方案 > 如何使用 P5js 中触摸对象的 X 和 Y 值?

问题描述

这是我玩 P5js 和 Processing 的第一周,所以要温柔。

我有以下 P5js 代码,我正在查看触摸值。通过在触摸屏上按下多点,可以将更多的圆圈固定在中心位置。

我想知道的是如何调用控制台中打印的对象 1 的 x 值?还是对象 0 的 y 值?我将如何在代码中声明它们?

我想做的是使用这些值来更改屏幕中间形状的尺寸,例如对象 0 中的某些东西驱动形状的高度或对象 1 上的 x 或 y 值驱动 strokeWeight 或颜色. 但是,我在第一周,完全迷失了如何在代码中使用这些值。

function setup() {
  createCanvas(windowWidth, windowHeight);
  background(200);
}

function draw() {
  background(255);

  fill(255, 0, 0);
  strokeWeight(0)

  for (var i = 0; i < touches.length; i++) {
    fill(255);
    strokeWeight(3)
    ellipse(touches[i].x, touches[i].y, 50, 50);
    line(touches[i].x, touches[i].y, windowWidth / 2, windowHeight / 2);



    fill(255);
    ellipse(windowWidth / 2, windowHeight / 2, 10, 10);
    print(touches);
  }
}

// do this prevent default touch interaction
function mousePressed() {
  return false;
}

document.addEventListener('gesturestart', function(e) {
  e.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.min.js"></script>

或草图文件在这里

任何帮助将不胜感激,我是一个试图找到自己的方式的新手

标签: p5.js

解决方案


到现在发现只有一个错误,背景颜色和填充颜色是一样的!您可以更改它们以实际查看形状、线条等。


推荐阅读