css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="mycanvas"></canvas> 

            
          
!
            
              /**A program that takes an Array of book objects and creates number of shelves according to the number of books and also orgnizes all the books. Part of a challange from Khan Academy. */

var sketchProc = function(processingInstance) {
	with(processingInstance) {
	size(400, 400); 
   frameRate(30);
		
		//An array of books
		var book = [{
			title: "The Giver",
			stars: 4,
			author: "Baboon Balon Jonas",
			recomended: true
		}, {
			title: "Just like that",
			stars: 2,
			author: "Mr Ice tea",
			recomended: false
		}, {
			title: "Bridge",
			stars: 1,
			author: "Crazy old dude",
			recomended: false
		}, {
			title: "kapara",
			stars: 1,
			author: "Filter all the noise",
			recomended: false
		}, {
			title: "Short term",
			author: "Memory",
			stars: 4,
			recomended: true
		}, {
			title: "hairy potter",
			author: "SSD Drive",
			stars: 2,
			recomened: false
		}, {
			title: "Smell flowers",
			author: "Nice!",
			stars: 4,
			recomended: true

		}

							 ];

		//Calulates how many shelfs there should be according to the number of books. 3 books fit in //one shelf.
		var numOfShelfs = 0;
		var numOfBooks = book.length;
		if (numOfBooks > 3) {
			numOfShelfs = Math.floor(numOfBooks / 3);
			if (numOfBooks % 3) {
				numOfShelfs++;
			}
		} else if (numOfBooks <= 3 && numOfBooks > 0) {
			numOfShelfs = 1;
		} else {
			numOfShelfs = 0;
		}

		/*test to check the amount of shelfs
    		println(numOfShelfs);
    		*/

		//draws as many shelves as necessary
		for (var i = 0; i < numOfShelfs; i++) {
			// draw shelf
			fill(173, 117, 33);
			rect(0, 120 + (i * 110), width, 10);
		}

		// draws the books in the array

		//the variable x controlls the xposition of the book. After adding a 4th book to the
		//array, the x position of the rectangle should rest (if x>2) and the book should be //presented on the left side of the screen. 
		//the y varible controlls the y position of the book. every 3 books the y 
		//variable should increase in 1. 
		var x = 0;
		var y = 0;
		for (var k = 0; k < book.length; k++) {

			if (x > 2) {
				x = 0;
				y++;
			}
			fill(214, 255, 219);
			rect(10 + (x * 140), 20 + 110 * y, 90, 100);
			fill(0, 0, 0);
			text(book[k].title, 25 + (x * 140), 29 + 110 * y, 70, 100);
			text(book[k].author, 25 + (x * 140), 57 + 110 * y, 75, 100);
/*
			for (var i = 0; i < book[k].stars; i++) {
				image(getImage("cute/Star"), 13 + (i * 20) + (x * 142), 90 + 100 * y, 20, 30);
				if (book[k].recomended) {
					image(getImage("avatars/old-spice-man-blue"), (x * 142) + 20, 90 + 100 * y, 20, 30);
				}
			}
*/
			x++;
		}
	}
};
// Gets the canvas that Processing-js will use
var canvas = document.getElementById("mycanvas");

var processingInstance = new Processing(canvas, sketchProc);



            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console