toolness / p5.js-widget

A reusable widget for embedding editable p5 sketches in web pages.
https://toolness.github.io/p5.js-widget/
GNU Lesser General Public License v2.1
161 stars 44 forks source link

A way of having the canvas fill the entire widget? #60

Open JobLeonard opened 8 years ago

JobLeonard commented 8 years ago

I was wondering if there was a way to make the widget work similar to the visualisations in this article by Mike Bostock about visualising algorithms:

https://bost.ocks.org/mike/algorithms/

... I'm sure I can do what with vanilla p5.js if I put in some effort (instance mode with a bit of extra work would probably do it), but wouldn't it be amazing if we could just write something like the following inline into an HTML page?

<script type="text/p5" canvas-only>
function setup() {
  //createCanvas(widgetWidth, widgetHeight); //implicit?
  background(255, 0, 200);
}

function draw() {
// something spectacular
}
</script>

or alternatively:

<script type="text/p5" canvas-only href='/js/widgetSketch.js' />

The canvas-only, widgetWidth and widgetHeight names are just placeholder ideas of course. I suppose the tricky parts would be calculating the size of these variables. Also, ideally there would be an implicit resizing event for the canvas if the window size changes, similar to windowResized for plain p5.js.

(if there already is a simple way to do this with p5 that I'm not aware of, please tell me!)

weegreenblobbie commented 7 years ago

+1

KevinWorkman commented 4 years ago

Does windowWidth and windowHeight work for you?

This works for me:

<script type="text/p5" data-p5-version="1.0.0" data-preview-width="400" data-height="480">
function setup() {
  createCanvas(windowWidth, windowHeight);
}
  
function draw() {
  background(220);
  circle(200, 200, 300);
}
</script>

Note that you need to add 80 to the data-height to get the desired windowHeight in the preview. So the above example uses data-height="480" to create a canvas that's 400 pixels wide.