cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
                <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>
            
          
!
            
              .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;
    }
            
          
!
            
              //変数の範囲の変更
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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console