keyIsPressed 在点击鼠标的画布上书写 p5js
我正在开发一个功能,该功能使用户可以在单击鼠标的情况下在屏幕上书写,但是存在一些问题。
首先: keyIsPressed 被执行多次,使得每个键单击一次就会出现多次。
其次:在将 mouseX 和 mouseY 设置回 -1 之前,它只允许打印单个字母。
这是我的代码:
function setup() {
createCanvas(400, 400);
var startMouseX = -1;
var startMouseY = -1;
var drawing = false;
}
function draw() {
background(220);
if(mouseIsPressed)
{
startMouseX = mouseX;
startMouseY = mouseY;
drawing = true;
}
else if(keyIsPressed)
{
textSize(20);
text(key,startMouseX,startMouseY);
startMouseX += textWidth(key);
}
else{
drawing = false;
startMouseX = -1;
startMouseY = -1;
}
}
任何帮助将不胜感激,谢谢
I am working on a function which enables users to write onto screen where mouse has been clicked however there are a few problems.
Firstly: keyIsPressed is executed multiple times making each key appear more than once with a single click.
Secondly: It will only allow for a single letter to be printed before the mouseX and mouseY are set back to -1.
Here is my code:
function setup() {
createCanvas(400, 400);
var startMouseX = -1;
var startMouseY = -1;
var drawing = false;
}
function draw() {
background(220);
if(mouseIsPressed)
{
startMouseX = mouseX;
startMouseY = mouseY;
drawing = true;
}
else if(keyIsPressed)
{
textSize(20);
text(key,startMouseX,startMouseY);
startMouseX += textWidth(key);
}
else{
drawing = false;
startMouseX = -1;
startMouseY = -1;
}
}
Any help would be appreciated thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为使用“绘图”变量的方法是有效的。
但是您可以使用 keyTyped() 函数,而不是在绘图中绘制新字母。以同样的方式,您可以使用 mouseMoved() 重置“drawing”变量
I think the approach with the 'drawing' variable works.
But instead of drawing the new letter in draw you can use the keyTyped() function. In the same manner you could use mouseMoved() to reset the 'drawing' variable
我的解决方案:
这使得您输入的内容永久显示在画布上。这是你想要的吗?如果没有,事情就会困难得多。
my solution:
This makes it so the things you type are permanently on the canvas. Is this what you wanted? if not, it would be a lot harder.