Skip to content

Commit 24f2901

Browse files
author
Lauren McCarthy
committed
Merge pull request #983 from futuremarc/style
more support for style
2 parents 928b6eb + 79dd089 commit 24f2901

File tree

1 file changed

+74
-14
lines changed

1 file changed

+74
-14
lines changed

lib/addons/p5.dom.js

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -895,15 +895,22 @@
895895
};
896896

897897
/**
898-
* Sets the given style (css) property of the element with the given value.
899-
* If no value is specified, returns the value of the given property,
900-
* or undefined if the property is not.
898+
* Sets the given style (css) property (1st arg) of the element with the
899+
* given value (2nd arg). If a single argument is given, .style()
900+
* returns the value of the given property; however, if the single argument
901+
* is given in css syntax ('text-align:center'), .style() sets the css
902+
* appropriatly. .style() also handles 2d and 3d css transforms. If
903+
* the 1st arg is 'rotate', 'translate', or 'position', the following arguments
904+
* accept Numbers as values. ('translate', 10, 100, 50);
901905
*
902906
* @method style
903907
* @param {String} property property to be set
904-
* @param {String} [value] value to assign to property
908+
* @param {String|Number} [value] value to assign to property
909+
* @param {String|Number} [value] value to assign to property (rotate/translate)
910+
* @param {String|Number} [value] value to assign to property (rotate/translate)
911+
* @param {String|Number} [value] value to assign to property (translate)
905912
* @return {String|Object/p5.Element} value of property, if no value is specified
906-
* or p5.Element
913+
* or p5.Element
907914
* @example
908915
* <div><code class="norender">
909916
* var myDiv = createDiv("I like pandas.");
@@ -912,19 +919,72 @@
912919
* </code></div>
913920
*/
914921
p5.Element.prototype.style = function(prop, val) {
922+
var self = this;
923+
915924
if (typeof val === 'undefined') {
916-
var attrs = prop.split(';');
917-
for (var i=0; i<attrs.length; i++) {
918-
var parts = attrs[i].split(':');
919-
if (parts[0] && parts[1]) {
920-
this.elt.style[parts[0].trim()] = parts[1].trim();
925+
if (prop.indexOf(':') === -1) {
926+
var styles = window.getComputedStyle(self.elt);
927+
var style = styles.getPropertyValue(prop);
928+
return style;
929+
} else {
930+
var attrs = prop.split(';');
931+
for (var i = 0; i < attrs.length; i++) {
932+
var parts = attrs[i].split(':');
933+
if (parts[0] && parts[1]) {
934+
this.elt.style[parts[0].trim()] = parts[1].trim();
935+
}
921936
}
922937
}
923938
} else {
924-
this.elt.style[prop] = val;
925-
if (prop === 'width' || prop === 'height' || prop === 'left' || prop === 'top'){
926-
var numVal = val.replace(/\D+/g,'');
927-
this[prop] = parseInt(numVal);
939+
if (prop === 'rotate'){
940+
if (arguments.length === 2) {
941+
var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, '');
942+
style = style.replace(/rotate[X-Z]?\(.*\)/g, '');
943+
this.elt.style.transform = 'rotate(' + arguments[0] + 'deg)';
944+
this.elt.style.transform += style;
945+
} else if (arguments.length === 3) {
946+
var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, '');
947+
style = style.replace(/rotate[X-Z]?\(.*\)/g, '');
948+
this.elt.style.transform = 'rotate(' + arguments[0] + 'deg, ' + arguments[1] + 'deg)';
949+
this.elt.style.transform += style;
950+
} else if (arguments.length === 4) {
951+
var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, '');
952+
style = style.replace(/rotate[X-Z]?\(.*\)/g, '');
953+
this.elt.style.transform = 'rotateX(' + arguments[0] + 'deg)';
954+
this.elt.style.transform += 'rotateY(' + arguments[1] + 'deg)';
955+
this.elt.style.transform += 'rotateZ(' + arguments[2] + 'deg)';
956+
this.elt.style.transform += style;
957+
}
958+
} else if (prop === 'translate') {
959+
if (arguments.length === 3) {
960+
var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, '');
961+
style = style.replace(/translate[X-Z]?\(.*\)/g, '');
962+
this.elt.style.transform = 'translate(' + arguments[0] + 'px, ' + arguments[1] + 'px)';
963+
this.elt.style.transform += style;
964+
} else if (arguments.length === 4) {
965+
var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, '');
966+
style = style.replace(/translate[X-Z]?\(.*\)/g, '');
967+
this.elt.style.transform = 'translate3d(' + arguments[0] + 'px,' + arguments[1] + 'px,' + arguments[2] + 'px)';
968+
this.elt.style.transform += style;
969+
this.elt.parentElement.style.perspective = '1000px';
970+
} else if (arguments.length === 5) {
971+
var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, '');
972+
style = style.replace(/translate[X-Z]?\(.*\)/g, '');
973+
this.elt.style.transform = 'translate3d(' + arguments[0] + 'px,' + arguments[1] + 'px,' + arguments[2] + 'px)';
974+
this.elt.style.transform += style;
975+
this.elt.parentElement.style.perspective = arguments[3] + 'px';
976+
}
977+
} else if (prop === 'position') {
978+
this.elt.style.left = arguments[1] + 'px';
979+
this.elt.style.top = arguments[2] + 'px';
980+
this.x = arguments[1];
981+
this.y = arguments[2];
982+
} else {
983+
this.elt.style[prop] = val;
984+
if (prop === 'width' || prop === 'height' || prop === 'left' || prop === 'top') {
985+
var numVal = val.replace(/\D+/g, '');
986+
this[prop] = parseInt(numVal, 10);
987+
}
928988
}
929989
}
930990
return this;

0 commit comments

Comments
 (0)