Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

div get dimensions #391

Closed
shiffman opened this issue Oct 11, 2014 · 10 comments
Closed

div get dimensions #391

shiffman opened this issue Oct 11, 2014 · 10 comments

Comments

@shiffman
Copy link
Member

A small thing, but also re: p5.dom a lot of my examples require dynamically accessing the size of a DIV (which changes according to the amount of text). I may be mistaken but I believe p5 only allows for setting the dimensions with size() but for retrieving the width/height I am doing the following:

var w = div.elt.offsetWidth;
var h = div.elt.offsetHeight;

Would it make sense to have a width()/height() method that returns the dimensions?

@brysonian
Copy link
Contributor

Would it make sense to integrate something like bonzo? https://github.com/ded/bonzo


Chandler McWilliams
http://brysonian.com

On Sat, Oct 11, 2014 at 10:03 AM, Daniel Shiffman
notifications@github.com wrote:

A small thing, but also re: p5.dom a lot of my examples require dynamically accessing the size of a DIV (which changes according to the amount of text). I may be mistaken but I believe p5 only allows for setting the dimensions with size() but for retrieving the width/height I am doing the following:

var w = div.elt.offsetWidth;
var h = div.elt.offsetHeight;

Would it make sense to have a width()/height() method that returns the dimensions?

Reply to this email directly or view it on GitHub:
https://github.com/lmccart/p5.js/issues/391

@lmccart
Copy link
Member

lmccart commented Oct 13, 2014

@shiffman this would be great. I've been making most of the things in this library work as both setters and getters, so maybe these methods could set the w and h or return vals if arguments are null.

@brysonian interesting suggestion! we might have already implemented most of this, and in some ways slightly different than this lib, but let me take a closer look.

@brysonian
Copy link
Contributor

@lmccart yeah to be more clear i guess i'm just wondering how much wheel-reinventing is necessary, but if the work is already done then that's great!

@futuremarc
Copy link
Contributor

.size() now returns a string with the current width & height of the element if no arguments are provided.

However, p5 Elements already have the variables 'width' and 'height' that store the element's initial offsetWidth/offsetHeight. Should we use a naming convention like eltWidth and eltHeight?

@shiffman
Copy link
Member Author

I ran some quick tests (with the following code).

var txt;

function setup() {
  noCanvas();
  txt = createDiv("Hello!");
  txt.style("background-color","#00FF00");
  txt.position(100,100);
  console.log(txt.width);
  txt.html("Hello again!")
  console.log(txt.width); 
}

function mousePressed() {
  console.log(txt.width);
  console.log(txt.size());
}

The current behavior I believe is:

  • size() returns the p5.Element instance. I like the idea of it returning dimensions, but a string is not particularly useful as it will almost always need to be parsed back into numbers. What if size() returns an object with a width and height, i.e.
{
  "width": elt.offsetWidth,
  "height: elt.offsetHeight
}
  • width and height both return the original width and height of the element no matter how much it changes. This would be a perfect solution as far as I'm concerned but is that original width/height being maintained for a reason I'm not aware of? An alternative would be to add width() and height() functions but that seems like overkill given we have size() already.

@futuremarc
Copy link
Contributor

I think an object returned from .size() is enough.

In this spirit of doubling as a getter/setter, what if with null arguments things like .style() return an object of the elt's styles.

This could also help when considering how to more easily deal with input events. We can make the setter/getter .events() which would either return an object of events or set an event handler.

Something like:

var btn = createButton();
var div = createDiv('im a div');
btn.events().onmousedown = div.html('listening to btn');

@lmccart
Copy link
Member

lmccart commented Jun 13, 2015

I think this syntax is a bit hard to follow. We have a mousedown event handler actually, so I wonder how it fits with this? Or are you suggesting replacing it?

btn.mousePressed( function(e) { // do stuff } )

If we wanted to add a generic event() method, I think a format like event(eventType, func) would follow the form set by style().

btn.event('mousedown', function(e) { // do stuff } );

In any case, the right side of the code above would need to be wrapped inside of a function, so it's not fired immediately, something like:

 =  function() { div.html('listening to btn'); };

@lmccart
Copy link
Member

lmccart commented Jun 13, 2015

re: width and height, this sounds like a bug. they should be updated when the size changes, though there is still room for these to be wrong. for example, if you created an element and set the width and height manually with css rather than using the style() function, so having size() return the width and height makes sense.

@futuremarc
Copy link
Contributor

  • width and height attributes now return the correct value and they update on change. (though only act as getters)
  • size() returns an object of width and height if empty.
  • size() bug fixed: actually sets the size of an element now (the css markup !important was breaking it).
  • position() returns an object of x and y if empty.
  • x and y are now attributes on the element and can be accessed via elt.x or elt.y (to stay consistent. re: Properties of a dom p5.element, position? #579 )

@lmccart I guess I'm trying to find a place for things that people might want when working with form elements. What if we had an onChange() to go with the listener functions?

@futuremarc
Copy link
Contributor

closed with 9015ffe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants