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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <html>
 <head>
  <meta charset="utf-8"/>
  <script type="application/javascript">
  </script>
  <style type = "application/CSS"></style>
 </head>
 <body onload="draw(1);">
   <canvas id="canvas" width="150" height="150"></canvas>
   
   <div class="introduce">二色を混ぜて<br/>吹き出しの色を作れ!</div>
   
   <div class="circle num1"></div>
   <div class="circle num2"></div>
   <div class="circle num3"></div>
   <div class="circle num4"></div>
   <div class="circle num5"></div>
   
   <div class="circle num6"></div>
   <div class="circle num7"></div>
   <div class="circle num8"></div>
   <div class="circle num9"></div>
   <div class="circle num10"></div>
   
   <div class="circle num11"></div>
   <div class="circle num12"></div>
   <div class="circle num13"></div>
   <div class="circle num14"></div>
   <div class="circle num15"></div>
   
   <div class="circle answer"></div>
   
   <br/>
   <button class = "level 1" onClick = "draw(1);">初級</button>
   <button class = "level 2" onclick = "draw(2);">中級</button>
   <button class = "level 3" onclick = "draw(3);">上級</button>

  </body>
</html>
              
            
!

CSS

              
                .circle{
  position:absolute;
  width: 50px;
  height: 50px;
	border-radius: 50%;
  visibility: hidden;
  
  transition-property: top,left;
  transition-duration:500;
  transition-timing-function:linear;
}

.circle.num1{
  top: 320px;
  left: 10px;
  background-color: #ff0000;
}

.circle.num2{
  top: 320px;
  left: 70px;
  background-color: #ff0000;
}
.circle.num3{
  top: 320px;
  left: 130px;
  background-color: #ff0000;
}
.circle.num4{
  top: 320px;
  left: 190px;
  background-color: #ff0000;
}
.circle.num5{
  top: 320px;
  left: 250px;
  background-color: #ff0000;
}

.circle.num6{
  top: 380px;
  left: 10px;
  background-color: #ff0000;
}

.circle.num7{
  top: 380px;
  left: 70px;
  background-color: #ff0000;
}
.circle.num8{
  top: 380px;
  left: 130px;
  background-color: #ff0000;
}
.circle.num9{
  top: 380px;
  left: 190px;
  background-color: #ff0000;
}
.circle.num10{
  top: 380px;
  left: 250px;
  background-color: #ff0000;
}

.circle.num11{
  top: 440px;
  left: 10px;
  background-color: #ff0000;
}

.circle.num12{
  top: 440px;
  left: 70px;
  background-color: #ff0000;
}
.circle.num13{
  top: 440px;
  left: 130px;
  background-color: #ff0000;
}
.circle.num14{
  top: 440px;
  left: 190px;
  background-color: #ff0000;
}
.circle.num15{
   top: 440px;
  left: 250px;
  background-color: #ff0000;
}

.circle.answer{
  top: 50px;
  left: 230px;
  background-color: #ffffff;
}

.introduce{
  font-size: 150%;
  font-weight: bold;
  color: #fff;
  text-shadow:
  0px 0px 1px #666,
  0px 0px 5px #666,
  0px 0px 6px #666;
}

.level{
  border:solid 1px #ccc;
padding:10px 25px;
margin:0 0 20px;
font-family:Arial, sans-serif;
font-size:1.2em;
text-transform:uppercase;
font-weight:bold;
color:#333;
cursor:pointer;
}

.level:hover{
    color: #ff0000;
}

.score
{
  position:absolute;
  top: 160px;  
  left: 200px;        
  font-size: 150%;
  font-weight: bold;
  visibility: hidden;
}

.score.Good
{
  
}
              
            
!

JS

              
                function draw(level) {
  $(".circle").off();
  var canvas = document.getElementById('canvas');
  if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    
    //仮吹出
    fuki(ctx);

    var StoneNum=LvChoice(level);
    //問題玉生成
    makeStone(StoneNum);
   
    //正解生成
    var ritCo = makeRight(ctx);
    
    //回答作成
    var aEnd = answer(ritCo,level);

  }
}
function LvChoice(level){
  if(level == 1) return 5;
  if(level == 2) return 10;
  if(level == 3) return 15;
}

function makeStone(level){
      for(var i=1; i<16; i++){
      $(".circle.num"+i).css("visibility","hidden");
      }
  
      for(var i=1; i<level+1; i++){
        
        var r = Math.floor(Math.random()*255);
        var g = Math.floor(Math.random()*255);
        var b = Math.floor(Math.random()*255);
      $(".circle.num"+i).css( 'background-color',"rgb("+ r +','+ g + ',' + b +')');
      $(".circle.num"+i).css("visibility","visible");
      }
 
} 

