获取canvas元素错误document.body)的偏移量.getElementsByTagName不是函数

时间:2017-11-17 02:18:42

标签: javascript jquery canvas

TypeError: ($(...) || document.body).getElementsByTagName is not a function
    at HTMLDocument.document.getElementsByClassName (prototype.js:888)
    at Function.k [as find] (jquery.js:17)
    at init.find (jquery.js:17)
    at new init (jquery.js:16)
    at e (jquery.js:16)
    at mousemove (MakeAWish.js:1739)
    at HTMLBodyElement.ev_canvas (MakeAWish.js:1901)

这是获得画布偏移时遇到的错误

这是代码

try {
  var hideModal = document.getElementById("bodyForm");
  hideModal.style.display = "none";

  rect.w = (e.pageX - $j('.feedback-canvas').offset().left) - rect.startX;
  rect.h = (e.pageY - $j('.feedback-canvas').offset().top) - rect.startY;

  ctx.clearRect(0, 0, $j('.feedback-canvas').width(), $j('.feedback-canvas').height());
  ctx.fillStyle = 'rgba(102,102,102,0.5)';
  ctx.fillRect(0, 0, $j('.feedback-canvas').width(), $j('.feedback-canvas').height());

  ctx.fillStyle = 'rgba(0,0,0,0.5)';

  item = highlightBox;
  item.width = rect.w;
  item.height = rect.h;

  ctx.drawImage($this.h2cCanvas, // Image
    startingX, startingY, // Start at 10 pixels from the left and the top of the image (crop),
    rect.w, rect.h, // "Get" a `80 * 30` (w * h) area from the source image (crop),
    0, 0, // Place the result at 0, 0 in the canvas,
    rect.w, rect.h); // With as width / height: 160 * 60 (scale)

  // we are only targetting IE>=9, so window.pageYOffset works fine
  item.style.left = window.pageXOffset + rect.startX + "px";
  item.style.top = window.pageYOffset + rect.startY + "px";
  item.style.width = rect.w + "px";
  item.style.height = rect.h + "px";
} catch (e) {
  console.log(e);
}

还有其他方法来获取jquery或其他东西的偏移吗?我需要这个在我的项目中有一个可拖动的突出显示。请帮忙

0 个答案:

没有答案