|
1 |
| -/*! p5.dom.js v0.2.6 November 24, 2015 */ |
| 1 | +/*! p5.dom.js v0.2.7 January 4, 2016 */ |
2 | 2 | /**
|
3 | 3 | * <p>The web is much more than just canvas and p5.dom makes it easy to interact
|
4 | 4 | * with other HTML5 objects, including text, hyperlink, image, input, video,
|
|
809 | 809 | * paths to different formats of the same audio. This is useful for ensuring
|
810 | 810 | * that your audio can play across different browsers, as each supports
|
811 | 811 | * different formats. See <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats">this
|
812 |
| - * page for further information about supported formats. |
| 812 | + * page for further information about supported formats</a>. |
813 | 813 | *
|
814 | 814 | * @method createAudio
|
815 | 815 | * @param {String|Array} src path to an audio file, or array of paths for
|
|
1180 | 1180 | * </code></div>
|
1181 | 1181 | */
|
1182 | 1182 | p5.Element.prototype.translate = function(){
|
| 1183 | + this.elt.style.position = 'absolute'; |
| 1184 | + // save out initial non-translate transform styling |
| 1185 | + var transform = ''; |
1183 | 1186 | if (this.elt.style.transform) {
|
1184 |
| - this.elt.style.position = 'absolute'; |
1185 |
| - if (arguments.length === 2){ |
1186 |
| - var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
1187 |
| - style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
1188 |
| - this.elt.style.transform = 'translate('+arguments[0]+'px, '+arguments[1]+'px)'; |
1189 |
| - this.elt.style.transform += style; |
1190 |
| - }else if (arguments.length === 3){ |
1191 |
| - var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
1192 |
| - style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
1193 |
| - this.elt.style.transform = 'translate3d('+arguments[0]+'px,'+arguments[1]+'px,'+arguments[2]+'px)'; |
1194 |
| - this.elt.style.transform += style; |
| 1187 | + transform = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
| 1188 | + transform = transform.replace(/translate[X-Z]?\(.*\)/g, ''); |
| 1189 | + } |
| 1190 | + if (arguments.length === 2) { |
| 1191 | + this.elt.style.transform = 'translate('+arguments[0]+'px, '+arguments[1]+'px)'; |
| 1192 | + } else if (arguments.length > 2) { |
| 1193 | + this.elt.style.transform = 'translate3d('+arguments[0]+'px,'+arguments[1]+'px,'+arguments[2]+'px)'; |
| 1194 | + if (arguments.length === 3) { |
1195 | 1195 | this.elt.parentElement.style.perspective = '1000px';
|
1196 |
| - }else if (arguments.length === 4){ |
1197 |
| - var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
1198 |
| - style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
1199 |
| - this.elt.style.transform = 'translate3d('+arguments[0]+'px,'+arguments[1]+'px,'+arguments[2]+'px)'; |
1200 |
| - this.elt.style.transform += style; |
| 1196 | + } else { |
1201 | 1197 | this.elt.parentElement.style.perspective = arguments[3]+'px';
|
1202 | 1198 | }
|
1203 |
| - } else { |
1204 |
| - console.log('Your browser does not support element translate function.'); |
1205 | 1199 | }
|
| 1200 | + // add any extra transform styling back on end |
| 1201 | + this.elt.style.transform += transform; |
1206 | 1202 | return this;
|
1207 | 1203 | };
|
1208 | 1204 |
|
|
1230 | 1226 | * </code></div>
|
1231 | 1227 | */
|
1232 | 1228 | p5.Element.prototype.rotate = function(){
|
| 1229 | + // save out initial non-rotate transform styling |
| 1230 | + var transform = ''; |
1233 | 1231 | if (this.elt.style.transform) {
|
1234 |
| - if (arguments.length === 1){ |
1235 |
| - var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
1236 |
| - style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1237 |
| - this.elt.style.transform = 'rotate('+arguments[0]+'deg)'; |
1238 |
| - this.elt.style.transform += style; |
1239 |
| - }else if (arguments.length === 2){ |
1240 |
| - var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
1241 |
| - style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1242 |
| - this.elt.style.transform = 'rotate('+arguments[0]+'deg, '+arguments[1]+'deg)'; |
1243 |
| - this.elt.style.transform += style; |
1244 |
| - }else if (arguments.length === 3){ |
1245 |
| - var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
1246 |
| - style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1247 |
| - this.elt.style.transform = 'rotateX('+arguments[0]+'deg)'; |
1248 |
| - this.elt.style.transform += 'rotateY('+arguments[1]+'deg)'; |
1249 |
| - this.elt.style.transform += 'rotateZ('+arguments[2]+'deg)'; |
1250 |
| - this.elt.style.transform += style; |
1251 |
| - } |
1252 |
| - } else { |
1253 |
| - console.log('Your browser does not support element rotate function.'); |
| 1232 | + var transform = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
| 1233 | + transform = transform.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1254 | 1234 | }
|
| 1235 | + |
| 1236 | + if (arguments.length === 1){ |
| 1237 | + this.elt.style.transform = 'rotate('+arguments[0]+'deg)'; |
| 1238 | + }else if (arguments.length === 2){ |
| 1239 | + this.elt.style.transform = 'rotate('+arguments[0]+'deg, '+arguments[1]+'deg)'; |
| 1240 | + }else if (arguments.length === 3){ |
| 1241 | + this.elt.style.transform = 'rotateX('+arguments[0]+'deg)'; |
| 1242 | + this.elt.style.transform += 'rotateY('+arguments[1]+'deg)'; |
| 1243 | + this.elt.style.transform += 'rotateZ('+arguments[2]+'deg)'; |
| 1244 | + } |
| 1245 | + // add remaining transform back on |
| 1246 | + this.elt.style.transform += transform; |
1255 | 1247 | return this;
|
1256 | 1248 | };
|
1257 | 1249 |
|
|
1277 | 1269 | * var myDiv = createDiv("I like pandas.");
|
1278 | 1270 | * myDiv.style("font-size", "18px");
|
1279 | 1271 | * myDiv.style("color", "#ff0000");
|
| 1272 | + * </code></div> |
| 1273 | + * <div><code class="norender"> |
1280 | 1274 | * var col = color(25,23,200,50);
|
1281 |
| - * createButton('button').style("background-color", col); |
| 1275 | + * var button = createButton("button"); |
| 1276 | + * button.style("background-color", col); |
| 1277 | + * button.position(10, 10); |
| 1278 | + * </code></div> |
| 1279 | + * <div><code class="norender"> |
| 1280 | + * var myDiv = createDiv("I like lizards."); |
| 1281 | + * myDiv.style("position", 20, 20); |
| 1282 | + * myDiv.style("rotate", 45); |
| 1283 | + * </code></div> |
| 1284 | + * <div><code class="norender"> |
| 1285 | + * var myDiv; |
| 1286 | + * function setup() { |
| 1287 | + * background(200); |
| 1288 | + * myDiv = createDiv("I like gray."); |
| 1289 | + * myDiv.position(20, 20); |
| 1290 | + * } |
| 1291 | + * |
| 1292 | + * function draw() { |
| 1293 | + * myDiv.style("font-size", mouseX+"px"); |
| 1294 | + * } |
1282 | 1295 | * </code></div>
|
1283 | 1296 | */
|
1284 | 1297 | p5.Element.prototype.style = function(prop, val) {
|
1285 | 1298 | var self = this;
|
1286 | 1299 |
|
1287 |
| - if (val instanceof p5.Color) |
| 1300 | + if (val instanceof p5.Color) { |
1288 | 1301 | val = 'rgba(' + val.levels[0] + ',' + val.levels[1] + ',' + val.levels[2] + ',' + val.levels[3]/255 + ')'
|
| 1302 | + } |
1289 | 1303 |
|
1290 | 1304 | if (typeof val === 'undefined') {
|
1291 | 1305 | if (prop.indexOf(':') === -1) {
|
|
1302 | 1316 | }
|
1303 | 1317 | }
|
1304 | 1318 | } else {
|
1305 |
| - if (prop === 'rotate'){ |
1306 |
| - if (this.elt.style.transform) { |
1307 |
| - if (arguments.length === 2) { |
1308 |
| - var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
1309 |
| - style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1310 |
| - this.elt.style.transform = 'rotate(' + arguments[0] + 'deg)'; |
1311 |
| - this.elt.style.transform += style; |
1312 |
| - } else if (arguments.length === 3) { |
1313 |
| - var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
1314 |
| - style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1315 |
| - this.elt.style.transform = 'rotate(' + arguments[0] + 'deg, ' + arguments[1] + 'deg)'; |
1316 |
| - this.elt.style.transform += style; |
1317 |
| - } else if (arguments.length === 4) { |
1318 |
| - var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
1319 |
| - style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
1320 |
| - this.elt.style.transform = 'rotateX(' + arguments[0] + 'deg)'; |
1321 |
| - this.elt.style.transform += 'rotateY(' + arguments[1] + 'deg)'; |
1322 |
| - this.elt.style.transform += 'rotateZ(' + arguments[2] + 'deg)'; |
1323 |
| - this.elt.style.transform += style; |
1324 |
| - } |
1325 |
| - } else if (prop === 'translate') { |
1326 |
| - if (arguments.length === 3) { |
1327 |
| - var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
1328 |
| - style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
1329 |
| - this.elt.style.transform = 'translate(' + arguments[0] + 'px, ' + arguments[1] + 'px)'; |
1330 |
| - this.elt.style.transform += style; |
1331 |
| - } else if (arguments.length === 4) { |
1332 |
| - var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
1333 |
| - style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
1334 |
| - this.elt.style.transform = 'translate3d(' + arguments[0] + 'px,' + arguments[1] + 'px,' + arguments[2] + 'px)'; |
1335 |
| - this.elt.style.transform += style; |
1336 |
| - this.elt.parentElement.style.perspective = '1000px'; |
1337 |
| - } else if (arguments.length === 5) { |
1338 |
| - var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
1339 |
| - style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
1340 |
| - this.elt.style.transform = 'translate3d(' + arguments[0] + 'px,' + arguments[1] + 'px,' + arguments[2] + 'px)'; |
1341 |
| - this.elt.style.transform += style; |
1342 |
| - this.elt.parentElement.style.perspective = arguments[3] + 'px'; |
1343 |
| - } |
1344 |
| - } else { |
1345 |
| - console.log('Your browser does not support element rotate or translate.'); |
1346 |
| - } |
1347 |
| - } else if (prop === 'position') { |
1348 |
| - this.elt.style.left = arguments[1] + 'px'; |
1349 |
| - this.elt.style.top = arguments[2] + 'px'; |
1350 |
| - this.x = arguments[1]; |
1351 |
| - this.y = arguments[2]; |
| 1319 | + if (prop === 'rotate' || prop === 'translate' || prop === 'position'){ |
| 1320 | + var trans = Array.prototype.shift.apply(arguments); |
| 1321 | + this[trans].apply(this, arguments); |
1352 | 1322 | } else {
|
1353 | 1323 | this.elt.style[prop] = val;
|
1354 | 1324 | if (prop === 'width' || prop === 'height' || prop === 'left' || prop === 'top') {
|
1355 | 1325 | var numVal = val.replace(/\D+/g, '');
|
1356 |
| - this[prop] = parseInt(numVal, 10); |
| 1326 | + this[prop] = parseInt(numVal, 10); // pend: is this necessary? |
1357 | 1327 | }
|
1358 | 1328 | }
|
1359 | 1329 | }
|
|
1706 | 1676 | this.drawingContext = this.canvas.getContext('2d');
|
1707 | 1677 | }
|
1708 | 1678 | if (this.canvas.width !== this.elt.width) {
|
1709 |
| - this.canvas.width = this.elt.videoWidth; |
1710 |
| - this.canvas.height = this.elt.videoHeight; |
| 1679 | + this.canvas.width = this.elt.width; |
| 1680 | + this.canvas.height = this.elt.height; |
1711 | 1681 | this.width = this.canvas.width;
|
1712 | 1682 | this.height = this.canvas.height;
|
1713 | 1683 | }
|
|
0 commit comments