Skip to content

Commit a6e42f8

Browse files
committed
Adds third argument to position method
1 parent 1957f26 commit a6e42f8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/dom/dom.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,15 +1694,27 @@ p5.Element.prototype.html = function() {
16941694
*/
16951695
/**
16961696
* @method position
1697-
* @param {Number} [x] x-position relative to upper left of window
1698-
* @param {Number} [y] y-position relative to upper left of window
1697+
* @param {Number} [x] x-position relative to upper left of window (optional)
1698+
* @param {Number} [y] y-position relative to upper left of window (optional)
1699+
* @param {String} positionType it can be static, fixed, relative, sticky, initial or inherit (optional)
16991700
* @chainable
17001701
*/
17011702
p5.Element.prototype.position = function() {
17021703
if (arguments.length === 0) {
17031704
return { x: this.elt.offsetLeft, y: this.elt.offsetTop };
17041705
} else {
1705-
this.elt.style.position = 'absolute';
1706+
let positionType = '';
1707+
if (
1708+
arguments[2] === 'static' ||
1709+
arguments[2] === 'fixed' ||
1710+
arguments[2] === 'relative' ||
1711+
arguments[2] === 'sticky' ||
1712+
arguments[2] === 'initial' ||
1713+
arguments[2] === 'inherit'
1714+
) {
1715+
positionType = arguments[2];
1716+
}
1717+
this.elt.style.position = positionType || 'absolute';
17061718
this.elt.style.left = arguments[0] + 'px';
17071719
this.elt.style.top = arguments[1] + 'px';
17081720
this.x = arguments[0];

test/unit/dom/dom.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,16 @@ suite('DOM', function() {
222222
};
223223
});
224224
});
225+
226+
suite('p5.Element.prototype.position', function() {
227+
let paragraph = myp5.createP('out of box');
228+
paragraph.position(20, 20, 'static');
229+
230+
elt = document.createElement('p');
231+
elt.appendChild(document.createTextNode('out of box'));
232+
elt.style.position = 'static';
233+
elt.style.left = '20px';
234+
elt.style.top = '20px';
235+
expect(JSON.stringify(paragraph.elt)).to.eql(JSON.stringify(elt));
236+
});
225237
});

0 commit comments

Comments
 (0)