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.

            
              <p id="log"></p>
<canvas id="canvas" width="800" height="300"></canvas>

            
          
!
            
              canvas { border: 1px solid black; 
display: block;
margin: 0 auto;}
            
          
!
            
              var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var dir = 'bottom';
var y;
var v = 0;
var frame = 5;
var frameDash = 1000 / 20;
var maxX = document.getElementById("canvas").clientWidth;
var maxY = document.getElementById("canvas").clientHeight;
var hozi = 1;

// 丸の移動方向がどちらか判別
function direction(){
  document.getElementById("log").innerHTML = y; // 呼び出し可
  // bottom方向ならtrue;
  if(dir=="bottom"){
    v+=frame;
    y = 9.8*((v/frameDash)*(v/frameDash));
      if(y > maxY){
        dir = "top";
      }
  }else{
    v-=frame;
    y = 9.8*((v/frameDash)*(v/frameDash));
    if(v<0){
        dir = "bottom";
    }
  }
  anime(400,y);
  setTimeout(direction,frame);
}

//  x,yの位置に丸を描写  今回、Xは固定で400
function anime(x,y){
  ctx.beginPath();  
  ctx.fillStyle = "rgba(255,0,0,0.2)";
  ctx.clearRect(0,0, maxX, maxY);
  ctx.arc(x,y,5,0,Math.PI*2,true);
  ctx.fill();
}


window.onload = function(){
  direction();
}
            
          
!
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