我有一个取决于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增加和减少。
有没有更简单的方法来做这一切?