简体   繁体   English

如何使用父divs属性设置画布的宽度/高度

[英]How to set canvas width / height using parent divs attributes

I have been trying to set a p5.js canvas inside a div, and use the divs width to control the canvas width, within a jekyll post. 我一直在尝试在div内设置p5.js画布,并在jekyll帖子中使用divs宽度控制画布的宽度。 In the markdown file I am constructing a div that I know sits at the top of the post. 在markdown文件中,我正在构造一个div,该div位于帖子的顶部。

<div id="myCanvas"</div>
<script src="/js/p5Sketches/firstP5Sketch.js" type="text/javascript"></script>

In the javascript I have the p5.js canvas assigned to the div. 在javascript中,我将p5.js画布分配给了div。 I have tried to get the parent's div clientWidth but I am getting some really strange outputs. 我已经尝试获取父级的div clientWidth,但是却得到了一些非常奇怪的输出。

var canvasDiv = document.getElementById('myCanvas');
var width = canvasDiv.clientWidth;

function setup() {
  var sketchCanvas = createCanvas(width,450);
  sketchCanvas.parent("myCanvas");
}

function draw() {
  if (mouseIsPressed){
    fill(0);
  } else {
    fill(255);
  }
  ellipse(mouseX, mouseY, 40, 40);

  if (keyIsPressed == true){
    clear();
  }
}

function windowResized() {
  resizeCanvas(width,450);
}

When I print out the canvasDiv attributes I get the correct clientWidth value (844px), but then print out the width variable and it is 1244px. 当我打印出canvasDiv属性时,我得到正确的clientWidth值(844px),但是随后打印出width变量,它是1244px。 In the chrome inspector the p5 canvas ends up being width=100px. 在Chrome检查器中,p5画布的宽度最终为width = 100px。

<canvas id="" width="200" height="900" style="width: 100px !important; height: 450px !important;" class=""></canvas>

The value is not passed on to the p5 canvas, and I can't work out why. 该值未传递到p5画布上,我不知道为什么。 As an aside I am not using any css to style the myCanvas or canvas elements. 顺便说一句,我没有使用任何CSS来设置myCanvas或canvas元素的样式。

Thanks in advance. 提前致谢。

You need to capture the parent elements information within setup() 您需要在setup()捕获父元素信息

The default width and height for setup() is 100x100. setup()的默认宽度和高度为100x100。 And if width isn't defined inside setup(), it'll be overwritten with the default value. 如果未在setup()中定义width,则它将被默认值覆盖。 From the documentation: 从文档中:

The system variables width and height are set by the parameters passed to this function. 系统变量width和height由传递给此函数的参数设置。 If createCanvas() is not used, the window will be given a default size of 100x100 pixels. 如果未使用createCanvas(),则窗口的默认大小为100x100像素。

https://p5js.org/reference/#/p5/createCanvas https://p5js.org/reference/#/p5/createCanvas

Which I tested and proved in this image: 我在这张图中测试并证明了这一点:

宽度和高度已经由setup()预定义

The following is the correct code you should replace your setup() function with: 以下是正确的代码,应使用以下代码替换setup()函数:

function setup() {
    var canvasDiv = document.getElementById('myCanvas');
    var width = canvasDiv.offsetWidth;
    var sketchCanvas = createCanvas(width,450);
    console.log(sketchCanvas);
    sketchCanvas.parent("myCanvas");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM