Home | History | Annotate | Download | only in canvas2d_balls_common
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 var ballRadius;
      6 var ballGradient;
      7 var segmentCount = 20;
      8 
      9 function drawBallInit(diameter) {
     10   ballRadius = diameter / 2;
     11   ballGradient = canvasContext.createRadialGradient(0, 0, 0, 0, 0, ballRadius);
     12   ballGradient.addColorStop(0, "#4040FF");
     13   ballGradient.addColorStop(1, "#00FF40");
     14 }
     15 
     16 function drawBall(x, y, angle) {
     17   canvasContext.save();
     18   canvasContext.fillStyle = ballGradient;
     19   canvasContext.translate(x, y);
     20   canvasContext.rotate(angle);
     21   canvasContext.beginPath();
     22   canvasContext.moveTo(ballRadius, 0);
     23   for (var i = 1; i < segmentCount; ++i) {
     24     var angle = i * 2.0 * Math.PI / segmentCount;
     25     canvasContext.lineTo(ballRadius*Math.cos(angle),
     26         ballRadius*Math.sin(angle));
     27   }
     28   canvasContext.fill();
     29   canvasContext.closePath();
     30   canvasContext.restore();
     31 }
     32