cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-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.

            
              <html>
<style type="text/css">
    canvas {
        border: 1px solid black;
    }
</style>

<body>
    <canvas id="area_dibujo" width="350" height="480" >

    </canvas>

    <script src="canvas.juego.js"  type="text/javascript" charset="utf-8"></script>

</body>

</html>
            
          
!
            
              (function () {

    var canvas = document.getElementById('area_dibujo');
    var ctx = canvas.getContext('2d');

    var radius = 100;
    var colors = ['#FF324A', '#31FFA6', '#206EFF', '#FFFF99'];

    var width = 200;
    var height = 200;
    var x = canvas.width / 2 - width / 2; //Center Contrained
    var y = (canvas.height / 6) * 3.4;


    var rotate = 0;
  
    function Ball(color, radius) {
        this.color = color;
        this.radius = radius;
        this.y = 0;
        this.x = 0;
    }

    Ball.prototype = {
        draw: function () {
            ctx.beginPath();
            ctx.arc(0, this.y, this.radius, 0, Math.PI * 2, true);
            ctx.fillStyle = this.color;
            ctx.fill();
        }
    }

    function SquareTriangles(x, y, squareWidth, squareHeight, colors) {
        this.x = x;
        this.y = y;
        this.squareWidth = squareWidth;
        this.squareHeight = squareHeight;
        this.colors = colors;
    }

    SquareTriangles.prototype = {


        draw: function () {

            //Triangle #1.
            //Vertex 0.0 => 0.1
            ctx.beginPath();
            ctx.moveTo(this.x, this.y);
            ctx.lineTo(this.x, this.y + this.squareHeight);
            ctx.lineTo(this.x + this.squareWidth / 2, this.y + this.squareHeight / 2);
            ctx.moveTo(this.x, this.y);
            ctx.fillStyle = this.colors[0];
            ctx.fill();


            //Triangle #2.
            //Vertex 0.0 => 1.0
            ctx.beginPath();
            ctx.moveTo(this.x, this.y);
            ctx.lineTo(this.x + this.squareWidth, this.y);
            ctx.lineTo(this.x + this.squareWidth / 2, this.y + this.squareHeight / 2);
            ctx.lineTo(this.x, this.y);
            ctx.fillStyle = this.colors[1];
            ctx.fill();



            //Triangle #3.
            //Vertex 1.0 => 1.1
            ctx.beginPath();
            ctx.moveTo(this.x + this.squareWidth, this.y);
            ctx.lineTo(this.x + this.squareWidth, this.y + this.squareHeight);
            ctx.lineTo(this.x + this.squareWidth / 2, this.y + this.squareHeight / 2);
            ctx.lineTo(this.x + this.squareWidth, this.y);
            ctx.fillStyle = this.colors[2];
            ctx.fill();


            //Triangle #4.
            //Vertex 0.1 => 1.1
            ctx.beginPath();
            ctx.moveTo(this.x, this.y + this.squareHeight);
            ctx.lineTo(this.x + this.squareWidth, this.y + this.squareHeight);
            ctx.lineTo(this.x + this.squareWidth / 2, this.y + this.squareHeight / 2);
            ctx.lineTo(this.x, this.y + this.squareHeight);
            ctx.fillStyle = this.colors[3];
            ctx.fill();

        }
    }


    //Create 
    var square = new SquareTriangles(0, 0, width, height, colors);
    var ball = new Ball(colors[0], 20);

  

    function rotateSquare() {
        if (rotate === 360) {
            //Reset
            rotate = 0;
        }
        rotate += 90;
    }

    canvas.onclick = function (event) {
        rotateSquare();
    };
  
    function play(){
       //1. Limpiar todo el contenido del canvas
       ctx.clearRect(0, 0, canvas.width, canvas.height);  
      
       //2. Dibujar el contenido
       //Draw Ball
       ctx.save();
       ctx.translate(canvas.width / 2, ball.radius);
       ball.draw();
       ctx.restore();

       //Draw Square
       ctx.save();
       ctx.translate(x, y);
      
       // rotate
       ctx.rotate((Math.PI / 180) * rotate); 
       square.draw();
       ctx.restore();
      
       //3. Continuar con el siguiente ciclo de animacion 
       window.requestAnimationFrame(play);
    }
  
    window.requestAnimationFrame(play);

})();
            
          
!
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