-1

So I'm trying to make a rotating arc on a transparent background for a site. Most of it works, except for some reason each iteration of my arc drawing function also draws each previous arc.

Here is the code, and be careful clicking the link because this can lock-up your browser if you stay on the page for too long (only confirmed to do that in Firefox).

http://jsfiddle.net/djaEq/3/

This is the JavaScript:

       function MovingArc(cx, cy, radius, size, angle){
    this.centerX = cx;
    this.centerY = cy;
    this.radius = radius; //thickness
    this.size = size; //width
    this.angle = angle;

    this.startAngle = 0;
    this.endAngle = 0;

    this.checkClockwise = function(){
        var num = Math.round(Math.random());
        return (num == 1);
    }

    this.clockwise = this.checkClockwise();
    this.update = function(){
        if(this.clockwise) this.angle += 2;
        else this.angle -= 2;

        this.startAngle = this.angle - (this.size/2);
        this.endAngle = this.angle + (this.size/2);
    }
}

var myArc;

function init() {
    myArc = new MovingArc(50,50, 50,50, 60);

    update();
    setInterval(update, 1000 / 60);
}

function drawCanvas(){
    var ctx = $('#canvasbanner')[0].getContext("2d"); //get context of canvas
    ctx.clearRect(0,0,400,400);

    ctx.fillStyle = "#000000";;
    ctx.arc(myArc.centerX,myArc.centerY,myArc.radius,myArc.startAngle,myArc.endAngle,false);
    ctx.stroke();
}

function update(){
    myArc.update();
    drawCanvas();
}

$(document).ready(function() {
    console.log("doc ready");
    init();
});
CyanPrime
  • 4,604
  • 12
  • 53
  • 75
  • 1
    [Clearing the canvas](http://stackoverflow.com/questions/2142535/how-to-clear-the-canvas-for-redrawing) might help, perhaps. – Anderson Green Feb 02 '14 at 03:57
  • Yeah, I do that, the JavaScript before was the wrong file, lol. Correct code is up, still broken though. – CyanPrime Feb 02 '14 at 04:02
  • The arc function draws, well arcs (not straight lines). Such as a circle or parts of a circle. The strait lines that are generated are from not closing your path and moving were the start and ending points were the arc is drawn. – Spencer Wieczorek Feb 02 '14 at 04:22
  • Here what a rotating arc might look like: http://jsfiddle.net/djaEq/7/ – Spencer Wieczorek Feb 02 '14 at 04:29

1 Answers1

0

The arc command takes radians for starting and ending angles and it looks like you're sending degree angles.

Make sure to ctx.beginPath() or else you're redrawing every previously draw arc with each iteration.

function drawCanvas(){
    var ctx = $('#canvasbanner')[0].getContext("2d"); //get context of canvas
    ctx.clearRect(0,0,400,400);

    ctx.beginPath();
    ctx.fillStyle = "#000000";;
    ctx.arc(myArc.centerX,myArc.centerY,myArc.radius,myArc.startAngle,myArc.endAngle,false);
    ctx.stroke();
}
markE
  • 94,798
  • 10
  • 135
  • 158
  • 1
    The arc command takes radians for starting and ending angles and it looks like you're sending degree angles. And you will also need to do beginPath(); – markE Feb 02 '14 at 04:20