Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                const frame = new Frame("fit", 1024, 768, yellow, grey);
frame.on("ready", ()=>{ // ES6 Arrow Function - similar to function(){}
	zog("ready from ZIM Frame"); // logs in console (F12 - choose console)

	// often need below - so consider it part of the template
	const stage = frame.stage;
	const stageW = frame.width;
	const stageH = frame.height;

	// REFERENCES for ZIM at https://zimjs.com
	// see https://zimjs.com/intro.html for an intro example
	// see https://zimjs.com/learn.html for video and code tutorials
	// see https://zimjs.com/docs.html for documentation
	// see https://codepen.io/topic/zim/ for ZIM on CodePen
	
	// *** NOTE: ZIM Cat defaults to time in seconds
	// All previous versions, examples, videos, etc. have time in milliseconds
	// This can be set back with TIME = "milliseconds" but we suggest you give it a try!
	// There will be a warning in the conslole if your animation is not moving ;-)

	// CODE HERE
	
	new Label("R E P E T I T I O N", 60, "impact", yellow.darken(.3)).pos(0,50, CENTER);
	
	// we will get a new color in this order 
	const colors = series(blue,pink,orange,yellow,red,light,green);
	
	// making two tiles that are almost the same so use STYLE
	STYLE = {
		cols:7, 
		rows:4, 
		colSize:130,
		rowSize:130,
		align:CENTER,
		valign:CENTER
	}

	// make the backing clickable 
	// just a little easier for the user than the shape when the shape changes
	const back = new Tile(new Rectangle(125, 125, purple, grey)).center().cur();	
	const tile = new Tile(new Circle(50, green)).center().noMouse();
	
	// turn off the STYLE - or use Style.clear()
	STYLE = {};
	
	// this will hold our data for each change that we want 
	// in the format of [tile index going backwards and the forwards] then shape index and color
	// we store one of these each time we click on a tile
	// [ [[4,6],1,color], [] ]
	const tasks = [];
	
	// set up an auto activate so it animates to start - good for CodePen people to see some motion 
	// choose a random tile number in the middle to start
	const r = rand(9, tile.items.length-9);
	let auto = interval(1, ()=>{
		activate(r);
	}, 3, true); // three times immediately
	
	// also call activate when press down on a tile
	// do not want to continue auto activating when users clicks so clear the auto interval
	back.on("mousedown", e=>{		
		activate(e.target.tileNum);		
		if (auto) {
			auto.clear();
			auto = null;
		}
	});
		
	function activate(tileIndex) {
		
		// pick the next color from the series 
		// get the tile at the index 
		// and swap for a new random shape		
		let color = colors();				
		let current = tile.items[tileIndex];
		let shapeIndex = rand(shapes.length-1);		
		swapShape(tileIndex, shapeIndex, color);
		stage.update();	
		
		// add the task to the tasks
		tasks.push( [[tileIndex-1, tileIndex+1], shapeIndex, color] );
	}	
	
	// use an interval to handle all updates
	// we will use a Slider to change the speed of the interval 
	// so store it in a variable and call a named function 
	// this allows us to clear the interval and call it again with a new speed 
	// we could use ZIM VEE to dynamically change the speed of the interval 
	// but that always waits for the current interval to finish 
	// before moving to the new interval speed - so can see sluggish
	// So... we will clear the interval and remake it in the slider event function later
	let inter = interval(.2, change);
	
	function change() {
		// loop through each task
		// each time reducing or increasing the back and fore values
		// and setting the appropriate shapes 
		loop(tasks, (task, i)=>{
			let back = task[0][0]--;
			let fore = task[0][1]++;
			if (back >= 0) {
				swapShape(back, task[1], task[2]);
			} 
			if (fore < tile.items.length) {
				swapShape(fore, task[1], task[2]);
			} 
			// remove the task when all done
			if (back < 0 && fore >= tile.items.length) tasks.splice(i,1);						
		}, true); // loop backwards when potentially removing
		
		stage.update();
	}
	
	// the shapes to randomly pick from
	// centerReg the Rectangle so they place on others which are already centerReg
	const shapes = [
		new Circle(50),
		new Rectangle(100,100).centerReg({add:false}),
		new Triangle(100,100,100),
		new Poly(60,[5,6,7,8],{min:.4, max:.6}),
		new Poly(60,[5,6,7,8],{min:.4, max:.6})
	];	
	
	function swapShape(tileIndex, shapeIndex, color) {
		// clone the shape - using true to make sure Poly is exact per task
		let shape = shapes[shapeIndex].clone(true); 
		shape.color = color;
		// record the shape we are replacing
		let replace = tile.items[tileIndex];
		// loc will place at the same location
		// need to add it to the tile
		shape.loc(replace, null, tile);
		// remove the old shape
		replace.removeFrom().dispose();
		// set the tile item
		tile.items[tileIndex] = shape;
		// and set the tileNum on the shape (used in activate)
		shape.tileNum = tileIndex;
	}
	
	// create the slider
	STYLE = {
		backgroundColor:purple,
		rollBackgroundColor:pink,
		borderColor:dark
	}	
	new Slider({
		min:.5,
		max:0,
		step:.05,
		useTicks:true,
		semiTicks:false,
		button:"aztec",
		currentValue:.2
	}).pos(70, 40, CENTER, BOTTOM).change(e=>{
		// clear the old interval
		inter.clear();
		// remake the interval with the slider speed
		inter = interval(e.target.currentValue, change);
	});	
	
	stage.update(); // this is needed to show any changes

	// DOCS FOR ITEMS USED
	// https://zimjs.com/docs.html?item=Frame
	// https://zimjs.com/docs.html?item=Circle
	// https://zimjs.com/docs.html?item=Rectangle
	// https://zimjs.com/docs.html?item=Triangle
	// https://zimjs.com/docs.html?item=Poly
	// https://zimjs.com/docs.html?item=Label
	// https://zimjs.com/docs.html?item=Slider
	// https://zimjs.com/docs.html?item=change
	// https://zimjs.com/docs.html?item=noMouse
	// https://zimjs.com/docs.html?item=loop
	// https://zimjs.com/docs.html?item=cur
	// https://zimjs.com/docs.html?item=pos
	// https://zimjs.com/docs.html?item=loc
	// https://zimjs.com/docs.html?item=removeFrom
	// https://zimjs.com/docs.html?item=centerReg
	// https://zimjs.com/docs.html?item=center
	// https://zimjs.com/docs.html?item=Tile
	// https://zimjs.com/docs.html?item=rand
	// https://zimjs.com/docs.html?item=interval
	// https://zimjs.com/docs.html?item=series
	// https://zimjs.com/docs.html?item=darken
	// https://zimjs.com/docs.html?item=zog
	// https://zimjs.com/docs.html?item=STYLE
	
	STYLE = {};
	
	// FOOTER
	// call remote script to make ZIM icon - you will not need this
	createIcon(); 
	createGreet();

}); // end of ready
              
            
!
999px

Console