Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

            
              <div class="container"></div>
<button>Generate</button>
            
          
!

CSS

            
              .container
	position: absolute
	left: 50%
	top: 50%
	transform: translate(-50%, -50%)
	z-index: 0
button
	background-color: red
	color: #fff
	border: none
	padding: 0.5em 2em
	border-radius: 3px
	transition: 0.3s ease
	cursor: pointer
	position: relative
	z-index: 100
	&:hover
		background-color: rgb(255, 40, 140)
            
          
!

JS

            
              
class CreateCanvas {
	constructor ({type, rootElement, width, height}) {
		this.root = document.querySelector(`${type}${rootElement}`);
		this.canvas = null;
		this.ctx = null;
		this.size = {
			width: width,
			height: height
		};
		this.createCanvas();
		this.getCanvas = this.getCanvas.bind(this);
		this.clearCanvas = this.clearCanvas.bind(this)
	}
	createCanvas () {
		this.canvas = document.createElement('canvas');
		this.canvas.width = this.size.width;
		this.canvas.height = this.size.height;
		this.ctx = this.canvas.getContext('2d');
		this.root.appendChild(this.canvas);
	}
	clearCanvas () {
		this.ctx.clearRect(0, 0, this.size.width, this.size.height);
	}
	getCanvas () {
		return {
			link: this.canvas,
			ctx: this.ctx,
			size: this.size
		}
	}
}



class FaceGenerator {
	constructor ({link, ctx, size}) {
		this.link = link;
		this.ctx = ctx;
		this.size = size;
		this.center = this.getCenter();
    this.tay = Math.PI * 2;
    this.eye = this.eye.bind(this);
		//this.init();
	}
	eye (lx, ly, rx, ry) {
    let bornCoord = {lx: 50, ly: 50, rx: 160, ry: 70};
    let eyebrow = {
      a: this.getRandomToFrom(10, 40), 
      b: this.getRandomToFrom(15, 25)
    };
		let leftCorner = {x: lx, y: ly};
    let rightCorner = {x: rx, y: ry};
    let eyeWidth = rightCorner.x - leftCorner.x;
    let eyeHeight;
      if((rightCorner.y - leftCorner.y) != 0 || (rightCorner.y - leftCorner.y) > 0 ){
        eyeHeight = rightCorner.y - leftCorner.y;
      }else{
        eyeHeight = 10;
        } 
    let topCurveVar = {
      a: this.getRandomToFrom (1, 1.4), 
      b: this.getRandomToFrom (0.2, 1),
      c: this.getRandomToFrom (0.5, 1),
      d: this.getRandomToFrom (0.4, 1)
    };
    let disTop = {
      x: eyeWidth * topCurveVar.a + (leftCorner.x - bornCoord.lx),
      y: eyeHeight * topCurveVar.b + (leftCorner.y - bornCoord.ly)
    };
    let disBottom = {
      x: eyeWidth * topCurveVar.c + this.getRandomToFrom(eyeHeight * 0.3, eyeHeight) + (rightCorner.x - bornCoord.rx),
      y:  eyeHeight * topCurveVar.d + this.getRandomToFrom(eyeWidth * 0.7, eyeWidth) + (rightCorner.y - bornCoord.ry)
    };
		
   //this.createHelperPoints(disTop, disBottom);
		
    let center = {
      x: disBottom.x + (disTop.x - disBottom.x) / 2,
      y: disBottom.y - (disBottom.y - disTop.y) / 2
    };

    this.ctx.beginPath();
		this.ctx.lineWidth = 3;
    this.ctx.moveTo(leftCorner.x, leftCorner.y);
    this.ctx.quadraticCurveTo(
      disTop.x,
      disTop.y,
      rightCorner.x, 
      rightCorner.y
    );
		this.ctx.moveTo(leftCorner.x, leftCorner.y);
    this.ctx.quadraticCurveTo(
      disBottom.x, 
      disBottom.y, 
      rightCorner.x, 
      rightCorner.y
    );
		this.ctx.stroke();
    this.ctx.closePath();

    /*********** * pupil * ***************/

    this.ctx.beginPath();
    this.ctx.arc(center.x, center.y, eyeHeight * 0.9, 0, this.tay);
    this.ctx.stroke();
    this.ctx.closePath();
    this.ctx.beginPath();
    this.ctx.fillStyle = `hsl(${this.getRandomToFrom(0, 360)}, 50%, 50%)`;
    this.ctx.arc(center.x, center.y, eyeHeight / 2.2, 0, this.tay);
    this.ctx.fill();
    this.ctx.stroke();
    this.ctx.closePath();

    /***********  * eyebrow *  *************/

    this.ctx.beginPath();
		this.ctx.lineWidth = 4;
    this.ctx.moveTo(leftCorner.x, leftCorner.y - eyebrow.a);
    this.ctx.quadraticCurveTo(
      eyeWidth * topCurveVar.a + (leftCorner.x - bornCoord.lx), 
      eyeHeight * topCurveVar.b + (leftCorner.y - bornCoord.ly), 
      rightCorner.x, 
      rightCorner.y - eyebrow.b
    );
    this.ctx.stroke();
    this.ctx.closePath();
	}
	createHelperPoints(disTop, disBottom) {
		this.ctx.beginPath();
    this.ctx.fillStyle = 'red';
    this.ctx.arc(disTop.x, disTop.y, 3, 0, this.tay);
    this.ctx.fill();
    this.ctx.closePath();
    this.ctx.beginPath();
    this.ctx.fillStyle = 'green';
    this.ctx.arc(disBottom.x, disBottom.y, 3, 0, this.tay);
    this.ctx.fill();
    this.ctx.closePath();
	}
	getCenter () {
		return this.size.width / 2;
	}
	getRandom(c){
		return Math.floor(Math.random()*(c - (-c)) + (-c));
	}
	getRandomToFrom (min, max) {
		return Math.random()*(max - (min)) + (min);
	}
	init () {
		for( let y = 0; y < 6; y++ ){
			let ay = 130 * y + 10;
			let by = ay + 110;
			for(let i = 0; i < 9; i++){
				let a =  85 * i + 50;
				let b =  a + 20;
				this.eye(ay, a, by, b);
			}
		}
	}
}

let btn = document.querySelector('button');
let config = {
	type: '.',
	rootElement: 'container',
	width: 800,
	height: 800
};

let canvas = new CreateCanvas(config);

let canvasElement = canvas.getCanvas();

//canvasElement.link.style.border = '1px solid red';

let pic = new FaceGenerator(canvasElement);

pic.init();

btn.addEventListener('click', () => {
	canvas.clearCanvas();
	pic.init();
});
            
          
!
999px

Console