function makeRight(ctx){
  //2色ランダムピック1
  var cPic1 = Math.floor(Math.random()*4)+1;
  
  var color1 = $(".circle.num"+cPic1).css("background-color");
  $("#pic1").html("<br/>"+"I have a <span style='color:" +
                         color1 + ";'>" + color1 + "</span>.");
      
  //背景色の余計な文字列削除
  color1 = color1.replace("rgb(","");
  color1 = color1.replace(")","");
  //文字列分割
  color1 = color1.split(",");
    
  console.log(color1);
    
  //2色ランダムピック2
  do
   var cPic2 = Math.floor(Math.random()*4)+1;
  while(cPic1 == cPic2);
  var color2 = $(".circle.num"+cPic2).css("background-color");
  $("#pic2").html("<br/>"+"I have a <span style='color:" +
                         color2 + ";'>" + color2 + "</span>.");
      
  //背景色の余計な文字列削除
  color2 = color2.replace("rgb(","");
  color2 = color2.replace(")","");
  //文字列分割
  color2 = color2.split(",");
    
  console.log(color2);
    
  var cRight = color1;
      cRight[0] = Math.floor((parseInt(color1[0])+parseInt(color2[0]))/2);
      cRight[1] = Math.floor((parseInt(color1[1])+parseInt(color2[1]))/2);
      cRight[2] = Math.floor((parseInt(color1[2])+parseInt(color2[2]))/2);
  console.log(cRight);
    
  //玉色生成
  var r = cRight[0];
  var g = cRight[1];
  var b = cRight[2];
    
  //お手本玉
  ctx.beginPath();
  ctx.fillStyle = "rgb("+ r +','+ g + ',' + b +')';
  ctx.arc(77,63,18,0,Math.PI*2,true);
  ctx.fill();
  
  return cRight;
}

function fuki(ctx){
  ctx.beginPath();
    ctx.moveTo(75,25);
    ctx.quadraticCurveTo(25,25,25,62.5);
    ctx.quadraticCurveTo(25,100,80,100);
    ctx.quadraticCurveTo(50,120,50,125);
    ctx.quadraticCurveTo(60,120,100,100);
    ctx.quadraticCurveTo(125,90,125,62.5);
    ctx.quadraticCurveTo(125,25,75,25);
    ctx.stroke();
}

function answer(ritCo,Lv){
  
  /*回答作成*/
  var pic1f=0;
    var c1buf = [];
    var colbuf = [];
    //玉から2色とる
    $(".circle").on("click", function () {
      if(pic1f == 0){
         var color1 = $(this).css("background-color");
      
        //背景色の余計な文字列削除
        color1 = color1.replace("rgb(","");
        color1 = color1.replace(")","");
        //文字列分割
        color1 = color1.split(",");   
        
        $(this).animate({
          'left': '230px',
          'top': '50px'
        },{
        'duration': 100
      });
     
        //10進数を16進数に変換して連結
        var color = "#"
          +("0" + parseInt(color1[0]).toString(16)).slice(-2)
          +("0" + parseInt(color1[1]).toString(16)).slice(-2)
          +("0" + parseInt(color1[2]).toString(16)).slice(-2);
        
        pic1f = 1;
        c1buf = color1;
      
      $(".circle.answer").css( 'background-color',color);
    }else{
         var color2 = $(this).css("background-color");
      
        //背景色の余計な文字列削除
        color2 = color2.replace("rgb(","");
        color2 = color2.replace(")","");
        //文字列分割
        color2 = color2.split(",");   
      
      var color1 = c1buf;

        //混色
      var cAns = [];
      cAns[0] = Math.floor((parseInt(color1[0])+parseInt(color2[0]))/2);
      cAns[1] = Math.floor((parseInt(color1[1])+parseInt(color2[1]))/2);
      cAns[2] = Math.floor((parseInt(color1[2])+parseInt(color2[2]))/2);

      pic1f = 0;
      
      $(this).animate({
          'left': '230px',
          'top': '50px'
        },{
        'duration': 100
      });
      
      colbuf=cAns;
      
      cAns = "#"
          +("0" + parseInt(cAns[0]).toString(16)).slice(-2)
          +("0" + parseInt(cAns[1]).toString(16)).slice(-2)
          +("0" + parseInt(cAns[2]).toString(16)).slice(-2);
      $(".circle.answer").css( 'background-color',cAns);
      
       $(".circle.answer").css( "visibility","visible");

      
      /*評価*/
      judge(ritCo,colbuf,Lv);
      
    }
   });
}

function judge(ritCo,ansCo,level){ 
  var errors = 0;
      //誤差の和を出す
      errors = Math.abs(parseInt(ritCo[0]) - parseInt(ansCo[0]))
        + Math.abs(parseInt(ritCo[1]) - parseInt(ansCo[1]))
        + Math.abs(parseInt(ritCo[2]) - parseInt(ansCo[2]));
      console.log("errors "+ errors);
  
  var border;
  if(level == 1) border = 100;
  if(level == 2) border = 60;
  if(level == 3) border = 30;
  
  console.log("border "+ border);
  
      if(errors>=0 && errors <= 10){
        setTimeout("Good();",1300);
      }else if(errors>=11 && errors <= border){
        setTimeout("Better();",1300);
      }else{
        setTimeout("Bad();",1300);
      }
    
  return 1;
}

function Good(){
  alert("Good");
  location.reload(true);
}

function Better(){
  alert("Better");
  location.reload(true);
}

function Bad(){
  alert("Bad");
  location.reload(true);
}
              
            
!
999px

Console