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, lighter, dark);
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 ;-)

	
	// It is unlikely that you will find this app anywhere.
	// The reason is it is quite tricky to code - this was coded in a day.
	// The reason it can be coded in a day is that we used ZIM.
	// The only other framework that might come close is PaperJS 
	// but that does not drag along a path so they would be stuck there 
	
	// In making the app, we identified a couple future methods for Squiggle and Blob 
	// These are reversing the points and joining points 
	// To reverse, the order needs to be changed and the Bezier rectangles swapped 
	// To join the last rectangles of the end point get replaced
	// with the last rectangles of the first point of the next path 
	// and then that first point of the next path is removed
	
	// ** EDIT - here is an example of the work done to ZIM NFT based on this app
	// https://zimjs.com/explore/pathediting.html
	
	// We may come back and refactor and add more comments later - need to get on to other things...
	
	// CODE HERE
	
	var top = new Rectangle(stageW, 80, dark).addTo();
	new Label("ZIM Tessellator!", null, null, lighter).pos(50,0,LEFT,CENTER,top)

	STYLE = {
		Circle:{
			radius:mobile()?16:8,
			color:light,
			borderColor:dark,
			borderWidth:2
		},
		Blob:{
			borderColor:purple, 
			borderWidth:8,
			interactive:false,
			strokeObj:{miterLimit:.8}
		}
	}

	var r = new Rectangle(500,500,orange,purple,8).center();
	var s = new Squiggle({
		color:purple, 
		thickness:8, 
		points:4, 
		length:500,
		move:false
	}).center();
	const s0 = s.pointControls[0];
	const s3 = s.pointControls[3];

	const b1 = new Circle()
		.loc(s0)
		.drag(new Boundary(r.x,r.y,0,r.height));
	const b2 = new Circle()
		.loc(s3)
		.drag(new Boundary(r.x+r.width,r.y,0,r.height));

	const pane = new Pane(stageW+100,80,"",yellow);
	let part1;
	let part2;    
	let holder = new Container(stageW, stageH).centerReg();
	let ticker2;

	let track1; // top and bottom squiggles in second part (invisible)
	let track2;
	let ball1; // follow path of track1, track2
	let ball2; 
	let sV; // vertical squiggle in second part - end points follow ball1, ball2

	let blob2; // part1 and part2 combined
	let blob3; // part3 and part4 combined
	let eye1; // optional circle for eye
	let eye2; // 
	let eyeLabel; 
	let arrow;

	const picker1 = new ColorPicker({colors:[red, orange, yellow, green, blue, pink, grey, white], cols:8, selectedColor:green});
	const picker2 = picker1.clone();
	picker2.selectedColor = blue;

	const pickerText = new Label("Pick colors\nfor tiles").sca(.8).alp(.7).pos(50,220,LEFT,BOTTOM).removeFrom();    
	picker1.sca(.5).rot(90).pos(50,300,LEFT,BOTTOM).removeFrom();
	picker2.sca(.5).rot(90).pos(110,300,LEFT,BOTTOM).removeFrom();

	const button = new Button({
		label:"CUT",
		backgroundColor:purple,
		rollBackgroundColor:pink,
		corner:10,
		width:170
	})
		.sca(.7)
		.pos(0,50,CENTER,BOTTOM)
		.tap(() => {            
			if (s.parent) { // first cut      
				s.approximateBounds(null,null,-3);
				let b = s.boundsToGlobal();
				if (b.x < r.x || b.x+b.width>r.x+r.width ||
					 b.y < r.y || b.y+b.height>r.y+r.height) {
					pane.text = "Please keep curve within square";
					pane.show();
				} else {
					makeFirstCut();     
				}      
			} else if (button.text == "CUT") { // second cut
				sV.approximateBounds(null,null,-3);
				let b = sV.boundsToGlobal();
				let h = holder.boundsToGlobal();
				if (b.x < h.x || b.x+b.width>h.x+h.width ||
					 b.y < h.y || b.y+b.height>h.y+h.height) {
					pane.text = "Please keep curve within shape";
					pane.show();
				} else {
					makeSecondCut();
				}          
			} else if (button.text == "TILE") {
				makeTessellation();
			} else if (button.text == "SAVE") { 
				button.removeFrom();
				arrow.removeFrom();
				new Loader().save(stage, "ZIM_Tessellation");     
				arrow.addTo();
				button.addTo();
				stage.update();
			} 
		});


	function makeFirstCut() {

		Ticker.remove(ticker1);
		button.enabled = false;

		let points = copy(s.points);
		trimEnds(points);

		points.push([r.width,r.y-s.y]);
		points.push([0,r.y-s.y]);
		part1 = new Blob({
			color:orange,                         
			points:points                       
		})
			.loc(s)
			.addTo(holder)
			.approximateBounds()
			.animate({
				wait:.5,
				props:{y:String(r.height/2)}
			});

		let points2 = copy(s.points);
		trimEnds(points2);

		points2.push([r.width,r.y-s.y+r.height]);
		points2.push([0,r.y-s.y+r.height]);
		part2 = new Blob({
			color:yellow,
			points:points2
		})
			.loc(s)
			.addTo(holder)
			.approximateBounds()
			.animate({
				wait:.5,
				props:{y:String(-r.height/2)}
			})

		r.removeFrom();
		s.removeFrom();
		b1.removeFrom();
		b2.removeFrom();

		let newHeight = part1.height + part2.height;
		let newScale = stageH/newHeight*.7;
		let yDiff = (part2.height-newHeight/2)*newScale; // simplified from longer formula

		holder.animate({
			wait:.5,
			props:{scale:newScale, y:String(yDiff)}, // brackets for relative animation
			call:() => {  
				// make two shapes into one                  
				let one = copy(part2.points);
				one.pop();
				one.pop();
				let two = copy(part1.points);
				two.pop();
				two.pop();
				// need to reverse the points 
				// which mean bezier rectangles need to reverse too 
				// should make a method for this on Squiggle and Blob
				two.reverse();
				loop(two, point=>{
					point[1] += r.height; // make second curve points relative to first curve origin
					[point[6], point[7], point[4], point[5]] = [point[4], point[5], point[6], point[7]];                        
				});

				// reset bounds of Rectangle                                        
				holder.setBounds(null); // set to adjusting bounds
				let b = holder.getBounds();
				holder.setBounds(b.x, b.y, b.width, b.height); // set to non-adjusting bounds

				blob2 = new Blob({
					color:green,
					points:one.concat(two)
				}).approximateBounds().center(holder);
				blob2Original = copy(blob2.points);             


				// add invisible tracks to run the ends of the new squiggle along
				track1 = s.clone().loc(part2.x, part2.y, holder).vis(false);
				track2 = s.clone().loc(part1.x, part1.y, holder).vis(false);
				track1.interactive = false;
				track2.interactive = false;

				part1.removeFrom();
				part2.removeFrom();

				ball1 = new Circle()
					.addTo()
					.vis(false)
					.animate({
					props:{path:track1},
					drag:true
				});
				ball1.percentComplete = 50;    
				ball2 = new Circle()
					.addTo()
					.vis(false)
					.animate({
					props:{path:track2},
					drag:true
				}) 
				ball2.percentComplete = 50; 

				let h1 = holder.globalToLocal(ball1.x, ball1.y);        
				let h2 = holder.globalToLocal(ball2.x, ball2.y);        
				sV = new Squiggle({
					thickness:8, 
					points:[
						[h1.x, h1.y, 0,0, 0,0, 0,75, "free"], 
						[h1.x-100,h1.y+150, 0,0, 0,-75, 0,75], 
						[h2.x+100,h2.y-150, 0,0, 0,-75, 0,75], 
						[h2.x, h2.y, 0,0, 0,-75, 0,0, "free"]
					], 
					length:newHeight, 
					color:purple,
					move:false
				})                        
					.addTo(holder, 2)
					.vis(false);
				let p1 = sV.pointControls[0];
				let length = sV.pointControls.length;
				let p2 = sV.pointControls[length-1];
				ticker2 = Ticker.add(() => {
					p1.loc(ball1);                                
					p2.loc(ball2);
					sV.update();
				});
				timeout(.1, () => {
					ball1.vis(true);
					ball2.vis(true);
					sV.vis(true);
					button.enabled = true;
					stage.update();
				});                                 
			}
		});    
	}

	function makeSecondCut() {

		Ticker.remove(ticker2);
		button.enabled = false;
		button.text = "TILE";

		track1.addPoint(ball1.percentComplete);
		track2.addPoint(ball2.percentComplete); 

		let b = holder.getBounds();

		// SPLIT TOP SQUIGGLE 

		let points = copy(track1.points);  
		let leftPoints = []; // start of top track squiggle (first of left blob)
		let lastPoints = []; // end of top track squiggle (last of right blob)
		let count = 0;
		let sPoints = s.points;
		let first = true;       
		loop(points, p=>{
			let pp = sPoints[count];
			let ppp = copy(p);

			let pppPoint = track1.localToLocal(ppp[0], ppp[1], holder);
			ppp[0]=pppPoint.x; // due to origin of holder not at corner of bounds
			ppp[1]=pppPoint.y;

			if (p[0]==pp[0]&&p[1]==pp[1]) {
				if (first) leftPoints.push(copy(ppp));
				else lastPoints.push(copy(ppp));
				count++;
			} else {
				leftPoints.push(copy(ppp));
				trimEnds(leftPoints);
				lastPoints.push(copy(ppp));
				first = false;
			}
		});
		let sVp = copy(sV.points);

		// give the right stick of the common point the squiggle's position
		// should make a join function in Squiggle and Blob that does this
		leftPoints[leftPoints.length-1][6] = sVp[0][6];
		leftPoints[leftPoints.length-1][7] = sVp[0][7];
		sVp.shift(); // remove the first point             
		leftPoints = leftPoints.concat(sVp);

		// SPLIT BOTTOM SQUIGGLE 

		let points2 = copy(track2.points);            
		let rightPoints = []; // right side backwards of bottom track squiggle 
		let thirdPoints = []; // left side backwards of bottom track squiggle
		count = 0;
		let total = sPoints.length;
		first = true;            
		loop(points2, p=>{ // loop through bottom track backwards (note last loop parameter is true)
			let pp = sPoints[total-count-1];
			let ppp = copy(p);
			let pppPoint = track2.localToLocal(ppp[0], ppp[1], holder);
			ppp[0]=pppPoint.x; // due to origin of holder not at corner of bounds
			ppp[1]=pppPoint.y;
			// bezier rectangles need to reverse too
			[ppp[6], ppp[7], ppp[4], ppp[5]] = [ppp[4], ppp[5], ppp[6], ppp[7]]; 
			if (p[0]==pp[0]&&p[1]==pp[1]) {
				if (first) rightPoints.push(copy(ppp));
				else thirdPoints.push(copy(ppp));
				count++
			} else {
				rightPoints.push(copy(ppp));
				trimEnds(rightPoints);
				thirdPoints.push(copy(ppp));
				first = false;
			}
		}, true);
		let sVp2 = copy(sV.points);
		sVp2.reverse(); // do sV backwards
		loop(sVp2, point=>{
			[point[6], point[7], point[4], point[5]] = [point[4], point[5], point[6], point[7]];                        
		});

		// give the right stick of the common point the squiggle's position
		rightPoints[rightPoints.length-1][6] = sVp2[0][6];
		rightPoints[rightPoints.length-1][7] = sVp2[0][7];
		sVp2.shift(); // remove the first point             
		rightPoints = rightPoints.concat(sVp2);

		// continue on left side with thirdPoints 

		// give the right stick of the common point the squiggle's position
		leftPoints[leftPoints.length-1][6] = thirdPoints[0][6];
		leftPoints[leftPoints.length-1][7] = thirdPoints[0][7];
		leftPoints[leftPoints.length-1][8] = "free";
		thirdPoints.shift(); // remove the first point             
		leftPoints = leftPoints.concat(thirdPoints);
		trimEnds(leftPoints);

		// continue on right side with lastPoints 

		// give the right stick of the common point the squiggle's position
		rightPoints[rightPoints.length-1][6] = lastPoints[0][6];
		rightPoints[rightPoints.length-1][7] = lastPoints[0][7];
		lastPoints.shift(); // remove the first point             
		rightPoints = rightPoints.concat(lastPoints);
		trimEnds(rightPoints);

		blob2.removeFrom();
		sV.removeFrom()
		track1.removeFrom();
		track2.removeFrom();
		ball1.removeFrom();
		ball2.removeFrom();

		let firstY = leftPoints[0][1];
		let lastY = rightPoints[rightPoints.length-1][1];

		part3 = new Blob({
			color:green,                         
			points:leftPoints                   
		})                
			.approximateBounds()
			.addTo(holder)
			.animate({
				wait:.5,
				props:{x:String(r.width/2), y:String((lastY-firstY)/2)}
			});


		part4 = new Blob({
			color:yellow,                         
			points:rightPoints                   
		})                
			.approximateBounds()
			.addTo(holder)
			.animate({
				wait:.5,
				props:{x:String(-r.width/2), y:String((firstY-lastY)/2)}
			});


		// want to animate to final scales and positions 
		// but was really hard to figure out what those were 
		// so just record current properties 
		// use scaleTo() and center() which do the hard calculations 
		// figure out the final properties 
		// reset the properties and animate to the final properties

		// record current
		part3.oX = part3.x;                   
		part3.oY = part3.y;
		part4.oX = part4.x; 
		part4.oY = part4.y;
		holder.oX = holder.x;
		holder.oY = holder.y;
		holder.oS = holder.scale;

		// set to parts to new places
		part3.x += r.width/2;
		part3.y += (lastY-firstY)/2;
		part4.x += -r.width/2;
		part4.y += (firstY-lastY)/2;

		// reset bounds of Rectangle                                        
		holder.setBounds(null); // set to adjusting bounds
		b = holder.getBounds();
		holder.setBounds(b.x, b.y, b.width, b.height); // set to non-adjusting bounds

		// set new scale and position
		holder.scaleTo(stage, 90, 70).center();    

		// record values                
		let nX = holder.x
		let nY = holder.y
		let nS = holder.scale;

		// set back
		holder.sca(holder.oS);
		holder.loc(holder.oX, holder.oY);
		part3.x = part3.oX;
		part3.y = part3.oY;
		part4.x = part4.oX;
		part4.y = part4.oY;                    

		holder.animate({
			wait:.5,
			props:{scale:nS, x:nX, y:nY}, // brackets for relative animation
			call:() => {
				// make final Blob 
				// make two shapes into one                  
				let three = copy(part3.points);      
				loop(three, point=>{
					let nP = part3.localToLocal(point[0], point[1], holder);
					point[0]=nP.x; 
					point[1]=nP.y;
				});              
				let four = copy(part4.points);
				loop(four, point=>{
					let nP = part4.localToLocal(point[0], point[1], holder);
					point[0]=nP.x; 
					point[1]=nP.y;
				});

				// give the last stick of the common point next point's last stick and delete next point
				three[three.length-1][6] = four[0][6];
				three[three.length-1][7] = four[0][7];
				four.shift(); // remove four's first point 

				// give the first stick of the first point the first stick of the last point and delete last point
				three[0][4] = four[four.length-1][4];
				three[0][5] = four[four.length-1][5];
				four.pop(); // remove four's last point                     

				blob3 = new Blob({
					color:orange,
					points:three.concat(four)
				}).approximateBounds().center(holder);

				part3.removeFrom();
				part4.removeFrom();  

				STYLE = {};

				eye1 = new Circle(25, purple)
					.pos(100,100,LEFT,BOTTOM)
					.addTo(holder)
					.drag()
					.alp(0)
					.animate({alpha:1});
				eye2 = new Circle(25, purple)
					.pos(200,100,LEFT,BOTTOM)
					.addTo(holder)
					.drag()
					.alp(0)
					.animate({alpha:1});
				eyeLabel = new Label("add optional eye(s)")
					.alp(0)
					.pos(50,50,LEFT,BOTTOM)
					.addTo(holder)
					.animate({alpha:.5});

				button.enabled = true;

				picker1.addTo().animate({
					wait:1,
					from:true,
					props:{x:-200},
					ease:"backOut",
					time:.5
				});
				picker2.addTo().animate({
					wait:.9,
					from:true,
					props:{x:-200},
					ease:"backOut",
					time:.5
				});
				pickerText.addTo().animate({
					wait:1.2,
					from:true,
					props:{x:-200},
					ease:"backOut",
					time:.5
				});

			}
		});

		stage.update();

	}       

	let e1;
	let e2;
	let tile;
	let colors;

	function makeTessellation() {

		colors = series(picker1.selectedColor,picker2.selectedColor);

		button.text = "SAVE";
		if (blob3.hitTestCircle(eye1)) e1 = eye1.localToLocal(0,0,blob3);
		else e1 = null;
		if (blob3.hitTestCircle(eye2)) e2 = eye2.localToLocal(0,0,blob3);
		else e2 = null;

		let tr = track1.points;
		let dt = tr[tr.length-1][1]-tr[0][1];

		tile = new Tile({
			obj:clonePattern, 
			cols:17,
			rows:16,
			colSize:r.width,
			rowSize:r.height,
			clone:false
		}).sca(.3);
		loop(tile.items2DCols, (col,i)=>{            
			loop(col, pattern=>{
				pattern.mov(0,dt*i);
			});                
		});
		tile.setBounds(null);
		tile.center();

		button.top();

		STYLE = {
			Triangle:{
				borderColor:white, 
				borderWidth:2
			}
		}
		arrow = new Arrow(purple, pink).sha(black.toAlpha(.1),5,5,10).rot(180).sca(1.3).pos(50,40,LEFT,BOTTOM).tap(() => {
			button.text = "TILE";
			tile.removeFrom(); 
			arrow.dispose();     
			stage.update();
		});
		STYLE = {};

		stage.update();
	}

	function clonePattern() {
		let pattern = blob3.clone();
		pattern.color = colors();
		if (e1) eye1.clone().loc(e1, null, pattern);
		if (e2) eye2.clone().loc(e2, null, pattern);
		return pattern;
	}

	function trimEnds(points) {
		points[0][4] = 0; // x
		points[0][5] = 0; // y
		points[0][8] = "free";                    
		points[points.length-1][6] = 0; // x
		points[points.length-1][7] = 0; // y
		points[points.length-1][8] = "free"; 
	}

	const ticker1 = Ticker.add(() => {    
		if (s.parent) {
			s0.loc(b1);
			s3.loc(b2);
			s.update();
		}
	});
    
	// DOCS FOR ITEMS USED
	// https://zimjs.com/docs.html?item=Frame
	// https://zimjs.com/docs.html?item=Container
	// https://zimjs.com/docs.html?item=Circle
	// https://zimjs.com/docs.html?item=Rectangle
	// https://zimjs.com/docs.html?item=Squiggle
	// https://zimjs.com/docs.html?item=Blob
	// https://zimjs.com/docs.html?item=Label
	// https://zimjs.com/docs.html?item=Button
	// https://zimjs.com/docs.html?item=Pane
	// https://zimjs.com/docs.html?item=ColorPicker
	// https://zimjs.com/docs.html?item=Loader
	// https://zimjs.com/docs.html?item=tap
	// https://zimjs.com/docs.html?item=drag
	// https://zimjs.com/docs.html?item=hitTestCircle
	// https://zimjs.com/docs.html?item=boundsToGlobal
	// https://zimjs.com/docs.html?item=animate
	// https://zimjs.com/docs.html?item=loop
	// https://zimjs.com/docs.html?item=sha
	// https://zimjs.com/docs.html?item=pos
	// https://zimjs.com/docs.html?item=loc
	// https://zimjs.com/docs.html?item=mov
	// https://zimjs.com/docs.html?item=top
	// https://zimjs.com/docs.html?item=alp
	// https://zimjs.com/docs.html?item=rot
	// https://zimjs.com/docs.html?item=sca
	// https://zimjs.com/docs.html?item=scaleTo
	// https://zimjs.com/docs.html?item=addTo
	// 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=Arrow
	// https://zimjs.com/docs.html?item=Tile
	// https://zimjs.com/docs.html?item=timeout
	// https://zimjs.com/docs.html?item=copy
	// https://zimjs.com/docs.html?item=Boundary
	// https://zimjs.com/docs.html?item=series
	// https://zimjs.com/docs.html?item=toAlpha
	// https://zimjs.com/docs.html?item=zog
	// https://zimjs.com/docs.html?item=STYLE
	// https://zimjs.com/docs.html?item=Ticker

	// FOOTER
	// call remote script to make ZIM icon - you will not need this
	createIcon(); 
	createGreet();

}); // end of ready
              
            
!
999px

Console