p5.js 如何动态调整图纸的高度和宽度?
How to dynamically adjust the height and width of drawings in p5.js ?
我有一个取决于窗口宽度的矩形。我希望矩形及其内部绘图根据 windowWidth 动态调整其大小。
这是我绘制的部分代码。
//Hardcoded date and time
var date = '15/05/17';
var time = '23:05';
var totaltime = '5h 45m';
var completeddate = '18/05/17';
var timeoflast = '4:50';
var canvas;
var canvasheight = 200;
var yellowrectanglewidth = 100;
var yellowrectangleheight = 59;
function setup() {
canvasheight = windowHeight / 3.7;
canvas = createCanvas(windowWidth,canvasheight);
//canvas.style('display', 'block');
canvas.parent('sketch-holder');
//Image
leftend = loadImage("leftend.png");
rightend = loadImage("rightend.png");
yellowrectanglewidth = windowWidth - 160;
}
function draw() {
//Main Rectangle
fill(67, 67, 73);
rect(0, 0, windowWidth, canvasheight);
//Firstyellowrect
fill(215, 198, 170);
rect(90, canvasheight /9, yellowrectanglewidth, yellowrectangleheight);
//Secondyellowrect
rect(90, canvasheight /2.2, yellowrectanglewidth, yellowrectangleheight);
//Left End
//Text date
textSize(15);
fill(215, 198, 170);
text(date, 10, canvasheight/6);
//Text Time
fill(215, 198, 170);
text(time, 20, canvasheight/3 - 10);
//Image
image(leftend, 20,canvasheight/3 , 70, 40);
//Text Time
textSize(25);
fill(215, 198, 170);
text(totaltime, 5,canvasheight / 3 + 70);
//Right End
textSize(15);
fill(215, 198, 170);
text(completeddate, windowWidth - 60, canvasheight/6);
fill(215, 198, 170);
text(timeoflast, windowWidth - 40, canvasheight/3 - 10);
//Image 2
image(rightend, windowWidth - 70,canvasheight/3, 70, 40);
}
正如您在这里看到的,我试图让我的大部分绘图都依赖于此处的 windowWidth。因为我希望我的绘图大小根据 WindowWidth 增加和减少。
有没有更简单的方法来完成这一切?
我有一个取决于窗口宽度的矩形。我希望矩形及其内部绘图根据 windowWidth 动态调整其大小。
这是我绘制的部分代码。
//Hardcoded date and time
var date = '15/05/17';
var time = '23:05';
var totaltime = '5h 45m';
var completeddate = '18/05/17';
var timeoflast = '4:50';
var canvas;
var canvasheight = 200;
var yellowrectanglewidth = 100;
var yellowrectangleheight = 59;
function setup() {
canvasheight = windowHeight / 3.7;
canvas = createCanvas(windowWidth,canvasheight);
//canvas.style('display', 'block');
canvas.parent('sketch-holder');
//Image
leftend = loadImage("leftend.png");
rightend = loadImage("rightend.png");
yellowrectanglewidth = windowWidth - 160;
}
function draw() {
//Main Rectangle
fill(67, 67, 73);
rect(0, 0, windowWidth, canvasheight);
//Firstyellowrect
fill(215, 198, 170);
rect(90, canvasheight /9, yellowrectanglewidth, yellowrectangleheight);
//Secondyellowrect
rect(90, canvasheight /2.2, yellowrectanglewidth, yellowrectangleheight);
//Left End
//Text date
textSize(15);
fill(215, 198, 170);
text(date, 10, canvasheight/6);
//Text Time
fill(215, 198, 170);
text(time, 20, canvasheight/3 - 10);
//Image
image(leftend, 20,canvasheight/3 , 70, 40);
//Text Time
textSize(25);
fill(215, 198, 170);
text(totaltime, 5,canvasheight / 3 + 70);
//Right End
textSize(15);
fill(215, 198, 170);
text(completeddate, windowWidth - 60, canvasheight/6);
fill(215, 198, 170);
text(timeoflast, windowWidth - 40, canvasheight/3 - 10);
//Image 2
image(rightend, windowWidth - 70,canvasheight/3, 70, 40);
}
正如您在这里看到的,我试图让我的大部分绘图都依赖于此处的 windowWidth。因为我希望我的绘图大小根据 WindowWidth 增加和减少。
有没有更简单的方法来完成这一切?