如何解决在Java脚本中将图像添加到按钮中
我需要使图像可点击(按钮)或将图像添加到按钮。
这是我要修改的代码
class Button{
constructor(){
this.button1 = createButton("Test");
}
display(){
this.button1.position(displayWidth - 1150,displayHeight/2 - 300);
this.button1.size(125.6,183.3);
}
this.button1.mousePressed(()=>{
console.log("Worked!!");
});
我正在使用Visual Studio代码制作代码,并且具有p5 play和p5.js库。
谢谢。
解决方法
您可以通过style()
设置css样式属性。例如,设置“ background”属性:
button.style('background',"url(https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/apple64.png)");
let button;
function setup() {
createCanvas(100,100);
background(0);
button = createButton("Test");
button.style('color',"white");
button.style('background',"url(https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/apple64.png)");
button.style('width',"64px");
button.style('height',"64px");
button.position(19,19);
button.mousePressed(buttonPressed);
}
function buttonPressed() {
button.style('background',"url(https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/banana64.png)");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。