简体   繁体   English

Canvas Child上的JQuery侦听器不起作用

[英]JQuery Listener on Canvas Child doesn't work

i have a problem i'm implementing a crop custom rectangle on a canvas i made a function in Javascript that when the crop function is called the create on existing canvas a child and then with the JQuery listener i draw the rectangle.The childNode is create correctly while the listener don't work they don't get the events. 我有一个问题,我在画布上实现了一个裁剪自定义矩形,我用Javascript编写了一个函数,当裁剪函数在现有画布上称为create时,它是一个孩子,然后使用JQuery侦听器绘制矩形。childNode是create侦听器无法正常工作时,他们不会收到事件。 This is my code: 这是我的代码:

var dragging = false;
var xstart = 0;
var ystart = 0;
var width = 0;
var height = 0;
var ctxotmp = null;
var ctxtmp = null; 
var canvastmp = null;
var mycanvas = null;
function draw() {
 ctxtmp.fillRect(xstart, ystart, width, height);
}

function init() {
    mycanvas = $('#mycanvas')[0];
   // create temp canvas
    canvastmp = document.createElement('canvas');
    canvastmp.id = "mycanvastmp";
    canvastmp.width = mycanvas.width;
    canvastmp.height = mycanvas.height;
    mycanvas.parentNode.appendChild(canvastmp);
    $("#mycanvastmp").css({position:"absolute",top:$("#mycanvas").css("top"),left:$("#mycanvas").css("left")});
    canvastmp = $('#mycanvastmp')[0];
   ctxtmp = canvastmp.getContext('2d');
   ctxtmp.lineWidth = 1;
   ctxtmp.fillStyle = "rgba(0, 0, 0, 0.5)";
}
//listener
$('#mycanvastmp').mousedown(function(e) {
    var xoffs = $(this).offset().left;
    var yoffs = $(this).offset().top;
    xstart = e.pageX - xoffs;
    ystart = e.pageY - yoffs;
    dragging = true;
  });

  $('#mycanvastmp').mousemove(function(e) {
    if(dragging) {
      var xoffs = $(this).offset().left;
      var yoffs = $(this).offset().top;
      width = e.pageX - xoffs - xstart;
      height = e.pageY - yoffs - ystart;
      ctxtmp.clearRect(0, 0, $(this).width(), $(this).height());
      draw();
    }
  });

  $('#mycanvastmp').mouseup(function() {
    dragging=false;
    alert('The rectangle for crop (x, y, width, height): ' + xstart + ', ' + ystart + ', ' + width + ', ' + height);
  });

Someone can help me? 有人能帮助我吗?

You need to use .on on method in order to bind events to dynamically created objects. 您需要使用.on on方法才能将事件绑定到动态创建的对象。 When your page initially loads and the dom fires, it doesn't see tempCanvas so it doesn't attach them initially. 当页面最初加载并且dom触发时,它不会看到tempCanvas因此它最初不会附加它们。

//listener
$('body').on('mousedown' ,'#mycanvastmp', function(e) {

    var xoffs = $(this).offset().left;
    var yoffs = $(this).offset().top;
    xstart = e.pageX - xoffs;
    ystart = e.pageY - yoffs;
    dragging = true;
  });

  $('body').on('mousemove' ,'#mycanvastmp', function(e) {
    if(dragging) {
      var xoffs = $(this).offset().left;
      var yoffs = $(this).offset().top;
      width = e.pageX - xoffs - xstart;
      height = e.pageY - yoffs - ystart;
      ctxtmp.clearRect(0, 0, $(this).width(), $(this).height());
      draw();
    }
  });

 $('body').on('mouseup' ,'#mycanvastmp', function(e) {
    dragging=false;
    alert('The rectangle for crop (x, y, width, height): ' + xstart + ', ' + ystart + ', ' + width + ', ' + height);
  });

init();

Live Demo 现场演示

Looks like you're attaching the events before the temp canvas is created. 看起来您是在创建临时画布之前附加事件的。 Either attach the events in the init() function after adding the temp canvas to the DOM or use .delegate() or .on() 将临时画布添加到DOM后,将事件附加到init()函数中,或者使用.delegate().on()

$("#mycanvas").on("mouseup", "#mycanvastmp", function() {
    //...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM