harsha
harsha

Reputation: 963

Can't draw more than eight rectangles on a canvas

I am trying to draw simple rectangles on a canvas. Here is my code. Code executes completely. But it draws only 7 rectangles completely and 8th one half. Last two rectangles are not drawn. What am I doing wrong? I tried on IE9 beta, FF3 and Chrome 9. Please help.

<html>
<head>
<script src="javascript/jquery-1.4.4.min.js"></script>
<script type="text/javascript" language="javascript">

$(document).ready(function () {
    drawsegment($('#divTree'));
});

function drawsegment(widget) {
    var $ctx = $('<canvas />', {
        width: '300',
        height: '200'
    });
    widget.html($ctx);
    var ctx = $ctx[0].getContext('2d');

    ctx.fillStyle = "red";
    ctx.fillRect(0, 0, 255, 20);
    ctx.fillStyle = "yellow";
    ctx.fillRect(0, 20, 255, 20);
    ctx.fillStyle = "blue";
    ctx.fillRect(0, 40, 255, 20);
    ctx.fillStyle = "green";
    ctx.fillRect(0, 60, 255, 20);
    ctx.fillStyle = "violet";
    ctx.fillRect(0, 80, 255, 20);
    ctx.fillStyle = "white";
    ctx.fillRect(0, 100, 255, 20);
    ctx.fillStyle = "black";
    ctx.fillRect(0, 120, 255, 20);
    ctx.fillStyle = "gray";
    ctx.fillRect(0, 140, 255, 20);
    ctx.fillStyle = "yellow";
    ctx.fillRect(0, 160, 255, 20);
    ctx.fillStyle = "blue";
    ctx.fillRect(0, 180, 255, 20);
}

</script>
</head>
<body>
    <div id="divTree"></div>
</body>
</html>

Upvotes: 0

Views: 313

Answers (1)

acm
acm

Reputation: 6637

Canvas width and height should be defined attributes of canvas element itself instead of styled attributes:

var $ctx = $('<canvas />');
widget.html($ctx);
widget.children('canvas').attr('width',300).attr('height',200);

Upvotes: 1

Related Questions