├── BST.cpp ├── DetectCycleInGraph ├── Dino game ├── dino.js ├── index.html ├── p5.dom.js ├── p5.js ├── p5.sound.js ├── plant.js └── sketch.js ├── Divide String into N parts.java ├── Hell0W0rl2d.java ├── Hell0W0rl3d.cc ├── Hell0W0rl4d.py ├── Hell0W0rl5d.js ├── Hello World.masm ├── Hello world ├── Hello world 5 times ├── HelloWorld ├── HelloWorld.c ├── HelloWorld.java ├── HelloWorld.ps1 ├── HelloWorld.ring ├── HelloWorldByBoat.js ├── HelloWorldC++.cpp ├── Hello_World.csh ├── Heloooo.py ├── KotlinFibo.kt ├── KruskalAlog.cpp ├── Parameterised constructor ├── README.md ├── README_Pedro.md ├── README_es.md ├── Swap variables without 3rd Variables.java ├── Updated Hello World.html ├── a_plus_b.cpp ├── app.js ├── baseNnumber ├── bcancer.py ├── bubble sort.cpp ├── get_quadratic_eqn.py ├── h.j ├── hello,world.py ├── hello-world-cs.cs ├── hello.cpp ├── hello.java ├── hello.js ├── hello.jsp ├── hello.php ├── hello.py ├── hello.ring ├── hello1.c ├── helloAlex.cpp ├── hello_world.go ├── hello_world.jl ├── hello_world.js ├── hello_world.py ├── hello_world.rb ├── hello_world.sh ├── hello_world.ts ├── hello_world_bimobim.sh ├── hello_world_vue.html ├── hello_worldp.cpp ├── hello_yctseng1227.cpp ├── hellok.ts ├── hellos.cpp ├── hellos.java ├── hellos.ring ├── hellos_world.ring ├── hellouk.abap ├── hellouk.csp ├── hellouk.php ├── hellov.java ├── hellov.js ├── hellov.py ├── hellov.ring ├── helloworld.c ├── helloworld.html ├── helloworld.js ├── helloworld.jsp ├── helloworld.nim ├── helloworld.php ├── hw.cpp ├── ishita.java ├── linked List.ipynb ├── linked cb .ipynb ├── mergesort.cpp ├── name.py ├── nextgreater.java ├── notes.js ├── notes.json ├── package-lock.json ├── package.json ├── print5hello.py ├── project.clj ├── pythagorasTriplet.cpp ├── python3.py ├── readfile.py ├── text_to_speech.py └── trie.java /BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* left; 8 | node* right; 9 | node(int data){ 10 | this->data=data; 11 | this->left=NULL; 12 | this->right=NULL; 13 | } 14 | }; 15 | node* root; 16 | class BinarySearchTree{ 17 | public: 18 | void insert(node* root,int* a,int i){ 19 | if(root==NULL){ 20 | return; 21 | } 22 | 23 | if(a[i]data){ 24 | if(root->left==NULL){ 25 | node*nn=new node(a[i]); 26 | root->left=nn; 27 | 28 | } 29 | else{ 30 | insert(root->left,a,i); 31 | } 32 | } 33 | else{ 34 | if(root->right==NULL){ 35 | node*nn=new node(a[i]); 36 | root->right=nn; 37 | 38 | 39 | } 40 | else{ 41 | insert(root->right,a,i); 42 | } 43 | } 44 | } 45 | 46 | void FindMax(node* root,node* max){ 47 | if(root!=NULL){ 48 | if(root->data > max->data){ 49 | max=root; 50 | 51 | } 52 | FindMax(root->right,max); 53 | } 54 | 55 | 56 | } 57 | 58 | void del(node* root,int val){ 59 | if(root==NULL){ 60 | return; 61 | } 62 | if(root->left==NULL && root->right==NULL){ 63 | cout<data<data){ 70 | node* max=root->left; 71 | FindMax(root->left,max); 72 | root->data=max->data; 73 | if(max->left==NULL && max->right==NULL){ 74 | root->left=root->left->left; 75 | return; 76 | } 77 | del(max,max->data); 78 | return; 79 | } 80 | if(valdata){ 81 | del(root->left,val); 82 | } 83 | else{ 84 | del(root->right,val); 85 | } 86 | } 87 | void preOrder(node* root){ 88 | if(root!=NULL){ 89 | cout<data<<" "; 90 | preOrder(root->left); 91 | preOrder(root->right); 92 | } 93 | } 94 | void postOrder(node* root){ 95 | if(root!=NULL){ 96 | postOrder(root->left); 97 | postOrder(root->right); 98 | cout<data<<" "; 99 | } 100 | } 101 | void inOrder(node*root){ 102 | if(root!=NULL){ 103 | inOrder(root->left); 104 | cout<data<<" "; 105 | inOrder(root->right); 106 | } 107 | } 108 | 109 | }; 110 | int main(){ 111 | int n; 112 | cin>>n; 113 | int* arr=new int[n]; 114 | for(int i=0;i>arr[i]; 116 | } 117 | node* nn=new node(arr[0]); 118 | root=nn; 119 | BinarySearchTree bst; 120 | for(int i=1;i>val; 126 | bst.del(root,val); 127 | bst.preOrder(root); 128 | } 129 | 130 | -------------------------------------------------------------------------------- /DetectCycleInGraph: -------------------------------------------------------------------------------- 1 | class Graph 2 | { 3 | int V; // No. of vertices 4 | list *adj; // Pointer to an array containing adjacency lists 5 | bool isCyclicUtil(int v, bool visited[], bool *rs); // used by isCyclic() 6 | public: 7 | Graph(int V); // Constructor 8 | void addEdge(int v, int w); // to add an edge to graph 9 | bool isCyclic(); // returns true if there is a cycle in this graph 10 | }; 11 | 12 | Graph::Graph(int V) 13 | { 14 | this->V = V; 15 | adj = new list[V]; 16 | } 17 | 18 | void Graph::addEdge(int v, int w) 19 | { 20 | adj[v].push_back(w); // Add w to v’s list. 21 | } 22 | 23 | // This function is a variation of DFSUtil() in https://www.geeksforgeeks.org/archives/18212 24 | bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack) 25 | { 26 | if(visited[v] == false) 27 | { 28 | // Mark the current node as visited and part of recursion stack 29 | visited[v] = true; 30 | recStack[v] = true; 31 | 32 | // Recur for all the vertices adjacent to this vertex 33 | list::iterator i; 34 | for(i = adj[v].begin(); i != adj[v].end(); ++i) 35 | { 36 | if ( !visited[*i] && isCyclicUtil(*i, visited, recStack) ) 37 | return true; 38 | else if (recStack[*i]) 39 | return true; 40 | } 41 | 42 | } 43 | recStack[v] = false; // remove the vertex from recursion stack 44 | return false; 45 | } 46 | 47 | // Returns true if the graph contains a cycle, else false. 48 | // This function is a variation of DFS() in https://www.geeksforgeeks.org/archives/18212 49 | bool Graph::isCyclic() 50 | { 51 | // Mark all the vertices as not visited and not part of recursion 52 | // stack 53 | bool *visited = new bool[V]; 54 | bool *recStack = new bool[V]; 55 | for(int i = 0; i < V; i++) 56 | { 57 | visited[i] = false; 58 | recStack[i] = false; 59 | } 60 | 61 | // Call the recursive helper function to detect cycle in different 62 | // DFS trees 63 | for(int i = 0; i < V; i++) 64 | if (isCyclicUtil(i, visited, recStack)) 65 | return true; 66 | 67 | return false; 68 | } 69 | -------------------------------------------------------------------------------- /Dino game/dino.js: -------------------------------------------------------------------------------- 1 | function Dino(){ 2 | this.y=550; 3 | this.x=60; 4 | this.gravity =.5; 5 | this.velocity=12; 6 | 7 | this.show = function(){ 8 | fill(200,40,60); 9 | ellipse(this.x ,this.y, 30, 30); 10 | } 11 | 12 | this.update =function(){ 13 | this.velocity-=this.gravity; 14 | this.y-=this.velocity; 15 | 16 | if(this.y>550){ 17 | this.velocity=0; 18 | this.y=550; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Dino game/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | The ultimate Game 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Dino game/p5.dom.js: -------------------------------------------------------------------------------- 1 | /*! p5.js v0.9.0 July 01, 2019 */ 2 | /** 3 | *

The web is much more than just canvas and p5.dom makes it easy to interact 4 | * with other HTML5 objects, including text, hyperlink, image, input, video, 5 | * audio, and webcam.

6 | *

There is a set of creation methods, DOM manipulation methods, and 7 | * an extended p5.Element that supports a range of HTML elements. See the 8 | * 9 | * beyond the canvas tutorial for a full overview of how this addon works. 10 | * 11 | *

Methods and properties shown in black are part of the p5.js core, items in 12 | * blue are part of the p5.dom library. You will need to include an extra file 13 | * in order to access the blue functions. See the 14 | * using a library 15 | * section for information on how to include this library. p5.dom comes with 16 | * p5 complete or you can download the single file 17 | * 18 | * here.

19 | *

See tutorial: beyond the canvas 20 | * for more info on how to use this library. 21 | * 22 | * @module p5.dom 23 | * @submodule p5.dom 24 | * @for p5 25 | * @main 26 | */ 27 | 28 | (function(root, factory) { 29 | if (typeof define === 'function' && define.amd) 30 | define('p5.dom', ['p5'], function(p5) { 31 | factory(p5); 32 | }); 33 | else if (typeof exports === 'object') factory(require('../p5')); 34 | else factory(root['p5']); 35 | })(this, function(p5) { 36 | // ============================================================================= 37 | // p5 additions 38 | // ============================================================================= 39 | 40 | /** 41 | * Searches the page for an element with the given ID, class, or tag name (using the '#' or '.' 42 | * prefixes to specify an ID or class respectively, and none for a tag) and returns it as 43 | * a p5.Element. If a class or tag name is given with more than 1 element, 44 | * only the first element will be returned. 45 | * The DOM node itself can be accessed with .elt. 46 | * Returns null if none found. You can also specify a container to search within. 47 | * 48 | * @method select 49 | * @param {String} name id, class, or tag name of element to search for 50 | * @param {String|p5.Element|HTMLElement} [container] id, p5.Element, or 51 | * HTML element to search within 52 | * @return {p5.Element|null} p5.Element containing node found 53 | * @example 54 | *

55 | * function setup() { 56 | * createCanvas(100, 100); 57 | * //translates canvas 50px down 58 | * select('canvas').position(100, 100); 59 | * } 60 | *
61 | *
62 | * // these are all valid calls to select() 63 | * var a = select('#moo'); 64 | * var b = select('#blah', '#myContainer'); 65 | * var c, e; 66 | * if (b) { 67 | * c = select('#foo', b); 68 | * } 69 | * var d = document.getElementById('beep'); 70 | * if (d) { 71 | * e = select('p', d); 72 | * } 73 | * [a, b, c, d, e]; // unused 74 | *
75 | * 76 | */ 77 | p5.prototype.select = function(e, p) { 78 | p5._validateParameters('select', arguments); 79 | var res = null; 80 | var container = getContainer(p); 81 | if (e[0] === '.') { 82 | e = e.slice(1); 83 | res = container.getElementsByClassName(e); 84 | if (res.length) { 85 | res = res[0]; 86 | } else { 87 | res = null; 88 | } 89 | } else if (e[0] === '#') { 90 | e = e.slice(1); 91 | res = container.getElementById(e); 92 | } else { 93 | res = container.getElementsByTagName(e); 94 | if (res.length) { 95 | res = res[0]; 96 | } else { 97 | res = null; 98 | } 99 | } 100 | if (res) { 101 | return this._wrapElement(res); 102 | } else { 103 | return null; 104 | } 105 | }; 106 | 107 | /** 108 | * Searches the page for elements with the given class or tag name (using the '.' prefix 109 | * to specify a class and no prefix for a tag) and returns them as p5.Elements 110 | * in an array. 111 | * The DOM node itself can be accessed with .elt. 112 | * Returns an empty array if none found. 113 | * You can also specify a container to search within. 114 | * 115 | * @method selectAll 116 | * @param {String} name class or tag name of elements to search for 117 | * @param {String} [container] id, p5.Element, or HTML element to search within 118 | * @return {p5.Element[]} Array of p5.Elements containing nodes found 119 | * @example 120 | *
121 | * function setup() { 122 | * createButton('btn'); 123 | * createButton('2nd btn'); 124 | * createButton('3rd btn'); 125 | * var buttons = selectAll('button'); 126 | * 127 | * for (var i = 0; i < buttons.length; i++) { 128 | * buttons[i].size(100, 100); 129 | * } 130 | * } 131 | *
132 | *
133 | * // these are all valid calls to selectAll() 134 | * var a = selectAll('.moo'); 135 | * a = selectAll('div'); 136 | * a = selectAll('button', '#myContainer'); 137 | * 138 | * var d = select('#container'); 139 | * a = selectAll('p', d); 140 | * 141 | * var f = document.getElementById('beep'); 142 | * a = select('.blah', f); 143 | * 144 | * a; // unused 145 | *
146 | * 147 | */ 148 | p5.prototype.selectAll = function(e, p) { 149 | p5._validateParameters('selectAll', arguments); 150 | var arr = []; 151 | var res; 152 | var container = getContainer(p); 153 | if (e[0] === '.') { 154 | e = e.slice(1); 155 | res = container.getElementsByClassName(e); 156 | } else { 157 | res = container.getElementsByTagName(e); 158 | } 159 | if (res) { 160 | for (var j = 0; j < res.length; j++) { 161 | var obj = this._wrapElement(res[j]); 162 | arr.push(obj); 163 | } 164 | } 165 | return arr; 166 | }; 167 | 168 | /** 169 | * Helper function for select and selectAll 170 | */ 171 | function getContainer(p) { 172 | var container = document; 173 | if (typeof p === 'string' && p[0] === '#') { 174 | p = p.slice(1); 175 | container = document.getElementById(p) || document; 176 | } else if (p instanceof p5.Element) { 177 | container = p.elt; 178 | } else if (p instanceof HTMLElement) { 179 | container = p; 180 | } 181 | return container; 182 | } 183 | 184 | /** 185 | * Helper function for getElement and getElements. 186 | */ 187 | p5.prototype._wrapElement = function(elt) { 188 | var children = Array.prototype.slice.call(elt.children); 189 | if (elt.tagName === 'INPUT' && elt.type === 'checkbox') { 190 | var converted = new p5.Element(elt, this); 191 | converted.checked = function() { 192 | if (arguments.length === 0) { 193 | return this.elt.checked; 194 | } else if (arguments[0]) { 195 | this.elt.checked = true; 196 | } else { 197 | this.elt.checked = false; 198 | } 199 | return this; 200 | }; 201 | return converted; 202 | } else if (elt.tagName === 'VIDEO' || elt.tagName === 'AUDIO') { 203 | return new p5.MediaElement(elt, this); 204 | } else if (elt.tagName === 'SELECT') { 205 | return this.createSelect(new p5.Element(elt, this)); 206 | } else if ( 207 | children.length > 0 && 208 | children.every(function(c) { 209 | return c.tagName === 'INPUT' || c.tagName === 'LABEL'; 210 | }) 211 | ) { 212 | return this.createRadio(new p5.Element(elt, this)); 213 | } else { 214 | return new p5.Element(elt, this); 215 | } 216 | }; 217 | 218 | /** 219 | * Removes all elements created by p5, except any canvas / graphics 220 | * elements created by createCanvas or createGraphics. 221 | * Event handlers are removed, and element is removed from the DOM. 222 | * @method removeElements 223 | * @example 224 | *
225 | * function setup() { 226 | * createCanvas(100, 100); 227 | * createDiv('this is some text'); 228 | * createP('this is a paragraph'); 229 | * } 230 | * function mousePressed() { 231 | * removeElements(); // this will remove the div and p, not canvas 232 | * } 233 | *
234 | * 235 | */ 236 | p5.prototype.removeElements = function(e) { 237 | p5._validateParameters('removeElements', arguments); 238 | for (var i = 0; i < this._elements.length; i++) { 239 | if (!(this._elements[i].elt instanceof HTMLCanvasElement)) { 240 | this._elements[i].remove(); 241 | } 242 | } 243 | }; 244 | 245 | /** 246 | * The .changed() function is called when the value of an 247 | * element changes. 248 | * This can be used to attach an element specific event listener. 249 | * 250 | * @method changed 251 | * @param {Function|Boolean} fxn function to be fired when the value of 252 | * an element changes. 253 | * if `false` is passed instead, the previously 254 | * firing function will no longer fire. 255 | * @chainable 256 | * @example 257 | *
258 | * var sel; 259 | * 260 | * function setup() { 261 | * textAlign(CENTER); 262 | * background(200); 263 | * sel = createSelect(); 264 | * sel.position(10, 10); 265 | * sel.option('pear'); 266 | * sel.option('kiwi'); 267 | * sel.option('grape'); 268 | * sel.changed(mySelectEvent); 269 | * } 270 | * 271 | * function mySelectEvent() { 272 | * var item = sel.value(); 273 | * background(200); 274 | * text("it's a " + item + '!', 50, 50); 275 | * } 276 | *
277 | * 278 | *
279 | * var checkbox; 280 | * var cnv; 281 | * 282 | * function setup() { 283 | * checkbox = createCheckbox(' fill'); 284 | * checkbox.changed(changeFill); 285 | * cnv = createCanvas(100, 100); 286 | * cnv.position(0, 30); 287 | * noFill(); 288 | * } 289 | * 290 | * function draw() { 291 | * background(200); 292 | * ellipse(50, 50, 50, 50); 293 | * } 294 | * 295 | * function changeFill() { 296 | * if (checkbox.checked()) { 297 | * fill(0); 298 | * } else { 299 | * noFill(); 300 | * } 301 | * } 302 | *
303 | * 304 | * @alt 305 | * dropdown: pear, kiwi, grape. When selected text "its a" + selection shown. 306 | * 307 | */ 308 | p5.Element.prototype.changed = function(fxn) { 309 | p5.Element._adjustListener('change', fxn, this); 310 | return this; 311 | }; 312 | 313 | /** 314 | * The .input() function is called when any user input is 315 | * detected with an element. The input event is often used 316 | * to detect keystrokes in a input element, or changes on a 317 | * slider element. This can be used to attach an element specific 318 | * event listener. 319 | * 320 | * @method input 321 | * @param {Function|Boolean} fxn function to be fired when any user input is 322 | * detected within the element. 323 | * if `false` is passed instead, the previously 324 | * firing function will no longer fire. 325 | * @chainable 326 | * @example 327 | *
328 | * // Open your console to see the output 329 | * function setup() { 330 | * var inp = createInput(''); 331 | * inp.input(myInputEvent); 332 | * } 333 | * 334 | * function myInputEvent() { 335 | * console.log('you are typing: ', this.value()); 336 | * } 337 | *
338 | * 339 | * @alt 340 | * no display. 341 | * 342 | */ 343 | p5.Element.prototype.input = function(fxn) { 344 | p5.Element._adjustListener('input', fxn, this); 345 | return this; 346 | }; 347 | 348 | /** 349 | * Helpers for create methods. 350 | */ 351 | function addElement(elt, pInst, media) { 352 | var node = pInst._userNode ? pInst._userNode : document.body; 353 | node.appendChild(elt); 354 | var c = media 355 | ? new p5.MediaElement(elt, pInst) 356 | : new p5.Element(elt, pInst); 357 | pInst._elements.push(c); 358 | return c; 359 | } 360 | 361 | /** 362 | * Creates a <div></div> element in the DOM with given inner HTML. 363 | * Appends to the container node if one is specified, otherwise 364 | * appends to body. 365 | * 366 | * @method createDiv 367 | * @param {String} [html] inner HTML for element created 368 | * @return {p5.Element} pointer to p5.Element holding created node 369 | * @example 370 | *
371 | * createDiv('this is some text'); 372 | *
373 | */ 374 | 375 | /** 376 | * Creates a <p></p> element in the DOM with given inner HTML. Used 377 | * for paragraph length text. 378 | * Appends to the container node if one is specified, otherwise 379 | * appends to body. 380 | * 381 | * @method createP 382 | * @param {String} [html] inner HTML for element created 383 | * @return {p5.Element} pointer to p5.Element holding created node 384 | * @example 385 | *
386 | * createP('this is some text'); 387 | *
388 | */ 389 | 390 | /** 391 | * Creates a <span></span> element in the DOM with given inner HTML. 392 | * Appends to the container node if one is specified, otherwise 393 | * appends to body. 394 | * 395 | * @method createSpan 396 | * @param {String} [html] inner HTML for element created 397 | * @return {p5.Element} pointer to p5.Element holding created node 398 | * @example 399 | *
400 | * createSpan('this is some text'); 401 | *
402 | */ 403 | var tags = ['div', 'p', 'span']; 404 | tags.forEach(function(tag) { 405 | var method = 'create' + tag.charAt(0).toUpperCase() + tag.slice(1); 406 | p5.prototype[method] = function(html) { 407 | var elt = document.createElement(tag); 408 | elt.innerHTML = typeof html === 'undefined' ? '' : html; 409 | return addElement(elt, this); 410 | }; 411 | }); 412 | 413 | /** 414 | * Creates an <img> element in the DOM with given src and 415 | * alternate text. 416 | * Appends to the container node if one is specified, otherwise 417 | * appends to body. 418 | * 419 | * @method createImg 420 | * @param {String} src src path or url for image 421 | * @param {String} [alt] alternate text to be used if image does not load 422 | * @param {Function} [successCallback] callback to be called once image data is loaded 423 | * @return {p5.Element} pointer to p5.Element holding created node 424 | * @example 425 | *
426 | * createImg('http://p5js.org/img/asterisk-01.png'); 427 | *
428 | */ 429 | /** 430 | * @method createImg 431 | * @param {String} src 432 | * @param {Function} successCallback 433 | * @return {Object|p5.Element} 434 | */ 435 | p5.prototype.createImg = function() { 436 | p5._validateParameters('createImg', arguments); 437 | var elt = document.createElement('img'); 438 | elt.crossOrigin = 'Anonymous'; 439 | var args = arguments; 440 | var self; 441 | var setAttrs = function() { 442 | self.width = elt.offsetWidth || elt.width; 443 | self.height = elt.offsetHeight || elt.height; 444 | if (args.length > 1 && typeof args[1] === 'function') { 445 | self.fn = args[1]; 446 | self.fn(); 447 | } else if (args.length > 1 && typeof args[2] === 'function') { 448 | self.fn = args[2]; 449 | self.fn(); 450 | } 451 | }; 452 | elt.src = args[0]; 453 | if (args.length > 1 && typeof args[1] === 'string') { 454 | elt.alt = args[1]; 455 | } 456 | elt.onload = function() { 457 | setAttrs(); 458 | }; 459 | self = addElement(elt, this); 460 | return self; 461 | }; 462 | 463 | /** 464 | * Creates an <a></a> element in the DOM for including a hyperlink. 465 | * Appends to the container node if one is specified, otherwise 466 | * appends to body. 467 | * 468 | * @method createA 469 | * @param {String} href url of page to link to 470 | * @param {String} html inner html of link element to display 471 | * @param {String} [target] target where new link should open, 472 | * could be _blank, _self, _parent, _top. 473 | * @return {p5.Element} pointer to p5.Element holding created node 474 | * @example 475 | *
476 | * createA('http://p5js.org/', 'this is a link'); 477 | *
478 | */ 479 | p5.prototype.createA = function(href, html, target) { 480 | p5._validateParameters('createA', arguments); 481 | var elt = document.createElement('a'); 482 | elt.href = href; 483 | elt.innerHTML = html; 484 | if (target) elt.target = target; 485 | return addElement(elt, this); 486 | }; 487 | 488 | /** INPUT **/ 489 | 490 | /** 491 | * Creates a slider <input></input> element in the DOM. 492 | * Use .size() to set the display length of the slider. 493 | * Appends to the container node if one is specified, otherwise 494 | * appends to body. 495 | * 496 | * @method createSlider 497 | * @param {Number} min minimum value of the slider 498 | * @param {Number} max maximum value of the slider 499 | * @param {Number} [value] default value of the slider 500 | * @param {Number} [step] step size for each tick of the slider (if step is set to 0, the slider will move continuously from the minimum to the maximum value) 501 | * @return {p5.Element} pointer to p5.Element holding created node 502 | * @example 503 | *
504 | * var slider; 505 | * function setup() { 506 | * slider = createSlider(0, 255, 100); 507 | * slider.position(10, 10); 508 | * slider.style('width', '80px'); 509 | * } 510 | * 511 | * function draw() { 512 | * var val = slider.value(); 513 | * background(val); 514 | * } 515 | *
516 | * 517 | *
518 | * var slider; 519 | * function setup() { 520 | * colorMode(HSB); 521 | * slider = createSlider(0, 360, 60, 40); 522 | * slider.position(10, 10); 523 | * slider.style('width', '80px'); 524 | * } 525 | * 526 | * function draw() { 527 | * var val = slider.value(); 528 | * background(val, 100, 100, 1); 529 | * } 530 | *
531 | */ 532 | p5.prototype.createSlider = function(min, max, value, step) { 533 | p5._validateParameters('createSlider', arguments); 534 | var elt = document.createElement('input'); 535 | elt.type = 'range'; 536 | elt.min = min; 537 | elt.max = max; 538 | if (step === 0) { 539 | elt.step = 0.000000000000000001; // smallest valid step 540 | } else if (step) { 541 | elt.step = step; 542 | } 543 | if (typeof value === 'number') elt.value = value; 544 | return addElement(elt, this); 545 | }; 546 | 547 | /** 548 | * Creates a <button></button> element in the DOM. 549 | * Use .size() to set the display size of the button. 550 | * Use .mousePressed() to specify behavior on press. 551 | * Appends to the container node if one is specified, otherwise 552 | * appends to body. 553 | * 554 | * @method createButton 555 | * @param {String} label label displayed on the button 556 | * @param {String} [value] value of the button 557 | * @return {p5.Element} pointer to p5.Element holding created node 558 | * @example 559 | *
560 | * var button; 561 | * function setup() { 562 | * createCanvas(100, 100); 563 | * background(0); 564 | * button = createButton('click me'); 565 | * button.position(19, 19); 566 | * button.mousePressed(changeBG); 567 | * } 568 | * 569 | * function changeBG() { 570 | * var val = random(255); 571 | * background(val); 572 | * } 573 | *
574 | */ 575 | p5.prototype.createButton = function(label, value) { 576 | p5._validateParameters('createButton', arguments); 577 | var elt = document.createElement('button'); 578 | elt.innerHTML = label; 579 | if (value) elt.value = value; 580 | return addElement(elt, this); 581 | }; 582 | 583 | /** 584 | * Creates a checkbox <input></input> element in the DOM. 585 | * Calling .checked() on a checkbox returns if it is checked or not 586 | * 587 | * @method createCheckbox 588 | * @param {String} [label] label displayed after checkbox 589 | * @param {boolean} [value] value of the checkbox; checked is true, unchecked is false 590 | * @return {p5.Element} pointer to p5.Element holding created node 591 | * @example 592 | *
593 | * var checkbox; 594 | * 595 | * function setup() { 596 | * checkbox = createCheckbox('label', false); 597 | * checkbox.changed(myCheckedEvent); 598 | * } 599 | * 600 | * function myCheckedEvent() { 601 | * if (this.checked()) { 602 | * console.log('Checking!'); 603 | * } else { 604 | * console.log('Unchecking!'); 605 | * } 606 | * } 607 | *
608 | */ 609 | p5.prototype.createCheckbox = function() { 610 | p5._validateParameters('createCheckbox', arguments); 611 | var elt = document.createElement('div'); 612 | var checkbox = document.createElement('input'); 613 | checkbox.type = 'checkbox'; 614 | elt.appendChild(checkbox); 615 | //checkbox must be wrapped in p5.Element before label so that label appears after 616 | var self = addElement(elt, this); 617 | self.checked = function() { 618 | var cb = self.elt.getElementsByTagName('input')[0]; 619 | if (cb) { 620 | if (arguments.length === 0) { 621 | return cb.checked; 622 | } else if (arguments[0]) { 623 | cb.checked = true; 624 | } else { 625 | cb.checked = false; 626 | } 627 | } 628 | return self; 629 | }; 630 | this.value = function(val) { 631 | self.value = val; 632 | return this; 633 | }; 634 | if (arguments[0]) { 635 | var ran = Math.random() 636 | .toString(36) 637 | .slice(2); 638 | var label = document.createElement('label'); 639 | checkbox.setAttribute('id', ran); 640 | label.htmlFor = ran; 641 | self.value(arguments[0]); 642 | label.appendChild(document.createTextNode(arguments[0])); 643 | elt.appendChild(label); 644 | } 645 | if (arguments[1]) { 646 | checkbox.checked = true; 647 | } 648 | return self; 649 | }; 650 | 651 | /** 652 | * Creates a dropdown menu <select></select> element in the DOM. 653 | * It also helps to assign select-box methods to p5.Element when selecting existing select box 654 | * @method createSelect 655 | * @param {boolean} [multiple] true if dropdown should support multiple selections 656 | * @return {p5.Element} 657 | * @example 658 | *
659 | * var sel; 660 | * 661 | * function setup() { 662 | * textAlign(CENTER); 663 | * background(200); 664 | * sel = createSelect(); 665 | * sel.position(10, 10); 666 | * sel.option('pear'); 667 | * sel.option('kiwi'); 668 | * sel.option('grape'); 669 | * sel.changed(mySelectEvent); 670 | * } 671 | * 672 | * function mySelectEvent() { 673 | * var item = sel.value(); 674 | * background(200); 675 | * text('It is a ' + item + '!', 50, 50); 676 | * } 677 | *
678 | */ 679 | /** 680 | * @method createSelect 681 | * @param {Object} existing DOM select element 682 | * @return {p5.Element} 683 | */ 684 | 685 | p5.prototype.createSelect = function() { 686 | p5._validateParameters('createSelect', arguments); 687 | var elt, self; 688 | var arg = arguments[0]; 689 | if (typeof arg === 'object' && arg.elt.nodeName === 'SELECT') { 690 | self = arg; 691 | elt = this.elt = arg.elt; 692 | } else { 693 | elt = document.createElement('select'); 694 | if (arg && typeof arg === 'boolean') { 695 | elt.setAttribute('multiple', 'true'); 696 | } 697 | self = addElement(elt, this); 698 | } 699 | self.option = function(name, value) { 700 | var index; 701 | //see if there is already an option with this name 702 | for (var i = 0; i < this.elt.length; i++) { 703 | if (this.elt[i].innerHTML === name) { 704 | index = i; 705 | break; 706 | } 707 | } 708 | //if there is an option with this name we will modify it 709 | if (index !== undefined) { 710 | //if the user passed in false then delete that option 711 | if (value === false) { 712 | this.elt.remove(index); 713 | } else { 714 | //otherwise if the name and value are the same then change both 715 | if (this.elt[index].innerHTML === this.elt[index].value) { 716 | this.elt[index].innerHTML = this.elt[index].value = value; 717 | //otherwise just change the value 718 | } else { 719 | this.elt[index].value = value; 720 | } 721 | } 722 | } else { 723 | //if it doesn't exist make it 724 | var opt = document.createElement('option'); 725 | opt.innerHTML = name; 726 | if (arguments.length > 1) opt.value = value; 727 | else opt.value = name; 728 | elt.appendChild(opt); 729 | } 730 | }; 731 | self.selected = function(value) { 732 | var arr = [], 733 | i; 734 | if (arguments.length > 0) { 735 | for (i = 0; i < this.elt.length; i++) { 736 | if (value.toString() === this.elt[i].value) { 737 | this.elt.selectedIndex = i; 738 | } 739 | } 740 | return this; 741 | } else { 742 | if (this.elt.getAttribute('multiple')) { 743 | for (i = 0; i < this.elt.selectedOptions.length; i++) { 744 | arr.push(this.elt.selectedOptions[i].value); 745 | } 746 | return arr; 747 | } else { 748 | return this.elt.value; 749 | } 750 | } 751 | }; 752 | return self; 753 | }; 754 | 755 | /** 756 | * Creates a radio button <input></input> element in the DOM. 757 | * The .option() method can be used to set options for the radio after it is 758 | * created. The .value() method will return the currently selected option. 759 | * 760 | * @method createRadio 761 | * @param {String} [divId] the id and name of the created div and input field respectively 762 | * @return {p5.Element} pointer to p5.Element holding created node 763 | * @example 764 | *
765 | * var radio; 766 | * 767 | * function setup() { 768 | * radio = createRadio(); 769 | * radio.option('black'); 770 | * radio.option('white'); 771 | * radio.option('gray'); 772 | * radio.style('width', '60px'); 773 | * textAlign(CENTER); 774 | * fill(255, 0, 0); 775 | * } 776 | * 777 | * function draw() { 778 | * var val = radio.value(); 779 | * background(val); 780 | * text(val, width / 2, height / 2); 781 | * } 782 | *
783 | *
784 | * var radio; 785 | * 786 | * function setup() { 787 | * radio = createRadio(); 788 | * radio.option('apple', 1); 789 | * radio.option('bread', 2); 790 | * radio.option('juice', 3); 791 | * radio.style('width', '60px'); 792 | * textAlign(CENTER); 793 | * } 794 | * 795 | * function draw() { 796 | * background(200); 797 | * var val = radio.value(); 798 | * if (val) { 799 | * text('item cost is $' + val, width / 2, height / 2); 800 | * } 801 | * } 802 | *
803 | */ 804 | p5.prototype.createRadio = function(existing_radios) { 805 | p5._validateParameters('createRadio', arguments); 806 | // do some prep by counting number of radios on page 807 | var radios = document.querySelectorAll('input[type=radio]'); 808 | var count = 0; 809 | if (radios.length > 1) { 810 | var length = radios.length; 811 | var prev = radios[0].name; 812 | var current = radios[1].name; 813 | count = 1; 814 | for (var i = 1; i < length; i++) { 815 | current = radios[i].name; 816 | if (prev !== current) { 817 | count++; 818 | } 819 | prev = current; 820 | } 821 | } else if (radios.length === 1) { 822 | count = 1; 823 | } 824 | // see if we got an existing set of radios from callee 825 | var elt, self; 826 | if (typeof existing_radios === 'object') { 827 | // use existing elements 828 | self = existing_radios; 829 | elt = this.elt = existing_radios.elt; 830 | } else { 831 | // create a set of radio buttons 832 | elt = document.createElement('div'); 833 | self = addElement(elt, this); 834 | } 835 | // setup member functions 836 | self._getInputChildrenArray = function() { 837 | return Array.prototype.slice.call(this.elt.children).filter(function(c) { 838 | return c.tagName === 'INPUT'; 839 | }); 840 | }; 841 | 842 | var times = -1; 843 | self.option = function(name, value) { 844 | var opt = document.createElement('input'); 845 | opt.type = 'radio'; 846 | opt.innerHTML = name; 847 | if (value) opt.value = value; 848 | else opt.value = name; 849 | opt.setAttribute('name', 'defaultradio' + count); 850 | elt.appendChild(opt); 851 | if (name) { 852 | times++; 853 | var label = document.createElement('label'); 854 | opt.setAttribute('id', 'defaultradio' + count + '-' + times); 855 | label.htmlFor = 'defaultradio' + count + '-' + times; 856 | label.appendChild(document.createTextNode(name)); 857 | elt.appendChild(label); 858 | } 859 | return opt; 860 | }; 861 | self.selected = function(value) { 862 | var i; 863 | var inputChildren = self._getInputChildrenArray(); 864 | if (value) { 865 | for (i = 0; i < inputChildren.length; i++) { 866 | if (inputChildren[i].value === value) inputChildren[i].checked = true; 867 | } 868 | return this; 869 | } else { 870 | for (i = 0; i < inputChildren.length; i++) { 871 | if (inputChildren[i].checked === true) return inputChildren[i].value; 872 | } 873 | } 874 | }; 875 | self.value = function(value) { 876 | var i; 877 | var inputChildren = self._getInputChildrenArray(); 878 | if (value) { 879 | for (i = 0; i < inputChildren.length; i++) { 880 | if (inputChildren[i].value === value) inputChildren[i].checked = true; 881 | } 882 | return this; 883 | } else { 884 | for (i = 0; i < inputChildren.length; i++) { 885 | if (inputChildren[i].checked === true) return inputChildren[i].value; 886 | } 887 | return ''; 888 | } 889 | }; 890 | return self; 891 | }; 892 | 893 | /** 894 | * Creates a colorPicker element in the DOM for color input. 895 | * The .value() method will return a hex string (#rrggbb) of the color. 896 | * The .color() method will return a p5.Color object with the current chosen color. 897 | * 898 | * @method createColorPicker 899 | * @param {String|p5.Color} [value] default color of element 900 | * @return {p5.Element} pointer to p5.Element holding created node 901 | * @example 902 | *
903 | * 904 | * var inp1, inp2; 905 | * function setup() { 906 | * createCanvas(100, 100); 907 | * background('grey'); 908 | * inp1 = createColorPicker('#ff0000'); 909 | * inp2 = createColorPicker(color('yellow')); 910 | * inp1.input(setShade1); 911 | * inp2.input(setShade2); 912 | * setMidShade(); 913 | * } 914 | * 915 | * function setMidShade() { 916 | * // Finding a shade between the two 917 | * var commonShade = lerpColor(inp1.color(), inp2.color(), 0.5); 918 | * fill(commonShade); 919 | * rect(20, 20, 60, 60); 920 | * } 921 | * 922 | * function setShade1() { 923 | * setMidShade(); 924 | * console.log('You are choosing shade 1 to be : ', this.value()); 925 | * } 926 | * function setShade2() { 927 | * setMidShade(); 928 | * console.log('You are choosing shade 2 to be : ', this.value()); 929 | * } 930 | * 931 | *
932 | */ 933 | p5.prototype.createColorPicker = function(value) { 934 | p5._validateParameters('createColorPicker', arguments); 935 | var elt = document.createElement('input'); 936 | var self; 937 | elt.type = 'color'; 938 | if (value) { 939 | if (value instanceof p5.Color) { 940 | elt.value = value.toString('#rrggbb'); 941 | } else { 942 | p5.prototype._colorMode = 'rgb'; 943 | p5.prototype._colorMaxes = { 944 | rgb: [255, 255, 255, 255], 945 | hsb: [360, 100, 100, 1], 946 | hsl: [360, 100, 100, 1] 947 | }; 948 | elt.value = p5.prototype.color(value).toString('#rrggbb'); 949 | } 950 | } else { 951 | elt.value = '#000000'; 952 | } 953 | self = addElement(elt, this); 954 | // Method to return a p5.Color object for the given color. 955 | self.color = function() { 956 | if (value.mode) { 957 | p5.prototype._colorMode = value.mode; 958 | } 959 | if (value.maxes) { 960 | p5.prototype._colorMaxes = value.maxes; 961 | } 962 | return p5.prototype.color(this.elt.value); 963 | }; 964 | return self; 965 | }; 966 | 967 | /** 968 | * Creates an <input></input> element in the DOM for text input. 969 | * Use .size() to set the display length of the box. 970 | * Appends to the container node if one is specified, otherwise 971 | * appends to body. 972 | * 973 | * @method createInput 974 | * @param {String} [value] default value of the input box 975 | * @param {String} [type] type of text, ie text, password etc. Defaults to text 976 | * @return {p5.Element} pointer to p5.Element holding created node 977 | * @example 978 | *
979 | * function setup() { 980 | * var inp = createInput(''); 981 | * inp.input(myInputEvent); 982 | * } 983 | * 984 | * function myInputEvent() { 985 | * console.log('you are typing: ', this.value()); 986 | * } 987 | *
988 | */ 989 | p5.prototype.createInput = function(value, type) { 990 | p5._validateParameters('createInput', arguments); 991 | var elt = document.createElement('input'); 992 | elt.type = type ? type : 'text'; 993 | if (value) elt.value = value; 994 | return addElement(elt, this); 995 | }; 996 | 997 | /** 998 | * Creates an <input></input> element in the DOM of type 'file'. 999 | * This allows users to select local files for use in a sketch. 1000 | * 1001 | * @method createFileInput 1002 | * @param {Function} [callback] callback function for when a file loaded 1003 | * @param {String} [multiple] optional to allow multiple files selected 1004 | * @return {p5.Element} pointer to p5.Element holding created DOM element 1005 | * @example 1006 | *
1007 | * let input; 1008 | * let img; 1009 | * 1010 | * function setup() { 1011 | * input = createFileInput(handleFile); 1012 | * input.position(0, 0); 1013 | * } 1014 | * 1015 | * function draw() { 1016 | * background(255); 1017 | * if (img) { 1018 | * image(img, 0, 0, width, height); 1019 | * } 1020 | * } 1021 | * 1022 | * function handleFile(file) { 1023 | * print(file); 1024 | * if (file.type === 'image') { 1025 | * img = createImg(file.data); 1026 | * img.hide(); 1027 | * } else { 1028 | * img = null; 1029 | * } 1030 | * } 1031 | *
1032 | */ 1033 | p5.prototype.createFileInput = function(callback, multiple) { 1034 | p5._validateParameters('createFileInput', arguments); 1035 | // Function to handle when a file is selected 1036 | // We're simplifying life and assuming that we always 1037 | // want to load every selected file 1038 | function handleFileSelect(evt) { 1039 | // These are the files 1040 | var files = evt.target.files; 1041 | // Load each one and trigger a callback 1042 | for (var i = 0; i < files.length; i++) { 1043 | var f = files[i]; 1044 | p5.File._load(f, callback); 1045 | } 1046 | } 1047 | // Is the file stuff supported? 1048 | if (window.File && window.FileReader && window.FileList && window.Blob) { 1049 | // Yup, we're ok and make an input file selector 1050 | var elt = document.createElement('input'); 1051 | elt.type = 'file'; 1052 | 1053 | // If we get a second argument that evaluates to true 1054 | // then we are looking for multiple files 1055 | if (multiple) { 1056 | // Anything gets the job done 1057 | elt.multiple = 'multiple'; 1058 | } 1059 | 1060 | // Now let's handle when a file was selected 1061 | elt.addEventListener('change', handleFileSelect, false); 1062 | return addElement(elt, this); 1063 | } else { 1064 | console.log( 1065 | 'The File APIs are not fully supported in this browser. Cannot create element.' 1066 | ); 1067 | } 1068 | }; 1069 | 1070 | /** VIDEO STUFF **/ 1071 | 1072 | function createMedia(pInst, type, src, callback) { 1073 | var elt = document.createElement(type); 1074 | 1075 | // allow src to be empty 1076 | src = src || ''; 1077 | if (typeof src === 'string') { 1078 | src = [src]; 1079 | } 1080 | for (var i = 0; i < src.length; i++) { 1081 | var source = document.createElement('source'); 1082 | source.src = src[i]; 1083 | elt.appendChild(source); 1084 | } 1085 | if (typeof callback !== 'undefined') { 1086 | var callbackHandler = function() { 1087 | callback(); 1088 | elt.removeEventListener('canplaythrough', callbackHandler); 1089 | }; 1090 | elt.addEventListener('canplaythrough', callbackHandler); 1091 | } 1092 | 1093 | var c = addElement(elt, pInst, true); 1094 | c.loadedmetadata = false; 1095 | // set width and height onload metadata 1096 | elt.addEventListener('loadedmetadata', function() { 1097 | c.width = elt.videoWidth; 1098 | c.height = elt.videoHeight; 1099 | //c.elt.playbackRate = s; 1100 | // set elt width and height if not set 1101 | if (c.elt.width === 0) c.elt.width = elt.videoWidth; 1102 | if (c.elt.height === 0) c.elt.height = elt.videoHeight; 1103 | if (c.presetPlaybackRate) { 1104 | c.elt.playbackRate = c.presetPlaybackRate; 1105 | delete c.presetPlaybackRate; 1106 | } 1107 | c.loadedmetadata = true; 1108 | }); 1109 | 1110 | return c; 1111 | } 1112 | /** 1113 | * Creates an HTML5 <video> element in the DOM for simple playback 1114 | * of audio/video. Shown by default, can be hidden with .hide() 1115 | * and drawn into canvas using video(). Appends to the container 1116 | * node if one is specified, otherwise appends to body. The first parameter 1117 | * can be either a single string path to a video file, or an array of string 1118 | * paths to different formats of the same video. This is useful for ensuring 1119 | * that your video can play across different browsers, as each supports 1120 | * different formats. See this 1121 | * page for further information about supported formats. 1122 | * 1123 | * @method createVideo 1124 | * @param {String|String[]} src path to a video file, or array of paths for 1125 | * supporting different browsers 1126 | * @param {Function} [callback] callback function to be called upon 1127 | * 'canplaythrough' event fire, that is, when the 1128 | * browser can play the media, and estimates that 1129 | * enough data has been loaded to play the media 1130 | * up to its end without having to stop for 1131 | * further buffering of content 1132 | * @return {p5.MediaElement} pointer to video p5.Element 1133 | * @example 1134 | *
1135 | * var vid; 1136 | * function setup() { 1137 | * noCanvas(); 1138 | * 1139 | * vid = createVideo( 1140 | * ['assets/small.mp4', 'assets/small.ogv', 'assets/small.webm'], 1141 | * vidLoad 1142 | * ); 1143 | * 1144 | * vid.size(100, 100); 1145 | * } 1146 | * 1147 | * // This function is called when the video loads 1148 | * function vidLoad() { 1149 | * vid.loop(); 1150 | * vid.volume(0); 1151 | * } 1152 | *
1153 | */ 1154 | p5.prototype.createVideo = function(src, callback) { 1155 | p5._validateParameters('createVideo', arguments); 1156 | return createMedia(this, 'video', src, callback); 1157 | }; 1158 | 1159 | /** AUDIO STUFF **/ 1160 | 1161 | /** 1162 | * Creates a hidden HTML5 <audio> element in the DOM for simple audio 1163 | * playback. Appends to the container node if one is specified, 1164 | * otherwise appends to body. The first parameter 1165 | * can be either a single string path to a audio file, or an array of string 1166 | * paths to different formats of the same audio. This is useful for ensuring 1167 | * that your audio can play across different browsers, as each supports 1168 | * different formats. See this 1169 | * page for further information about supported formats. 1170 | * 1171 | * @method createAudio 1172 | * @param {String|String[]} [src] path to an audio file, or array of paths 1173 | * for supporting different browsers 1174 | * @param {Function} [callback] callback function to be called upon 1175 | * 'canplaythrough' event fire, that is, when the 1176 | * browser can play the media, and estimates that 1177 | * enough data has been loaded to play the media 1178 | * up to its end without having to stop for 1179 | * further buffering of content 1180 | * @return {p5.MediaElement} pointer to audio p5.Element 1181 | * @example 1182 | *
1183 | * var ele; 1184 | * function setup() { 1185 | * ele = createAudio('assets/beat.mp3'); 1186 | * 1187 | * // here we set the element to autoplay 1188 | * // The element will play as soon 1189 | * // as it is able to do so. 1190 | * ele.autoplay(true); 1191 | * } 1192 | *
1193 | */ 1194 | p5.prototype.createAudio = function(src, callback) { 1195 | p5._validateParameters('createAudio', arguments); 1196 | return createMedia(this, 'audio', src, callback); 1197 | }; 1198 | 1199 | /** CAMERA STUFF **/ 1200 | 1201 | /** 1202 | * @property {String} VIDEO 1203 | * @final 1204 | * @category Constants 1205 | */ 1206 | p5.prototype.VIDEO = 'video'; 1207 | /** 1208 | * @property {String} AUDIO 1209 | * @final 1210 | * @category Constants 1211 | */ 1212 | p5.prototype.AUDIO = 'audio'; 1213 | 1214 | // from: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia 1215 | // Older browsers might not implement mediaDevices at all, so we set an empty object first 1216 | if (navigator.mediaDevices === undefined) { 1217 | navigator.mediaDevices = {}; 1218 | } 1219 | 1220 | // Some browsers partially implement mediaDevices. We can't just assign an object 1221 | // with getUserMedia as it would overwrite existing properties. 1222 | // Here, we will just add the getUserMedia property if it's missing. 1223 | if (navigator.mediaDevices.getUserMedia === undefined) { 1224 | navigator.mediaDevices.getUserMedia = function(constraints) { 1225 | // First get ahold of the legacy getUserMedia, if present 1226 | var getUserMedia = 1227 | navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 1228 | 1229 | // Some browsers just don't implement it - return a rejected promise with an error 1230 | // to keep a consistent interface 1231 | if (!getUserMedia) { 1232 | return Promise.reject( 1233 | new Error('getUserMedia is not implemented in this browser') 1234 | ); 1235 | } 1236 | 1237 | // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise 1238 | return new Promise(function(resolve, reject) { 1239 | getUserMedia.call(navigator, constraints, resolve, reject); 1240 | }); 1241 | }; 1242 | } 1243 | 1244 | /** 1245 | *

Creates a new HTML5 <video> element that contains the audio/video 1246 | * feed from a webcam. The element is separate from the canvas and is 1247 | * displayed by default. The element can be hidden using .hide(). The feed 1248 | * can be drawn onto the canvas using image(). The loadedmetadata property can 1249 | * be used to detect when the element has fully loaded (see second example).

1250 | *

More specific properties of the feed can be passing in a Constraints object. 1251 | * See the 1252 | * W3C 1253 | * spec for possible properties. Note that not all of these are supported 1254 | * by all browsers.

1255 | *

Security note: A new browser security specification requires that getUserMedia, 1256 | * which is behind createCapture(), only works when you're running the code locally, 1257 | * or on HTTPS. Learn more here 1258 | * and here.

1259 | * 1260 | * @method createCapture 1261 | * @param {String|Constant|Object} type type of capture, either VIDEO or 1262 | * AUDIO if none specified, default both, 1263 | * or a Constraints object 1264 | * @param {Function} [callback] function to be called once 1265 | * stream has loaded 1266 | * @return {p5.Element} capture video p5.Element 1267 | * @example 1268 | *
1269 | * var capture; 1270 | * 1271 | * function setup() { 1272 | * createCanvas(480, 480); 1273 | * capture = createCapture(VIDEO); 1274 | * capture.hide(); 1275 | * } 1276 | * 1277 | * function draw() { 1278 | * image(capture, 0, 0, width, width * capture.height / capture.width); 1279 | * filter(INVERT); 1280 | * } 1281 | *
1282 | *
1283 | * function setup() { 1284 | * createCanvas(480, 120); 1285 | * var constraints = { 1286 | * video: { 1287 | * mandatory: { 1288 | * minWidth: 1280, 1289 | * minHeight: 720 1290 | * }, 1291 | * optional: [{ maxFrameRate: 10 }] 1292 | * }, 1293 | * audio: true 1294 | * }; 1295 | * createCapture(constraints, function(stream) { 1296 | * console.log(stream); 1297 | * }); 1298 | * } 1299 | *
1300 | *
1301 | * var capture; 1302 | * 1303 | * function setup() { 1304 | * createCanvas(640, 480); 1305 | * capture = createCapture(VIDEO); 1306 | * } 1307 | * function draw() { 1308 | * background(0); 1309 | * if (capture.loadedmetadata) { 1310 | * var c = capture.get(0, 0, 100, 100); 1311 | * image(c, 0, 0); 1312 | * } 1313 | * } 1314 | *
1315 | */ 1316 | p5.prototype.createCapture = function() { 1317 | p5._validateParameters('createCapture', arguments); 1318 | var useVideo = true; 1319 | var useAudio = true; 1320 | var constraints; 1321 | var cb; 1322 | for (var i = 0; i < arguments.length; i++) { 1323 | if (arguments[i] === p5.prototype.VIDEO) { 1324 | useAudio = false; 1325 | } else if (arguments[i] === p5.prototype.AUDIO) { 1326 | useVideo = false; 1327 | } else if (typeof arguments[i] === 'object') { 1328 | constraints = arguments[i]; 1329 | } else if (typeof arguments[i] === 'function') { 1330 | cb = arguments[i]; 1331 | } 1332 | } 1333 | if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { 1334 | var elt = document.createElement('video'); 1335 | // required to work in iOS 11 & up: 1336 | elt.setAttribute('playsinline', ''); 1337 | 1338 | if (!constraints) { 1339 | constraints = { video: useVideo, audio: useAudio }; 1340 | } 1341 | 1342 | navigator.mediaDevices.getUserMedia(constraints).then( 1343 | function(stream) { 1344 | try { 1345 | if ('srcObject' in elt) { 1346 | elt.srcObject = stream; 1347 | } else { 1348 | elt.src = window.URL.createObjectURL(stream); 1349 | } 1350 | } catch (err) { 1351 | elt.src = stream; 1352 | } 1353 | }, 1354 | function(e) { 1355 | console.log(e); 1356 | } 1357 | ); 1358 | } else { 1359 | throw 'getUserMedia not supported in this browser'; 1360 | } 1361 | var c = addElement(elt, this, true); 1362 | c.loadedmetadata = false; 1363 | // set width and height onload metadata 1364 | elt.addEventListener('loadedmetadata', function() { 1365 | elt.play(); 1366 | if (elt.width) { 1367 | c.width = elt.videoWidth = elt.width; 1368 | c.height = elt.videoHeight = elt.height; 1369 | } else { 1370 | c.width = c.elt.width = elt.videoWidth; 1371 | c.height = c.elt.height = elt.videoHeight; 1372 | } 1373 | c.loadedmetadata = true; 1374 | if (cb) { 1375 | cb(elt.srcObject); 1376 | } 1377 | }); 1378 | return c; 1379 | }; 1380 | 1381 | /** 1382 | * Creates element with given tag in the DOM with given content. 1383 | * Appends to the container node if one is specified, otherwise 1384 | * appends to body. 1385 | * 1386 | * @method createElement 1387 | * @param {String} tag tag for the new element 1388 | * @param {String} [content] html content to be inserted into the element 1389 | * @return {p5.Element} pointer to p5.Element holding created node 1390 | * @example 1391 | *
1392 | * createElement('h2', 'im an h2 p5.element!'); 1393 | *
1394 | */ 1395 | p5.prototype.createElement = function(tag, content) { 1396 | p5._validateParameters('createElement', arguments); 1397 | var elt = document.createElement(tag); 1398 | if (typeof content !== 'undefined') { 1399 | elt.innerHTML = content; 1400 | } 1401 | return addElement(elt, this); 1402 | }; 1403 | 1404 | // ============================================================================= 1405 | // p5.Element additions 1406 | // ============================================================================= 1407 | /** 1408 | * 1409 | * Adds specified class to the element. 1410 | * 1411 | * @for p5.Element 1412 | * @method addClass 1413 | * @param {String} class name of class to add 1414 | * @chainable 1415 | * @example 1416 | *
1417 | * var div = createDiv('div'); 1418 | * div.addClass('myClass'); 1419 | *
1420 | */ 1421 | p5.Element.prototype.addClass = function(c) { 1422 | if (this.elt.className) { 1423 | if (!this.hasClass(c)) { 1424 | this.elt.className = this.elt.className + ' ' + c; 1425 | } 1426 | } else { 1427 | this.elt.className = c; 1428 | } 1429 | return this; 1430 | }; 1431 | 1432 | /** 1433 | * 1434 | * Removes specified class from the element. 1435 | * 1436 | * @method removeClass 1437 | * @param {String} class name of class to remove 1438 | * @chainable 1439 | * @example 1440 | *
1441 | * // In this example, a class is set when the div is created 1442 | * // and removed when mouse is pressed. This could link up 1443 | * // with a CSS style rule to toggle style properties. 1444 | * 1445 | * var div; 1446 | * 1447 | * function setup() { 1448 | * div = createDiv('div'); 1449 | * div.addClass('myClass'); 1450 | * } 1451 | * 1452 | * function mousePressed() { 1453 | * div.removeClass('myClass'); 1454 | * } 1455 | *
1456 | */ 1457 | p5.Element.prototype.removeClass = function(c) { 1458 | // Note: Removing a class that does not exist does NOT throw an error in classList.remove method 1459 | this.elt.classList.remove(c); 1460 | return this; 1461 | }; 1462 | 1463 | /** 1464 | * 1465 | * Checks if specified class already set to element 1466 | * 1467 | * @method hasClass 1468 | * @returns {boolean} a boolean value if element has specified class 1469 | * @param c {String} class name of class to check 1470 | * @example 1471 | *
1472 | * var div; 1473 | * 1474 | * function setup() { 1475 | * div = createDiv('div'); 1476 | * div.addClass('show'); 1477 | * } 1478 | * 1479 | * function mousePressed() { 1480 | * if (div.hasClass('show')) { 1481 | * div.addClass('show'); 1482 | * } else { 1483 | * div.removeClass('show'); 1484 | * } 1485 | * } 1486 | *
1487 | */ 1488 | p5.Element.prototype.hasClass = function(c) { 1489 | return this.elt.classList.contains(c); 1490 | }; 1491 | 1492 | /** 1493 | * 1494 | * Toggles element class 1495 | * 1496 | * @method toggleClass 1497 | * @param c {String} class name to toggle 1498 | * @chainable 1499 | * @example 1500 | *
1501 | * var div; 1502 | * 1503 | * function setup() { 1504 | * div = createDiv('div'); 1505 | * div.addClass('show'); 1506 | * } 1507 | * 1508 | * function mousePressed() { 1509 | * div.toggleClass('show'); 1510 | * } 1511 | *
1512 | */ 1513 | p5.Element.prototype.toggleClass = function(c) { 1514 | // classList also has a toggle() method, but we cannot use that yet as support is unclear. 1515 | // See https://github.com/processing/p5.js/issues/3631 1516 | // this.elt.classList.toggle(c); 1517 | if (this.elt.classList.contains(c)) { 1518 | this.elt.classList.remove(c); 1519 | } else { 1520 | this.elt.classList.add(c); 1521 | } 1522 | return this; 1523 | }; 1524 | 1525 | /** 1526 | * 1527 | * Attaches the element as a child to the parent specified. 1528 | * Accepts either a string ID, DOM node, or p5.Element. 1529 | * If no argument is specified, an array of children DOM nodes is returned. 1530 | * 1531 | * @method child 1532 | * @returns {Node[]} an array of child nodes 1533 | * @example 1534 | *
1535 | * var div0 = createDiv('this is the parent'); 1536 | * var div1 = createDiv('this is the child'); 1537 | * div0.child(div1); // use p5.Element 1538 | *
1539 | *
1540 | * var div0 = createDiv('this is the parent'); 1541 | * var div1 = createDiv('this is the child'); 1542 | * div1.id('apples'); 1543 | * div0.child('apples'); // use id 1544 | *
1545 | *
1546 | * // this example assumes there is a div already on the page 1547 | * // with id "myChildDiv" 1548 | * var div0 = createDiv('this is the parent'); 1549 | * var elt = document.getElementById('myChildDiv'); 1550 | * div0.child(elt); // use element from page 1551 | *
1552 | */ 1553 | /** 1554 | * @method child 1555 | * @param {String|p5.Element} [child] the ID, DOM node, or p5.Element 1556 | * to add to the current element 1557 | * @chainable 1558 | */ 1559 | p5.Element.prototype.child = function(c) { 1560 | if (typeof c === 'undefined') { 1561 | return this.elt.childNodes; 1562 | } 1563 | if (typeof c === 'string') { 1564 | if (c[0] === '#') { 1565 | c = c.substring(1); 1566 | } 1567 | c = document.getElementById(c); 1568 | } else if (c instanceof p5.Element) { 1569 | c = c.elt; 1570 | } 1571 | this.elt.appendChild(c); 1572 | return this; 1573 | }; 1574 | 1575 | /** 1576 | * Centers a p5 Element either vertically, horizontally, 1577 | * or both, relative to its parent or according to 1578 | * the body if the Element has no parent. If no argument is passed 1579 | * the Element is aligned both vertically and horizontally. 1580 | * 1581 | * @method center 1582 | * @param {String} [align] passing 'vertical', 'horizontal' aligns element accordingly 1583 | * @chainable 1584 | * 1585 | * @example 1586 | *
1587 | * function setup() { 1588 | * var div = createDiv('').size(10, 10); 1589 | * div.style('background-color', 'orange'); 1590 | * div.center(); 1591 | * } 1592 | *
1593 | */ 1594 | p5.Element.prototype.center = function(align) { 1595 | var style = this.elt.style.display; 1596 | var hidden = this.elt.style.display === 'none'; 1597 | var parentHidden = this.parent().style.display === 'none'; 1598 | var pos = { x: this.elt.offsetLeft, y: this.elt.offsetTop }; 1599 | 1600 | if (hidden) this.show(); 1601 | 1602 | this.elt.style.display = 'block'; 1603 | this.position(0, 0); 1604 | 1605 | if (parentHidden) this.parent().style.display = 'block'; 1606 | 1607 | var wOffset = Math.abs(this.parent().offsetWidth - this.elt.offsetWidth); 1608 | var hOffset = Math.abs(this.parent().offsetHeight - this.elt.offsetHeight); 1609 | var y = pos.y; 1610 | var x = pos.x; 1611 | 1612 | if (align === 'both' || align === undefined) { 1613 | this.position(wOffset / 2, hOffset / 2); 1614 | } else if (align === 'horizontal') { 1615 | this.position(wOffset / 2, y); 1616 | } else if (align === 'vertical') { 1617 | this.position(x, hOffset / 2); 1618 | } 1619 | 1620 | this.style('display', style); 1621 | 1622 | if (hidden) this.hide(); 1623 | 1624 | if (parentHidden) this.parent().style.display = 'none'; 1625 | 1626 | return this; 1627 | }; 1628 | 1629 | /** 1630 | * 1631 | * If an argument is given, sets the inner HTML of the element, 1632 | * replacing any existing html. If true is included as a second 1633 | * argument, html is appended instead of replacing existing html. 1634 | * If no arguments are given, returns 1635 | * the inner HTML of the element. 1636 | * 1637 | * @for p5.Element 1638 | * @method html 1639 | * @returns {String} the inner HTML of the element 1640 | * @example 1641 | *
1642 | * var div = createDiv('').size(100, 100); 1643 | * div.html('hi'); 1644 | *
1645 | *
1646 | * var div = createDiv('Hello ').size(100, 100); 1647 | * div.html('World', true); 1648 | *
1649 | */ 1650 | /** 1651 | * @method html 1652 | * @param {String} [html] the HTML to be placed inside the element 1653 | * @param {boolean} [append] whether to append HTML to existing 1654 | * @chainable 1655 | */ 1656 | p5.Element.prototype.html = function() { 1657 | if (arguments.length === 0) { 1658 | return this.elt.innerHTML; 1659 | } else if (arguments[1]) { 1660 | this.elt.innerHTML += arguments[0]; 1661 | return this; 1662 | } else { 1663 | this.elt.innerHTML = arguments[0]; 1664 | return this; 1665 | } 1666 | }; 1667 | 1668 | /** 1669 | * 1670 | * Sets the position of the element relative to (0, 0) of the 1671 | * window. Essentially, sets position:absolute and left and top 1672 | * properties of style. If no arguments given returns the x and y position 1673 | * of the element in an object. 1674 | * 1675 | * @method position 1676 | * @returns {Object} the x and y position of the element in an object 1677 | * @example 1678 | *
1679 | * function setup() { 1680 | * var cnv = createCanvas(100, 100); 1681 | * // positions canvas 50px to the right and 100px 1682 | * // below upper left corner of the window 1683 | * cnv.position(50, 100); 1684 | * } 1685 | *
1686 | */ 1687 | /** 1688 | * @method position 1689 | * @param {Number} [x] x-position relative to upper left of window 1690 | * @param {Number} [y] y-position relative to upper left of window 1691 | * @chainable 1692 | */ 1693 | p5.Element.prototype.position = function() { 1694 | if (arguments.length === 0) { 1695 | return { x: this.elt.offsetLeft, y: this.elt.offsetTop }; 1696 | } else { 1697 | this.elt.style.position = 'absolute'; 1698 | this.elt.style.left = arguments[0] + 'px'; 1699 | this.elt.style.top = arguments[1] + 'px'; 1700 | this.x = arguments[0]; 1701 | this.y = arguments[1]; 1702 | return this; 1703 | } 1704 | }; 1705 | 1706 | /* Helper method called by p5.Element.style() */ 1707 | p5.Element.prototype._translate = function() { 1708 | this.elt.style.position = 'absolute'; 1709 | // save out initial non-translate transform styling 1710 | var transform = ''; 1711 | if (this.elt.style.transform) { 1712 | transform = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); 1713 | transform = transform.replace(/translate[X-Z]?\(.*\)/g, ''); 1714 | } 1715 | if (arguments.length === 2) { 1716 | this.elt.style.transform = 1717 | 'translate(' + arguments[0] + 'px, ' + arguments[1] + 'px)'; 1718 | } else if (arguments.length > 2) { 1719 | this.elt.style.transform = 1720 | 'translate3d(' + 1721 | arguments[0] + 1722 | 'px,' + 1723 | arguments[1] + 1724 | 'px,' + 1725 | arguments[2] + 1726 | 'px)'; 1727 | if (arguments.length === 3) { 1728 | this.elt.parentElement.style.perspective = '1000px'; 1729 | } else { 1730 | this.elt.parentElement.style.perspective = arguments[3] + 'px'; 1731 | } 1732 | } 1733 | // add any extra transform styling back on end 1734 | this.elt.style.transform += transform; 1735 | return this; 1736 | }; 1737 | 1738 | /* Helper method called by p5.Element.style() */ 1739 | p5.Element.prototype._rotate = function() { 1740 | // save out initial non-rotate transform styling 1741 | var transform = ''; 1742 | if (this.elt.style.transform) { 1743 | transform = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); 1744 | transform = transform.replace(/rotate[X-Z]?\(.*\)/g, ''); 1745 | } 1746 | 1747 | if (arguments.length === 1) { 1748 | this.elt.style.transform = 'rotate(' + arguments[0] + 'deg)'; 1749 | } else if (arguments.length === 2) { 1750 | this.elt.style.transform = 1751 | 'rotate(' + arguments[0] + 'deg, ' + arguments[1] + 'deg)'; 1752 | } else if (arguments.length === 3) { 1753 | this.elt.style.transform = 'rotateX(' + arguments[0] + 'deg)'; 1754 | this.elt.style.transform += 'rotateY(' + arguments[1] + 'deg)'; 1755 | this.elt.style.transform += 'rotateZ(' + arguments[2] + 'deg)'; 1756 | } 1757 | // add remaining transform back on 1758 | this.elt.style.transform += transform; 1759 | return this; 1760 | }; 1761 | 1762 | /** 1763 | * Sets the given style (css) property (1st arg) of the element with the 1764 | * given value (2nd arg). If a single argument is given, .style() 1765 | * returns the value of the given property; however, if the single argument 1766 | * is given in css syntax ('text-align:center'), .style() sets the css 1767 | * appropriately. 1768 | * 1769 | * @method style 1770 | * @param {String} property property to be set 1771 | * @returns {String} value of property 1772 | * @example 1773 | *
1774 | * var myDiv = createDiv('I like pandas.'); 1775 | * myDiv.style('font-size', '18px'); 1776 | * myDiv.style('color', '#ff0000'); 1777 | *
1778 | *
1779 | * var col = color(25, 23, 200, 50); 1780 | * var button = createButton('button'); 1781 | * button.style('background-color', col); 1782 | * button.position(10, 10); 1783 | *
1784 | *
1785 | * var myDiv; 1786 | * function setup() { 1787 | * background(200); 1788 | * myDiv = createDiv('I like gray.'); 1789 | * myDiv.position(20, 20); 1790 | * } 1791 | * 1792 | * function draw() { 1793 | * myDiv.style('font-size', mouseX + 'px'); 1794 | * } 1795 | *
1796 | */ 1797 | /** 1798 | * @method style 1799 | * @param {String} property 1800 | * @param {String|Number|p5.Color} value value to assign to property 1801 | * @return {String} current value of property, if no value is given as second argument 1802 | * @chainable 1803 | */ 1804 | p5.Element.prototype.style = function(prop, val) { 1805 | var self = this; 1806 | 1807 | if (val instanceof p5.Color) { 1808 | val = 1809 | 'rgba(' + 1810 | val.levels[0] + 1811 | ',' + 1812 | val.levels[1] + 1813 | ',' + 1814 | val.levels[2] + 1815 | ',' + 1816 | val.levels[3] / 255 + 1817 | ')'; 1818 | } 1819 | 1820 | if (typeof val === 'undefined') { 1821 | // input provided as single line string 1822 | if (prop.indexOf(':') === -1) { 1823 | var styles = window.getComputedStyle(self.elt); 1824 | var style = styles.getPropertyValue(prop); 1825 | return style; 1826 | } else { 1827 | var attrs = prop.split(';'); 1828 | for (var i = 0; i < attrs.length; i++) { 1829 | var parts = attrs[i].split(':'); 1830 | if (parts[0] && parts[1]) { 1831 | this.elt.style[parts[0].trim()] = parts[1].trim(); 1832 | } 1833 | } 1834 | } 1835 | } else { 1836 | // input provided as key,val pair 1837 | this.elt.style[prop] = val; 1838 | if ( 1839 | prop === 'width' || 1840 | prop === 'height' || 1841 | prop === 'left' || 1842 | prop === 'top' 1843 | ) { 1844 | var numVal = val.replace(/\D+/g, ''); 1845 | this[prop] = parseInt(numVal, 10); 1846 | } 1847 | } 1848 | return this; 1849 | }; 1850 | 1851 | /** 1852 | * 1853 | * Adds a new attribute or changes the value of an existing attribute 1854 | * on the specified element. If no value is specified, returns the 1855 | * value of the given attribute, or null if attribute is not set. 1856 | * 1857 | * @method attribute 1858 | * @return {String} value of attribute 1859 | * 1860 | * @example 1861 | *
1862 | * var myDiv = createDiv('I like pandas.'); 1863 | * myDiv.attribute('align', 'center'); 1864 | *
1865 | */ 1866 | /** 1867 | * @method attribute 1868 | * @param {String} attr attribute to set 1869 | * @param {String} value value to assign to attribute 1870 | * @chainable 1871 | */ 1872 | p5.Element.prototype.attribute = function(attr, value) { 1873 | //handling for checkboxes and radios to ensure options get 1874 | //attributes not divs 1875 | if ( 1876 | this.elt.firstChild != null && 1877 | (this.elt.firstChild.type === 'checkbox' || 1878 | this.elt.firstChild.type === 'radio') 1879 | ) { 1880 | if (typeof value === 'undefined') { 1881 | return this.elt.firstChild.getAttribute(attr); 1882 | } else { 1883 | for (var i = 0; i < this.elt.childNodes.length; i++) { 1884 | this.elt.childNodes[i].setAttribute(attr, value); 1885 | } 1886 | } 1887 | } else if (typeof value === 'undefined') { 1888 | return this.elt.getAttribute(attr); 1889 | } else { 1890 | this.elt.setAttribute(attr, value); 1891 | return this; 1892 | } 1893 | }; 1894 | 1895 | /** 1896 | * 1897 | * Removes an attribute on the specified element. 1898 | * 1899 | * @method removeAttribute 1900 | * @param {String} attr attribute to remove 1901 | * @chainable 1902 | * 1903 | * @example 1904 | *
1905 | * var button; 1906 | * var checkbox; 1907 | * 1908 | * function setup() { 1909 | * checkbox = createCheckbox('enable', true); 1910 | * checkbox.changed(enableButton); 1911 | * button = createButton('button'); 1912 | * button.position(10, 10); 1913 | * } 1914 | * 1915 | * function enableButton() { 1916 | * if (this.checked()) { 1917 | * // Re-enable the button 1918 | * button.removeAttribute('disabled'); 1919 | * } else { 1920 | * // Disable the button 1921 | * button.attribute('disabled', ''); 1922 | * } 1923 | * } 1924 | *
1925 | */ 1926 | p5.Element.prototype.removeAttribute = function(attr) { 1927 | if ( 1928 | this.elt.firstChild != null && 1929 | (this.elt.firstChild.type === 'checkbox' || 1930 | this.elt.firstChild.type === 'radio') 1931 | ) { 1932 | for (var i = 0; i < this.elt.childNodes.length; i++) { 1933 | this.elt.childNodes[i].removeAttribute(attr); 1934 | } 1935 | } 1936 | this.elt.removeAttribute(attr); 1937 | return this; 1938 | }; 1939 | 1940 | /** 1941 | * Either returns the value of the element if no arguments 1942 | * given, or sets the value of the element. 1943 | * 1944 | * @method value 1945 | * @return {String|Number} value of the element 1946 | * @example 1947 | *
1948 | * // gets the value 1949 | * var inp; 1950 | * function setup() { 1951 | * inp = createInput(''); 1952 | * } 1953 | * 1954 | * function mousePressed() { 1955 | * print(inp.value()); 1956 | * } 1957 | *
1958 | *
1959 | * // sets the value 1960 | * var inp; 1961 | * function setup() { 1962 | * inp = createInput('myValue'); 1963 | * } 1964 | * 1965 | * function mousePressed() { 1966 | * inp.value('myValue'); 1967 | * } 1968 | *
1969 | */ 1970 | /** 1971 | * @method value 1972 | * @param {String|Number} value 1973 | * @chainable 1974 | */ 1975 | p5.Element.prototype.value = function() { 1976 | if (arguments.length > 0) { 1977 | this.elt.value = arguments[0]; 1978 | return this; 1979 | } else { 1980 | if (this.elt.type === 'range') { 1981 | return parseFloat(this.elt.value); 1982 | } else return this.elt.value; 1983 | } 1984 | }; 1985 | 1986 | /** 1987 | * 1988 | * Shows the current element. Essentially, setting display:block for the style. 1989 | * 1990 | * @method show 1991 | * @chainable 1992 | * @example 1993 | *
1994 | * var div = createDiv('div'); 1995 | * div.style('display', 'none'); 1996 | * div.show(); // turns display to block 1997 | *
1998 | */ 1999 | p5.Element.prototype.show = function() { 2000 | this.elt.style.display = 'block'; 2001 | return this; 2002 | }; 2003 | 2004 | /** 2005 | * Hides the current element. Essentially, setting display:none for the style. 2006 | * 2007 | * @method hide 2008 | * @chainable 2009 | * @example 2010 | *
2011 | * var div = createDiv('this is a div'); 2012 | * div.hide(); 2013 | *
2014 | */ 2015 | p5.Element.prototype.hide = function() { 2016 | this.elt.style.display = 'none'; 2017 | return this; 2018 | }; 2019 | 2020 | /** 2021 | * 2022 | * Sets the width and height of the element. AUTO can be used to 2023 | * only adjust one dimension at a time. If no arguments are given, it 2024 | * returns the width and height of the element in an object. In case of 2025 | * elements which need to be loaded, such as images, it is recommended 2026 | * to call the function after the element has finished loading. 2027 | * 2028 | * @method size 2029 | * @return {Object} the width and height of the element in an object 2030 | * @example 2031 | *
2032 | * let div = createDiv('this is a div'); 2033 | * div.size(100, 100); 2034 | * let img = createImg('assets/laDefense.jpg', () => { 2035 | * img.size(10, AUTO); 2036 | * }); 2037 | *
2038 | */ 2039 | /** 2040 | * @method size 2041 | * @param {Number|Constant} w width of the element, either AUTO, or a number 2042 | * @param {Number|Constant} [h] height of the element, either AUTO, or a number 2043 | * @chainable 2044 | */ 2045 | p5.Element.prototype.size = function(w, h) { 2046 | if (arguments.length === 0) { 2047 | return { width: this.elt.offsetWidth, height: this.elt.offsetHeight }; 2048 | } else { 2049 | var aW = w; 2050 | var aH = h; 2051 | var AUTO = p5.prototype.AUTO; 2052 | if (aW !== AUTO || aH !== AUTO) { 2053 | if (aW === AUTO) { 2054 | aW = h * this.width / this.height; 2055 | } else if (aH === AUTO) { 2056 | aH = w * this.height / this.width; 2057 | } 2058 | // set diff for cnv vs normal div 2059 | if (this.elt instanceof HTMLCanvasElement) { 2060 | var j = {}; 2061 | var k = this.elt.getContext('2d'); 2062 | var prop; 2063 | for (prop in k) { 2064 | j[prop] = k[prop]; 2065 | } 2066 | this.elt.setAttribute('width', aW * this._pInst._pixelDensity); 2067 | this.elt.setAttribute('height', aH * this._pInst._pixelDensity); 2068 | this.elt.style.width = aW + 'px'; 2069 | this.elt.style.height = aH + 'px'; 2070 | this._pInst.scale( 2071 | this._pInst._pixelDensity, 2072 | this._pInst._pixelDensity 2073 | ); 2074 | for (prop in j) { 2075 | this.elt.getContext('2d')[prop] = j[prop]; 2076 | } 2077 | } else { 2078 | this.elt.style.width = aW + 'px'; 2079 | this.elt.style.height = aH + 'px'; 2080 | this.elt.width = aW; 2081 | this.elt.height = aH; 2082 | } 2083 | 2084 | this.width = this.elt.offsetWidth; 2085 | this.height = this.elt.offsetHeight; 2086 | 2087 | if (this._pInst && this._pInst._curElement) { 2088 | // main canvas associated with p5 instance 2089 | if (this._pInst._curElement.elt === this.elt) { 2090 | this._pInst._setProperty('width', this.elt.offsetWidth); 2091 | this._pInst._setProperty('height', this.elt.offsetHeight); 2092 | } 2093 | } 2094 | } 2095 | return this; 2096 | } 2097 | }; 2098 | 2099 | /** 2100 | * Removes the element and deregisters all listeners. 2101 | * @method remove 2102 | * @example 2103 | *
2104 | * var myDiv = createDiv('this is some text'); 2105 | * myDiv.remove(); 2106 | *
2107 | */ 2108 | p5.Element.prototype.remove = function() { 2109 | // deregister events 2110 | for (var ev in this._events) { 2111 | this.elt.removeEventListener(ev, this._events[ev]); 2112 | } 2113 | if (this.elt.parentNode) { 2114 | this.elt.parentNode.removeChild(this.elt); 2115 | } 2116 | delete this; 2117 | }; 2118 | 2119 | /** 2120 | * Registers a callback that gets called every time a file that is 2121 | * dropped on the element has been loaded. 2122 | * p5 will load every dropped file into memory and pass it as a p5.File object to the callback. 2123 | * Multiple files dropped at the same time will result in multiple calls to the callback. 2124 | * 2125 | * You can optionally pass a second callback which will be registered to the raw 2126 | * drop event. 2127 | * The callback will thus be provided the original 2128 | * DragEvent. 2129 | * Dropping multiple files at the same time will trigger the second callback once per drop, 2130 | * whereas the first callback will trigger for each loaded file. 2131 | * 2132 | * @method drop 2133 | * @param {Function} callback callback to receive loaded file, called for each file dropped. 2134 | * @param {Function} [fxn] callback triggered once when files are dropped with the drop event. 2135 | * @chainable 2136 | * @example 2137 | *
2138 | * function setup() { 2139 | * var c = createCanvas(100, 100); 2140 | * background(200); 2141 | * textAlign(CENTER); 2142 | * text('drop file', width / 2, height / 2); 2143 | * c.drop(gotFile); 2144 | * } 2145 | * 2146 | * function gotFile(file) { 2147 | * background(200); 2148 | * text('received file:', width / 2, height / 2); 2149 | * text(file.name, width / 2, height / 2 + 50); 2150 | * } 2151 | *
2152 | * 2153 | *
2154 | * var img; 2155 | * 2156 | * function setup() { 2157 | * var c = createCanvas(100, 100); 2158 | * background(200); 2159 | * textAlign(CENTER); 2160 | * text('drop image', width / 2, height / 2); 2161 | * c.drop(gotFile); 2162 | * } 2163 | * 2164 | * function draw() { 2165 | * if (img) { 2166 | * image(img, 0, 0, width, height); 2167 | * } 2168 | * } 2169 | * 2170 | * function gotFile(file) { 2171 | * img = createImg(file.data).hide(); 2172 | * } 2173 | *
2174 | * 2175 | * @alt 2176 | * Canvas turns into whatever image is dragged/dropped onto it. 2177 | */ 2178 | p5.Element.prototype.drop = function(callback, fxn) { 2179 | // Is the file stuff supported? 2180 | if (window.File && window.FileReader && window.FileList && window.Blob) { 2181 | if (!this._dragDisabled) { 2182 | this._dragDisabled = true; 2183 | 2184 | var preventDefault = function(evt) { 2185 | evt.preventDefault(); 2186 | }; 2187 | 2188 | // If you want to be able to drop you've got to turn off 2189 | // a lot of default behavior. 2190 | // avoid `attachListener` here, since it overrides other handlers. 2191 | this.elt.addEventListener('dragover', preventDefault); 2192 | 2193 | // If this is a drag area we need to turn off the default behavior 2194 | this.elt.addEventListener('dragleave', preventDefault); 2195 | } 2196 | 2197 | // Deal with the files 2198 | p5.Element._attachListener( 2199 | 'drop', 2200 | function(evt) { 2201 | evt.preventDefault(); 2202 | // Call the second argument as a callback that receives the raw drop event 2203 | if (typeof fxn === 'function') { 2204 | fxn.call(this, evt); 2205 | } 2206 | // A FileList 2207 | var files = evt.dataTransfer.files; 2208 | 2209 | // Load each one and trigger the callback 2210 | for (var i = 0; i < files.length; i++) { 2211 | var f = files[i]; 2212 | p5.File._load(f, callback); 2213 | } 2214 | }, 2215 | this 2216 | ); 2217 | } else { 2218 | console.log('The File APIs are not fully supported in this browser.'); 2219 | } 2220 | 2221 | return this; 2222 | }; 2223 | 2224 | // ============================================================================= 2225 | // p5.MediaElement additions 2226 | // ============================================================================= 2227 | 2228 | /** 2229 | * Extends p5.Element to handle audio and video. In addition to the methods 2230 | * of p5.Element, it also contains methods for controlling media. It is not 2231 | * called directly, but p5.MediaElements are created by calling createVideo, 2232 | * createAudio, and createCapture. 2233 | * 2234 | * @class p5.MediaElement 2235 | * @constructor 2236 | * @param {String} elt DOM node that is wrapped 2237 | */ 2238 | p5.MediaElement = function(elt, pInst) { 2239 | p5.Element.call(this, elt, pInst); 2240 | 2241 | var self = this; 2242 | this.elt.crossOrigin = 'anonymous'; 2243 | 2244 | this._prevTime = 0; 2245 | this._cueIDCounter = 0; 2246 | this._cues = []; 2247 | this._pixelsState = this; 2248 | this._pixelDensity = 1; 2249 | this._modified = false; 2250 | this._pixelsDirty = true; 2251 | this._pixelsTime = -1; // the time at which we last updated 'pixels' 2252 | 2253 | /** 2254 | * Path to the media element source. 2255 | * 2256 | * @property src 2257 | * @return {String} src 2258 | * @example 2259 | *
2260 | * var ele; 2261 | * 2262 | * function setup() { 2263 | * background(250); 2264 | * 2265 | * //p5.MediaElement objects are usually created 2266 | * //by calling the createAudio(), createVideo(), 2267 | * //and createCapture() functions. 2268 | * 2269 | * //In this example we create 2270 | * //a new p5.MediaElement via createAudio(). 2271 | * ele = createAudio('assets/beat.mp3'); 2272 | * 2273 | * //We'll set up our example so that 2274 | * //when you click on the text, 2275 | * //an alert box displays the MediaElement's 2276 | * //src field. 2277 | * textAlign(CENTER); 2278 | * text('Click Me!', width / 2, height / 2); 2279 | * } 2280 | * 2281 | * function mouseClicked() { 2282 | * //here we test if the mouse is over the 2283 | * //canvas element when it's clicked 2284 | * if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) { 2285 | * //Show our p5.MediaElement's src field 2286 | * alert(ele.src); 2287 | * } 2288 | * } 2289 | *
2290 | */ 2291 | Object.defineProperty(self, 'src', { 2292 | get: function() { 2293 | var firstChildSrc = self.elt.children[0].src; 2294 | var srcVal = self.elt.src === window.location.href ? '' : self.elt.src; 2295 | var ret = 2296 | firstChildSrc === window.location.href ? srcVal : firstChildSrc; 2297 | return ret; 2298 | }, 2299 | set: function(newValue) { 2300 | for (var i = 0; i < self.elt.children.length; i++) { 2301 | self.elt.removeChild(self.elt.children[i]); 2302 | } 2303 | var source = document.createElement('source'); 2304 | source.src = newValue; 2305 | elt.appendChild(source); 2306 | self.elt.src = newValue; 2307 | self.modified = true; 2308 | } 2309 | }); 2310 | 2311 | // private _onended callback, set by the method: onended(callback) 2312 | self._onended = function() {}; 2313 | self.elt.onended = function() { 2314 | self._onended(self); 2315 | }; 2316 | }; 2317 | p5.MediaElement.prototype = Object.create(p5.Element.prototype); 2318 | 2319 | /** 2320 | * Play an HTML5 media element. 2321 | * 2322 | * @method play 2323 | * @chainable 2324 | * @example 2325 | *
2326 | * var ele; 2327 | * 2328 | * function setup() { 2329 | * //p5.MediaElement objects are usually created 2330 | * //by calling the createAudio(), createVideo(), 2331 | * //and createCapture() functions. 2332 | * 2333 | * //In this example we create 2334 | * //a new p5.MediaElement via createAudio(). 2335 | * ele = createAudio('assets/beat.mp3'); 2336 | * 2337 | * background(250); 2338 | * textAlign(CENTER); 2339 | * text('Click to Play!', width / 2, height / 2); 2340 | * } 2341 | * 2342 | * function mouseClicked() { 2343 | * //here we test if the mouse is over the 2344 | * //canvas element when it's clicked 2345 | * if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) { 2346 | * //Here we call the play() function on 2347 | * //the p5.MediaElement we created above. 2348 | * //This will start the audio sample. 2349 | * ele.play(); 2350 | * 2351 | * background(200); 2352 | * text('You clicked Play!', width / 2, height / 2); 2353 | * } 2354 | * } 2355 | *
2356 | */ 2357 | p5.MediaElement.prototype.play = function() { 2358 | if (this.elt.currentTime === this.elt.duration) { 2359 | this.elt.currentTime = 0; 2360 | } 2361 | var promise; 2362 | if (this.elt.readyState > 1) { 2363 | promise = this.elt.play(); 2364 | } else { 2365 | // in Chrome, playback cannot resume after being stopped and must reload 2366 | this.elt.load(); 2367 | promise = this.elt.play(); 2368 | } 2369 | if (promise && promise.catch) { 2370 | promise.catch(function(e) { 2371 | console.log( 2372 | 'WARN: Element play method raised an error asynchronously', 2373 | e 2374 | ); 2375 | }); 2376 | } 2377 | return this; 2378 | }; 2379 | 2380 | /** 2381 | * Stops an HTML5 media element (sets current time to zero). 2382 | * 2383 | * @method stop 2384 | * @chainable 2385 | * @example 2386 | *
2387 | * //This example both starts 2388 | * //and stops a sound sample 2389 | * //when the user clicks the canvas 2390 | * 2391 | * //We will store the p5.MediaElement 2392 | * //object in here 2393 | * var ele; 2394 | * 2395 | * //while our audio is playing, 2396 | * //this will be set to true 2397 | * var sampleIsPlaying = false; 2398 | * 2399 | * function setup() { 2400 | * //Here we create a p5.MediaElement object 2401 | * //using the createAudio() function. 2402 | * ele = createAudio('assets/beat.mp3'); 2403 | * background(200); 2404 | * textAlign(CENTER); 2405 | * text('Click to play!', width / 2, height / 2); 2406 | * } 2407 | * 2408 | * function mouseClicked() { 2409 | * //here we test if the mouse is over the 2410 | * //canvas element when it's clicked 2411 | * if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) { 2412 | * background(200); 2413 | * 2414 | * if (sampleIsPlaying) { 2415 | * //if the sample is currently playing 2416 | * //calling the stop() function on 2417 | * //our p5.MediaElement will stop 2418 | * //it and reset its current 2419 | * //time to 0 (i.e. it will start 2420 | * //at the beginning the next time 2421 | * //you play it) 2422 | * ele.stop(); 2423 | * 2424 | * sampleIsPlaying = false; 2425 | * text('Click to play!', width / 2, height / 2); 2426 | * } else { 2427 | * //loop our sound element until we 2428 | * //call ele.stop() on it. 2429 | * ele.loop(); 2430 | * 2431 | * sampleIsPlaying = true; 2432 | * text('Click to stop!', width / 2, height / 2); 2433 | * } 2434 | * } 2435 | * } 2436 | *
2437 | */ 2438 | p5.MediaElement.prototype.stop = function() { 2439 | this.elt.pause(); 2440 | this.elt.currentTime = 0; 2441 | return this; 2442 | }; 2443 | 2444 | /** 2445 | * Pauses an HTML5 media element. 2446 | * 2447 | * @method pause 2448 | * @chainable 2449 | * @example 2450 | *
2451 | * //This example both starts 2452 | * //and pauses a sound sample 2453 | * //when the user clicks the canvas 2454 | * 2455 | * //We will store the p5.MediaElement 2456 | * //object in here 2457 | * var ele; 2458 | * 2459 | * //while our audio is playing, 2460 | * //this will be set to true 2461 | * var sampleIsPlaying = false; 2462 | * 2463 | * function setup() { 2464 | * //Here we create a p5.MediaElement object 2465 | * //using the createAudio() function. 2466 | * ele = createAudio('assets/lucky_dragons.mp3'); 2467 | * background(200); 2468 | * textAlign(CENTER); 2469 | * text('Click to play!', width / 2, height / 2); 2470 | * } 2471 | * 2472 | * function mouseClicked() { 2473 | * //here we test if the mouse is over the 2474 | * //canvas element when it's clicked 2475 | * if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) { 2476 | * background(200); 2477 | * 2478 | * if (sampleIsPlaying) { 2479 | * //Calling pause() on our 2480 | * //p5.MediaElement will stop it 2481 | * //playing, but when we call the 2482 | * //loop() or play() functions 2483 | * //the sample will start from 2484 | * //where we paused it. 2485 | * ele.pause(); 2486 | * 2487 | * sampleIsPlaying = false; 2488 | * text('Click to resume!', width / 2, height / 2); 2489 | * } else { 2490 | * //loop our sound element until we 2491 | * //call ele.pause() on it. 2492 | * ele.loop(); 2493 | * 2494 | * sampleIsPlaying = true; 2495 | * text('Click to pause!', width / 2, height / 2); 2496 | * } 2497 | * } 2498 | * } 2499 | *
2500 | */ 2501 | p5.MediaElement.prototype.pause = function() { 2502 | this.elt.pause(); 2503 | return this; 2504 | }; 2505 | 2506 | /** 2507 | * Set 'loop' to true for an HTML5 media element, and starts playing. 2508 | * 2509 | * @method loop 2510 | * @chainable 2511 | * @example 2512 | *
2513 | * //Clicking the canvas will loop 2514 | * //the audio sample until the user 2515 | * //clicks again to stop it 2516 | * 2517 | * //We will store the p5.MediaElement 2518 | * //object in here 2519 | * var ele; 2520 | * 2521 | * //while our audio is playing, 2522 | * //this will be set to true 2523 | * var sampleIsLooping = false; 2524 | * 2525 | * function setup() { 2526 | * //Here we create a p5.MediaElement object 2527 | * //using the createAudio() function. 2528 | * ele = createAudio('assets/lucky_dragons.mp3'); 2529 | * background(200); 2530 | * textAlign(CENTER); 2531 | * text('Click to loop!', width / 2, height / 2); 2532 | * } 2533 | * 2534 | * function mouseClicked() { 2535 | * //here we test if the mouse is over the 2536 | * //canvas element when it's clicked 2537 | * if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) { 2538 | * background(200); 2539 | * 2540 | * if (!sampleIsLooping) { 2541 | * //loop our sound element until we 2542 | * //call ele.stop() on it. 2543 | * ele.loop(); 2544 | * 2545 | * sampleIsLooping = true; 2546 | * text('Click to stop!', width / 2, height / 2); 2547 | * } else { 2548 | * ele.stop(); 2549 | * 2550 | * sampleIsLooping = false; 2551 | * text('Click to loop!', width / 2, height / 2); 2552 | * } 2553 | * } 2554 | * } 2555 | *
2556 | */ 2557 | p5.MediaElement.prototype.loop = function() { 2558 | this.elt.setAttribute('loop', true); 2559 | this.play(); 2560 | return this; 2561 | }; 2562 | /** 2563 | * Set 'loop' to false for an HTML5 media element. Element will stop 2564 | * when it reaches the end. 2565 | * 2566 | * @method noLoop 2567 | * @chainable 2568 | * @example 2569 | *
2570 | * //This example both starts 2571 | * //and stops loop of sound sample 2572 | * //when the user clicks the canvas 2573 | * 2574 | * //We will store the p5.MediaElement 2575 | * //object in here 2576 | * var ele; 2577 | * //while our audio is playing, 2578 | * //this will be set to true 2579 | * var sampleIsPlaying = false; 2580 | * 2581 | * function setup() { 2582 | * //Here we create a p5.MediaElement object 2583 | * //using the createAudio() function. 2584 | * ele = createAudio('assets/beat.mp3'); 2585 | * background(200); 2586 | * textAlign(CENTER); 2587 | * text('Click to play!', width / 2, height / 2); 2588 | * } 2589 | * 2590 | * function mouseClicked() { 2591 | * //here we test if the mouse is over the 2592 | * //canvas element when it's clicked 2593 | * if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) { 2594 | * background(200); 2595 | * 2596 | * if (sampleIsPlaying) { 2597 | * ele.noLoop(); 2598 | * text('No more Loops!', width / 2, height / 2); 2599 | * } else { 2600 | * ele.loop(); 2601 | * sampleIsPlaying = true; 2602 | * text('Click to stop looping!', width / 2, height / 2); 2603 | * } 2604 | * } 2605 | * } 2606 | *
2607 | * 2608 | */ 2609 | p5.MediaElement.prototype.noLoop = function() { 2610 | this.elt.setAttribute('loop', false); 2611 | return this; 2612 | }; 2613 | 2614 | /** 2615 | * Set HTML5 media element to autoplay or not. 2616 | * 2617 | * @method autoplay 2618 | * @param {Boolean} autoplay whether the element should autoplay 2619 | * @chainable 2620 | */ 2621 | p5.MediaElement.prototype.autoplay = function(val) { 2622 | this.elt.setAttribute('autoplay', val); 2623 | return this; 2624 | }; 2625 | 2626 | /** 2627 | * Sets volume for this HTML5 media element. If no argument is given, 2628 | * returns the current volume. 2629 | * 2630 | * @method volume 2631 | * @return {Number} current volume 2632 | * 2633 | * @example 2634 | *
2635 | * var ele; 2636 | * function setup() { 2637 | * // p5.MediaElement objects are usually created 2638 | * // by calling the createAudio(), createVideo(), 2639 | * // and createCapture() functions. 2640 | * // In this example we create 2641 | * // a new p5.MediaElement via createAudio(). 2642 | * ele = createAudio('assets/lucky_dragons.mp3'); 2643 | * background(250); 2644 | * textAlign(CENTER); 2645 | * text('Click to Play!', width / 2, height / 2); 2646 | * } 2647 | * function mouseClicked() { 2648 | * // Here we call the volume() function 2649 | * // on the sound element to set its volume 2650 | * // Volume must be between 0.0 and 1.0 2651 | * ele.volume(0.2); 2652 | * ele.play(); 2653 | * background(200); 2654 | * text('You clicked Play!', width / 2, height / 2); 2655 | * } 2656 | *
2657 | *
2658 | * var audio; 2659 | * var counter = 0; 2660 | * 2661 | * function loaded() { 2662 | * audio.play(); 2663 | * } 2664 | * 2665 | * function setup() { 2666 | * audio = createAudio('assets/lucky_dragons.mp3', loaded); 2667 | * textAlign(CENTER); 2668 | * } 2669 | * 2670 | * function draw() { 2671 | * if (counter === 0) { 2672 | * background(0, 255, 0); 2673 | * text('volume(0.9)', width / 2, height / 2); 2674 | * } else if (counter === 1) { 2675 | * background(255, 255, 0); 2676 | * text('volume(0.5)', width / 2, height / 2); 2677 | * } else if (counter === 2) { 2678 | * background(255, 0, 0); 2679 | * text('volume(0.1)', width / 2, height / 2); 2680 | * } 2681 | * } 2682 | * 2683 | * function mousePressed() { 2684 | * counter++; 2685 | * if (counter === 0) { 2686 | * audio.volume(0.9); 2687 | * } else if (counter === 1) { 2688 | * audio.volume(0.5); 2689 | * } else if (counter === 2) { 2690 | * audio.volume(0.1); 2691 | * } else { 2692 | * counter = 0; 2693 | * audio.volume(0.9); 2694 | * } 2695 | * } 2696 | * 2697 | *
2698 | */ 2699 | /** 2700 | * @method volume 2701 | * @param {Number} val volume between 0.0 and 1.0 2702 | * @chainable 2703 | */ 2704 | p5.MediaElement.prototype.volume = function(val) { 2705 | if (typeof val === 'undefined') { 2706 | return this.elt.volume; 2707 | } else { 2708 | this.elt.volume = val; 2709 | } 2710 | }; 2711 | 2712 | /** 2713 | * If no arguments are given, returns the current playback speed of the 2714 | * element. The speed parameter sets the speed where 2.0 will play the 2715 | * element twice as fast, 0.5 will play at half the speed, and -1 will play 2716 | * the element in normal speed in reverse.(Note that not all browsers support 2717 | * backward playback and even if they do, playback might not be smooth.) 2718 | * 2719 | * @method speed 2720 | * @return {Number} current playback speed of the element 2721 | * 2722 | * @example 2723 | *
2724 | * //Clicking the canvas will loop 2725 | * //the audio sample until the user 2726 | * //clicks again to stop it 2727 | * 2728 | * //We will store the p5.MediaElement 2729 | * //object in here 2730 | * var ele; 2731 | * var button; 2732 | * 2733 | * function setup() { 2734 | * createCanvas(710, 400); 2735 | * //Here we create a p5.MediaElement object 2736 | * //using the createAudio() function. 2737 | * ele = createAudio('assets/beat.mp3'); 2738 | * ele.loop(); 2739 | * background(200); 2740 | * 2741 | * button = createButton('2x speed'); 2742 | * button.position(100, 68); 2743 | * button.mousePressed(twice_speed); 2744 | * 2745 | * button = createButton('half speed'); 2746 | * button.position(200, 68); 2747 | * button.mousePressed(half_speed); 2748 | * 2749 | * button = createButton('reverse play'); 2750 | * button.position(300, 68); 2751 | * button.mousePressed(reverse_speed); 2752 | * 2753 | * button = createButton('STOP'); 2754 | * button.position(400, 68); 2755 | * button.mousePressed(stop_song); 2756 | * 2757 | * button = createButton('PLAY!'); 2758 | * button.position(500, 68); 2759 | * button.mousePressed(play_speed); 2760 | * } 2761 | * 2762 | * function twice_speed() { 2763 | * ele.speed(2); 2764 | * } 2765 | * 2766 | * function half_speed() { 2767 | * ele.speed(0.5); 2768 | * } 2769 | * 2770 | * function reverse_speed() { 2771 | * ele.speed(-1); 2772 | * } 2773 | * 2774 | * function stop_song() { 2775 | * ele.stop(); 2776 | * } 2777 | * 2778 | * function play_speed() { 2779 | * ele.play(); 2780 | * } 2781 | *
2782 | */ 2783 | /** 2784 | * @method speed 2785 | * @param {Number} speed speed multiplier for element playback 2786 | * @chainable 2787 | */ 2788 | p5.MediaElement.prototype.speed = function(val) { 2789 | if (typeof val === 'undefined') { 2790 | return this.presetPlaybackRate || this.elt.playbackRate; 2791 | } else { 2792 | if (this.loadedmetadata) { 2793 | this.elt.playbackRate = val; 2794 | } else { 2795 | this.presetPlaybackRate = val; 2796 | } 2797 | } 2798 | }; 2799 | 2800 | /** 2801 | * If no arguments are given, returns the current time of the element. 2802 | * If an argument is given the current time of the element is set to it. 2803 | * 2804 | * @method time 2805 | * @return {Number} current time (in seconds) 2806 | * 2807 | * @example 2808 | *
2809 | * var ele; 2810 | * var beginning = true; 2811 | * function setup() { 2812 | * //p5.MediaElement objects are usually created 2813 | * //by calling the createAudio(), createVideo(), 2814 | * //and createCapture() functions. 2815 | * 2816 | * //In this example we create 2817 | * //a new p5.MediaElement via createAudio(). 2818 | * ele = createAudio('assets/lucky_dragons.mp3'); 2819 | * background(250); 2820 | * textAlign(CENTER); 2821 | * text('start at beginning', width / 2, height / 2); 2822 | * } 2823 | * 2824 | * // this function fires with click anywhere 2825 | * function mousePressed() { 2826 | * if (beginning === true) { 2827 | * // here we start the sound at the beginning 2828 | * // time(0) is not necessary here 2829 | * // as this produces the same result as 2830 | * // play() 2831 | * ele.play().time(0); 2832 | * background(200); 2833 | * text('jump 2 sec in', width / 2, height / 2); 2834 | * beginning = false; 2835 | * } else { 2836 | * // here we jump 2 seconds into the sound 2837 | * ele.play().time(2); 2838 | * background(250); 2839 | * text('start at beginning', width / 2, height / 2); 2840 | * beginning = true; 2841 | * } 2842 | * } 2843 | *
2844 | */ 2845 | /** 2846 | * @method time 2847 | * @param {Number} time time to jump to (in seconds) 2848 | * @chainable 2849 | */ 2850 | p5.MediaElement.prototype.time = function(val) { 2851 | if (typeof val === 'undefined') { 2852 | return this.elt.currentTime; 2853 | } else { 2854 | this.elt.currentTime = val; 2855 | return this; 2856 | } 2857 | }; 2858 | 2859 | /** 2860 | * Returns the duration of the HTML5 media element. 2861 | * 2862 | * @method duration 2863 | * @return {Number} duration 2864 | * 2865 | * @example 2866 | *
2867 | * var ele; 2868 | * function setup() { 2869 | * //p5.MediaElement objects are usually created 2870 | * //by calling the createAudio(), createVideo(), 2871 | * //and createCapture() functions. 2872 | * //In this example we create 2873 | * //a new p5.MediaElement via createAudio(). 2874 | * ele = createAudio('assets/doorbell.mp3'); 2875 | * background(250); 2876 | * textAlign(CENTER); 2877 | * text('Click to know the duration!', 10, 25, 70, 80); 2878 | * } 2879 | * function mouseClicked() { 2880 | * ele.play(); 2881 | * background(200); 2882 | * //ele.duration dislpays the duration 2883 | * text(ele.duration() + ' seconds', width / 2, height / 2); 2884 | * } 2885 | *
2886 | */ 2887 | p5.MediaElement.prototype.duration = function() { 2888 | return this.elt.duration; 2889 | }; 2890 | p5.MediaElement.prototype.pixels = []; 2891 | p5.MediaElement.prototype._ensureCanvas = function() { 2892 | if (!this.canvas) { 2893 | this.canvas = document.createElement('canvas'); 2894 | this.drawingContext = this.canvas.getContext('2d'); 2895 | this.setModified(true); 2896 | } 2897 | if (this.loadedmetadata) { 2898 | // wait for metadata for w/h 2899 | if (this.canvas.width !== this.elt.width) { 2900 | this.canvas.width = this.elt.width; 2901 | this.canvas.height = this.elt.height; 2902 | this.width = this.canvas.width; 2903 | this.height = this.canvas.height; 2904 | this._pixelsDirty = true; 2905 | } 2906 | 2907 | var currentTime = this.elt.currentTime; 2908 | if (this._pixelsDirty || this._pixelsTime !== currentTime) { 2909 | // only update the pixels array if it's dirty, or 2910 | // if the video time has changed. 2911 | this._pixelsTime = currentTime; 2912 | this._pixelsDirty = true; 2913 | 2914 | this.drawingContext.drawImage( 2915 | this.elt, 2916 | 0, 2917 | 0, 2918 | this.canvas.width, 2919 | this.canvas.height 2920 | ); 2921 | this.setModified(true); 2922 | } 2923 | } 2924 | }; 2925 | p5.MediaElement.prototype.loadPixels = function() { 2926 | this._ensureCanvas(); 2927 | return p5.Renderer2D.prototype.loadPixels.apply(this, arguments); 2928 | }; 2929 | p5.MediaElement.prototype.updatePixels = function(x, y, w, h) { 2930 | if (this.loadedmetadata) { 2931 | // wait for metadata 2932 | this._ensureCanvas(); 2933 | p5.Renderer2D.prototype.updatePixels.call(this, x, y, w, h); 2934 | } 2935 | this.setModified(true); 2936 | return this; 2937 | }; 2938 | p5.MediaElement.prototype.get = function() { 2939 | this._ensureCanvas(); 2940 | return p5.Renderer2D.prototype.get.apply(this, arguments); 2941 | }; 2942 | p5.MediaElement.prototype._getPixel = function() { 2943 | this.loadPixels(); 2944 | return p5.Renderer2D.prototype._getPixel.apply(this, arguments); 2945 | }; 2946 | 2947 | p5.MediaElement.prototype.set = function(x, y, imgOrCol) { 2948 | if (this.loadedmetadata) { 2949 | // wait for metadata 2950 | this._ensureCanvas(); 2951 | p5.Renderer2D.prototype.set.call(this, x, y, imgOrCol); 2952 | this.setModified(true); 2953 | } 2954 | }; 2955 | p5.MediaElement.prototype.copy = function() { 2956 | this._ensureCanvas(); 2957 | p5.Renderer2D.prototype.copy.apply(this, arguments); 2958 | }; 2959 | p5.MediaElement.prototype.mask = function() { 2960 | this.loadPixels(); 2961 | this.setModified(true); 2962 | p5.Image.prototype.mask.apply(this, arguments); 2963 | }; 2964 | /** 2965 | * helper method for web GL mode to figure out if the element 2966 | * has been modified and might need to be re-uploaded to texture 2967 | * memory between frames. 2968 | * @method isModified 2969 | * @private 2970 | * @return {boolean} a boolean indicating whether or not the 2971 | * image has been updated or modified since last texture upload. 2972 | */ 2973 | p5.MediaElement.prototype.isModified = function() { 2974 | return this._modified; 2975 | }; 2976 | /** 2977 | * helper method for web GL mode to indicate that an element has been 2978 | * changed or unchanged since last upload. gl texture upload will 2979 | * set this value to false after uploading the texture; or might set 2980 | * it to true if metadata has become available but there is no actual 2981 | * texture data available yet.. 2982 | * @method setModified 2983 | * @param {boolean} val sets whether or not the element has been 2984 | * modified. 2985 | * @private 2986 | */ 2987 | p5.MediaElement.prototype.setModified = function(value) { 2988 | this._modified = value; 2989 | }; 2990 | /** 2991 | * Schedule an event to be called when the audio or video 2992 | * element reaches the end. If the element is looping, 2993 | * this will not be called. The element is passed in 2994 | * as the argument to the onended callback. 2995 | * 2996 | * @method onended 2997 | * @param {Function} callback function to call when the 2998 | * soundfile has ended. The 2999 | * media element will be passed 3000 | * in as the argument to the 3001 | * callback. 3002 | * @chainable 3003 | * @example 3004 | *
3005 | * function setup() { 3006 | * var audioEl = createAudio('assets/beat.mp3'); 3007 | * audioEl.showControls(); 3008 | * audioEl.onended(sayDone); 3009 | * } 3010 | * 3011 | * function sayDone(elt) { 3012 | * alert('done playing ' + elt.src); 3013 | * } 3014 | *
3015 | */ 3016 | p5.MediaElement.prototype.onended = function(callback) { 3017 | this._onended = callback; 3018 | return this; 3019 | }; 3020 | 3021 | /*** CONNECT TO WEB AUDIO API / p5.sound.js ***/ 3022 | 3023 | /** 3024 | * Send the audio output of this element to a specified audioNode or 3025 | * p5.sound object. If no element is provided, connects to p5's master 3026 | * output. That connection is established when this method is first called. 3027 | * All connections are removed by the .disconnect() method. 3028 | * 3029 | * This method is meant to be used with the p5.sound.js addon library. 3030 | * 3031 | * @method connect 3032 | * @param {AudioNode|Object} audioNode AudioNode from the Web Audio API, 3033 | * or an object from the p5.sound library 3034 | */ 3035 | p5.MediaElement.prototype.connect = function(obj) { 3036 | var audioContext, masterOutput; 3037 | 3038 | // if p5.sound exists, same audio context 3039 | if (typeof p5.prototype.getAudioContext === 'function') { 3040 | audioContext = p5.prototype.getAudioContext(); 3041 | masterOutput = p5.soundOut.input; 3042 | } else { 3043 | try { 3044 | audioContext = obj.context; 3045 | masterOutput = audioContext.destination; 3046 | } catch (e) { 3047 | throw 'connect() is meant to be used with Web Audio API or p5.sound.js'; 3048 | } 3049 | } 3050 | 3051 | // create a Web Audio MediaElementAudioSourceNode if none already exists 3052 | if (!this.audioSourceNode) { 3053 | this.audioSourceNode = audioContext.createMediaElementSource(this.elt); 3054 | 3055 | // connect to master output when this method is first called 3056 | this.audioSourceNode.connect(masterOutput); 3057 | } 3058 | 3059 | // connect to object if provided 3060 | if (obj) { 3061 | if (obj.input) { 3062 | this.audioSourceNode.connect(obj.input); 3063 | } else { 3064 | this.audioSourceNode.connect(obj); 3065 | } 3066 | } else { 3067 | // otherwise connect to master output of p5.sound / AudioContext 3068 | this.audioSourceNode.connect(masterOutput); 3069 | } 3070 | }; 3071 | 3072 | /** 3073 | * Disconnect all Web Audio routing, including to master output. 3074 | * This is useful if you want to re-route the output through 3075 | * audio effects, for example. 3076 | * 3077 | * @method disconnect 3078 | */ 3079 | p5.MediaElement.prototype.disconnect = function() { 3080 | if (this.audioSourceNode) { 3081 | this.audioSourceNode.disconnect(); 3082 | } else { 3083 | throw 'nothing to disconnect'; 3084 | } 3085 | }; 3086 | 3087 | /*** SHOW / HIDE CONTROLS ***/ 3088 | 3089 | /** 3090 | * Show the default MediaElement controls, as determined by the web browser. 3091 | * 3092 | * @method showControls 3093 | * @example 3094 | *
3095 | * var ele; 3096 | * function setup() { 3097 | * //p5.MediaElement objects are usually created 3098 | * //by calling the createAudio(), createVideo(), 3099 | * //and createCapture() functions. 3100 | * //In this example we create 3101 | * //a new p5.MediaElement via createAudio() 3102 | * ele = createAudio('assets/lucky_dragons.mp3'); 3103 | * background(200); 3104 | * textAlign(CENTER); 3105 | * text('Click to Show Controls!', 10, 25, 70, 80); 3106 | * } 3107 | * function mousePressed() { 3108 | * ele.showControls(); 3109 | * background(200); 3110 | * text('Controls Shown', width / 2, height / 2); 3111 | * } 3112 | *
3113 | */ 3114 | p5.MediaElement.prototype.showControls = function() { 3115 | // must set style for the element to show on the page 3116 | this.elt.style['text-align'] = 'inherit'; 3117 | this.elt.controls = true; 3118 | }; 3119 | 3120 | /** 3121 | * Hide the default mediaElement controls. 3122 | * @method hideControls 3123 | * @example 3124 | *
3125 | * var ele; 3126 | * function setup() { 3127 | * //p5.MediaElement objects are usually created 3128 | * //by calling the createAudio(), createVideo(), 3129 | * //and createCapture() functions. 3130 | * //In this example we create 3131 | * //a new p5.MediaElement via createAudio() 3132 | * ele = createAudio('assets/lucky_dragons.mp3'); 3133 | * ele.showControls(); 3134 | * background(200); 3135 | * textAlign(CENTER); 3136 | * text('Click to hide Controls!', 10, 25, 70, 80); 3137 | * } 3138 | * function mousePressed() { 3139 | * ele.hideControls(); 3140 | * background(200); 3141 | * text('Controls hidden', width / 2, height / 2); 3142 | * } 3143 | *
3144 | */ 3145 | p5.MediaElement.prototype.hideControls = function() { 3146 | this.elt.controls = false; 3147 | }; 3148 | 3149 | /*** SCHEDULE EVENTS ***/ 3150 | 3151 | // Cue inspired by JavaScript setTimeout, and the 3152 | // Tone.js Transport Timeline Event, MIT License Yotam Mann 2015 tonejs.org 3153 | var Cue = function(callback, time, id, val) { 3154 | this.callback = callback; 3155 | this.time = time; 3156 | this.id = id; 3157 | this.val = val; 3158 | }; 3159 | 3160 | /** 3161 | * Schedule events to trigger every time a MediaElement 3162 | * (audio/video) reaches a playback cue point. 3163 | * 3164 | * Accepts a callback function, a time (in seconds) at which to trigger 3165 | * the callback, and an optional parameter for the callback. 3166 | * 3167 | * Time will be passed as the first parameter to the callback function, 3168 | * and param will be the second parameter. 3169 | * 3170 | * 3171 | * @method addCue 3172 | * @param {Number} time Time in seconds, relative to this media 3173 | * element's playback. For example, to trigger 3174 | * an event every time playback reaches two 3175 | * seconds, pass in the number 2. This will be 3176 | * passed as the first parameter to 3177 | * the callback function. 3178 | * @param {Function} callback Name of a function that will be 3179 | * called at the given time. The callback will 3180 | * receive time and (optionally) param as its 3181 | * two parameters. 3182 | * @param {Object} [value] An object to be passed as the 3183 | * second parameter to the 3184 | * callback function. 3185 | * @return {Number} id ID of this cue, 3186 | * useful for removeCue(id) 3187 | * @example 3188 | *
3189 | * // 3190 | * // 3191 | * function setup() { 3192 | * noCanvas(); 3193 | * 3194 | * var audioEl = createAudio('assets/beat.mp3'); 3195 | * audioEl.showControls(); 3196 | * 3197 | * // schedule three calls to changeBackground 3198 | * audioEl.addCue(0.5, changeBackground, color(255, 0, 0)); 3199 | * audioEl.addCue(1.0, changeBackground, color(0, 255, 0)); 3200 | * audioEl.addCue(2.5, changeBackground, color(0, 0, 255)); 3201 | * audioEl.addCue(3.0, changeBackground, color(0, 255, 255)); 3202 | * audioEl.addCue(4.2, changeBackground, color(255, 255, 0)); 3203 | * audioEl.addCue(5.0, changeBackground, color(255, 255, 0)); 3204 | * } 3205 | * 3206 | * function changeBackground(val) { 3207 | * background(val); 3208 | * } 3209 | *
3210 | */ 3211 | p5.MediaElement.prototype.addCue = function(time, callback, val) { 3212 | var id = this._cueIDCounter++; 3213 | 3214 | var cue = new Cue(callback, time, id, val); 3215 | this._cues.push(cue); 3216 | 3217 | if (!this.elt.ontimeupdate) { 3218 | this.elt.ontimeupdate = this._onTimeUpdate.bind(this); 3219 | } 3220 | 3221 | return id; 3222 | }; 3223 | 3224 | /** 3225 | * Remove a callback based on its ID. The ID is returned by the 3226 | * addCue method. 3227 | * @method removeCue 3228 | * @param {Number} id ID of the cue, as returned by addCue 3229 | * @example 3230 | *
3231 | * var audioEl, id1, id2; 3232 | * function setup() { 3233 | * background(255, 255, 255); 3234 | * audioEl = createAudio('assets/beat.mp3'); 3235 | * audioEl.showControls(); 3236 | * // schedule five calls to changeBackground 3237 | * id1 = audioEl.addCue(0.5, changeBackground, color(255, 0, 0)); 3238 | * audioEl.addCue(1.0, changeBackground, color(0, 255, 0)); 3239 | * audioEl.addCue(2.5, changeBackground, color(0, 0, 255)); 3240 | * audioEl.addCue(3.0, changeBackground, color(0, 255, 255)); 3241 | * id2 = audioEl.addCue(4.2, changeBackground, color(255, 255, 0)); 3242 | * text('Click to remove first and last Cue!', 10, 25, 70, 80); 3243 | * } 3244 | * function mousePressed() { 3245 | * audioEl.removeCue(id1); 3246 | * audioEl.removeCue(id2); 3247 | * } 3248 | * function changeBackground(val) { 3249 | * background(val); 3250 | * } 3251 | *
3252 | */ 3253 | p5.MediaElement.prototype.removeCue = function(id) { 3254 | for (var i = 0; i < this._cues.length; i++) { 3255 | if (this._cues[i].id === id) { 3256 | console.log(id); 3257 | this._cues.splice(i, 1); 3258 | } 3259 | } 3260 | 3261 | if (this._cues.length === 0) { 3262 | this.elt.ontimeupdate = null; 3263 | } 3264 | }; 3265 | 3266 | /** 3267 | * Remove all of the callbacks that had originally been scheduled 3268 | * via the addCue method. 3269 | * @method clearCues 3270 | * @param {Number} id ID of the cue, as returned by addCue 3271 | * @example 3272 | *
3273 | * var audioEl; 3274 | * function setup() { 3275 | * background(255, 255, 255); 3276 | * audioEl = createAudio('assets/beat.mp3'); 3277 | * //Show the default MediaElement controls, as determined by the web browser 3278 | * audioEl.showControls(); 3279 | * // schedule calls to changeBackground 3280 | * background(200); 3281 | * text('Click to change Cue!', 10, 25, 70, 80); 3282 | * audioEl.addCue(0.5, changeBackground, color(255, 0, 0)); 3283 | * audioEl.addCue(1.0, changeBackground, color(0, 255, 0)); 3284 | * audioEl.addCue(2.5, changeBackground, color(0, 0, 255)); 3285 | * audioEl.addCue(3.0, changeBackground, color(0, 255, 255)); 3286 | * audioEl.addCue(4.2, changeBackground, color(255, 255, 0)); 3287 | * } 3288 | * function mousePressed() { 3289 | * // here we clear the scheduled callbacks 3290 | * audioEl.clearCues(); 3291 | * // then we add some more callbacks 3292 | * audioEl.addCue(1, changeBackground, color(2, 2, 2)); 3293 | * audioEl.addCue(3, changeBackground, color(255, 255, 0)); 3294 | * } 3295 | * function changeBackground(val) { 3296 | * background(val); 3297 | * } 3298 | *
3299 | */ 3300 | p5.MediaElement.prototype.clearCues = function() { 3301 | this._cues = []; 3302 | this.elt.ontimeupdate = null; 3303 | }; 3304 | 3305 | // private method that checks for cues to be fired if events 3306 | // have been scheduled using addCue(callback, time). 3307 | p5.MediaElement.prototype._onTimeUpdate = function() { 3308 | var playbackTime = this.time(); 3309 | 3310 | for (var i = 0; i < this._cues.length; i++) { 3311 | var callbackTime = this._cues[i].time; 3312 | var val = this._cues[i].val; 3313 | 3314 | if (this._prevTime < callbackTime && callbackTime <= playbackTime) { 3315 | // pass the scheduled callbackTime as parameter to the callback 3316 | this._cues[i].callback(val); 3317 | } 3318 | } 3319 | 3320 | this._prevTime = playbackTime; 3321 | }; 3322 | 3323 | /** 3324 | * Base class for a file. 3325 | * Used for Element.drop and createFileInput. 3326 | * 3327 | * @class p5.File 3328 | * @constructor 3329 | * @param {File} file File that is wrapped 3330 | */ 3331 | p5.File = function(file, pInst) { 3332 | /** 3333 | * Underlying File object. All normal File methods can be called on this. 3334 | * 3335 | * @property file 3336 | */ 3337 | this.file = file; 3338 | 3339 | this._pInst = pInst; 3340 | 3341 | // Splitting out the file type into two components 3342 | // This makes determining if image or text etc simpler 3343 | var typeList = file.type.split('/'); 3344 | /** 3345 | * File type (image, text, etc.) 3346 | * 3347 | * @property type 3348 | */ 3349 | this.type = typeList[0]; 3350 | /** 3351 | * File subtype (usually the file extension jpg, png, xml, etc.) 3352 | * 3353 | * @property subtype 3354 | */ 3355 | this.subtype = typeList[1]; 3356 | /** 3357 | * File name 3358 | * 3359 | * @property name 3360 | */ 3361 | this.name = file.name; 3362 | /** 3363 | * File size 3364 | * 3365 | * @property size 3366 | */ 3367 | this.size = file.size; 3368 | 3369 | /** 3370 | * URL string containing image data. 3371 | * 3372 | * @property data 3373 | */ 3374 | this.data = undefined; 3375 | }; 3376 | 3377 | p5.File._createLoader = function(theFile, callback) { 3378 | var reader = new FileReader(); 3379 | reader.onload = function(e) { 3380 | var p5file = new p5.File(theFile); 3381 | p5file.data = e.target.result; 3382 | callback(p5file); 3383 | }; 3384 | return reader; 3385 | }; 3386 | 3387 | p5.File._load = function(f, callback) { 3388 | // Text or data? 3389 | // This should likely be improved 3390 | if (/^text\//.test(f.type)) { 3391 | p5.File._createLoader(f, callback).readAsText(f); 3392 | } else if (!/^(video|audio)\//.test(f.type)) { 3393 | p5.File._createLoader(f, callback).readAsDataURL(f); 3394 | } else { 3395 | var file = new p5.File(f); 3396 | file.data = URL.createObjectURL(f); 3397 | callback(file); 3398 | } 3399 | }; 3400 | }); 3401 | -------------------------------------------------------------------------------- /Dino game/plant.js: -------------------------------------------------------------------------------- 1 | function Plant(){ 2 | this.x= width; 3 | 4 | this.w=20; //width 5 | this.h=random(80,130); //height 6 | this.speed = 15; 7 | this.show = function(){ 8 | fill(200); 9 | noStroke(); 10 | rect(this.x,565-this.h,this.w,this.h); 11 | 12 | } 13 | this.update = function(){ 14 | this.x-=this.speed; 15 | } 16 | this.offScreen= function(){ 17 | if(this.x < -this.w){ 18 | return true; 19 | }else{ 20 | return false; 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Dino game/sketch.js: -------------------------------------------------------------------------------- 1 | var dino; 2 | var gameOver=0; 3 | var plants = []; 4 | var distan =0 5 | var count;//Distance b/w plants 6 | function setup() { 7 | // put the initial conditions here 8 | createCanvas(1500,750); 9 | dino = new Dino(); 10 | plants.push(new Plant()); 11 | 12 | 13 | 14 | } 15 | 16 | function draw() { 17 | // drawing code 18 | 19 | 20 | background(0); 21 | dino.show(); 22 | dino.update(); 23 | fill(0); 24 | stroke(200); 25 | line(0,565,1500,565); 26 | textSize(32); 27 | fill(40,200,40); 28 | text("Impact :",100,100); 29 | text(gameOver,230,100); 30 | textSize(25); 31 | text("Press space to jump",1200,100); 32 | text("Distance :",1200,150); 33 | text(distan,1330,150); 34 | count =random(54,56); 35 | count =floor(count); 36 | console.log(count); 37 | if(frameCount % count == 0){ 38 | plants.push(new Plant()); 39 | distan+=10; 40 | 41 | } 42 | for(var i=plants.length-1; i>=0; i--){ 43 | plants[i].show(); 44 | 45 | plants[i].update(); 46 | if( dino.y+15 > 565-plants[i].h && plants[i].x==60 ){ 47 | gameOver+=1; 48 | } 49 | 50 | if (plants[i].offScreen()){ 51 | plants.splice(i,1); 52 | } 53 | 54 | } 55 | } 56 | 57 | function keyPressed(){ 58 | if(key == " "){ 59 | if(dino.y==550){ 60 | dino.velocity=12; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Divide String into N parts.java: -------------------------------------------------------------------------------- 1 | public class DivideString { 2 | public static void main(String[] args) { 3 | String str = "aaaabbbbcccc"; 4 | 5 | //Stores the length of the string 6 | int len = str.length(); 7 | //n determines the variable that divide the string in 'n' equal parts 8 | int n = 3; 9 | int temp = 0, chars = len/n; 10 | //Stores the array of string 11 | String[] equalStr = new String [n]; 12 | //Check whether a string can be divided into n equal parts 13 | if(len % n != 0) { 14 | System.out.println("Sorry this string cannot be divided into "+ n +" equal parts."); 15 | } 16 | else { 17 | for(int i = 0; i < len; i = i+chars) { 18 | //Dividing string in n equal part using substring() 19 | String part = str.substring(i, i+chars); 20 | equalStr[temp] = part; 21 | temp++; 22 | } 23 | System.out.println(n + " equal parts of given string are "); 24 | for(int i = 0; i < equalStr.length; i++) { 25 | System.out.println(equalStr[i]); 26 | } 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Hell0W0rl2d.java: -------------------------------------------------------------------------------- 1 | class HelloWorldApp { 2 | public static void main(String[] args) { 3 | System.out.println("Hello World!"); // Prints the string to the console. 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Hell0W0rl3d.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::cout << "Hello, world!\n"; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Hell0W0rl4d.py: -------------------------------------------------------------------------------- 1 | # Program to print Hello World in Python3 2 | print("Hello World") 3 | -------------------------------------------------------------------------------- /Hell0W0rl5d.js: -------------------------------------------------------------------------------- 1 | console.log("Hello World!"); 2 | -------------------------------------------------------------------------------- /Hello World.masm: -------------------------------------------------------------------------------- 1 | # By: its-kos - https://github.com/its-kos 2 | 3 | .data 4 | txt: .asciiz "Hello World\n" 5 | 6 | section .text 7 | 8 | .text 9 | .globl main 10 | 11 | main: li $v0, 4 12 | la $a0, txt 13 | syscall 14 | -------------------------------------------------------------------------------- /Hello world: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main () 5 | { int n; 6 | cout<<"enter the value for n"<>n; 8 | for(int i=0; i 2 | using namespace std; 3 | int main () 4 | { 5 | for(int i=0;i<6;i++) 6 | { cout<<"hello world"< 2 | int main(){ 3 | printf("Hello World"); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /HelloWorld.java: -------------------------------------------------------------------------------- 1 | 2 | public class HelloWorld { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | System.out.println("Hello World!!!!"); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /HelloWorld.ps1: -------------------------------------------------------------------------------- 1 | function Hello{ 2 | Write-Host "Hello World" 3 | } 4 | 5 | # Alternatively, you can simply use 'Write-Host "Hello World"' 6 | # To call the function, simply type 'Hello' in PowerShell ISE 7 | -------------------------------------------------------------------------------- /HelloWorld.ring: -------------------------------------------------------------------------------- 1 | see "Hello World!" 2 | -------------------------------------------------------------------------------- /HelloWorldByBoat.js: -------------------------------------------------------------------------------- 1 | console.log('hello world by boatdev') 2 | -------------------------------------------------------------------------------- /HelloWorldC++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cout << "Hello, World! HacktoberFest is fun!"; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Hello_World.csh: -------------------------------------------------------------------------------- 1 | #!/bin/csh -f 2 | echo Hello, World! 3 | -------------------------------------------------------------------------------- /Heloooo.py: -------------------------------------------------------------------------------- 1 | print("Hello World") 2 | print("hacktober fest rock") 3 | for i in range(10): 4 | print('Hacktober fest') 5 | -------------------------------------------------------------------------------- /KotlinFibo.kt: -------------------------------------------------------------------------------- 1 | fun main(arr : Array) { 2 | val fibonacciSequence = generateSequence(1 to 1) //Generating Sequence starting from 1 { 3 | it.second to it.first + it.second //Calculating Fn = Fn-1 + Fn-2 and mapping to return Pair 4 | }.map{ it.second } //Getting first element of next sequence 5 | println("First X Fibonacci numbers are : 0,1, ${fibonacciSequence.take(20).joinToString()}") 6 | } 7 | -------------------------------------------------------------------------------- /KruskalAlog.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for Kruskal's algorithm to find Minimum by Jasani Dhruv 2 | // Spanning Tree of a given connected, undirected and 3 | // weighted graph 4 | #include 5 | using namespace std; 6 | 7 | // Creating shortcut for an integer pair 8 | typedef pair iPair; 9 | 10 | // Structure to represent a graph 11 | struct Graph 12 | { 13 | int V, E; 14 | vector< pair > edges; 15 | 16 | // Constructor 17 | Graph(int V, int E) 18 | { 19 | this->V = V; 20 | this->E = E; 21 | } 22 | 23 | // Utility function to add an edge 24 | void addEdge(int u, int v, int w) 25 | { 26 | edges.push_back({w, {u, v}}); 27 | } 28 | 29 | // Function to find MST using Kruskal's 30 | // MST algorithm 31 | int kruskalMST(); 32 | }; 33 | 34 | // To represent Disjoint Sets 35 | struct DisjointSets 36 | { 37 | int *parent, *rnk; 38 | int n; 39 | 40 | // Constructor. 41 | DisjointSets(int n) 42 | { 43 | // Allocate memory 44 | this->n = n; 45 | parent = new int[n+1]; 46 | rnk = new int[n+1]; 47 | 48 | // Initially, all vertices are in 49 | // different sets and have rank 0. 50 | for (int i = 0; i <= n; i++) 51 | { 52 | rnk[i] = 0; 53 | 54 | //every element is parent of itself 55 | parent[i] = i; 56 | } 57 | } 58 | 59 | // Find the parent of a node 'u' 60 | // Path Compression 61 | int find(int u) 62 | { 63 | /* Make the parent of the nodes in the path 64 | from u--> parent[u] point to parent[u] */ 65 | if (u != parent[u]) 66 | parent[u] = find(parent[u]); 67 | return parent[u]; 68 | } 69 | 70 | // Union by rank 71 | void merge(int x, int y) 72 | { 73 | x = find(x), y = find(y); 74 | 75 | /* Make tree with smaller height 76 | a subtree of the other tree */ 77 | if (rnk[x] > rnk[y]) 78 | parent[y] = x; 79 | else // If rnk[x] <= rnk[y] 80 | parent[x] = y; 81 | 82 | if (rnk[x] == rnk[y]) 83 | rnk[y]++; 84 | } 85 | }; 86 | 87 | /* Functions returns weight of the MST*/ 88 | 89 | int Graph::kruskalMST() 90 | { 91 | int mst_wt = 0; // Initialize result 92 | 93 | // Sort edges in increasing order on basis of cost 94 | sort(edges.begin(), edges.end()); 95 | 96 | // Create disjoint sets 97 | DisjointSets ds(V); 98 | 99 | // Iterate through all sorted edges 100 | vector< pair >::iterator it; 101 | for (it=edges.begin(); it!=edges.end(); it++) 102 | { 103 | int u = it->second.first; 104 | int v = it->second.second; 105 | 106 | int set_u = ds.find(u); 107 | int set_v = ds.find(v); 108 | 109 | // Check if the selected edge is creating 110 | // a cycle or not (Cycle is created if u 111 | // and v belong to same set) 112 | if (set_u != set_v) 113 | { 114 | // Current edge will be in the MST 115 | // so print it 116 | cout << u << " - " << v << endl; 117 | 118 | // Update MST weight 119 | mst_wt += it->first; 120 | 121 | // Merge two sets 122 | ds.merge(set_u, set_v); 123 | } 124 | } 125 | 126 | return mst_wt; 127 | } 128 | 129 | // Driver program to test above functions 130 | int main() 131 | { 132 | /* Let us create above shown weighted 133 | and unidrected graph */ 134 | int V = 9, E = 14; 135 | Graph g(V, E); 136 | 137 | // making above shown graph 138 | g.addEdge(0, 1, 4); 139 | g.addEdge(0, 7, 8); 140 | g.addEdge(1, 2, 8); 141 | g.addEdge(1, 7, 11); 142 | g.addEdge(2, 3, 7); 143 | g.addEdge(2, 8, 2); 144 | g.addEdge(2, 5, 4); 145 | g.addEdge(3, 4, 9); 146 | g.addEdge(3, 5, 14); 147 | g.addEdge(4, 5, 10); 148 | g.addEdge(5, 6, 2); 149 | g.addEdge(6, 7, 1); 150 | g.addEdge(6, 8, 6); 151 | g.addEdge(7, 8, 7); 152 | 153 | cout << "Edges of MST are \n"; 154 | int mst_wt = g.kruskalMST(); 155 | 156 | cout << "\nWeight of MST is " << mst_wt; 157 | 158 | return 0; 159 | } 160 | -------------------------------------------------------------------------------- /Parameterised constructor: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Example { 7 | int x, y; 8 | public: 9 | 10 | Example(int a, int b) { 11 | 12 | x = a; 13 | y = b; 14 | cout << "I am Constructor\n"; 15 | } 16 | 17 | void Display() { 18 | cout << "Values :" << x << "\t" << y; 19 | } 20 | }; 21 | 22 | int main() { 23 | Example Object(11, 25); 24 | 25 | Object.Display(); 26 | 27 | 28 | getch(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HacktoberFest-HelloWorld 2 | ### An easy way to get your 5 pull requests for Hacktoberfest ! 3 | This Repo is solely built for HacktoberFest 2019 !! Just make a pull request and it will be merged with in minutes ... 4 | ## examples that you can do 5 | 1. You can add any program. 6 | 2. You can update readme file. 7 | etc... 8 | -------------------------------------------------------------------------------- /README_Pedro.md: -------------------------------------------------------------------------------- 1 | ### A Grande Família 2 | 3 | Muito engracado essa serie 4 | -------------------------------------------------------------------------------- /README_es.md: -------------------------------------------------------------------------------- 1 | # Bienvenido al HacktoberFest 2019! 2 | ### Una manera sencialla de optener tus 5 **pull request** para el Hacktoberfest ! 3 | Este repositorio es solamente creado para el HacktoberFest 2019 !! Solo haz un **pull request** y será fusionada en minutos ... 4 | 5 | * Sigue los siguientes pasos para tu primer **pull request** 6 | 7 | 1. Haz **fork** de este repositorio 8 | 2. Clona tu repositorio copiado a tu máquina local 9 | 3. Agrega tu nombre a la lista de **Contributors** (Opcional) 10 | 4. Escribe estos comandos en tu terminal 11 | ``` 12 | git add -A 13 | git commit -m "Tu nombre" 14 | git push -u origin master 15 | ``` 16 | 5. Después crea tu **pull request**. 17 | 6. Da una estrella en este [repository] (https://github.com/uditkumar489/HacktoberFest-HelloWorld). 18 | 7. Felicidades!! Has crado tu **pull request** exitosamente. 19 | 8. Revisa tu progreso aqui (https://hacktoberfest.digitalocean.com/profile) 20 | 9. Espera por tu camisa 21 | 22 | ## Ejemplos de lo que puedes hacer 23 | 1. Puedes agregar cualquier programa 24 | 2. Puedes actualizar el archivo **README.md**. 25 | etc... 26 | -------------------------------------------------------------------------------- /Swap variables without 3rd Variables.java: -------------------------------------------------------------------------------- 1 | public class SwapTwoNumberWithoutThirdVariable 2 | { 3 | public static void main(String args[]) 4 | { 5 | int x, y; 6 | 7 | x = 5; 8 | y = 7; 9 | System.out.println("Before Swapping\nx = "+x+"\ny = "+y); 10 | x = x + y; 11 | y = x - y; 12 | x = x - y; 13 | System.out.println("After Swapping without third variable\nx = "+x+"\ny = "+y); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Updated Hello World.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello World 5 | 6 | 7 | 8 |

Hello World

9 |

Thank you!

10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /a_plus_b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b; 6 | scanf("%d%d", &a, &b); 7 | printf("%d", a+b); 8 | } 9 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const yargs= require('yargs') 2 | const chalk = require('chalk'); 3 | const notes = require('./notes.js'); 4 | // const msg = getNotes(); 5 | // console.log(msg); 6 | yargs.version('1.1.0') 7 | //to add notes 8 | yargs.command({ 9 | command:'add', 10 | describe:'Add a new note', 11 | builder:{ 12 | title:{ 13 | describe:"Note title", 14 | demandOption : true, 15 | type: 'string' 16 | }, 17 | body:{ 18 | describe:'write your note here', 19 | demandOption: true, 20 | type:'string' 21 | } 22 | }, 23 | handler(argv) { notes.addNote(argv.title,argv.body)} 24 | 25 | }) 26 | //to remove notes 27 | yargs.command({ 28 | command:'remove', 29 | describe:'to remove the note', 30 | handler: function(argv){ 31 | notes.removeNote(argv.title)} 32 | }) 33 | //to list notes 34 | yargs.command({ 35 | command: 'list', 36 | describe:'to list the notes', 37 | handler: function(){ notes.listNote()} 38 | }) 39 | //to read notes 40 | yargs.command({ 41 | command:'read', 42 | describe:'read the note', 43 | handler(argv) { notes.readNote(argv.title)} 44 | }) 45 | yargs.parse() 46 | //console.log(yargs.argv)//this command returns the handler function value 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | // add = require('./utils.js'); 60 | // //fs.writeFileSync('notes.txt','this is done by node.js');//thisWasForWritingfreshMessageeverytime 61 | // //fs.appendFileSync('notes.txt','So I am trying to append the messages'); 62 | // const sum = add(4,-2); 63 | // console.log(sum); -------------------------------------------------------------------------------- /baseNnumber: -------------------------------------------------------------------------------- 1 | #include 2 | #nclude 3 | #include 4 | 5 | class BaseN{ 6 | unsigned base; 7 | string numero; 8 | public: 9 | BaseN(int = 0, unsigned = 2); 10 | void setB(unsigned); 11 | void setN(string); 12 | void setN(int); 13 | 14 | void trocaB(unsigned); 15 | 16 | unsigned getB() const; 17 | string getN() const; 18 | 19 | BaseN operator+(BaseN); 20 | BaseN operator-(BaseN); 21 | BaseN operator*(BaseN); 22 | BaseN operator/(BaseN); 23 | 24 | 25 | ~BaseN(); 26 | }; 27 | -------------------------------------------------------------------------------- /bcancer.py: -------------------------------------------------------------------------------- 1 | #PRE PROCESSING PART 2 | 3 | import numpy as np 4 | from sklearn import preprocessing , neighbors , svm , model_selection 5 | import pandas as pd 6 | 7 | df= pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data') 8 | df.replace('?',-99999,inplace=True) 9 | col_names=['id','clump_thickness','cell_size','cell shape','adhesion','epithelial','nuclei','brandchlomatin','nucleoli','mitoses','class'] 10 | df.columns=col_names 11 | print(df.head()) 12 | df.drop(['id'],1,inplace=True) 13 | 14 | X=np.array(df.drop(['class'],1)) 15 | y=np.array(df['class']) 16 | 17 | X_train,X_test,y_train,y_test = model_selection.train_test_split(X,y,test_size=0.2) 18 | clf=neighbors.KNeighborsClassifier() 19 | clf.fit(X_train,y_train) 20 | confidence=clf.score(X_test,y_test) 21 | print("Confidence is :- ", int(confidence*100),'%') 22 | 23 | example_measures=np.array([4,.2,1,1,1,2,3,2,1]) 24 | example_measures.shape=(1,-1) 25 | prediction=clf.predict(example_measures) 26 | print('Cancer state : ','Benign' if prediction==2 else 'Malignant') 27 | from sklearn.pipeline import Pipeline 28 | from sklearn.model_selection import train_test_split , GridSearchCV 29 | 30 | pipeline=Pipeline([('clf',svm.SVC(kernel='linear',C=1,gamma=.1))]) 31 | params={'clf_C': (0.1,0.5,1,2,5,10,20) 32 | 33 | } 34 | clf=svm.SVC() 35 | clf.fit(X_train,y_train) 36 | confidence=clf.score(X_test,y_test) 37 | print("Confidence is :- ", int(confidence*100),'%') 38 | 39 | example_measures=np.array([4,.2,1,1,1,2,3,2,1]) 40 | example_measures.shape=(1,-1) 41 | prediction=clf.predict(example_measures) 42 | print('Cancer state : ','Benign' if prediction==2 else 'Malignant') 43 | print(clf) 44 | -------------------------------------------------------------------------------- /bubble sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | void bubblesort(int[],int); 7 | int main() 8 | { 9 | int a[30]; 10 | int n; 11 | cout<<"enter the size of array"; 12 | cin>>n; 13 | cout<<"enter array elements"; 14 | for(int i=0;i>a[i]; 17 | } 18 | bubblesort(a,n); 19 | cout<<"the sorted array is"; 20 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Hello World!" << endl; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /hello.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | class HelloWorld 4 | { 5 | public static void main(String args[]) 6 | { 7 | System.out.println("Hello, World"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /hello.js: -------------------------------------------------------------------------------- 1 | console.log("Hello, World!"); -------------------------------------------------------------------------------- /hello.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 3 | pageEncoding="ISO-8859-1"%> 4 | 5 | 6 | 7 | 8 | JSP - Hello World Tutorial - Programmer Gate 9 | 10 | 11 | <%= "Hello World!" %> 12 | 13 | 14 | -------------------------------------------------------------------------------- /hello.php: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /hello.py: -------------------------------------------------------------------------------- 1 | for i in range(10): 2 | print("Hello world") 3 | -------------------------------------------------------------------------------- /hello.ring: -------------------------------------------------------------------------------- 1 | see "Hello World!" 2 | -------------------------------------------------------------------------------- /hello1.c: -------------------------------------------------------------------------------- 1 | # This program prints Hello, world! 2 | 3 | print('Hello, world!') 4 | -------------------------------------------------------------------------------- /helloAlex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string name; 8 | 9 | cout << "What is your name?" << endl; 10 | cin >> name; 11 | 12 | cout << "Your name is:" << name; 13 | cout << "I now know your name..." << endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /hello_world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello World") 7 | } 8 | -------------------------------------------------------------------------------- /hello_world.jl: -------------------------------------------------------------------------------- 1 | println("hello world") 2 | 3 | 4 | -------------------------------------------------------------------------------- /hello_world.js: -------------------------------------------------------------------------------- 1 | console.log('Hello World') 2 | -------------------------------------------------------------------------------- /hello_world.py: -------------------------------------------------------------------------------- 1 | print('Hello World') 2 | -------------------------------------------------------------------------------- /hello_world.rb: -------------------------------------------------------------------------------- 1 | puts 'Hello, world!!' -------------------------------------------------------------------------------- /hello_world.sh: -------------------------------------------------------------------------------- 1 | echo "Hello world" 2 | -------------------------------------------------------------------------------- /hello_world.ts: -------------------------------------------------------------------------------- 1 | console.log('Hello World') 2 | -------------------------------------------------------------------------------- /hello_world_bimobim.sh: -------------------------------------------------------------------------------- 1 | echo "Hello World!" 2 | -------------------------------------------------------------------------------- /hello_world_vue.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 12 | 13 | 14 | 15 | 16 |
17 |

{{ message }}

18 | 19 |
20 | 21 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /hello_worldp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | cout<<:Hello world!< 2 | using namespace std; 3 | 4 | int main(){ 5 | cout << "HelloWorld-Hacktoberfest\n"; 6 | return 0; 7 | } -------------------------------------------------------------------------------- /hellok.ts: -------------------------------------------------------------------------------- 1 | console.log('Hello World') 2 | -------------------------------------------------------------------------------- /hellos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | cout << "Hello World!" << endl; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /hellos.java: -------------------------------------------------------------------------------- 1 | 2 | public class HelloWorld { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | System.out.println("Hello World!!!!"); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /hellos.ring: -------------------------------------------------------------------------------- 1 | 2 | see "Hello World!" 3 | -------------------------------------------------------------------------------- /hellos_world.ring: -------------------------------------------------------------------------------- 1 | 2 | see "Hello World!" 3 | -------------------------------------------------------------------------------- /hellouk.abap: -------------------------------------------------------------------------------- 1 | WRITE 'Hello, World!'. 2 | -------------------------------------------------------------------------------- /hellouk.csp: -------------------------------------------------------------------------------- 1 | Class Test.Hello Extends %CSP.Page [ ProcedureBlock ] 2 | { 3 | ClassMethod OnPage() As %Status 4 | { 5 | &html< 6 | 7 | 8 | > 9 | Write "Hello, world!",! 10 | &html< 11 | > 12 | Quit $$$OK 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hellouk.php: -------------------------------------------------------------------------------- 1 | 4 | -------------------------------------------------------------------------------- /hellov.java: -------------------------------------------------------------------------------- 1 | public class HelloWorld { 2 | 3 | public static void main(String[] args) { 4 | // TODO Auto-generated method stub 5 | System.out.println("Hello World!!!!"); 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /hellov.js: -------------------------------------------------------------------------------- 1 | 2 | console.log('Hello ') 3 | console.log('world') 4 | 5 | -------------------------------------------------------------------------------- /hellov.py: -------------------------------------------------------------------------------- 1 | for i in range(10): 2 | print("Hello") 3 | -------------------------------------------------------------------------------- /hellov.ring: -------------------------------------------------------------------------------- 1 | 2 | see "Hello World!" 3 | -------------------------------------------------------------------------------- /helloworld.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | printf("Hello world"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /helloworld.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
Hello World
4 | 5 | 6 |

Hello World

7 |

Let's begin coding

8 | 9 | 10 | -------------------------------------------------------------------------------- /helloworld.js: -------------------------------------------------------------------------------- 1 | console.log("hello world"); -------------------------------------------------------------------------------- /helloworld.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | 4 | 5 | 6 | 7 | JSP - Hello World Tutorial - Programmer Gate 8 | 9 | 10 | <%= "Hello World!" %> 11 | 12 | 13 | -------------------------------------------------------------------------------- /helloworld.nim: -------------------------------------------------------------------------------- 1 | echo "hello, world!" -------------------------------------------------------------------------------- /helloworld.php: -------------------------------------------------------------------------------- 1 | 5 | -------------------------------------------------------------------------------- /hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]) { 6 | const string cc = argv[1]; 7 | cout << cc << endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /ishita.java: -------------------------------------------------------------------------------- 1 | class A { 2 | public static void main(String args[]){ 3 | System.out.println("Hello World"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /linked List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "0\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "print(5+++++-++++++5)" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "\n", 27 | "# Node class \n", 28 | "class Node: \n", 29 | " \n", 30 | " # Function to initialize the node object \n", 31 | " def __init__(self, data): \n", 32 | " self.data = data # Assign data \n", 33 | " self.next = None # Initialize \n", 34 | " # next as null \n", 35 | " \n", 36 | "# Linked List class \n", 37 | "class LinkedList: \n", 38 | " \n", 39 | " # Function to initialize the Linked \n", 40 | " # List object \n", 41 | " def __init__(self): \n", 42 | " self.head = None\n" 43 | ] 44 | } 45 | ], 46 | "metadata": { 47 | "kernelspec": { 48 | "display_name": "Python 3", 49 | "language": "python", 50 | "name": "python3" 51 | }, 52 | "language_info": { 53 | "codemirror_mode": { 54 | "name": "ipython", 55 | "version": 3 56 | }, 57 | "file_extension": ".py", 58 | "mimetype": "text/x-python", 59 | "name": "python", 60 | "nbconvert_exporter": "python", 61 | "pygments_lexer": "ipython3", 62 | "version": "3.6.4" 63 | } 64 | }, 65 | "nbformat": 4, 66 | "nbformat_minor": 2 67 | } 68 | -------------------------------------------------------------------------------- /linked cb .ipynb: -------------------------------------------------------------------------------- 1 | class Node: 2 | 3 | # Function to initialize the node object 4 | def __init__(self, data): 5 | self.data = data # Assign data 6 | self.next = None # Initialize 7 | # next as null 8 | 9 | # Linked List class 10 | class LinkedList: 11 | 12 | # Function to initialize the Linked 13 | # List object 14 | def __init__(self): 15 | self.head = None 16 | -------------------------------------------------------------------------------- /mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void merge(int a[], int s, int e) //Function to merge the broken down arrays in a sorted way 4 | { 5 | int mid = (s+e)/2; //finding mid to get breakpoints 6 | int i = s; 7 | int j = mid + 1; 8 | int k = s; 9 | 10 | int fin[100]; //Array to store the sorted array 11 | 12 | 13 | while(i<=mid && j<=e) 14 | { 15 | if(a[i] st=new Stack<>(); 12 | 13 | 14 | for(int i: a){ 15 | if(st.isEmpty()){ 16 | st.add(i); 17 | } 18 | else{ while(!st.isEmpty()&&st.peek()"+i); 20 | } 21 | st.push(i); 22 | } 23 | } 24 | while(!st.isEmpty()){ 25 | System.out.println(st.pop()+"---"+"none"); 26 | 27 | 28 | } 29 | 30 | 31 | 32 | 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /notes.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const chalk = require('chalk') 3 | //const getNotes= () => "Your notes..."; 4 | 5 | 6 | 7 | const addNote = function (title,body) 8 | { 9 | 10 | const notes = loadNotes() 11 | //const duplicateNotes = notes.filter(function (notes){ 12 | //return notes.title === title}) 13 | const duplicateNote = notes.find((note) => note.title ===title) 14 | if(!duplicateNote){ 15 | 16 | notes.push({ 17 | title: title, 18 | body: body 19 | }) 20 | saveNotes(notes) 21 | console.log(chalk.green.inverse('note added!'))} 22 | else{console.log(chalk.red.inverse('title already taken!'))} 23 | 24 | } 25 | 26 | const removeNote = function(title) 27 | { 28 | const notes = loadNotes() 29 | const notesToKeep = notes.filter(function(notes){return notes.title !== title}) 30 | saveNotes(notesToKeep) 31 | if(notesToKeep.length === notes.length) 32 | console.log(chalk.inverse.red("No note found!")) 33 | else 34 | console.log(chalk.inverse.green("Note removed!")) 35 | } 36 | 37 | 38 | const saveNotes= function(notes) 39 | { 40 | const dataJSON = JSON.stringify(notes) 41 | fs.writeFileSync('notes.json', dataJSON) 42 | } 43 | 44 | 45 | const loadNotes = function() 46 | { 47 | try { 48 | const dataBuffer = fs.readFileSync('notes.json') 49 | const dataJSON = dataBuffer.toString() 50 | return JSON.parse(dataJSON) 51 | } catch(e) 52 | { 53 | return [] 54 | } 55 | } 56 | 57 | listNote = () => { 58 | console.log(chalk.magenta.inverse("Your Notes...")) 59 | const notes = loadNotes() 60 | notes.forEach(element => { console.log(chalk.cyanBright(element.title)) 61 | 62 | }); 63 | } 64 | 65 | readNote = (title) => { 66 | const notes = loadNotes() 67 | noteToRead = notes.find((note) => note.title === title) 68 | if(!noteToRead){ 69 | console.log(chalk.red("No note found!")) 70 | } else{ 71 | console.log(chalk.magenta(noteToRead.title)) 72 | console.log(noteToRead.body)} 73 | } 74 | 75 | 76 | module.exports= 77 | { 78 | addNote : addNote, 79 | removeNote: removeNote, 80 | listNote: listNote, 81 | readNote : readNote 82 | } -------------------------------------------------------------------------------- /notes.json: -------------------------------------------------------------------------------- 1 | [{"title":"list1","body":"xyz"},{"title":"list","body":"xyz"},{"title":"listA","body":"item1,item2"}] -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "notesapp", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "ansi-regex": { 8 | "version": "3.0.0", 9 | "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", 10 | "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=" 11 | }, 12 | "ansi-styles": { 13 | "version": "3.2.1", 14 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", 15 | "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", 16 | "requires": { 17 | "color-convert": "^1.9.0" 18 | } 19 | }, 20 | "camelcase": { 21 | "version": "4.1.0", 22 | "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-4.1.0.tgz", 23 | "integrity": "sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=" 24 | }, 25 | "chalk": { 26 | "version": "2.4.1", 27 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", 28 | "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", 29 | "requires": { 30 | "ansi-styles": "^3.2.1", 31 | "escape-string-regexp": "^1.0.5", 32 | "supports-color": "^5.3.0" 33 | } 34 | }, 35 | "cliui": { 36 | "version": "4.1.0", 37 | "resolved": "https://registry.npmjs.org/cliui/-/cliui-4.1.0.tgz", 38 | "integrity": "sha512-4FG+RSG9DL7uEwRUZXZn3SS34DiDPfzP0VOiEwtUWlE+AR2EIg+hSyvrIgUUfhdgR/UkAeW2QHgeP+hWrXs7jQ==", 39 | "requires": { 40 | "string-width": "^2.1.1", 41 | "strip-ansi": "^4.0.0", 42 | "wrap-ansi": "^2.0.0" 43 | } 44 | }, 45 | "code-point-at": { 46 | "version": "1.1.0", 47 | "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", 48 | "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" 49 | }, 50 | "color-convert": { 51 | "version": "1.9.3", 52 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", 53 | "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", 54 | "requires": { 55 | "color-name": "1.1.3" 56 | } 57 | }, 58 | "color-name": { 59 | "version": "1.1.3", 60 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", 61 | "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" 62 | }, 63 | "cross-spawn": { 64 | "version": "6.0.5", 65 | "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", 66 | "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", 67 | "requires": { 68 | "nice-try": "^1.0.4", 69 | "path-key": "^2.0.1", 70 | "semver": "^5.5.0", 71 | "shebang-command": "^1.2.0", 72 | "which": "^1.2.9" 73 | } 74 | }, 75 | "decamelize": { 76 | "version": "2.0.0", 77 | "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-2.0.0.tgz", 78 | "integrity": "sha512-Ikpp5scV3MSYxY39ymh45ZLEecsTdv/Xj2CaQfI8RLMuwi7XvjX9H/fhraiSuU+C5w5NTDu4ZU72xNiZnurBPg==", 79 | "requires": { 80 | "xregexp": "4.0.0" 81 | } 82 | }, 83 | "end-of-stream": { 84 | "version": "1.4.4", 85 | "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", 86 | "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", 87 | "requires": { 88 | "once": "^1.4.0" 89 | } 90 | }, 91 | "escape-string-regexp": { 92 | "version": "1.0.5", 93 | "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", 94 | "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" 95 | }, 96 | "execa": { 97 | "version": "1.0.0", 98 | "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", 99 | "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", 100 | "requires": { 101 | "cross-spawn": "^6.0.0", 102 | "get-stream": "^4.0.0", 103 | "is-stream": "^1.1.0", 104 | "npm-run-path": "^2.0.0", 105 | "p-finally": "^1.0.0", 106 | "signal-exit": "^3.0.0", 107 | "strip-eof": "^1.0.0" 108 | } 109 | }, 110 | "find-up": { 111 | "version": "3.0.0", 112 | "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", 113 | "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", 114 | "requires": { 115 | "locate-path": "^3.0.0" 116 | } 117 | }, 118 | "get-caller-file": { 119 | "version": "1.0.3", 120 | "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", 121 | "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==" 122 | }, 123 | "get-stream": { 124 | "version": "4.1.0", 125 | "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", 126 | "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", 127 | "requires": { 128 | "pump": "^3.0.0" 129 | } 130 | }, 131 | "has-flag": { 132 | "version": "3.0.0", 133 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", 134 | "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" 135 | }, 136 | "invert-kv": { 137 | "version": "2.0.0", 138 | "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", 139 | "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==" 140 | }, 141 | "is-fullwidth-code-point": { 142 | "version": "2.0.0", 143 | "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", 144 | "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" 145 | }, 146 | "is-stream": { 147 | "version": "1.1.0", 148 | "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", 149 | "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" 150 | }, 151 | "isexe": { 152 | "version": "2.0.0", 153 | "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", 154 | "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" 155 | }, 156 | "lcid": { 157 | "version": "2.0.0", 158 | "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", 159 | "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", 160 | "requires": { 161 | "invert-kv": "^2.0.0" 162 | } 163 | }, 164 | "locate-path": { 165 | "version": "3.0.0", 166 | "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", 167 | "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", 168 | "requires": { 169 | "p-locate": "^3.0.0", 170 | "path-exists": "^3.0.0" 171 | } 172 | }, 173 | "map-age-cleaner": { 174 | "version": "0.1.3", 175 | "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", 176 | "integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==", 177 | "requires": { 178 | "p-defer": "^1.0.0" 179 | } 180 | }, 181 | "mem": { 182 | "version": "4.3.0", 183 | "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", 184 | "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", 185 | "requires": { 186 | "map-age-cleaner": "^0.1.1", 187 | "mimic-fn": "^2.0.0", 188 | "p-is-promise": "^2.0.0" 189 | } 190 | }, 191 | "mimic-fn": { 192 | "version": "2.1.0", 193 | "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", 194 | "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==" 195 | }, 196 | "nice-try": { 197 | "version": "1.0.5", 198 | "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", 199 | "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" 200 | }, 201 | "npm-run-path": { 202 | "version": "2.0.2", 203 | "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", 204 | "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", 205 | "requires": { 206 | "path-key": "^2.0.0" 207 | } 208 | }, 209 | "number-is-nan": { 210 | "version": "1.0.1", 211 | "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", 212 | "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" 213 | }, 214 | "once": { 215 | "version": "1.4.0", 216 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 217 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 218 | "requires": { 219 | "wrappy": "1" 220 | } 221 | }, 222 | "os-locale": { 223 | "version": "3.1.0", 224 | "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", 225 | "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", 226 | "requires": { 227 | "execa": "^1.0.0", 228 | "lcid": "^2.0.0", 229 | "mem": "^4.0.0" 230 | } 231 | }, 232 | "p-defer": { 233 | "version": "1.0.0", 234 | "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", 235 | "integrity": "sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=" 236 | }, 237 | "p-finally": { 238 | "version": "1.0.0", 239 | "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", 240 | "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=" 241 | }, 242 | "p-is-promise": { 243 | "version": "2.1.0", 244 | "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", 245 | "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" 246 | }, 247 | "p-limit": { 248 | "version": "2.2.1", 249 | "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", 250 | "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", 251 | "requires": { 252 | "p-try": "^2.0.0" 253 | } 254 | }, 255 | "p-locate": { 256 | "version": "3.0.0", 257 | "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", 258 | "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", 259 | "requires": { 260 | "p-limit": "^2.0.0" 261 | } 262 | }, 263 | "p-try": { 264 | "version": "2.2.0", 265 | "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", 266 | "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" 267 | }, 268 | "path-exists": { 269 | "version": "3.0.0", 270 | "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", 271 | "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" 272 | }, 273 | "path-key": { 274 | "version": "2.0.1", 275 | "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", 276 | "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" 277 | }, 278 | "pump": { 279 | "version": "3.0.0", 280 | "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", 281 | "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", 282 | "requires": { 283 | "end-of-stream": "^1.1.0", 284 | "once": "^1.3.1" 285 | } 286 | }, 287 | "require-directory": { 288 | "version": "2.1.1", 289 | "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", 290 | "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=" 291 | }, 292 | "require-main-filename": { 293 | "version": "1.0.1", 294 | "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", 295 | "integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=" 296 | }, 297 | "semver": { 298 | "version": "5.7.1", 299 | "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", 300 | "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" 301 | }, 302 | "set-blocking": { 303 | "version": "2.0.0", 304 | "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", 305 | "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=" 306 | }, 307 | "shebang-command": { 308 | "version": "1.2.0", 309 | "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", 310 | "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", 311 | "requires": { 312 | "shebang-regex": "^1.0.0" 313 | } 314 | }, 315 | "shebang-regex": { 316 | "version": "1.0.0", 317 | "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", 318 | "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" 319 | }, 320 | "signal-exit": { 321 | "version": "3.0.2", 322 | "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", 323 | "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" 324 | }, 325 | "string-width": { 326 | "version": "2.1.1", 327 | "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", 328 | "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", 329 | "requires": { 330 | "is-fullwidth-code-point": "^2.0.0", 331 | "strip-ansi": "^4.0.0" 332 | } 333 | }, 334 | "strip-ansi": { 335 | "version": "4.0.0", 336 | "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", 337 | "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", 338 | "requires": { 339 | "ansi-regex": "^3.0.0" 340 | } 341 | }, 342 | "strip-eof": { 343 | "version": "1.0.0", 344 | "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", 345 | "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" 346 | }, 347 | "supports-color": { 348 | "version": "5.5.0", 349 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", 350 | "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", 351 | "requires": { 352 | "has-flag": "^3.0.0" 353 | } 354 | }, 355 | "validator": { 356 | "version": "10.8.0", 357 | "resolved": "https://registry.npmjs.org/validator/-/validator-10.8.0.tgz", 358 | "integrity": "sha512-mXqMxfCh5NLsVgYVKl9WvnHNDPCcbNppHSPPowu0VjtSsGWVY+z8hJF44edLR1nbLNzi3jYoYsIl8KZpioIk6g==" 359 | }, 360 | "which": { 361 | "version": "1.3.1", 362 | "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", 363 | "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", 364 | "requires": { 365 | "isexe": "^2.0.0" 366 | } 367 | }, 368 | "which-module": { 369 | "version": "2.0.0", 370 | "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", 371 | "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=" 372 | }, 373 | "wrap-ansi": { 374 | "version": "2.1.0", 375 | "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", 376 | "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", 377 | "requires": { 378 | "string-width": "^1.0.1", 379 | "strip-ansi": "^3.0.1" 380 | }, 381 | "dependencies": { 382 | "ansi-regex": { 383 | "version": "2.1.1", 384 | "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", 385 | "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" 386 | }, 387 | "is-fullwidth-code-point": { 388 | "version": "1.0.0", 389 | "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", 390 | "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", 391 | "requires": { 392 | "number-is-nan": "^1.0.0" 393 | } 394 | }, 395 | "string-width": { 396 | "version": "1.0.2", 397 | "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", 398 | "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", 399 | "requires": { 400 | "code-point-at": "^1.0.0", 401 | "is-fullwidth-code-point": "^1.0.0", 402 | "strip-ansi": "^3.0.0" 403 | } 404 | }, 405 | "strip-ansi": { 406 | "version": "3.0.1", 407 | "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", 408 | "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", 409 | "requires": { 410 | "ansi-regex": "^2.0.0" 411 | } 412 | } 413 | } 414 | }, 415 | "wrappy": { 416 | "version": "1.0.2", 417 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 418 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" 419 | }, 420 | "xregexp": { 421 | "version": "4.0.0", 422 | "resolved": "https://registry.npmjs.org/xregexp/-/xregexp-4.0.0.tgz", 423 | "integrity": "sha512-PHyM+sQouu7xspQQwELlGwwd05mXUFqwFYfqPO0cC7x4fxyHnnuetmQr6CjJiafIDoH4MogHb9dOoJzR/Y4rFg==" 424 | }, 425 | "y18n": { 426 | "version": "4.0.0", 427 | "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", 428 | "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" 429 | }, 430 | "yargs": { 431 | "version": "12.0.2", 432 | "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.2.tgz", 433 | "integrity": "sha512-e7SkEx6N6SIZ5c5H22RTZae61qtn3PYUE8JYbBFlK9sYmh3DMQ6E5ygtaG/2BW0JZi4WGgTR2IV5ChqlqrDGVQ==", 434 | "requires": { 435 | "cliui": "^4.0.0", 436 | "decamelize": "^2.0.0", 437 | "find-up": "^3.0.0", 438 | "get-caller-file": "^1.0.1", 439 | "os-locale": "^3.0.0", 440 | "require-directory": "^2.1.1", 441 | "require-main-filename": "^1.0.1", 442 | "set-blocking": "^2.0.0", 443 | "string-width": "^2.0.0", 444 | "which-module": "^2.0.0", 445 | "y18n": "^3.2.1 || ^4.0.0", 446 | "yargs-parser": "^10.1.0" 447 | } 448 | }, 449 | "yargs-parser": { 450 | "version": "10.1.0", 451 | "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-10.1.0.tgz", 452 | "integrity": "sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==", 453 | "requires": { 454 | "camelcase": "^4.1.0" 455 | } 456 | } 457 | } 458 | } 459 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "notesapp", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "chalk": "^2.4.1", 13 | "validator": "^10.8.0", 14 | "yargs": "^12.0.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /print5hello.py: -------------------------------------------------------------------------------- 1 | for x in range(6): 2 | print("HELLO WORLD") 3 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject basic-clj-project "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" 5 | :url "https://www.eclipse.org/legal/epl-2.0/"} 6 | :dependencies [[org.clojure/clojure "1.9.0"]] 7 | :repl-options {:init-ns basic-clj-project.bridge}) 8 | -------------------------------------------------------------------------------- /pythagorasTriplet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | long long int N; 5 | cin>>N; 6 | if(N<=2){ 7 | cout<<"-1"< li){ 61 | if(curr==null) return; 62 | if(curr.end==true){ 63 | System.out.println(li); 64 | } 65 | for(int i=0;i<26;i++){ 66 | if(curr.child[i]!=null){ 67 | li.add((char)(i+'a')); 68 | printTrie(curr.child[i],li); 69 | li.remove(li.size()-1); 70 | } 71 | } 72 | 73 | 74 | 75 | } 76 | 77 | 78 | public static void main(String[] args){ 79 | String s="these"; 80 | root=new trieNode(); 81 | insert(s); 82 | insert("hepl"); 83 | insert("thr"); 84 | insert("th"); 85 | //int i=0; 86 | /* while(root.end!=true){ 87 | System.out.print(s.charAt(i)+"--"); 88 | root=root.child[s.charAt(i)-'a']; 89 | i++; 90 | }*/ 91 | System.out.println(startswith("he")); 92 | System.out.println(search("hthese")); 93 | List li=new ArrayList<>(); 94 | printTrie(root,li); 95 | } 96 | 97 | } --------------------------------------------------------------------------------