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

            
              <label>
	Nombre de cercles : 
	<input type="number" min=1 max = 30 value=10 width=5 oninput="createCercles(this.value);">
</label>
<br>
<label>
	Couleur : 
	<input type="color" >
</label>
<br>
<label>
	Vitesse : 
	1 <input type="range" value=2 min=1 max=10 oninput="changeVitesse(this.value)"> 10
</label>
<p>
<canvas id="myCanvas" width=800 height=300></canvas>
            
          
!

CSS

            
              canvas {
	border:1px solid;
}

input:invalid {
	background-color: pink;
}

input:valid {
	background-color: lightgreen;
}
            
          
!

JS

            
              window.onload = init;
var gf;

function init() {
	gf = new GameFramework();
	
	gf.init();
}

function GameFramework() {
	let canvas, ctx, w, h;
	let tableauObjetGraphiques = [];

	function init() {
			canvas = document.querySelector("#myCanvas");
			ctx = canvas.getContext("2d");
			w = canvas.width;
			h = canvas.height;
	
			//rect1 = new Rectangle(10, 10, "red", 10);
			//rect2 = new Rectangle(20, 40, "green", 5); 
		
		//creerDesRectangles(10); 
		//creerDesCercles(30);

			requestAnimationFrame(anime);
	}

  function anime(timmeElapsed) {
		ctx.clearRect(0, 0, w, h);
	
		// dessin
		//rect1.draw(ctx);
	//rect2.draw(ctx);
		tableauObjetGraphiques.forEach(function(r) {
			r.draw(ctx);
			r.move();
			r.testeCollisionZone(w, h);
		});
		
		// deplacement
	  //rect1.move();
	//rect2.move();
		
	  // collisions
	  //rect1.testeCollisionZone(w, h);
		  //rect2.testeCollisionZone(w, h);

		requestAnimationFrame(anime);
	}
	
	function creerDesRectangles(n) {
		for(let i = 0; i < n; i++) {
			let x = w * Math.random();
			let y = h *Math.random();
			let r = Math.round(255 * Math.random());
			let g = Math.round(255 * Math.random());
			let b = Math.round(255 * Math.random());
			let couleur = "rgb(" + r + "," + g + "," + b + ")";
			console.log("couleur = " + couleur);
			let vx = 20 * Math.random();
			let vy = 20 * Math.random();

			let rect = new Rectangle(x, y, couleur, vx, vy);

			tableauObjetGraphiques.push(rect);
		}
	}
	
	function creerDesCercles(n) {
		for(let i = 0; i < n; i++) {
			let x = w * Math.random();
			let y = h *Math.random(); 
			let r = Math.round(255 * Math.random());
			let g = Math.round(255 * Math.random());
			let b = Math.round(255 * Math.random());
			let couleur = "rgb(" + r + "," + g + "," + b + ")";
			console.log("couleur = " + couleur); 
			let vx = 20 * Math.random();
			let vy = 20 * Math.random();
			let rayon = 10+60*Math.random();

			let c = new Cercle(x, y, rayon, couleur, vx, vy);

			tableauObjetGraphiques.push(c);
		}
	}
	
	function reset() {
		tableauObjetGraphiques = []; // on vire tous les objets
	}
	// return API methodes et attributs utilisables à l'extérieur
	// Avancé, black box model, cf: 
	return {
		init:init,
		reset:reset,
		creerDesRectangles: creerDesRectangles
	}
}



class ObjetGraphique {
	constructor(posx, posy, coul, vx, vy, l, h) {
		this.x = posx;
		this.y = posy;
		this.couleur = coul;
		this.vitesseX = vx;
		this.vitesseY = vy;
		this.width = l;
		this.height = h;
	}
	
	draw(ctx) {
		ctx.save();
		ctx.fillStyle = "black";
		ctx.fillRect(this.x, this.y, 10, 10);
		ctx.restore();
	}
	
	move() {
		this.x += this.vitesseX;
		this.y += this.vitesseY;
	}
	
	inverseSensDeplacementX() {
		this.vitesseX = -this.vitesseX;
	}
	
	inverseSensDeplacementY() {
		this.vitesseY = -this.vitesseY;
	}
	
	testeCollisionZone(w, h) {
		if(((this.x+this.width) >  w) || (this.x < 0)) {
			this.inverseSensDeplacementX();
		}
		if(((this.y+this.height) >  h) || (this.y < 0)) {
			this.inverseSensDeplacementY();
		}
	}
}

class Rectangle extends ObjetGraphique {
	constructor(posx, posy, coul, vx, vy) {
		super(posx, posy, coul, vx, vy, 50, 50);
		
	}
	
	draw(ctx) {
		ctx.save();
		ctx.translate(this.x, this.y);
		
		ctx.fillStyle = this.couleur;
		ctx.fillRect(0, 0, this.width, this.height);
		
		ctx.restore();
		
		super.draw(ctx);
	}
}

class Cercle extends ObjetGraphique {
	constructor(posx, posy, r, coul, vx, vy) {
		super(posx, posy, coul, vx, vy, 2*r, 2*r);
		this.rayon = r;
	}
	
	draw(ctx) {
		ctx.save();
		ctx.translate(this.x, this.y);
		
		ctx.fillStyle = this.couleur;
		ctx.beginPath();
		ctx.arc(0, 0, this.rayon, 0, 2*Math.PI);
		ctx.fill();
		
		ctx.restore();
		
		super.draw(ctx); 
	}
	
		testeCollisionZone(w, h) {
			/* marche mais merde quand l'objet est à cheval sur un mur
			if(((this.x+this.width/2) >  w) || (this.x-this.width/2 < 0)) {
				this.inverseSensDeplacementX();
			}
			if(((this.y+this.height/2) >  h) || (this.y-this.height/2 < 0)) {
				this.inverseSensDeplacementY();
			}
			*/
			
			// Mur de droite
			if((this.x+this.width/2) >  w) {
				//on remet à la position de contaxt
				this.x = w - this.width/2;
				
				// on inverse le sens de déplacement
				this.inverseSensDeplacementX();
			}
			
			// mur de gauche
			if((this.x-this.width/2) < 0) {
				// position de contact
				this.x = this.width/2;
				
				this.inverseSensDeplacementX();
			}
			
			// en bas
			if((this.y+this.height/2) >  h) {
				this.y = h - this.height/2;
				this.inverseSensDeplacementY();  
			}
			
			// en haut
			if((this.y-this.height/2) < 0) {
				this.y = this.height/2;
				this.inverseSensDeplacementY();
			}
	}
}

function createCercles(nb) {
	console.log(nb);
	gf.reset();
	gf.creerDesRectangles(nb);
}

function changeVitesse(v) {
	console.log("vitesse = " + v);
}
            
          
!
999px

Console