HTML5 canvas jQuery getContext on classes

前端 未结 3 373
清酒与你
清酒与你 2021-01-14 16:06

This code works:


    

        
相关标签:
3条回答
  • 2021-01-14 16:34

    You'll need to use .each() to iterate over all elements with that class.

    $('.myCanvas').each(function() {
       var canvas = $(this)[0];
       var context = canvas.getContext('2d');
      // Do stuff
    });
    

    ​ Working JSFiddle.

    0 讨论(0)
  • 2021-01-14 16:41

    That is because you are using class in jquery.

    Change this

    var context = $('.dropzone').getContext('2d');

    to

    var context = $('#dropzone').getContext('2d');

    0 讨论(0)
  • 2021-01-14 16:50

    Use .each()

    $('.dropzone').each(function(index, element) {
        var context = element.getContext('2d');
        context.beginPath();
        context.moveTo(0, 200);
        context.lineTo(578, 0);
        context.stroke();
    
        context.beginPath();
        context.moveTo(0, 0);
        context.lineTo(578, 200);
        context.stroke();   
    });
    

    Here's an example on jsfiddle.

    0 讨论(0)
提交回复
热议问题