javascript - 在 Java Script 中将图像添加到按钮中
问题描述
我需要使图像可点击(按钮)或将图像添加到按钮。
这是我要修改的代码
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 库。
先感谢您。
解决方案
您可以通过设置 css 样式属性style()
。例如设置“背景”属性:
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>
推荐阅读
- python - 如何在 Python 中由 3D 点云组成的形状的顶面上存储点?
- python - 如何缩放图像以使其具有各向同性?
- python - 使用python从Sharepoint文件夹中检索.xlsx文件列表
- sql - 根据那里的顺序标记行(连续值)
- python - Pandas DataFrame:用数字计算单元格的数量
- macos - 获取 WebStorm 上的所有代码完成选项
- python - 标签编码器 - 使用 Inverse_transform 函数
- r - 订购 ggplot 轴和颜色
- react-native - 有没有办法在反应原生路由器通量中隐藏父标签栏?
- terraform - 使用 SQL FO 组的专用链接并使用 Terraform 配置基础架构