cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

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

Console