jeudi 23 avril 2015

JQuery checkbox and shapes id

When I click Circle and Rectangle in checkbox ,I want to added in canvas only Circle and Rectangle. I tried to equalize id of shapes in html and switch case, but I can't.How can I do?

<input type="checkbox" value="Circle" class="chk" id='1'>Circle</input>
<input type="checkbox" value="Rectangle" class="chk" id='2'>Rectangle</input>
<input type="checkbox" value="Star" class="chk" id='3'>Star</input>

<canvas id="c" width=600" height="600" align="left"></canvas>
<script type="text/javascript">
var canvas = new fabric.Canvas('c');

var myObjects = [];

$(".chk").click(function() {
    myObjects=[];
    $('.chk:checked').each(function() {
        myObjects.push($(this).val());
    });
});
function addShape() {

    var pointer = myObjects.length * Math.random();
    var newShape = null;
    switch (pointer){
        case 0:
            newShape = new fabric.Circle({
                  id: '1',
                  radius: 20,
                  left: posX, 
                  top: posY
                });
            break;

        case 1:
            var newShape = new fabric.Rect({
              id: '2',
              left: posX, 
              top: posY,
              width: 50,
              height: 50,
            });
            break;
}
}




Aucun commentaire:

Enregistrer un commentaire