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 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

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.

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

              
                  <div class="tartanWrap cf">
        <div class="tartan">
            <canvas id="canvas0"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas1"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas2"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas3"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas4"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas5"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas6"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas7"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas8"></canvas>
            <input type="range" class="range"></input>
        </div>
        <div class="tartan">
            <canvas id="canvas9"></canvas>
            <input type="range" class="range"></input>
        </div>
    </div>
    <div class="generateBtn">generate</div>
              
            
!

CSS

              
                .cf:before,
    .cf:after {
        content: " ";
        display: table;
    }

    .cf:after {
        clear: both;
    }

    .cf {
        *zoom: 1;
    }
    .tartanWrap{
      clear: both;
    }
    .tartan {
        margin-bottom: 20px;
        margin-right: 10px;
        float: left;
    }
    .range {
        display: block;
        width: 100px;
        background-image: url();
        background-repeat: no-repeat;
    }
    .generateBtn {
        display: inline-block;
        padding: 10px;
        border: solid 1px #999;
        border-radius: 3px;
        color: #999;
    }
              
            
!

JS

              
                //変数の範囲の変更
function map(value, start1, end1, start2, end2) {
  return Math.floor(start2 + (end2 - start2) * ((value - start1) / (end1 - start1)));
}
//ラジアンを返す
var PI = Math.PI;
function angleToRadians(angle){
  return angle * PI / 180;
}
class Canvas {
	constructor(id, width, height, color) {
		this.elm = document.getElementById(id);
		this.ctx = this.elm.getContext('2d');
		this.width = width;
		this.height = height;
		this.color = color;
		this.init();
	}
	init() {
		this.elm.width = this.width;
		this.elm.height = this.height;
		this.ctx.fillStyle = this.color;
		this.ctx.fillRect(0, 0, this.width, this.height);
	}
	clear() {
		this.ctx.clearRect(0, 0, this.width, this.height);
	}
	repaint(color) {
		this.color = color;
		this.ctx.fillStyle = this.color;
		this.ctx.fillRect(0, 0, this.width, this.height);
	}
}
class DNA{
  constructor(genes){
    this.genesLength = 7;
    if(!genes){
      this.genes = [];
      for (var i = 0; i < this.genesLength; i++) {
        this.genes[i] = Math.floor(Math.random() * 10);
      }
    } else {
      this.genes = genes;
    }
  }
  //交配
  colssover(partnerDNA){
    var childGenes = [];
    var clossPoint = Math.floor(Math.random() * this.genesLength);
    for (var i = 0; i < this.genesLength; i++) {
      if(i < clossPoint){
        childGenes.push(this.genes[i]);
      } else {
        childGenes.push(partnerDNA.genes[i]);
      }
    }
    return new DNA(childGenes);
  }
  //突然変異
  mutate(mutationRate){
    for (var i = 0; i < this.genesLength; i++) {
      if(Math.random() < mutationRate){
        this.genes[i] = Math.floor(Math.random() * 10);
      }
    }
  }
}
class Tartan{
  constructor(dna, index, fitnessrangeElm){
    this.dna = dna;
    this.fitnessRangeElm = fitnessrangeElm.getElementsByClassName('range')[0];
    this.fitness = null;
    this.width = 100;
    this.height = 100;
    this.canvas = new Canvas('canvas' + index, this.width, this.height, 'rgb(255,255,255)');
    this.color1 = {};
    this.color2 = {};
  }
  //表示
  display(){
    //dnaの配列を描写用に変換
    this.color1.red = map(this.dna.genes[0], 0, 10, 0, 255);
    this.color1.green = map(this.dna.genes[1], 0, 10, 0, 255);
    this.color1.blue = map(this.dna.genes[2], 0, 10, 0, 255);
    this.color2.red = map(this.dna.genes[3], 0, 10, 0, 255);
    this.color2.green = map(this.dna.genes[4], 0, 10, 0, 255);
    this.color2.blue = map(this.dna.genes[5], 0, 10, 0, 255);
    this.ratio = map(this.dna.genes[6], 0, 10, 0, 100);

    var lineWidth1 = this.ratio / 2;
    var lineWidth2 = (this.width - this.ratio ) / 2;
    var color1Str = 'rgba('+ this.color1.red +' ,'+ this.color1.green +' ,'+ this.color1.blue +',0.5)';
    var color2Str = 'rgba('+ this.color2.red +' ,'+ this.color2.green +' ,'+ this.color2.blue +',0.5)';

    var drowLine = () => {
      this.canvas.ctx.fillStyle = color1Str;
      this.canvas.ctx.fillRect(0, 0, lineWidth1, this.height);
      this.canvas.ctx.fillStyle = color2Str;
      this.canvas.ctx.fillRect(lineWidth1, 0, lineWidth2, this.height);
      this.canvas.ctx.fillStyle = color1Str;
      this.canvas.ctx.fillRect(lineWidth1 + lineWidth2, 0, lineWidth1, this.height);
      this.canvas.ctx.fillStyle = color2Str;
      this.canvas.ctx.fillRect(lineWidth1*2 + lineWidth2, 0, lineWidth2, this.height);
    }
    drowLine();
    this.canvas.ctx.translate(this.width, 0);
    this.canvas.ctx.rotate(angleToRadians(90));
    drowLine();
    this.canvas.ctx.translate(- this.width, 0);
  }
  //適応度を取得
  getFitness(){
    this.fitness = this.fitnessRangeElm.value;
  }
}
class Population{
  constructor(mutationRate, length){
    this.mutationRate = mutationRate;
    this.matingPool = [];
    this.generations = 0;
    this.population = [];
    this.populationLength = length;
    this.fitnessrangeElm = document.getElementsByClassName('tartan');
    for (var i = 0; i < this.populationLength; i++) {
      this.population[i] = new Tartan(new DNA(), i, this.fitnessrangeElm[i]);
    }
  }
  //すべてを表示
  display(){
    for (var i = 0; i < this.populationLength; i++) {
      this.population[i].display();
    }
  }
  //変更した適応度を更新
  getFitness(){
    for (var i = 0; i < this.populationLength; i++) {
      this.population[i].getFitness();
    }
  }
  //適応度に応じてmatingPoolを作成
  selection(){
    this.matingPool = [];
    //一番高い適応度を調査
    var maxFitness = this.getMaxFitness();
    for (var i = 0; i < this.populationLength; i++) {
      //適応度に応じてmatingPoolに投入
      var fitnessNormal = map(this.population[i].fitness, 0, maxFitness, 0, 100);
      for (var j = 0; j < fitnessNormal; j++) {
        this.matingPool.push(this.population[i]);
      }
    }
  }
  //二つ選ぶ
  reproduction(){
    var newPopulation = [];
    for (var i = 0; i < this.populationLength; i++) {
      //親を選択
      var father = this.matingPool[Math.floor(Math.random() * this.matingPool.length)];
      var mother = this.matingPool[Math.floor(Math.random() * this.matingPool.length)];
      //子供を生成
      var childDna = father.dna.colssover(mother.dna);
      childDna.mutate(this.mutationRate);
      var child = new Tartan(childDna, i , this.fitnessrangeElm[i]);
      //突然変異
      newPopulation[i] = child;
    }
    //母集団を書き換え
    this.population = newPopulation;
  }
  //一番高い適応度を調査
  getMaxFitness(){
    var record = 0;
    for (var i = 0; i < this.populationLength; i++) {
    this.population[i].getFitness();
      if( record < this.population[i].fitness){
        record = this.population[i].fitness;
      }
    }
    return record;
  }
}
//適応度変更UIの用意
var generateBtn = document.getElementsByClassName('generateBtn')[0];
//母集団の用意
var population = new Population(0.2 , 10);
population.display();
//適応度変更が起こったら次の世代を生成する
generateBtn.addEventListener('click', function(e){
  e.preventDefault;
  population.selection();
  population.reproduction();
  population.display();
}, false);

              
            
!
999px

Console