簡體   English   中英

畫布繪制對象的鼠標事件

[英]Mouse events for canvas draw objects

我想使用這個插件創建基於畫布的網站菜單,以創建“乘法”效果。 然而,這個以及globalCompositeOperation正在同一個畫布中處理ctx對象。 (在上下文混合器中,它使用離屏畫布並使用其繪制信息,globalcompoperation混合相同的ctx)。 我想為每個ctx對象創建鼠標事件(懸停和單擊),因此每個ctx將導致不同的URL。

這是我的測試:

      function draw(){
      var ctx = document.getElementById('canvasOff1').getContext('2d');
      var ctx2 = document.getElementById('canvasReal').getContext('2d');
      var ctx3 = document.getElementById('canvasOff3').getContext('2d');

      // draw circles - each circle should link to different url and has its own focus
      ctx.fillStyle = "#c7302a";
      ctx.beginPath();
      ctx.arc(50,75,35,0,Math.PI*2,true);
      ctx.fill();

      ctx2.fillStyle = "#395797";
      ctx2.beginPath();
      ctx2.arc(100,75,35,0,Math.PI*2,true);
      ctx2.fill();

  ctx3.fillStyle = "#454";
      ctx3.beginPath();
      ctx3.arc(150,75,35,0,Math.PI*2,true);
      ctx3.fill();

    var over = canvasOff1.getContext('2d'); 
    var under = canvasReal.getContext('2d');
    over.blendOnto(under,'multiply');

    var over2 = canvasOff3.getContext('2d'); 
    var under2 = canvasReal.getContext('2d');
    over2.blendOnto(under2,'multiply',{destX:0,destY:0});
    }

很高興知道如何在這里實現jQuery。 謝謝。

您不能將事件偵聽器添加到上下文,只能添加到畫布:

document.getElementById('canvasOff1').addEventLsitener(
    'click',
    function(){ goToUrl('http://www.test1.com'); }
);
document.getElementById('canvasReal').addEventLsitener(
    'click',
    function(){ goToUrl('http://www.test2.com'); }
);
document.getElementById('canvasOff3').addEventLsitener(
    'click',
    function(){ goToUrl('http://www.test3.com'); }
);

function goToUrl(url){
    window.location = url;
}

或者,使用jQuery:

$('#canvasOff1').on(
    'click',
    function(){ goToUrl('http://www.test1.com'); }
);
$('#canvasReal').on(
    'click',
    function(){ goToUrl('http://www.test2.com'); }
);
$('#canvasOff3').on(
    'click',
    function(){ goToUrl('http://www.test3.com'); }
);

function goToUrl(url){
    window.location = url;
}

(我更喜歡為window.location = X使用單獨的函數,但當然,你也可以在onclick函數中使用它,如下所示:

function(){ window.location = 'http://www.test1.com'; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM